end
end
- # Returns `true` if the string contains only Numeric values (and one "," or one "." character)
+ # Is this string in a valid numeric format compatible with `to_f`?
#
# 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
+ # assert "".is_numeric == false
fun is_numeric: Bool
do
- var has_point_or_comma = false
+ var has_point = 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
+ if c == '.' and not has_point then
+ has_point = 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
end
end
- return true
+ return not is_empty
end
# Returns `true` if the string contains only Hex chars
return hash_cache.as(not null)
end
- # Gives the formatted string back as a Nit string with `args` in place
+ # Format `self` by replacing each `%n` with the `n`th item of `args`
+ #
+ # The character `%` followed by something other than a number are left as is.
+ # To represent a `%` followed by a number, double the `%`, as in `%%7`.
#
- # assert "This %1 is a %2.".format("String", "formatted String") == "This String is a formatted String."
- # assert "\\%1 This string".format("String") == "\\%1 This string"
+ # assert "This %0 is a %1.".format("String", "formatted String") == "This String is a formatted String."
+ # assert "Do not escape % nor %%1".format("unused") == "Do not escape % nor %1"
fun format(args: Object...): String do
var s = new Array[Text]
var curr_st = 0
var i = 0
while i < length do
- # Skip escaped characters
- if self[i] == '\\' then
- i += 1
- # In case of format
- else if self[i] == '%' then
+ if self[i] == '%' then
var fmt_st = i
i += 1
var ciph_st = i
while i < length and self[i].is_numeric do
i += 1
end
- i -= 1
- var fmt_end = i
- var ciph_len = fmt_end - ciph_st + 1
- var arg_index = substring(ciph_st, ciph_len).to_i - 1
+ var ciph_len = i - ciph_st
+ if ciph_len == 0 then
+ # What follows '%' is not a number.
+ s.push substring(curr_st, i - curr_st)
+ if i < length and self[i] == '%' then
+ # Skip the next `%`
+ i += 1
+ end
+ curr_st = i
+ continue
+ end
+
+ var arg_index = substring(ciph_st, ciph_len).to_i
if arg_index >= args.length then continue
s.push substring(curr_st, fmt_st - curr_st)
s.push args[arg_index].to_s
- curr_st = i + 1
+
+ curr_st = i
+ i -= 1
end
i += 1
end
redef class Int
# Wrapper of strerror C function
- private fun strerror_ext: NativeString `{ return strerror(self); `}
+ private fun strerror_ext: NativeString `{ return strerror((int)self); `}
# Returns a string describing error number
fun strerror: String do return strerror_ext.to_s