File tree Expand file tree Collapse file tree 3 files changed +14
-8
lines changed Expand file tree Collapse file tree 3 files changed +14
-8
lines changed Original file line number Diff line number Diff line change @@ -305,26 +305,32 @@ abstract class Text
305
305
end
306
306
end
307
307
308
- # Returns `true` if the string contains only Numeric values (and one "," or one "." character)
308
+ # Is this string in a valid numeric format compatible with `to_f`?
309
309
#
310
310
# assert "123".is_numeric == true
311
311
# assert "1.2".is_numeric == true
312
- # assert "1,2".is_numeric == true
312
+ # assert "-1.2".is_numeric == true
313
+ # assert "-1.23e-2".is_numeric == true
313
314
# assert "1..2".is_numeric == false
315
+ # assert "".is_numeric == false
314
316
fun is_numeric : Bool
315
317
do
316
- var has_point_or_comma = false
318
+ var has_point = false
319
+ var e_index = -1
317
320
for i in [0 ..length [ do
318
321
var c = chars [i ]
319
322
if not c .is_numeric then
320
- if (c == '.' or c == ',' ) and not has_point_or_comma then
321
- has_point_or_comma = true
323
+ if c == '.' and not has_point then
324
+ has_point = true
325
+ else if c == 'e' and e_index == -1 and i > 0 and i < length - 1 and chars [i -1 ] != '-' then
326
+ e_index = i
327
+ else if c == '-' and i == e_index + 1 and i < length - 1 then
322
328
else
323
329
return false
324
330
end
325
331
end
326
332
end
327
- return true
333
+ return not is_empty
328
334
end
329
335
330
336
# Returns `true` if the string contains only Hex chars
Original file line number Diff line number Diff line change 1
1
false
2
2
true
3
3
true
4
- true
4
+ false
5
5
false
6
6
false
7
7
false
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ print "45".is_numeric
25
25
#True
26
26
print "45.3 " . is_numeric
27
27
28
- #True
28
+ #False
29
29
print "45,3 " . is_numeric
30
30
31
31
#False
You can’t perform that action at this time.
0 commit comments