From: Alexis Laferrière Date: Sat, 19 Dec 2015 16:36:30 +0000 (-0500) Subject: lib/core: extend is_numeric to support negatives (!) and e notation X-Git-Tag: v0.8~16^2~3 X-Git-Url: http://nitlanguage.org lib/core: extend is_numeric to support negatives (!) and e notation Signed-off-by: Alexis Laferrière --- diff --git a/lib/core/text/abstract_text.nit b/lib/core/text/abstract_text.nit index 8b9b68b..e201e0c 100644 --- a/lib/core/text/abstract_text.nit +++ b/lib/core/text/abstract_text.nit @@ -309,16 +309,22 @@ abstract class Text # # assert "123".is_numeric == true # assert "1.2".is_numeric == true + # assert "-1.2".is_numeric == true # assert "1,2".is_numeric == true + # assert "-1.23e-2".is_numeric == true # assert "1..2".is_numeric == false fun is_numeric: Bool do var has_point_or_comma = false + var e_index = -1 for i in [0..length[ do var c = chars[i] if not c.is_numeric then if (c == '.' or c == ',') and not has_point_or_comma then has_point_or_comma = true + else if c == 'e' and e_index == -1 and i > 0 and i < length - 1 and chars[i-1] != '-' then + e_index = i + else if c == '-' and i == e_index + 1 and i < length - 1 then else return false end