1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
6 # This file is free software, which comes along with NIT. This software is
7 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
10 # is kept unaltered, and a notification of the changes is added.
11 # You are allowed to redistribute it and sell it, alone or is a part of
14 # Basic manipulations of strings of characters
19 intrude import collection
::array
26 ###############################################################################
28 ###############################################################################
30 # High-level abstraction for all text representations
34 redef type OTHER: Text
36 # Type of self (used for factorization of several methods, ex : substring_from, empty...)
39 # Gets a view on the chars of the Text object
41 # assert "hello".chars.to_a == ['h', 'e', 'l', 'l', 'o']
42 fun chars
: SequenceRead[Char] is abstract
44 # Number of characters contained in self.
46 # assert "12345".length == 5
47 # assert "".length == 0
48 fun length
: Int is abstract
52 # assert "abcd".substring(1, 2) == "bc"
53 # assert "abcd".substring(-1, 2) == "a"
54 # assert "abcd".substring(1, 0) == ""
55 # assert "abcd".substring(2, 5) == "cd"
57 # A `from` index < 0 will be replaced by 0.
58 # Unless a `count` value is > 0 at the same time.
59 # In this case, `from += count` and `count -= from`.
60 fun substring
(from
: Int, count
: Int): SELFTYPE is abstract
62 # Iterates on the substrings of self if any
63 fun substrings
: Iterator[Text] is abstract
65 # Is the current Text empty (== "")
68 # assert not "foo".is_empty
69 fun is_empty
: Bool do return self.length
== 0
71 # Returns an empty Text of the right type
73 # This method is used internally to get the right
74 # implementation of an empty string.
75 protected fun empty
: SELFTYPE is abstract
77 # Gets the first char of the Text
79 # DEPRECATED : Use self.chars.first instead
80 fun first
: Char do return self.chars
[0]
82 # Access a character at `index` in the string.
84 # assert "abcd"[2] == 'c'
86 # DEPRECATED : Use self.chars.[] instead
87 fun [](index
: Int): Char do return self.chars
[index
]
89 # Gets the index of the first occurence of 'c'
91 # Returns -1 if not found
93 # DEPRECATED : Use self.chars.index_of instead
94 fun index_of
(c
: Char): Int
96 return index_of_from
(c
, 0)
99 # Gets the last char of self
101 # DEPRECATED : Use self.chars.last instead
102 fun last
: Char do return self.chars
[length-1
]
104 # Gets the index of the first occurence of ´c´ starting from ´pos´
106 # Returns -1 if not found
108 # DEPRECATED : Use self.chars.index_of_from instead
109 fun index_of_from
(c
: Char, pos
: Int): Int
111 var iter
= self.chars
.iterator_from
(pos
)
113 if iter
.item
== c
then return iter
.index
119 # Gets the last index of char ´c´
121 # Returns -1 if not found
123 # DEPRECATED : Use self.chars.last_index_of instead
124 fun last_index_of
(c
: Char): Int
126 return last_index_of_from
(c
, length
- 1)
129 # Return a null terminated char *
130 fun to_cstring
: NativeString do return flatten
.to_cstring
132 # The index of the last occurrence of an element starting from pos (in reverse order).
134 # var s = "/etc/bin/test/test.nit"
135 # assert s.last_index_of_from('/', s.length-1) == 13
136 # assert s.last_index_of_from('/', 12) == 8
138 # Returns -1 if not found
140 # DEPRECATED : Use self.chars.last_index_of_from instead
141 fun last_index_of_from
(item
: Char, pos
: Int): Int
143 var iter
= self.chars
.reverse_iterator_from
(pos
)
145 if iter
.item
== item
then return iter
.index
151 # Gets an iterator on the chars of self
153 # DEPRECATED : Use self.chars.iterator instead
154 fun iterator
: Iterator[Char]
156 return self.chars
.iterator
160 # Gets an Array containing the chars of self
162 # DEPRECATED : Use self.chars.to_a instead
163 fun to_a
: Array[Char] do return chars
.to_a
165 # Create a substring from `self` beginning at the `from` position
167 # assert "abcd".substring_from(1) == "bcd"
168 # assert "abcd".substring_from(-1) == "abcd"
169 # assert "abcd".substring_from(2) == "cd"
171 # As with substring, a `from` index < 0 will be replaced by 0
172 fun substring_from
(from
: Int): SELFTYPE
174 if from
>= self.length
then return empty
175 if from
< 0 then from
= 0
176 return substring
(from
, length
- from
)
179 # Does self have a substring `str` starting from position `pos`?
181 # assert "abcd".has_substring("bc",1) == true
182 # assert "abcd".has_substring("bc",2) == false
184 # Returns true iff all characters of `str` are presents
185 # at the expected index in `self.`
186 # The first character of `str` being at `pos`, the second
187 # character being at `pos+1` and so on...
189 # This means that all characters of `str` need to be inside `self`.
191 # assert "abcd".has_substring("xab", -1) == false
192 # assert "abcd".has_substring("cdx", 2) == false
194 # And that the empty string is always a valid substring.
196 # assert "abcd".has_substring("", 2) == true
197 # assert "abcd".has_substring("", 200) == true
198 fun has_substring
(str
: String, pos
: Int): Bool
200 if str
.is_empty
then return true
201 if pos
< 0 or pos
+ str
.length
> length
then return false
202 var myiter
= self.chars
.iterator_from
(pos
)
203 var itsiter
= str
.chars
.iterator
204 while myiter
.is_ok
and itsiter
.is_ok
do
205 if myiter
.item
!= itsiter
.item
then return false
209 if itsiter
.is_ok
then return false
213 # Is this string prefixed by `prefix`?
215 # assert "abcd".has_prefix("ab") == true
216 # assert "abcbc".has_prefix("bc") == false
217 # assert "ab".has_prefix("abcd") == false
218 fun has_prefix
(prefix
: String): Bool do return has_substring
(prefix
,0)
220 # Is this string suffixed by `suffix`?
222 # assert "abcd".has_suffix("abc") == false
223 # assert "abcd".has_suffix("bcd") == true
224 fun has_suffix
(suffix
: String): Bool do return has_substring
(suffix
, length
- suffix
.length
)
226 # If `self` contains only digits, return the corresponding integer
228 # assert "123".to_i == 123
229 # assert "-1".to_i == -1
233 return to_s
.to_cstring
.atoi
236 # If `self` contains a float, return the corresponding float
238 # assert "123".to_f == 123.0
239 # assert "-1".to_f == -1.0
240 # assert "-1.2e-3".to_f == -0.0012
244 return to_s
.to_cstring
.atof
247 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
249 # assert "ff".to_hex == 255
250 fun to_hex
: Int do return a_to
(16)
252 # If `self` contains only digits and letters, return the corresponding integer in a given base
254 # assert "120".a_to(3) == 15
255 fun a_to
(base
: Int) : Int
260 for j
in [0..length
[ do
282 # Returns `true` if the string contains only Numeric values (and one "," or one "." character)
284 # assert "123".is_numeric == true
285 # assert "1.2".is_numeric == true
286 # assert "1,2".is_numeric == true
287 # assert "1..2".is_numeric == false
290 var has_point_or_comma
= false
291 for i
in [0..length
[ do
293 if not c
.is_numeric
then
294 if (c
== '.' or c
== ',') and not has_point_or_comma
then
295 has_point_or_comma
= true
304 # Returns `true` if the string contains only Hex chars
306 # assert "048bf".is_hex == true
307 # assert "ABCDEF".is_hex == true
308 # assert "0G".is_hex == false
311 for i
in [0..length
[ do
313 if not (c
>= 'a' and c
<= 'f') and
314 not (c
>= 'A' and c
<= 'F') and
315 not (c
>= '0' and c
<= '9') then return false
320 # Are all letters in `self` upper-case ?
322 # assert "HELLO WORLD".is_upper == true
323 # assert "%$&%!".is_upper == true
324 # assert "hello world".is_upper == false
325 # assert "Hello World".is_upper == false
328 for i
in [0..length
[ do
330 if char
.is_lower
then return false
335 # Are all letters in `self` lower-case ?
337 # assert "hello world".is_lower == true
338 # assert "%$&%!".is_lower == true
339 # assert "Hello World".is_lower == false
342 for i
in [0..length
[ do
344 if char
.is_upper
then return false
349 # Removes the whitespaces at the beginning of self
351 # assert " \n\thello \n\t".l_trim == "hello \n\t"
353 # A whitespace is defined as any character which ascii value is less than or equal to 32
356 var iter
= self.chars
.iterator
358 if iter
.item
.ascii
> 32 then break
361 if iter
.index
== length
then return self.empty
362 return self.substring_from
(iter
.index
)
365 # Removes the whitespaces at the end of self
367 # assert " \n\thello \n\t".r_trim == " \n\thello"
369 # A whitespace is defined as any character which ascii value is less than or equal to 32
372 var iter
= self.chars
.reverse_iterator
374 if iter
.item
.ascii
> 32 then break
377 if iter
.index
< 0 then return self.empty
378 return self.substring
(0, iter
.index
+ 1)
381 # Trims trailing and preceding white spaces
382 # A whitespace is defined as any character which ascii value is less than or equal to 32
384 # assert " Hello World ! ".trim == "Hello World !"
385 # assert "\na\nb\tc\t".trim == "a\nb\tc"
386 fun trim
: SELFTYPE do return (self.l_trim
).r_trim
388 # Returns `self` removed from its last `\n` (if any).
390 # assert "Hello\n".chomp == "Hello"
391 # assert "Hello".chomp == "Hello"
392 # assert "\n\n\n".chomp == "\n\n"
394 # This method is mainly used to remove the LINE_FEED character from lines of text.
397 if self.chars
.last
!= '\n' then return self
398 return substring
(0, length-1
)
401 # Justify a self in a space of `length`
403 # `left` is the space ratio on the left side.
404 # * 0.0 for left-justified (no space at the left)
405 # * 1.0 for right-justified (all spaces at the left)
406 # * 0.5 for centered (half the spaces at the left)
408 # assert "hello".justify(10, 0.0) == "hello "
409 # assert "hello".justify(10, 1.0) == " hello"
410 # assert "hello".justify(10, 0.5) == " hello "
412 # If `length` is not enough, `self` is returned as is.
414 # assert "hello".justify(2, 0.0) == "hello"
416 # REQUIRE: left >= 0.0 and left <= 1.0
417 # ENSURE: `self.length <= length implies result.length == length`
418 # ENSURE: `self.length >= length implies result == self
419 fun justify
(length
: Int, left
: Float): SELFTYPE
421 var diff
= length
- self.length
422 if diff
<= 0 then return self
423 assert left
>= 0.0 and left
<= 1.0
424 var before
= (diff
.to_f
* left
).to_i
425 return " " * before
+ self + " " * (diff-before
)
428 # Mangle a string to be a unique string only made of alphanumeric characters
429 fun to_cmangle
: String
431 var res
= new FlatBuffer
432 var underscore
= false
433 for i
in [0..length
[ do
435 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
441 res
.append
('_'.ascii
.to_s
)
444 if c
>= '0' and c
<= '9' then
447 else if c
== '_' then
452 res
.append
(c
.ascii
.to_s
)
460 # Escape " \ ' and non printable characters using the rules of literal C strings and characters
462 # assert "abAB12<>&".escape_to_c == "abAB12<>&"
463 # assert "\n\"'\\".escape_to_c == "\\n\\\"\\'\\\\"
464 fun escape_to_c
: String
466 var b
= new FlatBuffer
467 for i
in [0..length
[ do
471 else if c
== '\0' then
473 else if c
== '"' then
475 else if c == '\'' then
477 else if c == '\\
' then
479 else if c.ascii < 32 then
480 b.append("\\{c.ascii.to_base(8, false)}")
488 # Escape additionnal characters
489 # The result might no be legal in C but be used in other languages
491 # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
492 fun escape_more_to_c(chars: String): String
494 var b = new FlatBuffer
495 for c in escape_to_c.chars do
496 if chars.chars.has(c) then
504 # Escape to C plus braces
506 # assert "\n\"'\\\
{\}".escape_to_nit == "\\n\\\
"\\'\\\\\\\{\\\}"
507 fun escape_to_nit
: String do return escape_more_to_c
("\{\}")
509 # Escape to POSIX Shell (sh).
511 # Abort if the text contains a null byte.
513 # assert "\n\"'\\\{\}0".escape_to_sh == "'\n\"'\\''\\\{\}0'"
514 fun escape_to_sh
: String do
515 var b
= new FlatBuffer
517 for i in [0..length[ do
522 assert without_null_byte
: c
!= '\0'
530 # Escape to include in a Makefile
532 # Unfortunately, some characters are not escapable in Makefile.
533 # These characters are `;`, `|`, `\`, and the non-printable ones.
534 # They will be rendered as `"?{hex}"`.
535 fun escape_to_mk: String do
536 var b = new FlatBuffer
537 for i in [0..length[ do
541 else if c == ':' or c == ' ' or c == '#' then
544 else if c
.ascii
< 32 or c
== ';' or c
== '|' or c
== '\\' or c
== '=' then
545 b
.append
("?{c.ascii.to_base(16, false)}")
553 # Return a string where Nit escape sequences are transformed.
556 # assert s.length == 2
557 # var u = s.unescape_nit
558 # assert u.length == 1
559 # assert u.chars[0].ascii == 10 # (the ASCII value of the "new line" character)
560 fun unescape_nit
: String
562 var res
= new FlatBuffer.with_capacity
(self.length
)
563 var was_slash
= false
564 for i
in [0..length
[ do
566 if not was_slash
then
577 else if c
== 'r' then
579 else if c
== 't' then
581 else if c
== '0' then
590 # Encode `self` to percent (or URL) encoding
592 # assert "aBc09-._~".to_percent_encoding == "aBc09-._~"
593 # assert "%()< >".to_percent_encoding == "%25%28%29%3c%20%3e"
594 # assert ".com/post?e=asdf&f=123".to_percent_encoding == ".com%2fpost%3fe%3dasdf%26f%3d123"
595 fun to_percent_encoding
: String
597 var buf
= new FlatBuffer
599 for i
in [0..length
[ do
601 if (c
>= '0' and c
<= '9') or
602 (c
>= 'a' and c
<= 'z') or
603 (c
>= 'A' and c
<= 'Z') or
604 c
== '-' or c
== '.' or
608 else buf
.append
"%{c.ascii.to_hex}"
614 # Decode `self` from percent (or URL) encoding to a clear string
616 # Replace invalid use of '%' with '?'.
618 # assert "aBc09-._~".from_percent_encoding == "aBc09-._~"
619 # assert "%25%28%29%3c%20%3e".from_percent_encoding == "%()< >"
620 # assert ".com%2fpost%3fe%3dasdf%26f%3d123".from_percent_encoding == ".com/post?e=asdf&f=123"
621 # assert "%25%28%29%3C%20%3E".from_percent_encoding == "%()< >"
622 # assert "incomplete %".from_percent_encoding == "incomplete ?"
623 # assert "invalid % usage".from_percent_encoding == "invalid ? usage"
624 fun from_percent_encoding
: String
626 var buf
= new FlatBuffer
632 if i
+ 2 >= length
then
633 # What follows % has been cut off
637 var hex_s
= substring
(i
, 2)
639 var hex_i
= hex_s
.to_hex
643 # What follows a % is not Hex
656 # Escape the characters `<`, `>`, `&`, `"`, `'` and `/` as HTML/XML entity references.
658 # assert "a&b-<>\"x\"/'".html_escape == "a&b-<>"x"/'"
660 # SEE: <https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet#RULE_.231_-_HTML_Escape_Before_Inserting_Untrusted_Data_into_HTML_Element_Content>
661 fun html_escape
: SELFTYPE
663 var buf
= new FlatBuffer
665 for i
in [0..length
[ do
669 else if c
== '<' then
671 else if c
== '>' then
673 else if c
== '"' then
675 else if c
== '\'' then
677 else if c == '/' then
686 # Two pieces of text are equals if thez have the same characters in the same order.
688 # assert "hello" == "hello"
689 # assert "hello" != "HELLO"
690 # assert "hello" == "hel"+"lo"
692 # Things that are not Text are not equal.
695 # assert "9" != ['9']
698 # assert "9".chars.first == '9' # equality of Char
699 # assert "9".chars == ['9'] # equality of Sequence
700 # assert "9".to_i == 9 # equality of Int
703 if o == null then return false
704 if not o isa Text then return false
705 if self.is_same_instance(o) then return true
706 if self.length != o.length then return false
707 return self.chars == o.chars
710 # Lexicographical comparaison
712 # assert "abc" < "xy"
713 # assert "ABC" < "abc"
716 var self_chars = self.chars.iterator
717 var other_chars = other.chars.iterator
719 while self_chars.is_ok and other_chars.is_ok do
720 if self_chars.item < other_chars.item then return true
721 if self_chars.item > other_chars.item then return false
726 if self_chars.is_ok then
733 # Escape string used in labels for graphviz
735 # assert ">><<".escape_to_dot == "\\>\\>\\<\\<"
736 fun escape_to_dot: String
738 return escape_more_to_c("|\{\}<>")
741 # Flat representation of self
742 fun flatten: FlatText is abstract
744 private var hash_cache: nullable Int = null
748 if hash_cache == null then
749 # djb2 hash algorithm
752 for i in [0..length[ do
754 h = h.lshift(5) + h + char.ascii
759 return hash_cache.as(not null)
764 # All kinds of array-based text representations.
765 abstract class FlatText
768 # Underlying C-String (`char*`)
770 # Warning : Might be void in some subclasses, be sure to check
771 # if set before using it.
772 private var items: NativeString is noinit
774 # Real items, used as cache for to_cstring is called
775 private var real_items: nullable NativeString = null
777 redef var length: Int = 0
788 redef fun flatten do return self
791 # Abstract class for the SequenceRead compatible
792 # views on String and Buffer objects
793 private abstract class StringCharView
794 super SequenceRead[Char]
800 redef fun is_empty do return target.is_empty
802 redef fun length do return target.length
804 redef fun iterator: IndexedIterator[Char] do return self.iterator_from(0)
806 redef fun reverse_iterator do return self.reverse_iterator_from(self.length - 1)
809 # View on Buffer objects, extends Sequence
810 # for mutation operations
811 private abstract class BufferCharView
815 redef type SELFTYPE: Buffer
819 # A `String` holds and manipulates an arbitrary sequence of characters.
821 # String objects may be created using literals.
823 # assert "Hello World!" isa String
824 abstract class String
827 redef type SELFTYPE: String
829 redef fun to_s do return self
831 # Concatenates `o` to `self`
833 # assert "hello" + "world" == "helloworld"
834 # assert "" + "hello" + "" == "hello"
835 fun +(o: Text): SELFTYPE is abstract
837 # Concatenates self `i` times
839 # assert "abc" * 4 == "abcabcabcabc"
840 # assert "abc" * 1 == "abc"
841 # assert "abc" * 0 == ""
842 fun *(i: Int): SELFTYPE is abstract
844 # Insert `s` at `pos`.
846 # assert "helloworld".insert_at(" ", 5) == "hello world"
847 fun insert_at(s: String, pos: Int): SELFTYPE is abstract
849 redef fun substrings: Iterator[String] is abstract
851 # Returns a reversed version of self
853 # assert "hello".reversed == "olleh"
854 # assert "bob".reversed == "bob"
855 # assert "".reversed == ""
856 fun reversed: SELFTYPE is abstract
858 # A upper case version of `self`
860 # assert "Hello World!".to_upper == "HELLO WORLD!"
861 fun to_upper: SELFTYPE is abstract
863 # A lower case version of `self`
865 # assert "Hello World!".to_lower == "hello world!"
866 fun to_lower : SELFTYPE is abstract
868 # Takes a camel case `self` and converts it to snake case
870 # assert "randomMethodId".to_snake_case == "random_method_id"
872 # If `self` is upper, it is returned unchanged
874 # assert "RANDOM_METHOD_ID".to_snake_case == "RANDOM_METHOD_ID"
876 # If the identifier is prefixed by an underscore, the underscore is ignored
878 # assert "_privateField".to_snake_case == "_private_field"
879 fun to_snake_case: SELFTYPE
881 if self.is_upper then return self
883 var new_str = new FlatBuffer.with_capacity(self.length)
884 var is_first_char = true
886 for i in [0..length[ do
888 if is_first_char then
889 new_str.add(char.to_lower)
890 is_first_char = false
891 else if char.is_upper then
893 new_str.add(char.to_lower)
902 # Takes a snake case `self` and converts it to camel case
904 # assert "random_method_id".to_camel_case == "randomMethodId"
906 # If the identifier is prefixed by an underscore, the underscore is ignored
908 # assert "_private_field".to_camel_case == "_privateField"
910 # If `self` is upper, it is returned unchanged
912 # assert "RANDOM_ID".to_camel_case == "RANDOM_ID"
914 # If there are several consecutive underscores, they are considered as a single one
916 # assert "random__method_id".to_camel_case == "randomMethodId"
917 fun to_camel_case: SELFTYPE
919 if self.is_upper then return self
921 var new_str = new FlatBuffer
922 var is_first_char = true
923 var follows_us = false
925 for i in [0..length[ do
927 if is_first_char then
929 is_first_char = false
930 else if char == '_
' then
932 else if follows_us then
933 new_str.add(char.to_upper)
943 # Returns a capitalized `self`
945 # Letters that follow a letter are lowercased
946 # Letters that follow a non-letter are upcased.
948 # SEE : `Char::is_letter` for the definition of letter.
950 # assert "jAVASCRIPT".capitalized == "Javascript"
951 # assert "i am root".capitalized == "I Am Root"
952 # assert "ab_c -ab0c ab\nc".capitalized == "Ab_C -Ab0C Ab\nC"
953 fun capitalized: SELFTYPE do
954 if length == 0 then return self
956 var buf = new FlatBuffer.with_capacity(length)
958 var curr = chars[0].to_upper
962 for i in [1 .. length[ do
965 if prev.is_letter then
966 buf[i] = curr.to_lower
968 buf[i] = curr.to_upper
976 private class FlatSubstringsIter
977 super Iterator[FlatText]
979 var tgt: nullable FlatText
983 return tgt.as(not null)
986 redef fun is_ok do return tgt != null
988 redef fun next do tgt = null
991 # Immutable strings of characters.
996 # Index in _items of the start of the string
997 private var index_from: Int is noinit
999 # Indes in _items of the last item of the string
1000 private var index_to: Int is noinit
1002 redef var chars: SequenceRead[Char] = new FlatStringCharView(self)
1006 # Check that the index (+ index_from) is not larger than indexTo
1007 # In other terms, if the index is valid
1009 assert (index + index_from) <= index_to
1010 return items[index + index_from]
1013 ################################################
1014 # AbstractString specific methods #
1015 ################################################
1019 var native = new NativeString(self.length + 1)
1020 var length = self.length
1021 var items = self.items
1024 while pos < length do
1025 native[pos] = items[ipos]
1029 return native.to_s_with_length(self.length)
1032 redef fun substring(from, count)
1038 if count < 0 then count = 0
1042 var realFrom = index_from + from
1044 if (realFrom + count) > index_to then return new FlatString.with_infos(items, index_to - realFrom + 1, realFrom, index_to)
1046 if count == 0 then return empty
1048 var to = realFrom + count - 1
1050 return new FlatString.with_infos(items, to - realFrom + 1, realFrom, to)
1053 redef fun empty do return "".as(FlatString)
1057 var outstr = new NativeString(self.length + 1)
1060 var myitems = self.items
1061 var index_from = self.index_from
1062 var max = self.index_to
1064 while index_from <= max do
1065 outstr[out_index] = myitems[index_from].to_upper
1070 outstr[self.length] = '\
0'
1072 return outstr.to_s_with_length(self.length)
1077 var outstr = new NativeString(self.length + 1)
1080 var myitems = self.items
1081 var index_from = self.index_from
1082 var max = self.index_to
1084 while index_from <= max do
1085 outstr[out_index] = myitems[index_from].to_lower
1090 outstr[self.length] = '\
0'
1092 return outstr.to_s_with_length(self.length)
1097 var i = self.index_from
1098 var imax = self.index_to
1105 ##################################################
1106 # String Specific Methods #
1107 ##################################################
1109 private init with_infos(items: NativeString, len: Int, from: Int, to: Int)
1117 redef fun to_cstring: NativeString
1119 if real_items != null then
1120 return real_items.as(not null)
1122 var newItems = new NativeString(length + 1)
1123 self.items.copy_to(newItems, length, index_from, 0)
1124 newItems[length] = '\
0'
1125 self.real_items = newItems
1132 if not other isa FlatString then return super
1134 if self.object_id == other.object_id then return true
1136 var my_length = length
1138 if other.length != my_length then return false
1140 var my_index = index_from
1141 var its_index = other.index_from
1143 var last_iteration = my_index + my_length
1145 var itsitems = other.items
1146 var myitems = self.items
1148 while my_index < last_iteration do
1149 if myitems[my_index] != itsitems[its_index] then return false
1159 if not other isa FlatString then return super
1161 if self.object_id == other.object_id then return false
1163 var my_curr_char : Char
1164 var its_curr_char : Char
1166 var curr_id_self = self.index_from
1167 var curr_id_other = other.index_from
1169 var my_items = self.items
1170 var its_items = other.items
1172 var my_length = self.length
1173 var its_length = other.length
1175 var max_iterations = curr_id_self + my_length
1177 while curr_id_self < max_iterations do
1178 my_curr_char = my_items[curr_id_self]
1179 its_curr_char = its_items[curr_id_other]
1181 if my_curr_char != its_curr_char then
1182 if my_curr_char < its_curr_char then return true
1190 return my_length < its_length
1195 var my_length = self.length
1196 var its_length = s.length
1198 var total_length = my_length + its_length
1200 var target_string = new NativeString(my_length + its_length + 1)
1202 self.items.copy_to(target_string, my_length, index_from, 0)
1203 if s isa FlatString then
1204 s.items.copy_to(target_string, its_length, s.index_from, my_length)
1205 else if s isa FlatBuffer then
1206 s.items.copy_to(target_string, its_length, 0, my_length)
1208 var curr_pos = my_length
1209 for i in [0..s.length[ do
1211 target_string[curr_pos] = c
1216 target_string[total_length] = '\
0'
1218 return target_string.to_s_with_length(total_length)
1225 var my_length = self.length
1227 var final_length = my_length * i
1229 var my_items = self.items
1231 var target_string = new NativeString(final_length + 1)
1233 target_string[final_length] = '\
0'
1235 var current_last = 0
1237 for iteration in [1 .. i] do
1238 my_items.copy_to(target_string, my_length, 0, current_last)
1239 current_last += my_length
1242 return target_string.to_s_with_length(final_length)
1247 if hash_cache == null then
1248 # djb2 hash algorithm
1254 while i <= index_to do
1255 h = h.lshift(5) + h + myitems[i].ascii
1262 return hash_cache.as(not null)
1265 redef fun substrings do return new FlatSubstringsIter(self)
1268 private class FlatStringReverseIterator
1269 super IndexedIterator[Char]
1271 var target: FlatString
1273 var target_items: NativeString
1277 init with_pos(tgt: FlatString, pos: Int)
1280 target_items = tgt.items
1281 curr_pos = pos + tgt.index_from
1284 redef fun is_ok do return curr_pos >= target.index_from
1286 redef fun item do return target_items[curr_pos]
1288 redef fun next do curr_pos -= 1
1290 redef fun index do return curr_pos - target.index_from
1294 private class FlatStringIterator
1295 super IndexedIterator[Char]
1297 var target: FlatString
1299 var target_items: NativeString
1303 init with_pos(tgt: FlatString, pos: Int)
1306 target_items = tgt.items
1307 curr_pos = pos + target.index_from
1310 redef fun is_ok do return curr_pos <= target.index_to
1312 redef fun item do return target_items[curr_pos]
1314 redef fun next do curr_pos += 1
1316 redef fun index do return curr_pos - target.index_from
1320 private class FlatStringCharView
1321 super StringCharView
1323 redef type SELFTYPE: FlatString
1327 # Check that the index (+ index_from) is not larger than indexTo
1328 # In other terms, if the index is valid
1330 var target = self.target
1331 assert (index + target.index_from) <= target.index_to
1332 return target.items[index + target.index_from]
1335 redef fun iterator_from(start) do return new FlatStringIterator.with_pos(target, start)
1337 redef fun reverse_iterator_from(start) do return new FlatStringReverseIterator.with_pos(target, start)
1341 # A mutable sequence of characters.
1342 abstract class Buffer
1345 redef type SELFTYPE: Buffer
1347 # Specific implementations MUST set this to `true` in order to invalidate caches
1348 protected var is_dirty = true
1350 # Copy-On-Write flag
1352 # If the `Buffer` was to_s'd
, the next in-place altering
1353 # operation will cause the current `Buffer` to be re-allocated.
1355 # The flag will then be set at `false`.
1356 protected var written
= false
1358 # Modifies the char contained at pos `index`
1360 # DEPRECATED : Use self.chars.[]= instead
1361 fun []=(index
: Int, item
: Char) is abstract
1363 # Adds a char `c` at the end of self
1365 # DEPRECATED : Use self.chars.add instead
1366 fun add
(c
: Char) is abstract
1370 # var b = new FlatBuffer
1372 # assert not b.is_empty
1375 fun clear
is abstract
1377 # Enlarges the subsequent array containing the chars of self
1378 fun enlarge
(cap
: Int) is abstract
1380 # Adds the content of text `s` at the end of self
1382 # var b = new FlatBuffer
1385 # assert b == "helloworld"
1386 fun append
(s
: Text) is abstract
1388 # `self` is appended in such a way that `self` is repeated `r` times
1390 # var b = new FlatBuffer
1393 # assert b == "hellohellohello"
1394 fun times
(r
: Int) is abstract
1396 # Reverses itself in-place
1398 # var b = new FlatBuffer
1401 # assert b == "olleh"
1402 fun reverse
is abstract
1404 # Changes each lower-case char in `self` by its upper-case variant
1406 # var b = new FlatBuffer
1407 # b.append("Hello World!")
1409 # assert b == "HELLO WORLD!"
1410 fun upper
is abstract
1412 # Changes each upper-case char in `self` by its lower-case variant
1414 # var b = new FlatBuffer
1415 # b.append("Hello World!")
1417 # assert b == "hello world!"
1418 fun lower
is abstract
1420 # Capitalizes each word in `self`
1422 # Letters that follow a letter are lowercased
1423 # Letters that follow a non-letter are upcased.
1425 # SEE: `Char::is_letter` for the definition of a letter.
1427 # var b = new FlatBuffer.from("jAVAsCriPt")
1429 # assert b == "Javascript"
1430 # b = new FlatBuffer.from("i am root")
1432 # assert b == "I Am Root"
1433 # b = new FlatBuffer.from("ab_c -ab0c ab\nc")
1435 # assert b == "Ab_C -Ab0C Ab\nC"
1437 if length
== 0 then return
1438 var c
= self[0].to_upper
1441 for i
in [1 .. length
[ do
1444 if prev
.is_letter
then
1445 self[i
] = c
.to_lower
1447 self[i
] = c
.to_upper
1454 if is_dirty
then hash_cache
= null
1458 # In Buffers, the internal sequence of character is mutable
1459 # Thus, `chars` can be used to modify the buffer.
1460 redef fun chars
: Sequence[Char] is abstract
1463 # Mutable strings of characters.
1468 redef type SELFTYPE: FlatBuffer
1470 redef var chars
: Sequence[Char] = new FlatBufferCharView(self)
1472 private var capacity
: Int = 0
1474 redef fun substrings
do return new FlatSubstringsIter(self)
1476 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
1478 # This happens when an operation modifies the current `Buffer` and
1479 # the Copy-On-Write flag `written` is set at true.
1480 private fun reset
do
1481 var nns
= new NativeString(capacity
)
1482 items
.copy_to
(nns
, length
, 0, 0)
1490 assert index
< length
1494 redef fun []=(index
, item
)
1497 if index
== length
then
1501 if written
then reset
1502 assert index
>= 0 and index
< length
1509 if capacity
<= length
then enlarge
(length
+ 5)
1516 if written
then reset
1520 redef fun empty
do return new FlatBuffer
1522 redef fun enlarge
(cap
)
1525 if cap
<= c
then return
1526 while c
<= cap
do c
= c
* 2 + 2
1527 # The COW flag can be set at false here, since
1528 # it does a copy of the current `Buffer`
1530 var a
= new NativeString(c
+1)
1531 if length
> 0 then items
.copy_to
(a
, length
, 0, 0)
1536 redef fun to_s
: String
1539 if length
== 0 then items
= new NativeString(1)
1540 return new FlatString.with_infos
(items
, length
, 0, length
- 1)
1543 redef fun to_cstring
1546 var new_native
= new NativeString(length
+ 1)
1547 new_native
[length
] = '\0'
1548 if length
> 0 then items
.copy_to
(new_native
, length
, 0, 0)
1549 real_items
= new_native
1552 return real_items
.as(not null)
1555 # Create a new empty string.
1558 # Create a new string copied from `s`.
1561 capacity
= s
.length
+ 1
1563 items
= new NativeString(capacity
)
1564 if s
isa FlatString then
1565 s
.items
.copy_to
(items
, length
, s
.index_from
, 0)
1566 else if s
isa FlatBuffer then
1567 s
.items
.copy_to
(items
, length
, 0, 0)
1570 for i
in [0..s
.length
[ do
1578 # Create a new empty string with a given capacity.
1579 init with_capacity
(cap
: Int)
1582 # _items = new NativeString.calloc(cap)
1583 items
= new NativeString(cap
+1)
1590 if s
.is_empty
then return
1593 if capacity
< length
+ sl
then enlarge
(length
+ sl
)
1594 if s
isa FlatString then
1595 s
.items
.copy_to
(items
, sl
, s
.index_from
, length
)
1596 else if s
isa FlatBuffer then
1597 s
.items
.copy_to
(items
, sl
, 0, length
)
1599 var curr_pos
= self.length
1600 for i
in [0..s
.length
[ do
1609 # Copies the content of self in `dest`
1610 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
1612 var self_chars
= self.chars
1613 var dest_chars
= dest
.chars
1614 for i
in [0..len-1
] do
1615 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
1619 redef fun substring
(from
, count
)
1623 if from
< 0 then from
= 0
1624 if count
> length
then count
= length
1625 if from
< count
then
1626 var r
= new FlatBuffer.with_capacity
(count
- from
)
1627 while from
< count
do
1628 r
.chars
.push
(items
[from
])
1633 return new FlatBuffer
1640 var ns
= new NativeString(capacity
)
1652 redef fun times
(repeats
)
1654 var x
= new FlatString.with_infos
(items
, length
, 0, length
- 1)
1655 for i
in [1..repeats
[ do
1662 if written
then reset
1666 it
[id
] = it
[id
].to_upper
1673 if written
then reset
1677 it
[id
] = it
[id
].to_lower
1683 private class FlatBufferReverseIterator
1684 super IndexedIterator[Char]
1686 var target
: FlatBuffer
1688 var target_items
: NativeString
1692 init with_pos
(tgt
: FlatBuffer, pos
: Int)
1695 if tgt
.length
> 0 then target_items
= tgt
.items
1699 redef fun index
do return curr_pos
1701 redef fun is_ok
do return curr_pos
>= 0
1703 redef fun item
do return target_items
[curr_pos
]
1705 redef fun next
do curr_pos
-= 1
1709 private class FlatBufferCharView
1710 super BufferCharView
1712 redef type SELFTYPE: FlatBuffer
1714 redef fun [](index
) do return target
.items
[index
]
1716 redef fun []=(index
, item
)
1718 assert index
>= 0 and index
<= length
1719 if index
== length
then
1723 target
.items
[index
] = item
1736 fun enlarge
(cap
: Int)
1743 var s_length
= s
.length
1744 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
1747 redef fun iterator_from
(pos
) do return new FlatBufferIterator.with_pos
(target
, pos
)
1749 redef fun reverse_iterator_from
(pos
) do return new FlatBufferReverseIterator.with_pos
(target
, pos
)
1753 private class FlatBufferIterator
1754 super IndexedIterator[Char]
1756 var target
: FlatBuffer
1758 var target_items
: NativeString
1762 init with_pos
(tgt
: FlatBuffer, pos
: Int)
1765 if tgt
.length
> 0 then target_items
= tgt
.items
1769 redef fun index
do return curr_pos
1771 redef fun is_ok
do return curr_pos
< target
.length
1773 redef fun item
do return target_items
[curr_pos
]
1775 redef fun next
do curr_pos
+= 1
1779 ###############################################################################
1781 ###############################################################################
1784 # User readable representation of `self`.
1785 fun to_s
: String do return inspect
1787 # The class name of the object in NativeString format.
1788 private fun native_class_name
: NativeString is intern
1790 # The class name of the object.
1792 # assert 5.class_name == "Int"
1793 fun class_name
: String do return native_class_name
.to_s
1795 # Developer readable representation of `self`.
1796 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
1799 return "<{inspect_head}>"
1802 # Return "CLASSNAME:#OBJECTID".
1803 # This function is mainly used with the redefinition of the inspect method
1804 protected fun inspect_head
: String
1806 return "{class_name}:#{object_id.to_hex}"
1811 # assert true.to_s == "true"
1812 # assert false.to_s == "false"
1825 # Wrapper of strerror C function
1826 private fun strerror_ext
: NativeString is extern `{
1827 return strerror(recv);
1830 # Returns a string describing error number
1831 fun strerror
: String do return strerror_ext
.to_s
1833 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
1834 # assume < to_c max const of char
1835 private fun fill_buffer
(s
: Buffer, base
: Int, signed
: Bool)
1842 else if self == 0 then
1849 var pos
= digit_count
(base
) - 1
1850 while pos
>= 0 and n
> 0 do
1851 s
.chars
[pos
] = (n
% base
).to_c
1857 # C function to convert an nit Int to a NativeString (char*)
1858 private fun native_int_to_s
: NativeString is extern "native_int_to_s"
1860 # return displayable int in base 10 and signed
1862 # assert 1.to_s == "1"
1863 # assert (-123).to_s == "-123"
1865 return native_int_to_s
.to_s
1868 # return displayable int in hexadecimal
1870 # assert 1.to_hex == "1"
1871 # assert (-255).to_hex == "-ff"
1872 fun to_hex
: String do return to_base
(16,false)
1874 # return displayable int in base base and signed
1875 fun to_base
(base
: Int, signed
: Bool): String
1877 var l
= digit_count
(base
)
1878 var s
= new FlatBuffer.from
(" " * l
)
1879 fill_buffer
(s
, base
, signed
)
1885 # Pretty representation of `self`, with decimals as needed from 1 to a maximum of 3
1887 # assert 12.34.to_s == "12.34"
1888 # assert (-0120.030).to_s == "-120.03"
1890 # see `to_precision` for a custom precision.
1892 var str
= to_precision
( 3 )
1893 if is_inf
!= 0 or is_nan
then return str
1894 var len
= str
.length
1895 for i
in [0..len-1
] do
1897 var c
= str
.chars
[j
]
1900 else if c
== '.' then
1901 return str
.substring
( 0, j
+2 )
1903 return str
.substring
( 0, j
+1 )
1909 # `String` representation of `self` with the given number of `decimals`
1911 # assert 12.345.to_precision(0) == "12"
1912 # assert 12.345.to_precision(3) == "12.345"
1913 # assert (-12.345).to_precision(3) == "-12.345"
1914 # assert (-0.123).to_precision(3) == "-0.123"
1915 # assert 0.999.to_precision(2) == "1.00"
1916 # assert 0.999.to_precision(4) == "0.9990"
1917 fun to_precision
(decimals
: Int): String
1919 if is_nan
then return "nan"
1921 var isinf
= self.is_inf
1924 else if isinf
== -1 then
1928 if decimals
== 0 then return self.to_i
.to_s
1930 for i
in [0..decimals
[ do f
= f
* 10.0
1937 if i
== 0 then return "0." + "0"*decimals
1939 # Prepare both parts of the float, before and after the "."
1944 if sl
> decimals
then
1945 # Has something before the "."
1946 p1
= s
.substring
(0, sl-decimals
)
1947 p2
= s
.substring
(sl-decimals
, decimals
)
1950 p2
= "0"*(decimals-sl
) + s
1953 if i
< 0 then p1
= "-" + p1
1955 return p1
+ "." + p2
1958 # `self` representation with `nb` digits after the '.'.
1960 # assert 12.345.to_precision_native(1) == "12.3"
1961 # assert 12.345.to_precision_native(2) == "12.35"
1962 # assert 12.345.to_precision_native(3) == "12.345"
1963 # assert 12.345.to_precision_native(4) == "12.3450"
1964 fun to_precision_native
(nb
: Int): String import NativeString.to_s
`{
1968 size = snprintf(NULL, 0, "%.*f", (int)nb, recv);
1969 str = malloc(size + 1);
1970 sprintf(str, "%.*f", (int)nb, recv );
1972 return NativeString_to_s( str );
1977 # assert 'x'.to_s == "x"
1980 var s
= new FlatBuffer.with_capacity
(1)
1985 # Returns true if the char is a numerical digit
1987 # assert '0'.is_numeric
1988 # assert '9'.is_numeric
1989 # assert not 'a'.is_numeric
1990 # assert not '?'.is_numeric
1991 fun is_numeric
: Bool
1993 return self >= '0' and self <= '9'
1996 # Returns true if the char is an alpha digit
1998 # assert 'a'.is_alpha
1999 # assert 'Z'.is_alpha
2000 # assert not '0'.is_alpha
2001 # assert not '?'.is_alpha
2004 return (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z')
2007 # Returns true if the char is an alpha or a numeric digit
2009 # assert 'a'.is_alphanumeric
2010 # assert 'Z'.is_alphanumeric
2011 # assert '0'.is_alphanumeric
2012 # assert '9'.is_alphanumeric
2013 # assert not '?'.is_alphanumeric
2014 fun is_alphanumeric
: Bool
2016 return self.is_numeric
or self.is_alpha
2020 redef class Collection[E
]
2021 # Concatenate elements.
2024 var s
= new FlatBuffer
2025 for e
in self do if e
!= null then s
.append
(e
.to_s
)
2029 # Concatenate and separate each elements with `sep`.
2031 # assert [1, 2, 3].join(":") == "1:2:3"
2032 # assert [1..3].join(":") == "1:2:3"
2033 fun join
(sep
: Text): String
2035 if is_empty
then return ""
2037 var s
= new FlatBuffer # Result
2042 if e
!= null then s
.append
(e
.to_s
)
2044 # Concat other items
2049 if e
!= null then s
.append
(e
.to_s
)
2056 redef class Array[E
]
2058 # Fast implementation
2062 if l
== 0 then return ""
2063 if l
== 1 then if self[0] == null then return "" else return self[0].to_s
2065 var na
= new NativeArray[String](l
)
2071 if itsi
== null then
2081 var ns
= new NativeString(sl
+ 1)
2087 var tpl
= tmp
.length
2088 if tmp
isa FlatString then
2089 tmp
.items
.copy_to
(ns
, tpl
, tmp
.index_from
, off
)
2092 for j
in tmp
.substrings
do
2093 var s
= j
.as(FlatString)
2095 s
.items
.copy_to
(ns
, slen
, s
.index_from
, off
)
2101 return ns
.to_s_with_length
(sl
)
2105 redef class Map[K
,V
]
2106 # Concatenate couple of 'key value'.
2107 # key and value are separated by `couple_sep`.
2108 # each couple is separated each couple with `sep`.
2110 # var m = new ArrayMap[Int, String]
2113 # assert m.join("; ", "=") == "1=one; 10=ten"
2114 fun join
(sep
: String, couple_sep
: String): String
2116 if is_empty
then return ""
2118 var s
= new FlatBuffer # Result
2124 s
.append
("{k}{couple_sep}{e or else "<null>"}")
2126 # Concat other items
2132 s
.append
("{k}{couple_sep}{e or else "<null>"}")
2139 ###############################################################################
2141 ###############################################################################
2143 # Native strings are simple C char *
2144 extern class NativeString `{ char* `}
2145 # Creates a new NativeString with a capacity of `length
`
2146 new(length: Int) is intern
2148 # Get char at `index
`.
2149 fun [](index: Int): Char is intern
2151 # Set char `item
` at index.
2152 fun []=(index: Int, item: Char) is intern
2154 # Copy `self` to `dest
`.
2155 fun copy_to(dest: NativeString, length: Int, from: Int, to: Int) is intern
2157 # Position of the first nul character.
2158 fun cstring_length: Int
2161 while self[l] != '\0' do l += 1
2165 # Parse `self` as an Int.
2166 fun atoi: Int is intern
2168 # Parse `self` as a Float.
2169 fun atof: Float is extern "atof"
2173 return to_s_with_length(cstring_length)
2176 # Returns `self` as a String of `length
`.
2177 fun to_s_with_length(length: Int): FlatString
2180 var str = new FlatString.with_infos(self, length, 0, length - 1)
2184 # Returns `self` as a new String.
2185 fun to_s_with_copy: FlatString
2187 var length = cstring_length
2188 var new_self = new NativeString(length + 1)
2189 copy_to(new_self, length, 0, 0)
2190 var str = new FlatString.with_infos(new_self, length, 0, length - 1)
2191 new_self[length] = '\0'
2192 str.real_items = new_self
2198 private var args_cache: nullable Sequence[String]
2200 # The arguments of the program as given by the OS
2201 fun program_args: Sequence[String]
2203 if _args_cache == null then init_args
2204 return _args_cache.as(not null)
2207 # The name of the program as given by the OS
2208 fun program_name: String
2210 return native_argv(0).to_s
2213 # Initialize `program_args
` with the contents of `native_argc
` and `native_argv
`.
2214 private fun init_args
2216 var argc = native_argc
2217 var args = new Array[String].with_capacity(0)
2220 args[i-1] = native_argv(i).to_s
2226 # First argument of the main C function.
2227 private fun native_argc: Int is intern
2229 # Second argument of the main C function.
2230 private fun native_argv(i: Int): NativeString is intern
2233 # Comparator that efficienlty use `to_s
` to compare things
2235 # The comparaison call `to_s
` on object and use the result to order things.
2237 # var a = [1, 2, 3, 10, 20]
2238 # (new CachedAlphaComparator).sort(a)
2239 # assert a == [1, 10, 2, 20, 3]
2241 # Internally the result of `to_s
` is cached in a HashMap to counter
2242 # uneficient implementation of `to_s
`.
2244 # Note: it caching is not usefull, see `alpha_comparator
`
2245 class CachedAlphaComparator
2247 redef type COMPARED: Object
2249 private var cache = new HashMap[Object, String]
2251 private fun do_to_s(a: Object): String do
2252 if cache.has_key(a) then return cache[a]
2258 redef fun compare(a, b) do
2259 return do_to_s(a) <=> do_to_s(b)
2263 # see `alpha_comparator
`
2264 private class AlphaComparator
2266 redef fun compare(a, b) do return a.to_s <=> b.to_s
2269 # Stateless comparator that naively use `to_s
` to compare things.
2271 # Note: the result of `to_s
` is not cached, thus can be invoked a lot
2272 # on a single instace. See `CachedAlphaComparator` as an alternative.
2274 # var a = [1, 2, 3, 10, 20]
2275 # alpha_comparator.sort(a)
2276 # assert a == [1, 10, 2, 20, 3]
2277 fun alpha_comparator: Comparator do return once new AlphaComparator
2279 # The arguments of the program as given by the OS
2280 fun args: Sequence[String]
2282 return sys.program_args