Skip to content

Commit 8bf3f0f

Browse files
authored
fix: data race in remove accents (#80)
1 parent f84ed45 commit 8bf3f0f

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

goaway.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const (
1717

1818
var (
1919
defaultProfanityDetector *ProfanityDetector
20-
removeAccentsTransformer transform.Transformer
2120
)
2221

2322
// ProfanityDetector contains the dictionaries as well as the configuration
@@ -244,9 +243,7 @@ func (g ProfanityDetector) sanitize(s string, rememberOriginalIndexes bool) (str
244243
// removeAccents strips all accents from characters.
245244
// Only called if ProfanityDetector.removeAccents is set to true
246245
func removeAccents(s string) string {
247-
if removeAccentsTransformer == nil {
248-
removeAccentsTransformer = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
249-
}
246+
removeAccentsTransformer := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
250247
for _, character := range s {
251248
// If there's a character outside the range of supported runes, there might be some accented words
252249
if character < firstRuneSupported || character > lastRuneSupported {

goaway_bench_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,12 @@ func BenchmarkIsProfaneConcurrently(b *testing.B) {
135135
})
136136
b.ReportAllocs()
137137
}
138+
139+
func BenchmarkIsProfaneConcurrently_WithAccents(b *testing.B) {
140+
b.RunParallel(func(pb *testing.PB) {
141+
for pb.Next() {
142+
IsProfane("ÄšŚ")
143+
}
144+
})
145+
b.ReportAllocs()
146+
}

0 commit comments

Comments
 (0)