Skip to content

Commit

Permalink
fix: data race in remove accents (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafax committed Feb 16, 2024
1 parent f84ed45 commit 8bf3f0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 1 addition & 4 deletions goaway.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (

var (
defaultProfanityDetector *ProfanityDetector
removeAccentsTransformer transform.Transformer
)

// ProfanityDetector contains the dictionaries as well as the configuration
Expand Down Expand Up @@ -244,9 +243,7 @@ func (g ProfanityDetector) sanitize(s string, rememberOriginalIndexes bool) (str
// removeAccents strips all accents from characters.
// Only called if ProfanityDetector.removeAccents is set to true
func removeAccents(s string) string {
if removeAccentsTransformer == nil {
removeAccentsTransformer = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
}
removeAccentsTransformer := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
for _, character := range s {
// If there's a character outside the range of supported runes, there might be some accented words
if character < firstRuneSupported || character > lastRuneSupported {
Expand Down
9 changes: 9 additions & 0 deletions goaway_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,12 @@ func BenchmarkIsProfaneConcurrently(b *testing.B) {
})
b.ReportAllocs()
}

func BenchmarkIsProfaneConcurrently_WithAccents(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
IsProfane("ÄšŚ")
}
})
b.ReportAllocs()
}

0 comments on commit 8bf3f0f

Please sign in to comment.