X-Git-Url: http://nitlanguage.org diff --git a/lib/standard/string.nit b/lib/standard/string.nit index 59a286d..38bd492 100644 --- a/lib/standard/string.nit +++ b/lib/standard/string.nit @@ -15,7 +15,8 @@ module string import math -intrude import collection # FIXME should be collection::array +import collection +intrude import collection::array `{ #include @@ -59,18 +60,8 @@ abstract class Text # In this case, `from += count` and `count -= from`. fun substring(from: Int, count: Int): SELFTYPE is abstract - # Concatenates `o` to `self` - # - # assert "hello" + "world" == "helloworld" - # assert "" + "hello" + "" == "hello" - fun +(o: Text): SELFTYPE is abstract - - # Auto-concatenates self `i` times - # - # assert "abc" * 4 == "abcabcabcabc" - # assert "abc" * 1 == "abc" - # assert "abc" * 0 == "" - fun *(i: Int): SELFTYPE is abstract + # Iterates on the substrings of self if any + fun substrings: Iterator[Text] is abstract # Is the current Text empty (== "") # @@ -166,13 +157,6 @@ abstract class Text return self.chars.iterator end - # Is 'c' contained in self ? - # - # DEPRECATED : Use self.chars.has instead - fun has(c: Char): Bool - do - return self.chars.has(c) - end # Gets an Array containing the chars of self # @@ -188,18 +172,11 @@ abstract class Text # As with substring, a `from` index < 0 will be replaced by 0 fun substring_from(from: Int): SELFTYPE do - if from > self.length then return empty + if from >= self.length then return empty if from < 0 then from = 0 return substring(from, length - from) end - # Returns a reversed version of self - # - # assert "hello".reversed == "olleh" - # assert "bob".reversed == "bob" - # assert "".reversed == "" - fun reversed: SELFTYPE is abstract - # Does self have a substring `str` starting from position `pos`? # # assert "abcd".has_substring("bc",1) == true @@ -264,8 +241,8 @@ abstract class Text 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 @@ -295,12 +272,10 @@ abstract class Text 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 @@ -310,16 +285,51 @@ abstract class Text return true end - # A upper case version of `self` + # Returns `true` if the string contains only Hex chars # - # assert "Hello World!".to_upper == "HELLO WORLD!" - fun to_upper: SELFTYPE is abstract + # 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 - # A lower case version of `self` + # Are all letters in `self` upper-case ? # - # assert "Hello World!".to_lower == "hello world!" - fun to_lower : SELFTYPE is abstract + # assert "HELLO WORLD".is_upper == true + # assert "%$&%!".is_upper == true + # assert "hello world".is_upper == false + # assert "Hello World".is_upper == false + fun is_upper: Bool + do + for i in [0..length[ do + var char = chars[i] + if char.is_lower then return false + end + return true + end + # Are all letters in `self` lower-case ? + # + # assert "hello world".is_lower == true + # assert "%$&%!".is_lower == true + # assert "Hello World".is_lower == false + fun is_lower: Bool + do + for i in [0..length[ do + var char = chars[i] + if char.is_upper then return false + end + return true + end + # Removes the whitespaces at the beginning of self # # assert " \n\thello \n\t".l_trim == "hello \n\t" @@ -348,7 +358,7 @@ abstract class Text if iter.item.ascii > 32 then break iter.next end - if iter.index == length then return self.empty + if iter.index < 0 then return self.empty return self.substring(0, iter.index + 1) end @@ -364,7 +374,8 @@ abstract class Text 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 @@ -397,7 +408,8 @@ abstract class Text 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 @@ -449,7 +461,8 @@ abstract class Text 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 @@ -474,6 +487,95 @@ abstract class Text 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. # @@ -533,7 +635,8 @@ abstract class Text # 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 @@ -609,6 +712,129 @@ abstract class String redef fun to_s do return self + # Concatenates `o` to `self` + # + # assert "hello" + "world" == "helloworld" + # assert "" + "hello" + "" == "hello" + fun +(o: Text): SELFTYPE is abstract + + # Concatenates self `i` times + # + # assert "abc" * 4 == "abcabcabcabc" + # assert "abc" * 1 == "abc" + # assert "abc" * 0 == "" + fun *(i: Int): SELFTYPE is abstract + + fun insert_at(s: String, pos: Int): SELFTYPE is abstract + + # Returns a reversed version of self + # + # assert "hello".reversed == "olleh" + # assert "bob".reversed == "bob" + # assert "".reversed == "" + fun reversed: SELFTYPE is abstract + + # A upper case version of `self` + # + # assert "Hello World!".to_upper == "HELLO WORLD!" + fun to_upper: SELFTYPE is abstract + + # A lower case version of `self` + # + # assert "Hello World!".to_lower == "hello world!" + fun to_lower : SELFTYPE is abstract + + # Takes a camel case `self` and converts it to snake case + # + # assert "randomMethodId".to_snake_case == "random_method_id" + # + # If `self` is upper, it is returned unchanged + # + # assert "RANDOM_METHOD_ID".to_snake_case == "RANDOM_METHOD_ID" + # + # If the identifier is prefixed by an underscore, the underscore is ignored + # + # assert "_privateField".to_snake_case == "_private_field" + fun to_snake_case: SELFTYPE + do + if self.is_upper then return self + + var new_str = new FlatBuffer.with_capacity(self.length) + var is_first_char = true + + 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 + else if char.is_upper then + new_str.add('_') + new_str.add(char.to_lower) + else + new_str.add(char) + end + end + + return new_str.to_s + end + + # Takes a snake case `self` and converts it to camel case + # + # assert "random_method_id".to_camel_case == "randomMethodId" + # + # If the identifier is prefixed by an underscore, the underscore is ignored + # + # assert "_private_field".to_camel_case == "_privateField" + # + # If `self` is upper, it is returned unchanged + # + # assert "RANDOM_ID".to_camel_case == "RANDOM_ID" + # + # If there are several consecutive underscores, they are considered as a single one + # + # assert "random__method_id".to_camel_case == "randomMethodId" + fun to_camel_case: SELFTYPE + do + if self.is_upper then return self + + var new_str = new FlatBuffer + var is_first_char = true + var follows_us = false + + for i in [0..length[ do + var char = chars[i] + if is_first_char then + new_str.add(char) + is_first_char = false + else if char == '_' then + follows_us = true + else if follows_us then + new_str.add(char.to_upper) + follows_us = false + else + new_str.add(char) + end + end + + return new_str.to_s + end +end + +private class FlatSubstringsIter + super Iterator[FlatText] + + var tgt: nullable FlatText + + init(tgt: FlatText) do self.tgt = tgt + + redef fun item do + assert is_ok + return tgt.as(not null) + end + + redef fun is_ok do return tgt != null + + redef fun next do tgt = null end # Immutable strings of characters. @@ -616,8 +842,6 @@ class FlatString super FlatText super String - redef type SELFTYPE: FlatString - # Index in _items of the start of the string private var index_from: Int @@ -626,6 +850,15 @@ class FlatString redef var chars: SequenceRead[Char] = new FlatStringCharView(self) + redef fun [](index) + do + # Check that the index (+ index_from) is not larger than indexTo + # In other terms, if the index is valid + assert index >= 0 + assert (index + index_from) <= index_to + return items[index + index_from] + end + ################################################ # AbstractString specific methods # ################################################ @@ -822,8 +1055,9 @@ class FlatString 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 @@ -876,6 +1110,8 @@ class FlatString return hash_cache.as(not null) end + + redef fun substrings do return new FlatSubstringsIter(self) end private class FlatStringReverseIterator @@ -894,7 +1130,7 @@ private class FlatStringReverseIterator curr_pos = pos + tgt.index_from end - redef fun is_ok do return curr_pos >= 0 + redef fun is_ok do return curr_pos >= target.index_from redef fun item do return target_items[curr_pos] @@ -989,6 +1225,38 @@ abstract class Buffer # assert b == "helloworld" fun append(s: Text) is abstract + # `self` is appended in such a way that `self` is repeated `r` times + # + # var b = new FlatBuffer + # b.append "hello" + # b.times 3 + # assert b == "hellohellohello" + fun times(r: Int) is abstract + + # Reverses itself in-place + # + # var b = new FlatBuffer + # b.append("hello") + # b.reverse + # assert b == "olleh" + fun reverse is abstract + + # Changes each lower-case char in `self` by its upper-case variant + # + # var b = new FlatBuffer + # b.append("Hello World!") + # b.upper + # assert b == "HELLO WORLD!" + fun upper is abstract + + # Changes each upper-case char in `self` by its lower-case variant + # + # var b = new FlatBuffer + # b.append("Hello World!") + # b.lower + # assert b == "hello world!" + fun lower is abstract + redef fun hash do if is_dirty then hash_cache = null @@ -1011,6 +1279,15 @@ class FlatBuffer private var capacity: Int = 0 + redef fun substrings do return new FlatSubstringsIter(self) + + redef fun [](index) + do + assert index >= 0 + assert index < length + return items[index] + end + redef fun []=(index, item) do is_dirty = true @@ -1079,8 +1356,9 @@ class FlatBuffer 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 @@ -1108,8 +1386,9 @@ class FlatBuffer 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 @@ -1144,50 +1423,46 @@ class FlatBuffer end end - redef fun reversed + redef fun reverse do - var new_buf = new FlatBuffer.with_capacity(self.length) - var reviter = self.chars.reverse_iterator - while reviter.is_ok do - new_buf.add(reviter.item) - reviter.next + var ns = calloc_string(capacity) + var si = length - 1 + var ni = 0 + var it = items + while si >= 0 do + ns[ni] = it[si] + ni += 1 + si -= 1 end - return new_buf - end - - redef fun +(other) - do - var new_buf = new FlatBuffer.with_capacity(self.length + other.length) - new_buf.append(self) - new_buf.append(other) - return new_buf + items = ns end - redef fun *(repeats) + redef fun times(repeats) do - var new_buf = new FlatBuffer.with_capacity(self.length * repeats) - for i in [0..repeats[ do - new_buf.append(self) + var x = new FlatString.with_infos(items, length, 0, length - 1) + for i in [1..repeats[ do + append(x) end - return new_buf end - redef fun to_upper + redef fun upper do - var new_buf = new FlatBuffer.with_capacity(self.length) - for i in self.chars do - new_buf.add(i.to_upper) + var it = items + var id = length - 1 + while id >= 0 do + it[id] = it[id].to_upper + id -= 1 end - return new_buf end - redef fun to_lower + redef fun lower do - var new_buf = new FlatBuffer.with_capacity(self.length) - for i in self.chars do - new_buf.add(i.to_lower) + var it = items + var id = length - 1 + while id >= 0 do + it[id] = it[id].to_lower + id -= 1 end - return new_buf end end @@ -1318,11 +1593,6 @@ redef class Object do return "{class_name}:#{object_id.to_hex}" end - - protected fun args: Sequence[String] - do - return sys.args - end end redef class Bool @@ -1373,15 +1643,14 @@ redef class Int end # C function to convert an nit Int to a NativeString (char*) - private fun native_int_to_s(len: Int): NativeString is extern "native_int_to_s" + private fun native_int_to_s: NativeString is extern "native_int_to_s" # return displayable int in base 10 and signed # # assert 1.to_s == "1" # assert (-123).to_s == "-123" redef fun to_s do - var len = digit_count(10) - return native_int_to_s(len).to_s_with_length(len) + return native_int_to_s.to_s end # return displayable int in hexadecimal @@ -1562,18 +1831,51 @@ redef class Collection[E] end redef class Array[E] + # Fast implementation redef fun to_s do - var s = new FlatBuffer - var i = 0 var l = length + if l == 0 then return "" + if l == 1 then if self[0] == null then return "" else return self[0].to_s + var its = _items + var na = new NativeArray[String](l) + var i = 0 + var sl = 0 + var mypos = 0 while i < l do - var e = self[i] - if e != null then s.append(e.to_s) + var itsi = its[i] + if itsi == null then + i += 1 + continue + end + var tmp = itsi.to_s + sl += tmp.length + na[mypos] = tmp i += 1 + mypos += 1 end - return s.to_s + var ns = new NativeString(sl + 1) + ns[sl] = '\0' + i = 0 + var off = 0 + while i < mypos do + var tmp = na[i] + var tpl = tmp.length + if tmp isa FlatString then + tmp.items.copy_to(ns, tpl, tmp.index_from, off) + off += tpl + else + for j in tmp.substrings do + var s = j.as(FlatString) + var slen = s.length + s.items.copy_to(ns, slen, s.index_from, off) + off += slen + end + end + i += 1 + end + return ns.to_s_with_length(sl) end end @@ -1616,9 +1918,10 @@ end ############################################################################### # Native strings are simple C char * -class NativeString +extern class NativeString `{ char* `} super StringCapable - + # Creates a new NativeString with a capacity of `length` + new(length: Int) is intern fun [](index: Int): Char is intern fun []=(index: Int, item: Char) is intern fun copy_to(dest: NativeString, length: Int, from: Int, to: Int) is intern @@ -1651,7 +1954,6 @@ class NativeString copy_to(new_self, length, 0, 0) return new FlatString.with_infos(new_self, length, 0, length - 1) end - end # StringCapable objects can create native strings @@ -1660,9 +1962,10 @@ interface StringCapable end redef class Sys - var _args_cache: nullable Sequence[String] + private 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) @@ -1674,7 +1977,7 @@ redef class Sys 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 @@ -1738,3 +2041,9 @@ end # 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