| 1 |
package plural |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strconv" |
| 6 |
"strings" |
| 7 |
) |
| 8 |
|
| 9 |
// Operands is a representation of http://unicode.org/reports/tr35/tr35-numbers.html#Operands |
| 10 |
// If there is a compact decimal exponent value C, then the N, I, V, W, F, and T values are computed after shifting the decimal point in the original by the ācā value. |
| 11 |
// So for 1.2c3, the values are the same as those of 1200: i=1200 and f=0. |
| 12 |
// Similarly, 1.2005c3 has i=1200 and f=5 (corresponding to 1200.5). |
| 13 |
type Operands struct { |
| 14 |
N float64 // absolute value of the source number (integer and decimals) |
| 15 |
I int64 // integer digits of n |
| 16 |
V int64 // number of visible fraction digits in n, with trailing zeros |
| 17 |
W int64 // number of visible fraction digits in n, without trailing zeros |
| 18 |
F int64 // visible fractional digits in n, with trailing zeros |
| 19 |
T int64 // visible fractional digits in n, without trailing zeros |
| 20 |
C int64 // compact decimal exponent value: exponent of the power of 10 used in compact decimal formatting. |
| 21 |
} |
| 22 |
|
| 23 |
// NEqualsAny returns true if o represents an integer equal to any of the arguments. |
| 24 |
func (o *Operands) NEqualsAny(any ...int64) bool { |
| 25 |
for _, i := range any { |
| 26 |
if o.I == i && o.T == 0 { |
| 27 |
return true |
| 28 |
} |
| 29 |
} |
| 30 |
return false |
| 31 |
} |
| 32 |
|
| 33 |
// NModEqualsAny returns true if o represents an integer equal to any of the arguments modulo mod. |
| 34 |
func (o *Operands) NModEqualsAny(mod int64, any ...int64) bool { |
| 35 |
modI := o.I % mod |
| 36 |
for _, i := range any { |
| 37 |
if modI == i && o.T == 0 { |
| 38 |
return true |
| 39 |
} |
| 40 |
} |
| 41 |
return false |
| 42 |
} |
| 43 |
|
| 44 |
// NInRange returns true if o represents an integer in the closed interval [from, to]. |
| 45 |
func (o *Operands) NInRange(from, to int64) bool { |
| 46 |
return o.T == 0 && from <= o.I && o.I <= to |
| 47 |
} |
| 48 |
|
| 49 |
// NModInRange returns true if o represents an integer in the closed interval [from, to] modulo mod. |
| 50 |
func (o *Operands) NModInRange(mod, from, to int64) bool { |
| 51 |
modI := o.I % mod |
| 52 |
return o.T == 0 && from <= modI && modI <= to |
| 53 |
} |
| 54 |
|
| 55 |
// NewOperands returns the operands for number. |
| 56 |
func NewOperands(number interface{}) (*Operands, error) { |
| 57 |
switch number := number.(type) { |
| 58 |
case int: |
| 59 |
return newOperandsInt64(int64(number)), nil |
| 60 |
case int8: |
| 61 |
return newOperandsInt64(int64(number)), nil |
| 62 |
case int16: |
| 63 |
return newOperandsInt64(int64(number)), nil |
| 64 |
case int32: |
| 65 |
return newOperandsInt64(int64(number)), nil |
| 66 |
case int64: |
| 67 |
return newOperandsInt64(number), nil |
| 68 |
case string: |
| 69 |
return newOperandsString(number) |
| 70 |
case float32, float64: |
| 71 |
return nil, fmt.Errorf("floats should be formatted into a string") |
| 72 |
default: |
| 73 |
return nil, fmt.Errorf("invalid type %T; expected integer or string", number) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func newOperandsInt64(i int64) *Operands { |
| 78 |
if i < 0 { |
| 79 |
i = -i |
| 80 |
} |
| 81 |
return &Operands{float64(i), i, 0, 0, 0, 0, 0} |
| 82 |
} |
| 83 |
|
| 84 |
func splitSignificandExponent(s string) (significand, exponent string) { |
| 85 |
i := strings.IndexAny(s, "eE") |
| 86 |
if i < 0 { |
| 87 |
return s, "" |
| 88 |
} |
| 89 |
return s[:i], s[i+1:] |
| 90 |
} |
| 91 |
|
| 92 |
func shiftDecimalLeft(s string, n int) string { |
| 93 |
if n <= 0 { |
| 94 |
return s |
| 95 |
} |
| 96 |
i := strings.IndexRune(s, '.') |
| 97 |
tilt := 0 |
| 98 |
if i < 0 { |
| 99 |
i = len(s) |
| 100 |
tilt = -1 |
| 101 |
} |
| 102 |
switch { |
| 103 |
case n == i: |
| 104 |
return "0." + s[:i] + s[i+1+tilt:] |
| 105 |
case n > i: |
| 106 |
return "0." + strings.Repeat("0", n-i) + s[:i] + s[i+1+tilt:] |
| 107 |
default: |
| 108 |
return s[:i-n] + "." + s[i-n:i] + s[i+1+tilt:] |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func shiftDecimalRight(s string, n int) string { |
| 113 |
if n <= 0 { |
| 114 |
return s |
| 115 |
} |
| 116 |
i := strings.IndexRune(s, '.') |
| 117 |
if i < 0 { |
| 118 |
return s + strings.Repeat("0", n) |
| 119 |
} |
| 120 |
switch rest := len(s) - i - 1; { |
| 121 |
case n == rest: |
| 122 |
return s[:i] + s[i+1:] |
| 123 |
case n > rest: |
| 124 |
return s[:i] + s[i+1:] + strings.Repeat("0", n-rest) |
| 125 |
default: |
| 126 |
return s[:i] + s[i+1:i+1+n] + "." + s[i+1+n:] |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
func applyExponent(s string, exponent int) string { |
| 131 |
switch { |
| 132 |
case exponent > 0: |
| 133 |
return shiftDecimalRight(s, exponent) |
| 134 |
case exponent < 0: |
| 135 |
return shiftDecimalLeft(s, -exponent) |
| 136 |
} |
| 137 |
return s |
| 138 |
} |
| 139 |
|
| 140 |
func newOperandsString(s string) (*Operands, error) { |
| 141 |
if s[0] == '-' { |
| 142 |
s = s[1:] |
| 143 |
} |
| 144 |
ops := &Operands{} |
| 145 |
var err error |
| 146 |
ops.N, err = strconv.ParseFloat(s, 64) |
| 147 |
if err != nil { |
| 148 |
return nil, err |
| 149 |
} |
| 150 |
significand, exponent := splitSignificandExponent(s) |
| 151 |
if exponent != "" { |
| 152 |
// We are storing C as an int64 but only allowing |
| 153 |
// numbers that fit into the bitsize of an int |
| 154 |
// so C is safe to cast as a int later. |
| 155 |
ops.C, err = strconv.ParseInt(exponent, 10, 0) |
| 156 |
if err != nil { |
| 157 |
return nil, err |
| 158 |
} |
| 159 |
} |
| 160 |
value := applyExponent(significand, int(ops.C)) |
| 161 |
parts := strings.SplitN(value, ".", 2) |
| 162 |
ops.I, err = strconv.ParseInt(parts[0], 10, 64) |
| 163 |
if err != nil { |
| 164 |
return nil, err |
| 165 |
} |
| 166 |
if len(parts) == 1 { |
| 167 |
return ops, nil |
| 168 |
} |
| 169 |
fraction := parts[1] |
| 170 |
ops.V = int64(len(fraction)) |
| 171 |
for i := ops.V - 1; i >= 0; i-- { |
| 172 |
if fraction[i] != '0' { |
| 173 |
ops.W = i + 1 |
| 174 |
break |
| 175 |
} |
| 176 |
} |
| 177 |
if ops.V > 0 { |
| 178 |
f, err := strconv.ParseInt(fraction, 10, 0) |
| 179 |
if err != nil { |
| 180 |
return nil, err |
| 181 |
} |
| 182 |
ops.F = f |
| 183 |
} |
| 184 |
if ops.W > 0 { |
| 185 |
t, err := strconv.ParseInt(fraction[:ops.W], 10, 0) |
| 186 |
if err != nil { |
| 187 |
return nil, err |
| 188 |
} |
| 189 |
ops.T = t |
| 190 |
} |
| 191 |
return ops, nil |
| 192 |
} |