Skip to content

Commit

Permalink
Export language.RegisterPluralSpec and language.Operands (nicksnyder#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksnyder committed Sep 17, 2017
1 parent 1623e59 commit d0056c4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 96 deletions.
20 changes: 10 additions & 10 deletions i18n/language/operands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// http://unicode.org/reports/tr35/tr35-numbers.html#Operands
type operands struct {
type Operands struct {
N float64 // absolute value of the source number (integer and decimals)
I int64 // integer digits of n
V int64 // number of visible fraction digits in n, with trailing zeros
Expand All @@ -17,7 +17,7 @@ type operands struct {
}

// NmodEqualAny returns true if o represents an integer equal to any of the arguments.
func (o *operands) NequalsAny(any ...int64) bool {
func (o *Operands) NequalsAny(any ...int64) bool {
for _, i := range any {
if o.I == i && o.T == 0 {
return true
Expand All @@ -27,7 +27,7 @@ func (o *operands) NequalsAny(any ...int64) bool {
}

// NmodEqualAny returns true if o represents an integer equal to any of the arguments modulo mod.
func (o *operands) NmodEqualsAny(mod int64, any ...int64) bool {
func (o *Operands) NmodEqualsAny(mod int64, any ...int64) bool {
modI := o.I % mod
for _, i := range any {
if modI == i && o.T == 0 {
Expand All @@ -38,17 +38,17 @@ func (o *operands) NmodEqualsAny(mod int64, any ...int64) bool {
}

// NmodInRange returns true if o represents an integer in the closed interval [from, to].
func (o *operands) NinRange(from, to int64) bool {
func (o *Operands) NinRange(from, to int64) bool {
return o.T == 0 && from <= o.I && o.I <= to
}

// NmodInRange returns true if o represents an integer in the closed interval [from, to] modulo mod.
func (o *operands) NmodInRange(mod, from, to int64) bool {
func (o *Operands) NmodInRange(mod, from, to int64) bool {
modI := o.I % mod
return o.T == 0 && from <= modI && modI <= to
}

func newOperands(v interface{}) (*operands, error) {
func newOperands(v interface{}) (*Operands, error) {
switch v := v.(type) {
case int:
return newOperandsInt64(int64(v)), nil
Expand All @@ -69,22 +69,22 @@ func newOperands(v interface{}) (*operands, error) {
}
}

func newOperandsInt64(i int64) *operands {
func newOperandsInt64(i int64) *Operands {
if i < 0 {
i = -i
}
return &operands{float64(i), i, 0, 0, 0, 0}
return &Operands{float64(i), i, 0, 0, 0, 0}
}

func newOperandsString(s string) (*operands, error) {
func newOperandsString(s string) (*Operands, error) {
if s[0] == '-' {
s = s[1:]
}
n, err := strconv.ParseFloat(s, 64)
if err != nil {
return nil, err
}
ops := &operands{N: n}
ops := &Operands{N: n}
parts := strings.SplitN(s, ".", 2)
ops.I, err = strconv.ParseInt(parts[0], 10, 64)
if err != nil {
Expand Down
24 changes: 12 additions & 12 deletions i18n/language/operands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import (
func TestNewOperands(t *testing.T) {
tests := []struct {
input interface{}
ops *operands
ops *Operands
err bool
}{
{int64(0), &operands{0.0, 0, 0, 0, 0, 0}, false},
{int64(1), &operands{1.0, 1, 0, 0, 0, 0}, false},
{"0", &operands{0.0, 0, 0, 0, 0, 0}, false},
{"1", &operands{1.0, 1, 0, 0, 0, 0}, false},
{"1.0", &operands{1.0, 1, 1, 0, 0, 0}, false},
{"1.00", &operands{1.0, 1, 2, 0, 0, 0}, false},
{"1.3", &operands{1.3, 1, 1, 1, 3, 3}, false},
{"1.30", &operands{1.3, 1, 2, 1, 30, 3}, false},
{"1.03", &operands{1.03, 1, 2, 2, 3, 3}, false},
{"1.230", &operands{1.23, 1, 3, 2, 230, 23}, false},
{"20.0230", &operands{20.023, 20, 4, 3, 230, 23}, false},
{int64(0), &Operands{0.0, 0, 0, 0, 0, 0}, false},
{int64(1), &Operands{1.0, 1, 0, 0, 0, 0}, false},
{"0", &Operands{0.0, 0, 0, 0, 0, 0}, false},
{"1", &Operands{1.0, 1, 0, 0, 0, 0}, false},
{"1.0", &Operands{1.0, 1, 1, 0, 0, 0}, false},
{"1.00", &Operands{1.0, 1, 2, 0, 0, 0}, false},
{"1.3", &Operands{1.3, 1, 1, 1, 3, 3}, false},
{"1.30", &Operands{1.3, 1, 2, 1, 30, 3}, false},
{"1.03", &Operands{1.03, 1, 2, 2, 3, 3}, false},
{"1.230", &Operands{1.23, 1, 3, 2, 230, 23}, false},
{"20.0230", &Operands{20.023, 20, 4, 3, 230, 23}, false},
{20.0230, nil, true},
}
for _, test := range tests {
Expand Down
5 changes: 3 additions & 2 deletions i18n/language/pluralspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "strings"
// http://unicode.org/reports/tr35/tr35-numbers.html#Operands
type PluralSpec struct {
Plurals map[Plural]struct{}
PluralFunc func(*operands) Plural
PluralFunc func(*Operands) Plural
}

var pluralSpecs = make(map[string]*PluralSpec)
Expand All @@ -18,7 +18,8 @@ func normalizePluralSpecID(id string) string {
return id
}

func registerPluralSpec(ids []string, ps *PluralSpec) {
// RegisterPluralSpec registers a new plural spec for the language ids.
func RegisterPluralSpec(ids []string, ps *PluralSpec) {
for _, id := range ids {
id = normalizePluralSpecID(id)
pluralSpecs[id] = ps
Expand Down
Loading

0 comments on commit d0056c4

Please sign in to comment.