bojanz / currency

Currency handling for Go.
https://pkg.go.dev/github.com/bojanz/currency
MIT License
557 stars 45 forks source link

Incorrect symbol chosen for various locales (e.g. "de-AT") #2

Closed bojanz closed 4 years ago

bojanz commented 4 years ago

Started investigating this when I realized that de-AT and de-CH use the wrong currency symbol for USD, US$ instead of $. The tests were passing cause they were confirming the broken selection. GetSymbol() has a bug.

bojanz commented 4 years ago

Turns out the bug is in the used helper, contains():

        if i := sort.SearchStrings(a, x); i < n {
            return true
        }

This code assumes that sort.SearchStrings returns i == n when the element was not found, but that's not true. It will gladly return an i smaller than n because that's where the element would go if it was present. I see from a Google search that I'm not the first one burned by this.

We need:

        i := sort.SearchStrings(a, x)
        if i < n && a[i] == x {
            return true
        }