currency: Fix edge case in NewCode where all string characters are digits (#1593)

* add fixes and test

* glorious: nits, privatise upper case field, add item field for case sensitivity for format checks, rm dead code.

* fix potential panic

* gk/glorious: nits

* gk: nits and other things

* improve commentary lol

* glorious+gk: nits and improvements (with no sillyness this time)

* Update currency/pairs.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* thrasher: nitssssss

* linter: fix

* bye bye silly boy

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
Ryan O'Hara-Reid
2024-11-25 14:51:35 +11:00
committed by GitHub
parent 1fab9c72d2
commit 4bbaf75d34
13 changed files with 267 additions and 288 deletions

View File

@@ -186,14 +186,7 @@ func (b *BaseCodes) Register(c string, newRole Role) Code {
return EMPTYCODE
}
var format bool
// Digits fool upper and lower casing. So find first letter and check case.
for x := range c {
if unicode.IsLetter(rune(c[x])) {
format = unicode.IsUpper(rune(c[x]))
break
}
}
isUpperCase := strings.ContainsFunc(c, func(r rune) bool { return unicode.IsLetter(r) && unicode.IsUpper(r) })
// Force upper string storage and matching
c = strings.ToUpper(c)
@@ -218,13 +211,13 @@ func (b *BaseCodes) Register(c string, newRole Role) Code {
}
stored[x].Role = newRole
}
return Code{Item: stored[x], UpperCase: format}
return Code{Item: stored[x], upperCase: isUpperCase}
}
}
newItem := &Item{Symbol: c, Lower: strings.ToLower(c), Role: newRole}
b.Items[c] = append(b.Items[c], newItem)
return Code{Item: newItem, UpperCase: format}
return Code{Item: newItem, upperCase: isUpperCase}
}
// LoadItem sets item data
@@ -275,21 +268,29 @@ func (c Code) String() string {
if c.Item == nil {
return ""
}
if c.UpperCase {
if c.upperCase {
return c.Item.Symbol
}
return c.Item.Lower
}
// Lower converts the code to lowercase formatting
// Lower flags the Code to use LowerCase formatting, but does not change Symbol
// If Code cannot be lowercased then it will return Code unchanged
func (c Code) Lower() Code {
c.UpperCase = false
if c.Item == nil {
return c
}
c.upperCase = false
return c
}
// Upper converts the code to uppercase formatting
// Upper flags the Code to use UpperCase formatting, but does not change Symbol
// If Code cannot be uppercased then it will return Code unchanged
func (c Code) Upper() Code {
c.UpperCase = true
if c.Item == nil {
return c
}
c.upperCase = true
return c
}
@@ -346,21 +347,3 @@ func (i *Item) Currency() Code {
}
return NewCode(i.Symbol)
}
// UpperCurrency allows an item to revert to a code
// taking an upper
func (i *Item) UpperCurrency() Code {
if i == nil {
return EMPTYCODE.Upper()
}
return NewCode(i.Symbol).Upper()
}
// LowerCurrency allows an item to revert to a code
// returning in lower format
func (i *Item) LowerCurrency() Code {
if i == nil {
return EMPTYCODE.Lower()
}
return NewCode(i.Symbol).Lower()
}