/
numeric.go
356 lines (306 loc) · 9.32 KB
/
numeric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package multiparse
import (
"errors"
"regexp"
"strconv"
"strings"
)
// Numeric instances are containers for the various valid numerical types
// that a string may be parsed into.
type Numeric struct {
isInt bool
isFloat bool
isMoney bool
f float64
}
// A NumericParser ingests a string and determines whether it is
// numeric or monetary value by using
// its configuration dictionary. This dictionary consists
// of a currency symbol, a digit separator and a decimal separator.
type NumericParser struct {
CurrencySymbol string
DigitSeparator string
DecimalSeparator string
// Unexported fields.
digitReStr string
decimalReStr string
currencyReStr string
digitRegex *regexp.Regexp
decimalRegex *regexp.Regexp
currencyRegex *regexp.Regexp
}
// NewNumericParser with the default dictionary
// currency symbol -> "", digit separator -> ",", decimal separator -> ".".
func NewNumericParser() *NumericParser {
return NewCustomNumericParser("", ",", ".")
}
// NewUSDNumericParser is configured with a currency symbol "$",
// digit separator ",", and decimal separator ".".
// It properly detects that "123.456" is a real number, but not an integer.
func NewUSDNumericParser() *NumericParser {
return NewCustomNumericParser("$", ",", ".")
}
// NewNumericParser with the dictionary defined by the inititialization
// parameters.
//
// Valid inputs for the currency symbol are: "", "$", or any
// regular expression.
//
// Valid inputs for the separators are: "", ".", ",", or any
// regular expression.
func NewCustomNumericParser(currencySym, digitSep, decimalSep string) *NumericParser {
p := &NumericParser{
CurrencySymbol: currencySym,
DigitSeparator: digitSep,
DecimalSeparator: decimalSep,
}
// Define the regular expression maps to convert string inputs into valid
// regular expressions.
sepMap := map[string]string{
"": "[\\.,]",
".": "[\\.]",
",": "[,]",
}
// Construct the predefined currency regex (dcre) in a series of steps.
tmp := []string{
"^\\p{Sc}", // Perl currency symbol
"^Дин\\.", // Serbian Dinar
"^p\\.", // Belarus Ruble
"^[SB]/\\.", // Peru Nuevo Sol and Panama Balboa
"^[^0-9\\.\\+\\s-_]{1,3}\\s?", // Match things like Lek, HK$ or "USD "
}
cre := strings.Join(tmp, "|")
currencyMap := map[string]string{
"": cre,
"$": "^[\\$]",
}
// Input -> regex string
f := func(t string, m map[string]string) string {
if restr, prs := m[t]; prs {
return restr
}
return t
}
p.digitReStr = f(p.DigitSeparator, sepMap)
p.decimalReStr = f(p.DecimalSeparator, sepMap)
p.currencyReStr = f(p.CurrencySymbol, currencyMap)
p.digitRegex = regexp.MustCompile(p.digitReStr)
p.decimalRegex = regexp.MustCompile(p.decimalReStr)
p.currencyRegex = regexp.MustCompile(p.currencyReStr)
return p
}
// Parse a string to determine if it represents a numeric type.
func (p NumericParser) Parse(s string) (interface{}, error) {
return p.parse(s)
}
// ParseNumeric a string to determine if it represents a numeric type.
func (p NumericParser) ParseNumeric(s string) (*Numeric, error) {
return p.parse(s)
}
func (p NumericParser) removeCurrencySymbol(s string) string {
loc := p.currencyRegex.FindStringIndex(s)
if len(loc) == 2 {
return s[loc[1]:]
}
return s
}
func (p NumericParser) removeDigitSeparators(s string) (string, error) {
if p.digitReStr == p.decimalReStr {
return "", errors.New(ParseMoneySeparatorError)
}
cleaned := p.digitRegex.ReplaceAllString(s, "")
return cleaned, nil
}
// replace the last occurence of a decimal separator with "."
func (p NumericParser) replaceDecimalSeparator(s string) (string, error) {
if p.digitReStr == p.decimalReStr {
return "", errors.New(ParseMoneySeparatorError)
}
// Do not need to do anything if the decimal separator is already "."
if p.decimalReStr == "[\\.]" {
return s, nil
}
locs := p.decimalRegex.FindAllStringIndex(s, -1)
if len(locs) == 0 {
return s, nil
}
loc := locs[len(locs)-1]
cleaned := s[:loc[0]] + "." + s[loc[1]:]
return cleaned, nil
}
func (p NumericParser) sanitize(s string) (string, error) {
s = p.removeCurrencySymbol(s)
tmp, err1 := p.removeDigitSeparators(s)
if err1 == nil {
s = tmp
}
tmp, err2 := p.replaceDecimalSeparator(s)
if err2 == nil {
s = tmp
}
var err error
if err1 != nil || err2 != nil {
err = errors.New(err1.Error() + err2.Error())
}
return s, err
}
// Parse a string representation of a money value which has one "." or ",".
// Any string passed in should not begin or end with a delimiter.
func (p NumericParser) parseOneUnknownSeparator(m string, i int) (string, error) {
// Split the string at the delimiter.
before := m[:i]
after := m[i+1:]
// Initially, assume the after var contins the non- integral part of m.
decimalSep := "."
if len(before) <= 3 && len(after) == 3 {
// Then m looks like "1,238" or "123.456" and we assume m is integral.
decimalSep = ""
}
fs := strings.Join([]string{before, after}, decimalSep)
return fs, nil
}
// Parse a string representation of a money value which has many "." or ",".
func (p NumericParser) parseManyUnknownSeparators(m string, locs [][]int) (string, error) {
i := locs[len(locs)-1][0]
j := locs[len(locs)-2][0]
ci := string(m[i]) // last delimiter
cj := string(m[j]) // second to last delimiter
// Split the string at the last delimiter we encounter.
before := m[:i]
after := m[i+1:]
decimalSep := "."
if ci == cj {
// If the last two delimiters are equal, check whether all delimiters
// are equal. If not, error, otherwise, we assume the string
// represents an integer.
for i, loc := range locs {
if i == len(locs)-1 {
break
}
if m[loc[0]] != m[locs[i+1][0]] {
return "", errors.New(ParseMonetaryStringError)
}
}
decimalSep = ""
}
// Remove all non-decimal indicator delimiters.
re := regexp.MustCompile(p.digitReStr)
before = re.ReplaceAllString(before, "")
fs := strings.Join([]string{before, after}, decimalSep)
return fs, nil
}
func (p NumericParser) parse(s string) (*Numeric, error) {
var (
n *Numeric
err error
sign string
reStr string
re *regexp.Regexp
parseErr = errors.New(ParseNumericError)
)
// Record whether the input string has a currency symbol.
// If so, it can only be a monetary value.
hasCurrency := p.currencyRegex.MatchString(s)
if hasCurrency {
s = p.removeCurrencySymbol(s)
}
// Now determine whether the string's initial character is a + or -.
// If so, strip it away and record the sign.
sign = ""
re = regexp.MustCompile("^[\\+-]")
if re.MatchString(s) {
if re.FindString(s) == "-" {
sign = "-"
}
s = s[1:]
}
// Since currency and sign symbols have been stripped, we now check that the
// expression begins with a decimal separator (possibly) and digit.
// Valid strings thus look like either: .x* or x*.
reStr = "^" + p.decimalReStr + "?" + "[0-9]"
re = regexp.MustCompile(reStr)
if !re.MatchString(s) {
return nil, parseErr
}
// Prepend a 0 if the string begins with a decimal separator.
reStr = "^" + p.decimalReStr
re = regexp.MustCompile(reStr)
if re.MatchString(s) {
s = "0" + s
}
// If the input ends with the decimal separator, remove it.
re = regexp.MustCompile(p.decimalReStr + "$")
if re.MatchString(s) {
s = re.ReplaceAllString(s, "")
}
// Create the main validating regex.
reStr = "^\\d+" + "(" + p.digitReStr + "\\d{3})*" + p.decimalReStr + "?\\d*$"
re = regexp.MustCompile(reStr)
if !re.MatchString(s) {
return nil, parseErr
}
// We can now assume that the string is valid except for
// intermediate delimiters.
// Before attempting to parse the string further, we (possibly) perform
// some basic sanitization.
var parsed string
tmp, err := p.sanitize(s)
if err == nil {
parsed = tmp
} else {
// Probably the parser cannot distinguish between decimal and digit
// separators. So we handle this case separately.
re = regexp.MustCompile(p.digitReStr + "|" + p.decimalReStr)
locs := re.FindAllStringSubmatchIndex(s, -1)
switch len(locs) {
case 0: // The number is an integer. No additional parsing needed.
parsed = s
err = nil
case 1: // Need to deal with 1,234 vs 123,456 vs 12.345, etc.
parsed, err = p.parseOneUnknownSeparator(s, locs[0][0])
default: // Try to find the last separator and determine its type.
parsed, err = p.parseManyUnknownSeparators(s, locs)
}
}
parsed = sign + parsed
f, ferr := strconv.ParseFloat(parsed, 64)
if err != nil || ferr != nil {
return nil, err
}
// We now know that the parsed string correctly parses as a float.
n = &Numeric{
isFloat: true,
f: f,
}
if hasCurrency {
n.isMoney = true
}
_, err = strconv.Atoi(parsed)
if err == nil {
n.isInt = true
}
return n, nil
}
// Int reports whether the Numeric instance can be an integer
// and returns its value.
func (x Numeric) Int() int {
return int(x.f)
}
// Float reports whether the Numeric instance can be a float
// and returns its value.
func (x Numeric) Float() float64 {
return x.f
}
// IsInt reports if the instance can represent an integer.
func (x Numeric) IsInt() bool {
return x.isInt
}
// IsFloat reports if the instance can represent a floating point number.
func (x Numeric) IsFloat() bool {
return x.isFloat
}
// IsMoney reports if the original string has currency symbols
// and correctly prases to a numeric value.
func (x Numeric) IsMoney() bool {
return x.isMoney
}