-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Copy filterwriter from bitrise * Update filterwriter to use go-utils logger * Fix linter errors * Move mocks to root level * Fix linter errors * Exclude filterwriter tests from race detector * Rename package to redactwriter --------- Co-authored-by: Szabolcs Toth <[email protected]>
- Loading branch information
Showing
8 changed files
with
1,051 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package redactwriter | ||
|
||
import ( | ||
"bytes" | ||
"sort" | ||
) | ||
|
||
type matchRange struct{ first, last int } | ||
|
||
// allRanges returns every indexes of instance of pattern in b, or nil if pattern is not present in b. | ||
func allRanges(b, pattern []byte) (ranges []matchRange) { | ||
i := 0 | ||
for { | ||
sub := b[i:] | ||
idx := bytes.Index(sub, pattern) | ||
if idx == -1 { | ||
return | ||
} | ||
|
||
ranges = append(ranges, matchRange{first: idx + i, last: idx + i + len(pattern)}) | ||
|
||
i += idx + 1 | ||
if i > len(b)-1 { | ||
return | ||
} | ||
} | ||
} | ||
|
||
// mergeAllRanges merges every overlapping ranges in r. | ||
func mergeAllRanges(r []matchRange) []matchRange { | ||
sort.Slice(r, func(i, j int) bool { return r[i].first < r[j].first }) | ||
for i := 0; i < len(r)-1; i++ { | ||
for i+1 < len(r) && r[i+1].first <= r[i].last { | ||
if r[i+1].last > r[i].last { | ||
r[i].last = r[i+1].last | ||
} | ||
r = append(r[:i+1], r[i+2:]...) | ||
} | ||
} | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package redactwriter | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAllRanges(t *testing.T) { | ||
{ | ||
ranges := allRanges([]byte("test"), []byte("t")) | ||
require.Equal(t, []matchRange{{first: 0, last: 1}, {first: 3, last: 4}}, ranges) | ||
} | ||
|
||
{ | ||
ranges := allRanges([]byte("test rangetest"), []byte("test")) | ||
require.Equal(t, []matchRange{{first: 0, last: 4}, {first: 10, last: 14}}, ranges) | ||
} | ||
|
||
{ | ||
ranges := allRanges([]byte("\n"), []byte("\n")) | ||
require.Equal(t, []matchRange{{first: 0, last: 1}}, ranges) | ||
} | ||
|
||
{ | ||
ranges := allRanges([]byte("test\n"), []byte("\n")) | ||
require.Equal(t, []matchRange{{first: 4, last: 5}}, ranges) | ||
} | ||
|
||
{ | ||
ranges := allRanges([]byte("\n\ntest\n"), []byte("\n")) | ||
require.Equal(t, []matchRange{{first: 0, last: 1}, {first: 1, last: 2}, {first: 6, last: 7}}, ranges) | ||
} | ||
|
||
{ | ||
ranges := allRanges([]byte("\n\ntest\n"), []byte("test\n")) | ||
require.Equal(t, []matchRange{{first: 2, last: 7}}, ranges) | ||
} | ||
} | ||
|
||
func TestMergeAllRanges(t *testing.T) { | ||
var testCases = []struct { | ||
name string | ||
ranges []matchRange | ||
want []matchRange | ||
}{ | ||
{ | ||
name: "merges overlapping ranges", | ||
ranges: []matchRange{{0, 2}, {1, 3}}, | ||
want: []matchRange{{0, 3}}, | ||
}, | ||
{ | ||
name: "does not merge distinct ranges", | ||
ranges: []matchRange{{0, 2}, {3, 5}}, | ||
want: []matchRange{{0, 2}, {3, 5}}, | ||
}, | ||
{ | ||
name: "returns the wider range", | ||
ranges: []matchRange{{0, 2}, {1, 2}}, | ||
want: []matchRange{{0, 2}}, | ||
}, | ||
{ | ||
name: "complex test", | ||
ranges: []matchRange{{11, 15}, {0, 2}, {11, 13}, {2, 4}, {6, 9}, {5, 10}}, | ||
want: []matchRange{{0, 4}, {5, 10}, {11, 15}}, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
if got := mergeAllRanges(tc.ranges); !reflect.DeepEqual(got, tc.want) { | ||
t.Errorf("got %v, want %v", got, tc.want) | ||
} | ||
}) | ||
} | ||
|
||
} |
Oops, something went wrong.