module string
import math
-intrude import collection # FIXME should be collection::array
+import collection
`{
#include <stdio.h>
var i = 0
var neg = false
- for c in self.chars
- do
+ for j in [0..length[ do
+ var c = chars[j]
var v = c.to_i
if v > base then
if neg then
fun is_numeric: Bool
do
var has_point_or_comma = false
- for i in self.chars
- do
- if not i.is_numeric
- then
- if (i == '.' or i == ',') and not has_point_or_comma
- then
+ 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
return false
return true
end
+ # Returns `true` if the string contains only Hex chars
+ #
+ # assert "048bf".is_hex == true
+ # assert "ABCDEF".is_hex == true
+ # assert "0G".is_hex == false
+ fun is_hex: Bool
+ do
+ for i in [0..length[ do
+ var c = chars[i]
+ if not (c >= 'a' and c <= 'f') and
+ not (c >= 'A' and c <= 'F') and
+ not (c >= '0' and c <= '9') then return false
+ end
+ return true
+ end
+
# Are all letters in `self` upper-case ?
#
# assert "HELLO WORLD".is_upper == true
# assert "Hello World".is_upper == false
fun is_upper: Bool
do
- for char in self.chars do
+ for i in [0..length[ do
+ var char = chars[i]
if char.is_lower then return false
end
return true
# assert "Hello World".is_lower == false
fun is_lower: Bool
do
- for char in self.chars do
+ for i in [0..length[ do
+ var char = chars[i]
if char.is_upper then return false
end
return true
do
var res = new FlatBuffer
var underscore = false
- for c in self.chars do
+ for i in [0..length[ do
+ var c = chars[i]
if (c >= 'a' and c <= 'z') or (c >='A' and c <= 'Z') then
res.add(c)
underscore = false
fun escape_to_c: String
do
var b = new FlatBuffer
- for c in self.chars do
+ for i in [0..length[ do
+ var c = chars[i]
if c == '\n' then
b.append("\\n")
else if c == '\0' then
do
var res = new FlatBuffer.with_capacity(self.length)
var was_slash = false
- for c in chars do
+ for i in [0..length[ do
+ var c = chars[i]
if not was_slash then
if c == '\\' then
was_slash = true
return res.to_s
end
+ # Encode `self` to percent (or URL) encoding
+ #
+ # assert "aBc09-._~".to_percent_encoding == "aBc09-._~"
+ # assert "%()< >".to_percent_encoding == "%25%28%29%3c%20%3e"
+ # assert ".com/post?e=asdf&f=123".to_percent_encoding == ".com%2fpost%3fe%3dasdf%26f%3d123"
+ fun to_percent_encoding: String
+ do
+ var buf = new FlatBuffer
+
+ for i in [0..length[ do
+ var c = chars[i]
+ if (c >= '0' and c <= '9') or
+ (c >= 'a' and c <= 'z') or
+ (c >= 'A' and c <= 'Z') or
+ c == '-' or c == '.' or
+ c == '_' or c == '~'
+ then
+ buf.add c
+ else buf.append "%{c.ascii.to_hex}"
+ end
+
+ return buf.to_s
+ end
+
+ # Decode `self` from percent (or URL) encoding to a clear string
+ #
+ # Replace invalid use of '%' with '?'.
+ #
+ # assert "aBc09-._~".from_percent_encoding == "aBc09-._~"
+ # assert "%25%28%29%3c%20%3e".from_percent_encoding == "%()< >"
+ # assert ".com%2fpost%3fe%3dasdf%26f%3d123".from_percent_encoding == ".com/post?e=asdf&f=123"
+ # assert "%25%28%29%3C%20%3E".from_percent_encoding == "%()< >"
+ # assert "incomplete %".from_percent_encoding == "incomplete ?"
+ # assert "invalid % usage".from_percent_encoding == "invalid ? usage"
+ fun from_percent_encoding: String
+ do
+ var buf = new FlatBuffer
+
+ var i = 0
+ while i < length do
+ var c = chars[i]
+ if c == '%' then
+ if i + 2 >= length then
+ # What follows % has been cut off
+ buf.add '?'
+ else
+ i += 1
+ var hex_s = substring(i, 2)
+ if hex_s.is_hex then
+ var hex_i = hex_s.to_hex
+ buf.add hex_i.ascii
+ i += 1
+ else
+ # What follows a % is not Hex
+ buf.add '?'
+ i -= 1
+ end
+ end
+ else buf.add c
+
+ i += 1
+ end
+
+ return buf.to_s
+ end
+
+ # Escape the four characters `<`, `>`, `&`, and `"` with their html counterpart
+ #
+ # assert "a&b->\"x\"".html_escape == "a&b->"x""
+ fun html_escape: SELFTYPE
+ do
+ var buf = new FlatBuffer
+
+ for i in [0..length[ do
+ var c = chars[i]
+ if c == '&' then
+ buf.append "&"
+ else if c == '<' then
+ buf.append "<"
+ else if c == '>' then
+ buf.append ">"
+ else if c == '"' then
+ buf.append """
+ else buf.add c
+ end
+
+ return buf.to_s
+ end
+
# Equality of text
# Two pieces of text are equals if thez have the same characters in the same order.
#
# djb2 hash algorithm
var h = 5381
- for char in self.chars do
+ for i in [0..length[ do
+ var char = chars[i]
h = h.lshift(5) + h + char.ascii
end
var new_str = new FlatBuffer.with_capacity(self.length)
var is_first_char = true
- for char in self.chars do
+ for i in [0..length[ do
+ var char = chars[i]
if is_first_char then
new_str.add(char.to_lower)
is_first_char = false
var is_first_char = true
var follows_us = false
- for char in self.chars do
+ for i in [0..length[ do
+ var char = chars[i]
if is_first_char then
new_str.add(char)
is_first_char = false
s.items.copy_to(target_string, its_length, 0, my_length)
else
var curr_pos = my_length
- for i in s.chars do
- target_string[curr_pos] = i
+ for i in [0..s.length[ do
+ var c = s.chars[i]
+ target_string[curr_pos] = c
curr_pos += 1
end
end
s.items.copy_to(items, length, 0, 0)
else
var curr_pos = 0
- for i in s.chars do
- items[curr_pos] = i
+ for i in [0..s.length[ do
+ var c = s.chars[i]
+ items[curr_pos] = c
curr_pos += 1
end
end
s.items.copy_to(items, sl, 0, length)
else
var curr_pos = self.length
- for i in s.chars do
- items[curr_pos] = i
+ for i in [0..s.length[ do
+ var c = s.chars[i]
+ items[curr_pos] = c
curr_pos += 1
end
end
do
return "{class_name}:#{object_id.to_hex}"
end
-
- protected fun args: Sequence[String]
- do
- return sys.args
- end
end
redef class Bool
redef class Sys
var _args_cache: nullable Sequence[String]
- redef fun args: Sequence[String]
+ # The arguments of the program as given by the OS
+ fun program_args: Sequence[String]
do
if _args_cache == null then init_args
return _args_cache.as(not null)
return native_argv(0).to_s
end
- # Initialize `args` with the contents of `native_argc` and `native_argv`.
+ # Initialize `program_args` with the contents of `native_argc` and `native_argv`.
private fun init_args
do
var argc = native_argc
# alpha_comparator.sort(a)
# assert a == [1, 10, 2, 20, 3]
fun alpha_comparator: Comparator[Object] do return once new AlphaComparator
+
+# The arguments of the program as given by the OS
+fun args: Sequence[String]
+do
+ return sys.program_args
+end