Skip to content

Commit

Permalink
Fix thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Berkner committed Apr 14, 2020
1 parent 76a4b34 commit 9684da5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
14 changes: 7 additions & 7 deletions diacritics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ import (
"golang.org/x/text/unicode/norm"
)

var transformChain = transform.Chain(
runes.Map(mapDecomposeUnavailable),
norm.NFD,
runes.Remove(runes.In(unicode.Mn)),
norm.NFC,
)

var unavailableMapping = map[rune]rune{
'\u0181': 'B',
'\u1d81': 'd',
Expand Down Expand Up @@ -193,6 +186,13 @@ var unavailableMapping = map[rune]rune{

// Normalize removes diacritical characters and replaces them with their ASCII representation
func Normalize(input string) string {
var transformChain = transform.Chain(
runes.Map(mapDecomposeUnavailable),
norm.NFD,
runes.Remove(runes.In(unicode.Mn)),
norm.NFC,
)

input = strings.Replace(input, "\u00df", "ss", -1) // ß to ss handling
result, _, _ := transform.String(transformChain, input)
return result
Expand Down
17 changes: 17 additions & 0 deletions diacritics_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package godiacritics

import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -27,3 +28,19 @@ func TestRemoval(t *testing.T) {
})
}
}

func TestRemovalParallel(t *testing.T) {
wg := sync.WaitGroup{}

for i := 0; i < 1000; i++ {
wg.Add(1)

go func() {
actual := Normalize("Ä möre ȼꝋmpleᶍ ⱸxamplé")
assert.Equal(t, "A more complex example", actual)
wg.Done()
}()
}

wg.Wait()
}

0 comments on commit 9684da5

Please sign in to comment.