Skip to content

Commit

Permalink
#4 Implemented lookup tables for faster conversion from string to co…
Browse files Browse the repository at this point in the history
…nstant
  • Loading branch information
ferdypruis committed Jun 11, 2022
1 parent bdccf53 commit ef94a5a
Show file tree
Hide file tree
Showing 4 changed files with 513 additions and 10 deletions.
12 changes: 4 additions & 8 deletions currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,17 @@ func (c Currency) Name() string { return currencies[c].name }
// FromAlpha returns Currency for the three-letter alpha code.
// Or an error if it does not exist.
func FromAlpha(alpha string) (Currency, error) {
for c, currency := range currencies {
if currency.alpha == alpha {
return Currency(c), nil
}
if c, ok := fromAlpha[alpha]; ok {
return c, nil
}
return Currency(0), Error("no currency exists with alphabetic code " + alpha)
}

// FromNumeric returns Currency for the three-digit numeric code.
// Or an error if it does not exist.
func FromNumeric(numeric string) (Currency, error) {
for c, currency := range currencies {
if currency.numeric == numeric {
return Currency(c), nil
}
if c, ok := fromNumeric[numeric]; ok {
return c, nil
}
return Currency(0), Error("no currency exists with numeric code " + numeric)
}
Expand Down
Loading

0 comments on commit ef94a5a

Please sign in to comment.