1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
11 # Abstract class for manipulation of sequences of characters
17 intrude import collection
::array
25 # High-level abstraction for all text representations
29 redef type OTHER: Text
31 # Type of self (used for factorization of several methods, ex : substring_from, empty...)
34 # Gets a view on the chars of the Text object
36 # assert "hello".chars.to_a == ['h', 'e', 'l', 'l', 'o']
37 fun chars
: SequenceRead[Char] is abstract
39 # Gets a view on the bytes of the Text object
41 # assert "hello".bytes.to_a == [104u8, 101u8, 108u8, 108u8, 111u8]
42 fun bytes
: SequenceRead[Byte] is abstract
44 # Number of characters contained in self.
46 # assert "12345".length == 5
47 # assert "".length == 0
48 # assert "あいうえお".length == 5
49 fun length
: Int is abstract
51 # Number of bytes in `self`
53 # assert "12345".bytelen == 5
54 # assert "あいうえお".bytelen == 15
55 fun bytelen
: Int is abstract
59 # assert "abcd".substring(1, 2) == "bc"
60 # assert "abcd".substring(-1, 2) == "a"
61 # assert "abcd".substring(1, 0) == ""
62 # assert "abcd".substring(2, 5) == "cd"
63 # assert "あいうえお".substring(1,3) == "いうえ"
65 # A `from` index < 0 will be replaced by 0.
66 # Unless a `count` value is > 0 at the same time.
67 # In this case, `from += count` and `count -= from`.
68 fun substring
(from
: Int, count
: Int): SELFTYPE is abstract
70 # Iterates on the substrings of self if any
71 private fun substrings
: Iterator[FlatText] is abstract
73 # Is the current Text empty (== "")
76 # assert not "foo".is_empty
77 fun is_empty
: Bool do return self.length
== 0
79 # Returns an empty Text of the right type
81 # This method is used internally to get the right
82 # implementation of an empty string.
83 protected fun empty
: SELFTYPE is abstract
85 # Gets the first char of the Text
87 # DEPRECATED : Use self.chars.first instead
88 fun first
: Char do return self.chars
[0]
90 # Access a character at `index` in the string.
92 # assert "abcd"[2] == 'c'
94 # DEPRECATED : Use self.chars.[] instead
95 fun [](index
: Int): Char do return self.chars
[index
]
97 # Gets the index of the first occurence of 'c'
99 # Returns -1 if not found
101 # DEPRECATED : Use self.chars.index_of instead
102 fun index_of
(c
: Char): Int
104 return index_of_from
(c
, 0)
107 # Gets the last char of self
109 # DEPRECATED : Use self.chars.last instead
110 fun last
: Char do return self.chars
[length-1
]
112 # Gets the index of the first occurence of ´c´ starting from ´pos´
114 # Returns -1 if not found
116 # DEPRECATED : Use self.chars.index_of_from instead
117 fun index_of_from
(c
: Char, pos
: Int): Int
119 var iter
= self.chars
.iterator_from
(pos
)
121 if iter
.item
== c
then return iter
.index
127 # Gets the last index of char ´c´
129 # Returns -1 if not found
131 # DEPRECATED : Use self.chars.last_index_of instead
132 fun last_index_of
(c
: Char): Int
134 return last_index_of_from
(c
, length
- 1)
137 # Return a null terminated char *
138 fun to_cstring
: NativeString is abstract
140 # The index of the last occurrence of an element starting from pos (in reverse order).
142 # var s = "/etc/bin/test/test.nit"
143 # assert s.last_index_of_from('/', s.length-1) == 13
144 # assert s.last_index_of_from('/', 12) == 8
146 # Returns -1 if not found
148 # DEPRECATED : Use self.chars.last_index_of_from instead
149 fun last_index_of_from
(item
: Char, pos
: Int): Int do return chars
.last_index_of_from
(item
, pos
)
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 # Returns `self` as the corresponding integer
228 # assert "123".to_i == 123
229 # assert "-1".to_i == -1
230 # assert "0x64".to_i == 100
231 # assert "0b1100_0011".to_i== 195
232 # assert "--12".to_i == 12
234 # REQUIRE: `self`.`is_int`
235 fun to_i
: Int is abstract
237 # If `self` contains a float, return the corresponding float
239 # assert "123".to_f == 123.0
240 # assert "-1".to_f == -1.0
241 # assert "-1.2e-3".to_f == -0.0012
245 return to_s
.to_cstring
.atof
248 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
250 # assert "ff".to_hex == 255
251 fun to_hex
(pos
, ln
: nullable Int): Int do
253 if pos
== null then pos
= 0
254 if ln
== null then ln
= length
- pos
256 for i
in [pos
.. max
[ do
258 res
+= self[i
].from_hex
263 # If `self` contains only digits <= '7', return the corresponding integer.
265 # assert "714".to_oct == 460
266 fun to_oct
: Int do return a_to
(8)
268 # If `self` contains only '0' et '1', return the corresponding integer.
270 # assert "101101".to_bin == 45
271 fun to_bin
: Int do return a_to
(2)
273 # If `self` contains only digits '0' .. '9', return the corresponding integer.
275 # assert "108".to_dec == 108
276 fun to_dec
: Int do return a_to
(10)
278 # If `self` contains only digits and letters, return the corresponding integer in a given base
280 # assert "120".a_to(3) == 15
281 fun a_to
(base
: Int) : Int
286 for j
in [0..length
[ do
308 # Is this string in a valid numeric format compatible with `to_f`?
310 # assert "123".is_numeric == true
311 # assert "1.2".is_numeric == true
312 # assert "-1.2".is_numeric == true
313 # assert "-1.23e-2".is_numeric == true
314 # assert "1..2".is_numeric == false
315 # assert "".is_numeric == false
318 var has_point
= false
320 for i
in [0..length
[ do
322 if not c
.is_numeric
then
323 if c
== '.' and not has_point
then
325 else if c
== 'e' and e_index
== -1 and i
> 0 and i
< length
- 1 and chars
[i-1
] != '-' then
327 else if c
== '-' and i
== e_index
+ 1 and i
< length
- 1 then
336 # Returns `true` if the string contains only Hex chars
338 # assert "048bf".is_hex == true
339 # assert "ABCDEF".is_hex == true
340 # assert "0G".is_hex == false
343 for i
in [0..length
[ do
345 if not (c
>= 'a' and c
<= 'f') and
346 not (c
>= 'A' and c
<= 'F') and
347 not (c
>= '0' and c
<= '9') then return false
352 # Returns `true` if the string contains only Binary digits
354 # assert "1101100".is_bin == true
355 # assert "1101020".is_bin == false
357 for i
in chars
do if i
!= '0' and i
!= '1' then return false
361 # Returns `true` if the string contains only Octal digits
363 # assert "213453".is_oct == true
364 # assert "781".is_oct == false
366 for i
in chars
do if i
< '0' or i
> '7' then return false
370 # Returns `true` if the string contains only Decimal digits
372 # assert "10839".is_dec == true
373 # assert "164F".is_dec == false
375 for i
in chars
do if i
< '0' or i
> '9' then return false
379 # Are all letters in `self` upper-case ?
381 # assert "HELLO WORLD".is_upper == true
382 # assert "%$&%!".is_upper == true
383 # assert "hello world".is_upper == false
384 # assert "Hello World".is_upper == false
387 for i
in [0..length
[ do
389 if char
.is_lower
then return false
394 # Are all letters in `self` lower-case ?
396 # assert "hello world".is_lower == true
397 # assert "%$&%!".is_lower == true
398 # assert "Hello World".is_lower == false
401 for i
in [0..length
[ do
403 if char
.is_upper
then return false
408 # Removes the whitespaces at the beginning of self
410 # assert " \n\thello \n\t".l_trim == "hello \n\t"
412 # `Char::is_whitespace` determines what is a whitespace.
415 var iter
= self.chars
.iterator
417 if not iter
.item
.is_whitespace
then break
420 if iter
.index
== length
then return self.empty
421 return self.substring_from
(iter
.index
)
424 # Removes the whitespaces at the end of self
426 # assert " \n\thello \n\t".r_trim == " \n\thello"
428 # `Char::is_whitespace` determines what is a whitespace.
431 var iter
= self.chars
.reverse_iterator
433 if not iter
.item
.is_whitespace
then break
436 if iter
.index
< 0 then return self.empty
437 return self.substring
(0, iter
.index
+ 1)
440 # Trims trailing and preceding white spaces
442 # assert " Hello World ! ".trim == "Hello World !"
443 # assert "\na\nb\tc\t".trim == "a\nb\tc"
445 # `Char::is_whitespace` determines what is a whitespace.
446 fun trim
: SELFTYPE do return (self.l_trim
).r_trim
448 # Is the string non-empty but only made of whitespaces?
450 # assert " \n\t ".is_whitespace == true
451 # assert " hello ".is_whitespace == false
452 # assert "".is_whitespace == false
454 # `Char::is_whitespace` determines what is a whitespace.
455 fun is_whitespace
: Bool
457 if is_empty
then return false
458 for c
in self.chars
do
459 if not c
.is_whitespace
then return false
464 # Returns `self` removed from its last line terminator (if any).
466 # assert "Hello\n".chomp == "Hello"
467 # assert "Hello".chomp == "Hello"
469 # assert "\n".chomp == ""
470 # assert "".chomp == ""
472 # Line terminators are `"\n"`, `"\r\n"` and `"\r"`.
473 # A single line terminator, the last one, is removed.
475 # assert "\r\n".chomp == ""
476 # assert "\r\n\n".chomp == "\r\n"
477 # assert "\r\n\r\n".chomp == "\r\n"
478 # assert "\r\n\r".chomp == "\r\n"
480 # Note: unlike with most IO methods like `Reader::read_line`,
481 # a single `\r` is considered here to be a line terminator and will be removed.
485 if len
== 0 then return self
486 var l
= self.chars
.last
488 return substring
(0, len-1
)
489 else if l
!= '\n' then
491 else if len
> 1 and self.chars
[len-2
] == '\r' then
492 return substring
(0, len-2
)
494 return substring
(0, len-1
)
498 # Justify `self` in a space of `length`
500 # `left` is the space ratio on the left side.
501 # * 0.0 for left-justified (no space at the left)
502 # * 1.0 for right-justified (all spaces at the left)
503 # * 0.5 for centered (half the spaces at the left)
505 # `char`, or `' '` by default, is repeated to pad the empty space.
509 # assert "hello".justify(10, 0.0) == "hello "
510 # assert "hello".justify(10, 1.0) == " hello"
511 # assert "hello".justify(10, 0.5) == " hello "
512 # assert "hello".justify(10, 0.5, '.') == "..hello..."
514 # If `length` is not enough, `self` is returned as is.
516 # assert "hello".justify(2, 0.0) == "hello"
518 # REQUIRE: `left >= 0.0 and left <= 1.0`
519 # ENSURE: `self.length <= length implies result.length == length`
520 # ENSURE: `self.length >= length implies result == self`
521 fun justify
(length
: Int, left
: Float, char
: nullable Char): String
523 var pad
= (char
or else ' ').to_s
524 var diff
= length
- self.length
525 if diff
<= 0 then return to_s
526 assert left
>= 0.0 and left
<= 1.0
527 var before
= (diff
.to_f
* left
).to_i
528 return pad
* before
+ self + pad
* (diff-before
)
531 # Mangle a string to be a unique string only made of alphanumeric characters and underscores.
533 # This method is injective (two different inputs never produce the same
534 # output) and the returned string always respect the following rules:
536 # * Contains only US-ASCII letters, digits and underscores.
537 # * Never starts with a digit.
538 # * Never ends with an underscore.
539 # * Never contains two contiguous underscores.
541 # assert "42_is/The answer!".to_cmangle == "_52d2_is_47dThe_32danswer_33d"
542 # assert "__".to_cmangle == "_95d_95d"
543 # assert "__d".to_cmangle == "_95d_d"
544 # assert "_d_".to_cmangle == "_d_95d"
545 # assert "_42".to_cmangle == "_95d42"
546 # assert "foo".to_cmangle == "foo"
547 # assert "".to_cmangle == ""
548 fun to_cmangle
: String
550 if is_empty
then return ""
552 var underscore
= false
556 if c
>= '0' and c
<= '9' then
558 res
.append
(c
.code_point
.to_s
)
562 for i
in [start
..length
[ do
564 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
570 res
.append
('_'.code_point
.to_s
)
573 if c
>= '0' and c
<= '9' then
576 else if c
== '_' then
581 res
.append
(c
.code_point
.to_s
)
587 res
.append
('_'.code_point
.to_s
)
593 # Escape `"` `\` `'`, trigraphs and non printable characters using the rules of literal C strings and characters
595 # assert "abAB12<>&".escape_to_c == "abAB12<>&"
596 # assert "\n\"'\\".escape_to_c == "\\n\\\"\\'\\\\"
597 # assert "allo???!".escape_to_c == "allo??\\?!"
598 # assert "??=??/??'??(??)".escape_to_c == "?\\?=?\\?/??\\'?\\?(?\\?)"
599 # assert "??!??<??>??-".escape_to_c == "?\\?!?\\?<?\\?>?\\?-"
601 # Most non-printable characters (bellow ASCII 32) are escaped to an octal form `\nnn`.
602 # Three digits are always used to avoid following digits to be interpreted as an element
603 # of the octal sequence.
605 # assert "{0.code_point}{1.code_point}{8.code_point}{31.code_point}{32.code_point}".escape_to_c == "\\000\\001\\010\\037 "
607 # The exceptions are the common `\t` and `\n`.
608 fun escape_to_c
: String
611 for i
in [0..length
[ do
615 else if c
== '\t' then
617 else if c
== '"' then
619 else if c == '\'' then
621 else if c == '\\
' then
623 else if c == '?' then
624 # Escape if it is the last question mark of a ANSI C trigraph.
628 # We ignore `??'` because it will be escaped as `??\
'`.
641 else if c.code_point < 32 then
643 var oct = c.code_point.to_base(8)
644 # Force 3 octal digits since it is the
645 # maximum allowed in the C specification
646 if oct.length == 1 then
649 else if oct.length == 2 then
660 # Escape additionnal characters
661 # The result might no be legal in C but be used in other languages
663 # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
664 # assert "allo???!".escape_more_to_c("") == "allo??\\?!"
665 fun escape_more_to_c(chars: String): String
668 for c in escape_to_c.chars do
669 if chars.chars.has(c) then
677 # Escape to C plus braces
679 # assert "\n\"'\\\
{\}".escape_to_nit == "\\n\\\
"\\'\\\\\\\{\\\}"
680 fun escape_to_nit
: String do return escape_more_to_c
("\{\}")
682 # Escape to POSIX Shell (sh).
684 # Abort if the text contains a null byte.
686 # assert "\n\"'\\\{\}0".escape_to_sh == "'\n\"'\\''\\\{\}0'"
687 fun escape_to_sh
: String do
690 for i in [0..length[ do
695 assert without_null_byte
: c
!= '\0'
703 # Escape to include in a Makefile
705 # Unfortunately, some characters are not escapable in Makefile.
706 # These characters are `;`, `|`, `\`, and the non-printable ones.
707 # They will be rendered as `"?{hex}"`.
708 fun escape_to_mk: String do
710 for i in [0..length[ do
714 else if c == ':' or c == ' ' or c == '#' then
717 else if c
.code_point
< 32 or c
== ';' or c
== '|' or c
== '\\' or c
== '=' then
718 b
.append
("?{c.code_point.to_base(16)}")
726 # Return a string where Nit escape sequences are transformed.
729 # assert s.length == 2
730 # var u = s.unescape_nit
731 # assert u.length == 1
732 # assert u.chars[0].code_point == 10 # (the ASCII value of the "new line" character)
733 fun unescape_nit
: String
735 var res
= new Buffer.with_cap
(self.length
)
736 var was_slash
= false
737 for i
in [0..length
[ do
739 if not was_slash
then
750 else if c
== 'r' then
752 else if c
== 't' then
754 else if c
== '0' then
763 # Returns `self` with all characters escaped with their UTF-16 representation
765 # assert "Aèあ𐏓".escape_to_utf16 == "\\u0041\\u00e8\\u3042\\ud800\\udfd3"
766 fun escape_to_utf16
: String do
768 for i
in chars
do buf
.append i
.escape_to_utf16
772 # Returns the Unicode char escaped by `self`
774 # assert "\\u0041".from_utf16_escape == 'A'
775 # assert "\\ud800\\udfd3".from_utf16_escape == '𐏓'
776 # assert "\\u00e8".from_utf16_escape == 'è'
777 # assert "\\u3042".from_utf16_escape == 'あ'
778 fun from_utf16_escape
(pos
, ln
: nullable Int): Char do
779 if pos
== null then pos
= 0
780 if ln
== null then ln
= length
- pos
781 if ln
< 6 then return 0xFFFD.code_point
782 var cp
= from_utf16_digit
(pos
+ 2)
783 if cp
< 0xD800 then return cp
.code_point
784 if cp
> 0xDFFF then return cp
.code_point
785 if cp
> 0xDBFF then return 0xFFFD.code_point
786 if ln
== 6 then return 0xFFFD.code_point
787 if ln
< 12 then return 0xFFFD.code_point
789 cp
+= from_utf16_digit
(pos
+ 8)
790 var cplo
= cp
& 0xFFFF
791 if cplo
< 0xDC00 then return 0xFFFD.code_point
792 if cplo
> 0xDFFF then return 0xFFFD.code_point
793 return cp
.from_utf16_surr
.code_point
796 # Returns a UTF-16 escape value
798 # var s = "\\ud800\\udfd3"
799 # assert s.from_utf16_digit(2) == 0xD800
800 # assert s.from_utf16_digit(8) == 0xDFD3
801 fun from_utf16_digit
(pos
: nullable Int): Int do
802 if pos
== null then pos
= 0
803 return to_hex
(pos
, 4)
806 # Encode `self` to percent (or URL) encoding
808 # assert "aBc09-._~".to_percent_encoding == "aBc09-._~"
809 # assert "%()< >".to_percent_encoding == "%25%28%29%3c%20%3e"
810 # assert ".com/post?e=asdf&f=123".to_percent_encoding == ".com%2fpost%3fe%3dasdf%26f%3d123"
811 # assert "éあいう".to_percent_encoding == "%c3%a9%e3%81%82%e3%81%84%e3%81%86"
812 fun to_percent_encoding
: String
816 for i
in [0..length
[ do
818 if (c
>= '0' and c
<= '9') or
819 (c
>= 'a' and c
<= 'z') or
820 (c
>= 'A' and c
<= 'Z') or
821 c
== '-' or c
== '.' or
826 var bytes
= c
.to_s
.bytes
827 for b
in bytes
do buf
.append
"%{b.to_i.to_hex}"
834 # Decode `self` from percent (or URL) encoding to a clear string
836 # Replace invalid use of '%' with '?'.
838 # assert "aBc09-._~".from_percent_encoding == "aBc09-._~"
839 # assert "%25%28%29%3c%20%3e".from_percent_encoding == "%()< >"
840 # assert ".com%2fpost%3fe%3dasdf%26f%3d123".from_percent_encoding == ".com/post?e=asdf&f=123"
841 # assert "%25%28%29%3C%20%3E".from_percent_encoding == "%()< >"
842 # assert "incomplete %".from_percent_encoding == "incomplete ?"
843 # assert "invalid % usage".from_percent_encoding == "invalid ? usage"
844 # assert "%c3%a9%e3%81%82%e3%81%84%e3%81%86".from_percent_encoding == "éあいう"
845 fun from_percent_encoding
: String
848 var has_percent
= false
856 # If no transformation is needed, return self as a string
857 if not has_percent
then return to_s
859 var buf
= new NativeString(len
)
865 if i
+ 2 >= length
then
866 # What follows % has been cut off
870 var hex_s
= substring
(i
, 2)
872 var hex_i
= hex_s
.to_hex
876 # What follows a % is not Hex
881 else buf
[l
] = c
.ascii
887 return buf
.to_s_unsafe
(l
)
890 # Escape the characters `<`, `>`, `&`, `"`, `'` and `/` as HTML/XML entity references.
892 # assert "a&b-<>\"x\"/'".html_escape == "a&b-<>"x"/'"
894 # 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>
895 fun html_escape
: String
899 for i
in [0..length
[ do
903 else if c
== '<' then
905 else if c
== '>' then
907 else if c
== '"' then
909 else if c
== '\'' then
911 else if c == '/' then
920 # Two pieces of text are equals if thez have the same characters in the same order.
922 # assert "hello" == "hello"
923 # assert "hello" != "HELLO"
924 # assert "hello" == "hel"+"lo"
926 # Things that are not Text are not equal.
929 # assert "9" != ['9']
932 # assert "9".chars.first == '9' # equality of Char
933 # assert "9".chars == ['9'] # equality of Sequence
934 # assert "9".to_i == 9 # equality of Int
937 if o == null then return false
938 if not o isa Text then return false
939 if self.is_same_instance(o) then return true
940 if self.length != o.length then return false
941 return self.chars == o.chars
944 # Lexicographical comparaison
946 # assert "abc" < "xy"
947 # assert "ABC" < "abc"
950 var self_chars = self.chars.iterator
951 var other_chars = other.chars.iterator
953 while self_chars.is_ok and other_chars.is_ok do
954 if self_chars.item < other_chars.item then return true
955 if self_chars.item > other_chars.item then return false
960 if self_chars.is_ok then
967 # Escape string used in labels for graphviz
969 # assert ">><<".escape_to_dot == "\\>\\>\\<\\<"
970 fun escape_to_dot: String
972 return escape_more_to_c("|\{\}<>")
975 private var hash_cache: nullable Int = null
979 if hash_cache == null then
980 # djb2 hash algorithm
983 for i in [0..length[ do
985 h = (h << 5) + h + char.code_point
990 return hash_cache.as(not null)
993 # Format `self` by replacing each `%n` with the `n`th item of `args`
995 # The character `%` followed by something other than a number are left as is.
996 # To represent a `%` followed by a number, double the `%`, as in `%%7`.
998 # assert "This %0 is a %1.".format("String", "formatted String") == "This String is a formatted String."
999 # assert "Do not escape % nor %%1".format("unused") == "Do not escape % nor %1"
1000 fun format(args: Object...): String do
1001 var s = new Array[Text]
1005 if self[i] == '%' then
1009 while i < length and self[i].is_numeric do
1013 var ciph_len = i - ciph_st
1014 if ciph_len == 0 then
1015 # What follows '%' is not a number.
1016 s.push substring(curr_st, i - curr_st)
1017 if i < length and self[i] == '%' then
1025 var arg_index = substring(ciph_st, ciph_len).to_i
1026 if arg_index >= args.length then continue
1028 s.push substring(curr_st, fmt_st - curr_st)
1029 s.push args[arg_index].to_s
1036 s.push substring(curr_st, length - curr_st)
1040 # Return the Levenshtein distance between two strings
1043 # assert "abcd".levenshtein_distance("abcd") == 0
1044 # assert "".levenshtein_distance("abcd") == 4
1045 # assert "abcd".levenshtein_distance("") == 4
1046 # assert "abcd".levenshtein_distance("xyz") == 4
1047 # assert "abcd".levenshtein_distance("xbdy") == 3
1049 fun levenshtein_distance(other: String): Int
1051 var slen = self.length
1052 var olen = other.length
1055 if slen == 0 then return olen
1056 if olen == 0 then return slen
1057 if self == other then return 0
1059 # previous row of distances
1060 var v0 = new Array[Int].with_capacity(olen+1)
1062 # current row of distances
1063 var v1 = new Array[Int].with_capacity(olen+1)
1065 for j in [0..olen] do
1066 # prefix insert cost
1070 for i in [0..slen[ do
1072 # prefix delete cost
1075 for j in [0..olen[ do
1077 var cost1 = v1[j] + 1
1079 var cost2 = v0[j + 1] + 1
1080 # same char cost (+0)
1083 if self[i] != other[j] then cost3 += 1
1085 v1[j+1] = cost1.min(cost2).min(cost3)
1089 # * v1 become v0 in the next iteration
1090 # * old v0 is reused as the new v1
1099 # Copies `n` bytes from `self` at `src_offset` into `dest` starting at `dest_offset`
1101 # Basically a high-level synonym of NativeString::copy_to
1103 # REQUIRE: `n` must be large enough to contain `len` bytes
1105 # var ns = new NativeString(8)
1106 # "Text is String".copy_to_native(ns, 8, 2, 0)
1107 # assert ns.to_s_unsafe(8) == "xt is St"
1109 fun copy_to_native(dest: NativeString, n, src_offset, dest_offset: Int) do
1110 var mypos = src_offset
1111 var itspos = dest_offset
1113 dest[itspos] = self.bytes[mypos]
1120 # Packs the content of a string in packs of `ln` chars.
1121 # This variant ensures that only the last element might be smaller than `ln`
1124 # var s = "abcdefghijklmnopqrstuvwxyz"
1125 # assert s.pack_l(4) == ["abcd","efgh","ijkl","mnop","qrst","uvwx","yz"]
1127 fun pack_l(ln: Int): Array[Text] do
1129 var retarr = new Array[Text].with_capacity(length / ln + length % ln)
1130 while st < length do
1131 retarr.add(substring(st, ln))
1137 # Packs the content of a string in packs of `ln` chars.
1138 # This variant ensures that only the first element might be smaller than `ln`
1141 # var s = "abcdefghijklmnopqrstuvwxyz"
1142 # assert s.pack_r(4) == ["ab","cdef","ghij","klmn","opqr","stuv","wxyz"]
1144 fun pack_r(ln: Int): Array[Text] do
1146 var retarr = new Array[Text].with_capacity(length / ln + length % ln)
1148 retarr.add(substring(st - ln, ln))
1151 return retarr.reversed
1155 # All kinds of array-based text representations.
1156 abstract class FlatText
1159 # Underlying C-String (`char*`)
1161 # Warning : Might be void in some subclasses, be sure to check
1162 # if set before using it.
1163 var items: NativeString is noinit
1165 # Returns a char* starting at position `first_byte`
1167 # WARNING: If you choose to use this service, be careful of the following.
1169 # Strings and NativeString are *ideally* always allocated through a Garbage Collector.
1170 # Since the GC tracks the use of the pointer for the beginning of the char*, it may be
1171 # deallocated at any moment, rendering the pointer returned by this function invalid.
1172 # Any access to freed memory may very likely cause undefined behaviour or a crash.
1173 # (Failure to do so will most certainly result in long and painful debugging hours)
1175 # The only safe use of this pointer is if it is ephemeral (e.g. read in a C function
1176 # then immediately return).
1178 # As always, do not modify the content of the String in C code, if this is what you want
1179 # copy locally the char* as Nit Strings are immutable.
1180 fun fast_cstring: NativeString is abstract
1182 redef var length = 0
1184 redef var bytelen = 0
1195 redef fun copy_to_native(dest, n, src_offset, dest_offset) do
1196 items.copy_to(dest, n, src_offset, dest_offset)
1200 # Abstract class for the SequenceRead compatible
1201 # views on the chars of any Text
1202 private abstract class StringCharView
1203 super SequenceRead[Char]
1207 var target: SELFTYPE
1209 redef fun is_empty do return target.is_empty
1211 redef fun length do return target.length
1213 redef fun iterator: IndexedIterator[Char] do return self.iterator_from(0)
1215 redef fun reverse_iterator do return self.reverse_iterator_from(self.length - 1)
1218 # Abstract class for the SequenceRead compatible
1219 # views on the bytes of any Text
1220 private abstract class StringByteView
1221 super SequenceRead[Byte]
1225 var target: SELFTYPE
1227 redef fun is_empty do return target.is_empty
1229 redef fun length do return target.bytelen
1231 redef fun iterator do return self.iterator_from(0)
1233 redef fun reverse_iterator do return self.reverse_iterator_from(target.bytelen - 1)
1236 # Immutable sequence of characters.
1238 # String objects may be created using literals.
1240 # assert "Hello World!" isa String
1241 abstract class String
1244 redef type SELFTYPE: String is fixed
1246 redef fun to_s do return self
1248 # Concatenates `o` to `self`
1250 # assert "hello" + "world" == "helloworld"
1251 # assert "" + "hello" + "" == "hello"
1252 fun +(o: Text): SELFTYPE is abstract
1254 # Concatenates self `i` times
1256 # assert "abc" * 4 == "abcabcabcabc"
1257 # assert "abc" * 1 == "abc"
1258 # assert "abc" * 0 == ""
1259 fun *(i: Int): SELFTYPE is abstract
1261 # Insert `s` at `pos`.
1263 # assert "helloworld".insert_at(" ", 5) == "hello world"
1264 fun insert_at(s: String, pos: Int): SELFTYPE is abstract
1266 redef fun substrings is abstract
1268 # Returns a reversed version of self
1270 # assert "hello".reversed == "olleh"
1271 # assert "bob".reversed == "bob"
1272 # assert "".reversed == ""
1273 fun reversed: SELFTYPE is abstract
1275 # A upper case version of `self`
1277 # assert "Hello World!".to_upper == "HELLO WORLD!"
1278 fun to_upper: SELFTYPE is abstract
1280 # A lower case version of `self`
1282 # assert "Hello World!".to_lower == "hello world!"
1283 fun to_lower : SELFTYPE is abstract
1285 # Takes a camel case `self` and converts it to snake case
1287 # assert "randomMethodId".to_snake_case == "random_method_id"
1289 # The rules are the following:
1291 # An uppercase is always converted to a lowercase
1293 # assert "HELLO_WORLD".to_snake_case == "hello_world"
1295 # An uppercase that follows a lowercase is prefixed with an underscore
1297 # assert "HelloTheWORLD".to_snake_case == "hello_the_world"
1299 # An uppercase that follows an uppercase and is followed by a lowercase, is prefixed with an underscore
1301 # assert "HelloTHEWorld".to_snake_case == "hello_the_world"
1303 # All other characters are kept as is; `self` does not need to be a proper CamelCased string.
1305 # assert "=-_H3ll0Th3W0rld_-=".to_snake_case == "=-_h3ll0th3w0rld_-="
1306 fun to_snake_case: SELFTYPE
1308 if self.is_lower then return self
1310 var new_str = new Buffer.with_cap(self.length)
1311 var prev_is_lower = false
1312 var prev_is_upper = false
1314 for i in [0..length[ do
1316 if char.is_lower then
1318 prev_is_lower = true
1319 prev_is_upper = false
1320 else if char.is_upper then
1321 if prev_is_lower then
1323 else if prev_is_upper and i+1 < length and chars[i+1].is_lower then
1326 new_str.add(char.to_lower)
1327 prev_is_lower = false
1328 prev_is_upper = true
1331 prev_is_lower = false
1332 prev_is_upper = false
1339 # Takes a snake case `self` and converts it to camel case
1341 # assert "random_method_id".to_camel_case == "randomMethodId"
1343 # If the identifier is prefixed by an underscore, the underscore is ignored
1345 # assert "_private_field".to_camel_case == "_privateField"
1347 # If `self` is upper, it is returned unchanged
1349 # assert "RANDOM_ID".to_camel_case == "RANDOM_ID"
1351 # If there are several consecutive underscores, they are considered as a single one
1353 # assert "random__method_id".to_camel_case == "randomMethodId"
1354 fun to_camel_case: SELFTYPE
1356 if self.is_upper then return self
1358 var new_str = new Buffer
1359 var is_first_char = true
1360 var follows_us = false
1362 for i in [0..length[ do
1364 if is_first_char then
1366 is_first_char = false
1367 else if char == '_
' then
1369 else if follows_us then
1370 new_str.add(char.to_upper)
1380 # Returns a capitalized `self`
1382 # Letters that follow a letter are lowercased
1383 # Letters that follow a non-letter are upcased.
1385 # SEE : `Char::is_letter` for the definition of letter.
1387 # assert "jAVASCRIPT".capitalized == "Javascript"
1388 # assert "i am root".capitalized == "I Am Root"
1389 # assert "ab_c -ab0c ab\nc".capitalized == "Ab_C -Ab0C Ab\nC"
1390 fun capitalized: SELFTYPE do
1391 if length == 0 then return self
1393 var buf = new Buffer.with_cap(length)
1395 var curr = chars[0].to_upper
1399 for i in [1 .. length[ do
1402 if prev.is_letter then
1403 buf[i] = curr.to_lower
1405 buf[i] = curr.to_upper
1413 # A mutable sequence of characters.
1414 abstract class Buffer
1417 # Returns an arbitrary subclass of `Buffer` with default parameters
1420 # Returns an instance of a subclass of `Buffer` with `i` base capacity
1421 new with_cap(i: Int) is abstract
1423 redef type SELFTYPE: Buffer is fixed
1425 # Specific implementations MUST set this to `true` in order to invalidate caches
1426 protected var is_dirty = true
1428 # Copy-On-Write flag
1430 # If the `Buffer` was to_s'd
, the next in-place altering
1431 # operation will cause the current `Buffer` to be re-allocated.
1433 # The flag will then be set at `false`.
1434 protected var written
= false
1436 # Modifies the char contained at pos `index`
1438 # DEPRECATED : Use self.chars.[]= instead
1439 fun []=(index
: Int, item
: Char) is abstract
1441 # Adds a char `c` at the end of self
1443 # DEPRECATED : Use self.chars.add instead
1444 fun add
(c
: Char) is abstract
1448 # var b = new Buffer
1450 # assert not b.is_empty
1453 fun clear
is abstract
1455 # Enlarges the subsequent array containing the chars of self
1456 fun enlarge
(cap
: Int) is abstract
1458 # Adds the content of text `s` at the end of self
1460 # var b = new Buffer
1463 # assert b == "helloworld"
1464 fun append
(s
: Text) is abstract
1466 # `self` is appended in such a way that `self` is repeated `r` times
1468 # var b = new Buffer
1471 # assert b == "hellohellohello"
1472 fun times
(r
: Int) is abstract
1474 # Reverses itself in-place
1476 # var b = new Buffer
1479 # assert b == "olleh"
1480 fun reverse
is abstract
1482 # Changes each lower-case char in `self` by its upper-case variant
1484 # var b = new Buffer
1485 # b.append("Hello World!")
1487 # assert b == "HELLO WORLD!"
1488 fun upper
is abstract
1490 # Changes each upper-case char in `self` by its lower-case variant
1492 # var b = new Buffer
1493 # b.append("Hello World!")
1495 # assert b == "hello world!"
1496 fun lower
is abstract
1498 # Capitalizes each word in `self`
1500 # Letters that follow a letter are lowercased
1501 # Letters that follow a non-letter are upcased.
1503 # SEE: `Char::is_letter` for the definition of a letter.
1505 # var b = new FlatBuffer.from("jAVAsCriPt")
1507 # assert b == "Javascript"
1508 # b = new FlatBuffer.from("i am root")
1510 # assert b == "I Am Root"
1511 # b = new FlatBuffer.from("ab_c -ab0c ab\nc")
1513 # assert b == "Ab_C -Ab0C Ab\nC"
1515 if length
== 0 then return
1516 var c
= self[0].to_upper
1519 for i
in [1 .. length
[ do
1522 if prev
.is_letter
then
1523 self[i
] = c
.to_lower
1525 self[i
] = c
.to_upper
1532 if is_dirty
then hash_cache
= null
1536 # In Buffers, the internal sequence of character is mutable
1537 # Thus, `chars` can be used to modify the buffer.
1538 redef fun chars
: Sequence[Char] is abstract
1540 # Appends `length` chars from `s` starting at index `from`
1543 # var b = new Buffer
1544 # b.append_substring("abcde", 1, 2)
1546 # b.append_substring("vwxyz", 2, 3)
1547 # assert b == "bcxyz"
1548 # b.append_substring("ABCDE", 4, 300)
1549 # assert b == "bcxyzE"
1550 # b.append_substring("VWXYZ", 400, 1)
1551 # assert b == "bcxyzE"
1553 fun append_substring
(s
: Text, from
, length
: Int) do
1559 if (length
+ from
) > ln
then length
= ln
- from
1560 if length
<= 0 then return
1561 append_substring_impl
(s
, from
, length
)
1564 # Unsafe version of `append_substring` for performance
1566 # NOTE: Use only if sure about `from` and `length`, no checks
1567 # or bound recalculation is done
1568 fun append_substring_impl
(s
: Text, from
, length
: Int) do
1570 for i
in [0 .. length
[ do
1577 # View for chars on Buffer objects, extends Sequence
1578 # for mutation operations
1579 private abstract class BufferCharView
1580 super StringCharView
1581 super Sequence[Char]
1583 redef type SELFTYPE: Buffer
1587 # View for bytes on Buffer objects, extends Sequence
1588 # for mutation operations
1589 private abstract class BufferByteView
1590 super StringByteView
1592 redef type SELFTYPE: Buffer
1596 # User readable representation of `self`.
1597 fun to_s
: String do return inspect
1599 # The class name of the object in NativeString format.
1600 private fun native_class_name
: NativeString is intern
1602 # The class name of the object.
1604 # assert 5.class_name == "Int"
1605 fun class_name
: String do return native_class_name
.to_s
1607 # Developer readable representation of `self`.
1608 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
1611 return "<{inspect_head}>"
1614 # Return "CLASSNAME:#OBJECTID".
1615 # This function is mainly used with the redefinition of the inspect method
1616 protected fun inspect_head
: String
1618 return "{class_name}:#{object_id.to_hex}"
1623 # assert true.to_s == "true"
1624 # assert false.to_s == "false"
1636 # C function to calculate the length of the `NativeString` to receive `self`
1637 private fun byte_to_s_len
: Int `{
1638 return snprintf(NULL, 0, "0x%02x", self);
1641 # C function to convert an nit Int to a NativeString (char*)
1642 private fun native_byte_to_s
(nstr
: NativeString, strlen
: Int) `{
1643 snprintf(nstr, strlen, "0x%02x", self);
1646 # Displayable byte in its hexadecimal form (0x..)
1648 # assert 1.to_b.to_s == "0x01"
1649 # assert (-123).to_b.to_s == "0x85"
1651 var nslen
= byte_to_s_len
1652 var ns
= new NativeString(nslen
+ 1)
1654 native_byte_to_s
(ns
, nslen
+ 1)
1655 return ns
.to_s_unsafe
(nslen
)
1661 # Wrapper of strerror C function
1662 private fun strerror_ext
: NativeString `{ return strerror((int)self); `}
1664 # Returns a string describing error number
1665 fun strerror: String do return strerror_ext.to_s
1667 # Fill `s
` with the digits in base `base
` of `self` (and with the '-' sign if negative).
1668 # assume < to_c max const of char
1669 private fun fill_buffer(s: Buffer, base: Int)
1676 else if self == 0 then
1683 var pos = digit_count(base) - 1
1684 while pos >= 0 and n > 0 do
1685 s.chars[pos] = (n % base).to_c
1691 # C function to calculate the length of the `NativeString` to receive `self`
1692 private fun int_to_s_len: Int `{
1693 return snprintf
(NULL, 0, "%ld", self);
1696 # C function to convert an nit Int to a NativeString (char*)
1697 private fun native_int_to_s(nstr: NativeString, strlen: Int) `{
1698 snprintf
(nstr
, strlen
, "%ld", self);
1701 # String representation of `self` in the given `base
`
1704 # assert 15.to_base(10) == "15"
1705 # assert 15.to_base(16) == "f"
1706 # assert 15.to_base(2) == "1111"
1707 # assert (-10).to_base(3) == "-101"
1709 fun to_base(base: Int): String
1711 var l = digit_count(base)
1714 for x in [0..l[ do s.add(' ')
1715 fill_buffer(s, base)
1720 # return displayable int in hexadecimal
1722 # assert 1.to_hex == "1"
1723 # assert (-255).to_hex == "-ff"
1724 fun to_hex: String do return to_base(16)
1728 # Pretty representation of `self`, with decimals as needed from 1 to a maximum of 3
1730 # assert 12.34.to_s == "12.34"
1731 # assert (-0120.030).to_s == "-120.03"
1733 # see `to_precision
` for a custom precision.
1735 var str = to_precision( 3 )
1736 if is_inf != 0 or is_nan then return str
1737 var len = str.length
1738 for i in [0..len-1] do
1740 var c = str.chars[j]
1743 else if c == '.' then
1744 return str.substring( 0, j+2 )
1746 return str.substring( 0, j+1 )
1752 # `String` representation of `self` with the given number of `decimals
`
1754 # assert 12.345.to_precision(0) == "12"
1755 # assert 12.345.to_precision(3) == "12.345"
1756 # assert (-12.345).to_precision(3) == "-12.345"
1757 # assert (-0.123).to_precision(3) == "-0.123"
1758 # assert 0.999.to_precision(2) == "1.00"
1759 # assert 0.999.to_precision(4) == "0.9990"
1760 fun to_precision(decimals: Int): String
1762 if is_nan then return "nan"
1764 var isinf = self.is_inf
1767 else if isinf == -1 then
1771 if decimals == 0 then return self.to_i.to_s
1773 for i in [0..decimals[ do f = f * 10.0
1780 if i == 0 then return "0." + "0"*decimals
1782 # Prepare both parts of the float, before and after the "."
1787 if sl > decimals then
1788 # Has something before the "."
1789 p1 = s.substring(0, sl-decimals)
1790 p2 = s.substring(sl-decimals, decimals)
1793 p2 = "0"*(decimals-sl) + s
1796 if i < 0 then p1 = "-" + p1
1798 return p1 + "." + p2
1804 # Returns a sequence with the UTF-8 bytes of `self`
1806 # assert 'a'.bytes == [0x61u8]
1807 # assert 'ま'.bytes == [0xE3u8, 0x81u8, 0xBEu8]
1808 fun bytes: SequenceRead[Byte] do return to_s.bytes
1810 # Is `self` an UTF-16 surrogate pair ?
1811 fun is_surrogate: Bool do
1813 return cp >= 0xD800 and cp <= 0xDFFF
1816 # Is `self` a UTF-16 high surrogate ?
1817 fun is_hi_surrogate: Bool do
1819 return cp >= 0xD800 and cp <= 0xDBFF
1822 # Is `self` a UTF-16 low surrogate ?
1823 fun is_lo_surrogate: Bool do
1825 return cp >= 0xDC00 and cp <= 0xDFFF
1828 # Length of `self` in a UTF-8 String
1829 fun u8char_len: Int do
1830 var c = self.code_point
1831 if c < 0x80 then return 1
1832 if c <= 0x7FF then return 2
1833 if c <= 0xFFFF then return 3
1834 if c <= 0x10FFFF then return 4
1835 # Bad character format
1839 # assert 'x'.to_s == "x"
1842 var ns = new NativeString(ln + 1)
1844 return ns.to_s_unsafe(ln)
1847 # Returns `self` escaped to UTF-16
1849 # i.e. Represents `self`.`code_point
` using UTF-16 codets escaped
1852 # assert 'A'.escape_to_utf16 == "\\u0041"
1853 # assert 'è'.escape_to_utf16 == "\\u00e8"
1854 # assert 'あ'.escape_to_utf16 == "\\u3042"
1855 # assert '𐏓'.escape_to_utf16 == "\\ud800\\udfd3"
1856 fun escape_to_utf16: String do
1859 if cp < 0xD800 or (cp >= 0xE000 and cp <= 0xFFFF) then
1860 buf = new Buffer.with_cap(6)
1861 buf.append("\\u0000")
1864 for i in hx.chars.reverse_iterator do
1869 buf = new Buffer.with_cap(12)
1870 buf.append("\\u0000\\u0000")
1871 var lo = (((cp - 0x10000) & 0x3FF) + 0xDC00).to_hex
1872 var hi = ((((cp - 0x10000) & 0xFFC00) >> 10) + 0xD800).to_hex
1887 private fun u8char_tos(r: NativeString, len: Int) `{
1894 r
[0] = 0xC0 | ((self & 0x7C0) >> 6);
1895 r
[1] = 0x80 | (self & 0x3F);
1898 r
[0] = 0xE0 | ((self & 0xF000) >> 12);
1899 r
[1] = 0x80 | ((self & 0xFC0) >> 6);
1900 r
[2] = 0x80 | (self & 0x3F);
1903 r
[0] = 0xF0 | ((self & 0x1C0000) >> 18);
1904 r
[1] = 0x80 | ((self & 0x3F000) >> 12);
1905 r
[2] = 0x80 | ((self & 0xFC0) >> 6);
1906 r
[3] = 0x80 | (self & 0x3F);
1911 # Returns true if the char is a numerical digit
1913 # assert '0'.is_numeric
1914 # assert '9'.is_numeric
1915 # assert not 'a'.is_numeric
1916 # assert not '?'.is_numeric
1918 # FIXME: Works on ASCII-range only
1919 fun is_numeric: Bool
1921 return self >= '0' and self <= '9'
1924 # Returns true if the char is an alpha digit
1926 # assert 'a'.is_alpha
1927 # assert 'Z'.is_alpha
1928 # assert not '0'.is_alpha
1929 # assert not '?'.is_alpha
1931 # FIXME: Works on ASCII-range only
1934 return (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z')
1937 # Is `self` an hexadecimal digit ?
1939 # assert 'A'.is_hexdigit
1940 # assert not 'G'.is_hexdigit
1941 # assert 'a'.is_hexdigit
1942 # assert not 'g'.is_hexdigit
1943 # assert '5'.is_hexdigit
1944 fun is_hexdigit: Bool do return (self >= '0' and self <= '9') or (self >= 'A' and self <= 'F') or
1945 (self >= 'a' and self <= 'f')
1947 # Returns true if the char is an alpha or a numeric digit
1949 # assert 'a'.is_alphanumeric
1950 # assert 'Z'.is_alphanumeric
1951 # assert '0'.is_alphanumeric
1952 # assert '9'.is_alphanumeric
1953 # assert not '?'.is_alphanumeric
1955 # FIXME: Works on ASCII-range only
1956 fun is_alphanumeric: Bool
1958 return self.is_numeric or self.is_alpha
1961 # Returns `self` to its int value
1963 # REQUIRE: `is_hexdigit
`
1964 fun from_hex: Int do
1965 if self >= '0' and self <= '9' then return code_point - 0x30
1966 if self >= 'A' and self <= 'F' then return code_point - 0x37
1967 if self >= 'a' and self <= 'f' then return code_point - 0x57
1968 # Happens if self is not a hexdigit
1969 assert self.is_hexdigit
1970 # To make flow analysis happy
1975 redef class Collection[E]
1976 # String representation of the content of the collection.
1978 # The standard representation is the list of elements separated with commas.
1981 # assert [1,2,3].to_s == "[1,2,3]"
1982 # assert [1..3].to_s == "[1,2,3]"
1983 # assert (new Array[Int]).to_s == "[]" # empty collection
1986 # Subclasses may return a more specific string representation.
1989 return "[" + join(",") + "]"
1992 # Concatenate elements without separators
1995 # assert [1,2,3].plain_to_s == "123"
1996 # assert [11..13].plain_to_s == "111213"
1997 # assert (new Array[Int]).plain_to_s == "" # empty collection
1999 fun plain_to_s: String
2002 for e in self do if e != null then s.append(e.to_s)
2006 # Concatenate and separate each elements with `separator
`.
2008 # Only concatenate if `separator
== null`.
2010 # assert [1, 2, 3].join(":") == "1:2:3"
2011 # assert [1..3].join(":") == "1:2:3"
2012 # assert [1..3].join == "123"
2014 # if `last_separator
` is given, then it is used to separate the last element.
2016 # assert [1, 2, 3, 4].join(", ", " and ") == "1, 2, 3 and 4"
2017 fun join(separator: nullable Text, last_separator: nullable Text): String
2019 if is_empty then return ""
2021 var s = new Buffer # Result
2026 if e != null then s.append(e.to_s)
2028 if last_separator == null then last_separator = separator
2030 # Concat other items
2036 if separator != null then s.append(separator)
2038 if last_separator != null then s.append(last_separator)
2040 if e != null then s.append(e.to_s)
2046 redef class Map[K,V]
2047 # Concatenate couples of key value.
2048 # Key and value are separated by `couple_sep
`.
2049 # Couples are separated by `sep
`.
2051 # var m = new HashMap[Int, String]
2054 # assert m.join("; ", "=") == "1=one; 10=ten"
2055 fun join(sep, couple_sep: String): String is abstract
2059 private var args_cache: nullable Sequence[String] = null
2061 # The arguments of the program as given by the OS
2062 fun program_args: Sequence[String]
2064 if _args_cache == null then init_args
2065 return _args_cache.as(not null)
2068 # The name of the program as given by the OS
2069 fun program_name: String
2071 return native_argv(0).to_s
2074 # Initialize `program_args
` with the contents of `native_argc
` and `native_argv
`.
2075 private fun init_args
2077 var argc = native_argc
2078 var args = new Array[String].with_capacity(0)
2081 args[i-1] = native_argv(i).to_s
2087 # First argument of the main C function.
2088 private fun native_argc: Int is intern
2090 # Second argument of the main C function.
2091 private fun native_argv(i: Int): NativeString is intern
2094 # Comparator that efficienlty use `to_s
` to compare things
2096 # The comparaison call `to_s
` on object and use the result to order things.
2098 # var a = [1, 2, 3, 10, 20]
2099 # (new CachedAlphaComparator).sort(a)
2100 # assert a == [1, 10, 2, 20, 3]
2102 # Internally the result of `to_s
` is cached in a HashMap to counter
2103 # uneficient implementation of `to_s
`.
2105 # Note: it caching is not usefull, see `alpha_comparator
`
2106 class CachedAlphaComparator
2108 redef type COMPARED: Object
2110 private var cache = new HashMap[Object, String]
2112 private fun do_to_s(a: Object): String do
2113 if cache.has_key(a) then return cache[a]
2119 redef fun compare(a, b) do
2120 return do_to_s(a) <=> do_to_s(b)
2124 # see `alpha_comparator
`
2125 private class AlphaComparator
2127 redef fun compare(a, b) do return a.to_s <=> b.to_s
2130 # Stateless comparator that naively use `to_s
` to compare things.
2132 # Note: the result of `to_s
` is not cached, thus can be invoked a lot
2133 # on a single instace. See `CachedAlphaComparator` as an alternative.
2135 # var a = [1, 2, 3, 10, 20]
2136 # alpha_comparator.sort(a)
2137 # assert a == [1, 10, 2, 20, 3]
2138 fun alpha_comparator: Comparator do return once new AlphaComparator
2140 # The arguments of the program as given by the OS
2141 fun args: Sequence[String]
2143 return sys.program_args
2146 redef class NativeString
2147 # Get a `String` from the data at `self` copied into Nit memory
2149 # Require: `self` is a null-terminated string.
2150 fun to_s_with_copy: String is abstract
2152 # Get a `String` from `length
` bytes at `self`
2154 # The result may point to the data at `self` or
2155 # it may make a copy in Nit controlled memory.
2156 # This method should only be used when `self` was allocated by the Nit GC,
2157 # or when manually controlling the deallocation of `self`.
2158 fun to_s_with_length(length: Int): String is abstract
2160 # Get a `String` from the raw `length
` bytes at `self`
2162 # The default value of `length
` is the number of bytes before
2163 # the first null character.
2165 # The created `String` points to the data at `self`.
2166 # This method should be used when `self` was allocated by the Nit GC,
2167 # or when manually controlling the deallocation of `self`.
2169 # /!\: This service does not clean the items for compliance with UTF-8,
2170 # use only when the data has already been verified as valid UTF-8.
2171 fun to_s_unsafe(length: nullable Int): String is abstract
2173 # Get a `String` from the raw `bytelen
` bytes at `self` with `unilen
` Unicode characters
2175 # The created `String` points to the data at `self`.
2176 # This method should be used when `self` was allocated by the Nit GC,
2177 # or when manually controlling the deallocation of `self`.
2179 # /!\: This service does not clean the items for compliance with UTF-8,
2180 # use only when the data has already been verified as valid UTF-8.
2182 # SEE: `abstract_text
::Text` for more info on the difference
2183 # between `Text::bytelen
` and `Text::length
`.
2184 fun to_s_full(bytelen, unilen: Int): String is abstract
2186 # Copies the content of `src
` to `self`
2188 # NOTE: `self` must be large enough to withold `self.bytelen
` bytes
2189 fun fill_from(src: Text) do src.copy_to_native(self, src.bytelen, 0, 0)
2192 redef class NativeArray[E]
2193 # Join all the elements using `to_s
`
2195 # REQUIRE: `self isa NativeArray[String]`
2196 # REQUIRE: all elements are initialized
2197 fun native_to_s: String is abstract