Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redactwriter #181

Merged
merged 9 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion analytics/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"testing"
"time"

"github.com/bitrise-io/go-utils/v2/analytics/mocks"
"github.com/bitrise-io/go-utils/v2/mocks"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand Down
3 changes: 2 additions & 1 deletion analytics/track_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"testing"
"time"

"github.com/bitrise-io/go-utils/v2/analytics/mocks"
"github.com/bitrise-io/go-utils/v2/mocks"

"github.com/stretchr/testify/mock"
)

Expand Down
File renamed without changes.
File renamed without changes.
41 changes: 41 additions & 0 deletions redactwriter/range.go
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
}
77 changes: 77 additions & 0 deletions redactwriter/range_test.go
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)
}
})
}

}
Loading