Skip to content

Commit

Permalink
- r use RegexScrubberWithLabeler for CreateRegexScrubber
Browse files Browse the repository at this point in the history
  • Loading branch information
hownowstephen committed Jan 15, 2025
1 parent 8c7e7a6 commit ff9f466
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions scrubber.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,19 @@ func (v verifyOptions) WithRegexScrubber(regex *regexp.Regexp, replacer string)

// CreateRegexScrubber allows you to create a scrubber that uses a regular expression to scrub data
func CreateRegexScrubber(regex *regexp.Regexp, replacer string) scrubber {
return func(s string) string {
return regex.ReplaceAllString(s, replacer)
}
return CreateRegexScrubberWithLabeler(regex, func(int) string { return replacer })
}

// CreateRegexScrubberWithLabeler allows you to create a scrubber that uses a regular expression to scrub data
func CreateRegexScrubberWithLabeler(regex *regexp.Regexp, replacer func(int) string) scrubber {
return func(s string) string {
m := map[string]int{}
seen := map[string]int{}
replacefn := func(s string) string {
idx := 0
if i, ok := m[s]; ok {
idx = i
} else {
idx = len(m)
m[s] = idx
idx, ok := seen[s]
if !ok {
idx = len(seen)
seen[s] = idx
}

return replacer(idx)
}
return regex.ReplaceAllStringFunc(s, replacefn)
Expand Down

0 comments on commit ff9f466

Please sign in to comment.