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
30 redef type OTHER: Text
32 # Type of self (used for factorization of several methods, ex : substring_from, empty...)
35 # Gets a view on the chars of the Text object
38 # assert "hello".chars.to_a == ['h', 'e', 'l', 'l', 'o']
40 fun chars
: SequenceRead[Char] is abstract
42 # Gets a view on the bytes of the Text object
45 # assert "hello".bytes.to_a == [104, 101, 108, 108, 111]
47 fun bytes
: SequenceRead[Int] is abstract
49 # Number of characters contained in self.
52 # assert "12345".length == 5
53 # assert "".length == 0
54 # assert "あいうえお".length == 5
56 fun length
: Int is abstract
58 # Number of bytes in `self`
61 # assert "12345".byte_length == 5
62 # assert "あいうえお".byte_length == 15
64 fun byte_length
: Int is abstract
69 # assert "abcd".substring(1, 2) == "bc"
70 # assert "abcd".substring(-1, 2) == "a"
71 # assert "abcd".substring(1, 0) == ""
72 # assert "abcd".substring(2, 5) == "cd"
73 # assert "あいうえお".substring(1,3) == "いうえ"
76 # A `from` index < 0 will be replaced by 0.
77 # Unless a `count` value is > 0 at the same time.
78 # In this case, `from += count` and `count -= from`.
79 fun substring
(from
: Int, count
: Int): SELFTYPE is abstract
81 # Iterates on the substrings of self if any
82 private fun substrings
: Iterator[FlatText] is abstract
84 # Is the current Text empty (== "")
88 # assert not "foo".is_empty
90 fun is_empty
: Bool do return self.length
== 0
92 # Returns an empty Text of the right type
94 # This method is used internally to get the right
95 # implementation of an empty string.
96 protected fun empty
: SELFTYPE is abstract
98 # Returns a copy of `self` as a Buffer
99 fun to_buffer
: Buffer is abstract
101 # Gets the first char of the Text
102 fun first
: Char do return self.chars
[0]
104 # Access a character at `index` in the string.
107 # assert "abcd"[2] == 'c'
109 fun [](index
: Int): Char do return self.chars
[index
]
111 # Gets the index of the first occurence of 'c'
113 # Returns -1 if not found
114 fun index_of
(c
: Char): Int
116 return index_of_from
(c
, 0)
119 # Gets the last char of self
120 fun last
: Char do return self.chars
[length-1
]
122 # Gets the index of the first occurence of ´c´ starting from ´pos´
124 # Returns -1 if not found
125 fun index_of_from
(c
: Char, pos
: Int): Int
127 var iter
= self.chars
.iterator_from
(pos
)
129 if iter
.item
== c
then return iter
.index
135 # Gets the last index of char ´c´
137 # Returns -1 if not found
138 fun last_index_of
(c
: Char): Int
140 return last_index_of_from
(c
, length
- 1)
143 # Return a null terminated char *
144 fun to_cstring
: CString is abstract
146 # The index of the last occurrence of an element starting from pos (in reverse order).
149 # var s = "/etc/bin/test/test.nit"
150 # assert s.last_index_of_from('/', s.length-1) == 13
151 # assert s.last_index_of_from('/', 12) == 8
154 # Returns -1 if not found
155 fun last_index_of_from
(item
: Char, pos
: Int): Int do return chars
.last_index_of_from
(item
, pos
)
157 # Concatenates `o` to `self`
160 # assert "hello" + "world" == "helloworld"
161 # assert "" + "hello" + "" == "hello"
163 fun +(o
: Text): SELFTYPE is abstract
165 # Gets an iterator on the chars of self
166 fun iterator
: Iterator[Char]
168 return self.chars
.iterator
172 # Gets an Array containing the chars of self
173 fun to_a
: Array[Char] do return chars
.to_a
175 # Create a substring from `self` beginning at the `from` position
178 # assert "abcd".substring_from(1) == "bcd"
179 # assert "abcd".substring_from(-1) == "abcd"
180 # assert "abcd".substring_from(2) == "cd"
183 # As with substring, a `from` index < 0 will be replaced by 0
184 fun substring_from
(from
: Int): SELFTYPE
186 if from
>= self.length
then return empty
187 if from
< 0 then from
= 0
188 return substring
(from
, length
- from
)
191 # Does self have a substring `str` starting from position `pos`?
194 # assert "abcd".has_substring("bc",1) == true
195 # assert "abcd".has_substring("bc",2) == false
198 # Returns true iff all characters of `str` are presents
199 # at the expected index in `self.`
200 # The first character of `str` being at `pos`, the second
201 # character being at `pos+1` and so on...
203 # This means that all characters of `str` need to be inside `self`.
206 # assert "abcd".has_substring("xab", -1) == false
207 # assert "abcd".has_substring("cdx", 2) == false
210 # And that the empty string is always a valid substring.
213 # assert "abcd".has_substring("", 2) == true
214 # assert "abcd".has_substring("", 200) == true
216 fun has_substring
(str
: String, pos
: Int): Bool
218 if str
.is_empty
then return true
219 if pos
< 0 or pos
+ str
.length
> length
then return false
220 var myiter
= self.chars
.iterator_from
(pos
)
221 var itsiter
= str
.chars
.iterator
222 while myiter
.is_ok
and itsiter
.is_ok
do
223 if myiter
.item
!= itsiter
.item
then return false
227 if itsiter
.is_ok
then return false
231 # Is this string prefixed by `prefix`?
234 # assert "abcd".has_prefix("ab") == true
235 # assert "abcbc".has_prefix("bc") == false
236 # assert "ab".has_prefix("abcd") == false
238 fun has_prefix
(prefix
: String): Bool do return has_substring
(prefix
,0)
240 # Is this string suffixed by `suffix`?
243 # assert "abcd".has_suffix("abc") == false
244 # assert "abcd".has_suffix("bcd") == true
246 fun has_suffix
(suffix
: String): Bool do return has_substring
(suffix
, length
- suffix
.length
)
248 # Returns `self` as the corresponding integer
251 # assert "123".to_i == 123
252 # assert "-1".to_i == -1
253 # assert "0x64".to_i == 100
254 # assert "0b1100_0011".to_i== 195
255 # assert "--12".to_i == 12
256 # assert "+45".to_i == 45
259 # REQUIRE: `self`.`is_int`
260 fun to_i
: Int is abstract
262 # If `self` contains a float, return the corresponding float
265 # assert "123".to_f == 123.0
266 # assert "-1".to_f == -1.0
267 # assert "-1.2e-3".to_f == -0.0012
272 return to_s
.to_cstring
.atof
275 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
278 # assert "ff".to_hex == 255
280 fun to_hex
(pos
, ln
: nullable Int): Int do
282 if pos
== null then pos
= 0
283 if ln
== null then ln
= length
- pos
285 for i
in [pos
.. max
[ do
287 res
+= self[i
].from_hex
292 # If `self` contains only digits <= '7', return the corresponding integer.
295 # assert "714".to_oct == 460
297 fun to_oct
: Int do return a_to
(8)
299 # If `self` contains only '0' et '1', return the corresponding integer.
302 # assert "101101".to_bin == 45
304 fun to_bin
: Int do return a_to
(2)
306 # If `self` contains only digits '0' .. '9', return the corresponding integer.
309 # assert "108".to_dec == 108
311 fun to_dec
: Int do return a_to
(10)
313 # If `self` contains only digits and letters, return the corresponding integer in a given base
316 # assert "120".a_to(3) == 15
318 fun a_to
(base
: Int) : Int
323 for j
in [0..length
[ do
345 # Is this string in a valid numeric format compatible with `to_f`?
348 # assert "123".is_numeric == true
349 # assert "1.2".is_numeric == true
350 # assert "-1.2".is_numeric == true
351 # assert "-1.23e-2".is_numeric == true
352 # assert "1..2".is_numeric == false
353 # assert "".is_numeric == false
357 var has_point
= false
359 for i
in [0..length
[ do
361 if not c
.is_numeric
then
362 if c
== '.' and not has_point
then
364 else if c
== 'e' and e_index
== -1 and i
> 0 and i
< length
- 1 and chars
[i-1
] != '-' then
366 else if c
== '-' and i
== e_index
+ 1 and i
< length
- 1 then
375 # Returns `true` if the string contains only Hex chars
378 # assert "048bf".is_hex == true
379 # assert "ABCDEF".is_hex == true
380 # assert "0G".is_hex == false
384 for i
in [0..length
[ do
386 if not (c
>= 'a' and c
<= 'f') and
387 not (c
>= 'A' and c
<= 'F') and
388 not (c
>= '0' and c
<= '9') then return false
393 # Returns `true` if the string contains only Binary digits
396 # assert "1101100".is_bin == true
397 # assert "1101020".is_bin == false
400 for i
in chars
do if i
!= '0' and i
!= '1' then return false
404 # Returns `true` if the string contains only Octal digits
407 # assert "213453".is_oct == true
408 # assert "781".is_oct == false
411 for i
in chars
do if i
< '0' or i
> '7' then return false
415 # Returns `true` if the string contains only Decimal digits
418 # assert "10839".is_dec == true
419 # assert "164F".is_dec == false
422 for i
in chars
do if i
< '0' or i
> '9' then return false
426 # Are all letters in `self` upper-case ?
429 # assert "HELLO WORLD".is_upper == true
430 # assert "%$&%!".is_upper == true
431 # assert "hello world".is_upper == false
432 # assert "Hello World".is_upper == false
436 for i
in [0..length
[ do
438 if char
.is_lower
then return false
443 # Are all letters in `self` lower-case ?
446 # assert "hello world".is_lower == true
447 # assert "%$&%!".is_lower == true
448 # assert "Hello World".is_lower == false
452 for i
in [0..length
[ do
454 if char
.is_upper
then return false
459 # Removes the whitespaces at the beginning of self
462 # assert " \n\thello \n\t".l_trim == "hello \n\t"
465 # `Char::is_whitespace` determines what is a whitespace.
468 var iter
= self.chars
.iterator
470 if not iter
.item
.is_whitespace
then break
473 if iter
.index
== length
then return self.empty
474 return self.substring_from
(iter
.index
)
477 # Removes the whitespaces at the end of self
480 # assert " \n\thello \n\t".r_trim == " \n\thello"
483 # `Char::is_whitespace` determines what is a whitespace.
486 var iter
= self.chars
.reverse_iterator
488 if not iter
.item
.is_whitespace
then break
491 if iter
.index
< 0 then return self.empty
492 return self.substring
(0, iter
.index
+ 1)
495 # Trims trailing and preceding white spaces
498 # assert " Hello World ! ".trim == "Hello World !"
499 # assert "\na\nb\tc\t".trim == "a\nb\tc"
502 # `Char::is_whitespace` determines what is a whitespace.
503 fun trim
: SELFTYPE do return (self.l_trim
).r_trim
505 # Is the string non-empty but only made of whitespaces?
508 # assert " \n\t ".is_whitespace == true
509 # assert " hello ".is_whitespace == false
510 # assert "".is_whitespace == false
513 # `Char::is_whitespace` determines what is a whitespace.
514 fun is_whitespace
: Bool
516 if is_empty
then return false
517 for c
in self.chars
do
518 if not c
.is_whitespace
then return false
523 # Returns `self` removed from its last line terminator (if any).
526 # assert "Hello\n".chomp == "Hello"
527 # assert "Hello".chomp == "Hello"
529 # assert "\n".chomp == ""
530 # assert "".chomp == ""
533 # Line terminators are `"\n"`, `"\r\n"` and `"\r"`.
534 # A single line terminator, the last one, is removed.
537 # assert "\r\n".chomp == ""
538 # assert "\r\n\n".chomp == "\r\n"
539 # assert "\r\n\r\n".chomp == "\r\n"
540 # assert "\r\n\r".chomp == "\r\n"
543 # Note: unlike with most IO methods like `Reader::read_line`,
544 # a single `\r` is considered here to be a line terminator and will be removed.
548 if len
== 0 then return self
549 var l
= self.chars
.last
551 return substring
(0, len-1
)
552 else if l
!= '\n' then
554 else if len
> 1 and self.chars
[len-2
] == '\r' then
555 return substring
(0, len-2
)
557 return substring
(0, len-1
)
561 # Justify `self` in a space of `length`
563 # `left` is the space ratio on the left side.
564 # * 0.0 for left-justified (no space at the left)
565 # * 1.0 for right-justified (all spaces at the left)
566 # * 0.5 for centered (half the spaces at the left)
568 # `char`, or `' '` by default, is repeated to pad the empty space.
573 # assert "hello".justify(10, 0.0) == "hello "
574 # assert "hello".justify(10, 1.0) == " hello"
575 # assert "hello".justify(10, 0.5) == " hello "
576 # assert "hello".justify(10, 0.5, '.') == "..hello..."
579 # If `length` is not enough, `self` is returned as is.
582 # assert "hello".justify(2, 0.0) == "hello"
585 # REQUIRE: `left >= 0.0 and left <= 1.0`
586 # ENSURE: `self.length <= length implies result.length == length`
587 # ENSURE: `self.length >= length implies result == self`
588 fun justify
(length
: Int, left
: Float, char
: nullable Char): String
590 var pad
= (char
or else ' ').to_s
591 var diff
= length
- self.length
592 if diff
<= 0 then return to_s
593 assert left
>= 0.0 and left
<= 1.0
594 var before
= (diff
.to_f
* left
).to_i
595 return pad
* before
+ self + pad
* (diff-before
)
598 # Mangle a string to be a unique string only made of alphanumeric characters and underscores.
600 # This method is injective (two different inputs never produce the same
601 # output) and the returned string always respect the following rules:
603 # * Contains only US-ASCII letters, digits and underscores.
604 # * Never starts with a digit.
605 # * Never ends with an underscore.
606 # * Never contains two contiguous underscores.
611 # assert "42_is/The answer!".to_cmangle == "_52d2_is_47dThe_32danswer_33d"
612 # assert "__".to_cmangle == "_95d_95d"
613 # assert "__d".to_cmangle == "_95d_d"
614 # assert "_d_".to_cmangle == "_d_95d"
615 # assert "_42".to_cmangle == "_95d42"
616 # assert "foo".to_cmangle == "foo"
617 # assert "".to_cmangle == ""
619 fun to_cmangle
: String
621 if is_empty
then return ""
623 var underscore
= false
627 if c
>= '0' and c
<= '9' then
629 res
.append
(c
.code_point
.to_s
)
633 for i
in [start
..length
[ do
635 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
641 res
.append
('_'.code_point
.to_s
)
644 if c
>= '0' and c
<= '9' then
647 else if c
== '_' then
652 res
.append
(c
.code_point
.to_s
)
658 res
.append
('_'.code_point
.to_s
)
664 # Escape `"` `\` `'`, trigraphs and non printable characters using the rules of literal C strings and characters
667 # assert "abAB12<>&".escape_to_c == "abAB12<>&"
668 # assert "\n\"'\\".escape_to_c == "\\n\\\"\\'\\\\"
669 # assert "allo???!".escape_to_c == "allo??\\?!"
670 # assert "??=??/??'??(??)".escape_to_c == "?\\?=?\\?/??\\'?\\?(?\\?)"
671 # assert "??!??<??>??-".escape_to_c == "?\\?!?\\?<?\\?>?\\?-"
674 # Most non-printable characters (bellow ASCII 32) are escaped to an octal form `\nnn`.
675 # Three digits are always used to avoid following digits to be interpreted as an element
676 # of the octal sequence.
679 # assert "{0.code_point}{1.code_point}{8.code_point}{31.code_point}{32.code_point}".escape_to_c == "\\000\\001\\010\\037 "
682 # The exceptions are the common `\t` and `\n`.
683 fun escape_to_c
: String
686 for i
in [0..length
[ do
690 else if c
== '\t' then
692 else if c
== '"' then
694 else if c == '\'' then
696 else if c == '\\
' then
698 else if c == '?' then
699 # Escape if it is the last question mark of a ANSI C trigraph.
703 # We ignore `??'` because it will be escaped as `??\
'`.
716 else if c.code_point < 32 then
718 var oct = c.code_point.to_base(8)
719 # Force 3 octal digits since it is the
720 # maximum allowed in the C specification
721 if oct.length == 1 then
724 else if oct.length == 2 then
735 # Escape additionnal characters
736 # The result might no be legal in C but be used in other languages
739 # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
740 # assert "allo???!".escape_more_to_c("") == "allo??\\?!"
742 fun escape_more_to_c(chars: String): String
745 for c in escape_to_c.chars do
746 if chars.chars.has(c) then
754 # Escape to C plus braces
757 # assert "\n\"'\\\
{\}".escape_to_nit == "\\n\\\
"\\'\\\\\\\{\\\}"
759 fun escape_to_nit
: String do return escape_more_to_c
("\{\}")
761 # Escape to POSIX Shell (sh).
763 # Abort if the text contains a null byte.
766 # assert "\n\"'\\\{\}0".escape_to_sh == "'\n\"'\\''\\\{\}0'"
768 fun escape_to_sh
: String do
771 for i in [0..length[ do
776 assert without_null_byte
: c
!= '\0'
784 # Escape to include in a Makefile
786 # Unfortunately, some characters are not escapable in Makefile.
787 # These characters are `;`, `|`, `\`, and the non-printable ones.
788 # They will be rendered as `"?{hex}"`.
789 fun escape_to_mk: String do
791 for i in [0..length[ do
795 else if c == ':' or c == ' ' or c == '#' then
798 else if c
.code_point
< 32 or c
== ';' or c
== '|' or c
== '\\' then
799 b
.append
("?{c.code_point.to_base(16)}")
807 # Return a string where Nit escape sequences are transformed.
811 # assert s.length == 2
812 # var u = s.unescape_nit
813 # assert u.length == 1
814 # assert u.chars[0].code_point == 10 # (the ASCII value of the "new line" character)
816 fun unescape_nit
: String
818 var res
= new Buffer.with_cap
(self.length
)
819 var was_slash
= false
820 for i
in [0..length
[ do
822 if not was_slash
then
833 else if c
== 'r' then
835 else if c
== 't' then
837 else if c
== '0' then
846 # Returns `self` with all characters escaped with their UTF-16 representation
849 # assert "Aèあ𐏓".escape_to_utf16 == "\\u0041\\u00e8\\u3042\\ud800\\udfd3"
851 fun escape_to_utf16
: String do
853 for i
in chars
do buf
.append i
.escape_to_utf16
857 # Returns the Unicode char escaped by `self`
860 # assert "\\u0041".from_utf16_escape == 'A'
861 # assert "\\ud800\\udfd3".from_utf16_escape == '𐏓'
862 # assert "\\u00e8".from_utf16_escape == 'è'
863 # assert "\\u3042".from_utf16_escape == 'あ'
865 fun from_utf16_escape
(pos
, ln
: nullable Int): Char do
866 if pos
== null then pos
= 0
867 if ln
== null then ln
= length
- pos
868 if ln
< 6 then return 0xFFFD.code_point
869 var cp
= from_utf16_digit
(pos
+ 2).to_u32
870 if cp
< 0xD800u
32 then return cp
.code_point
871 if cp
> 0xDFFFu
32 then return cp
.code_point
872 if cp
> 0xDBFFu
32 then return 0xFFFD.code_point
873 if ln
== 6 then return 0xFFFD.code_point
874 if ln
< 12 then return 0xFFFD.code_point
876 cp
+= from_utf16_digit
(pos
+ 8).to_u32
877 var cplo
= cp
& 0xFFFFu
32
878 if cplo
< 0xDC00u
32 then return 0xFFFD.code_point
879 if cplo
> 0xDFFFu
32 then return 0xFFFD.code_point
880 return cp
.from_utf16_surr
.code_point
883 # Returns a UTF-16 escape value
886 # var s = "\\ud800\\udfd3"
887 # assert s.from_utf16_digit(2) == 0xD800
888 # assert s.from_utf16_digit(8) == 0xDFD3
890 fun from_utf16_digit
(pos
: nullable Int): Int do
891 if pos
== null then pos
= 0
892 return to_hex
(pos
, 4)
895 # Encode `self` to percent (or URL) encoding
898 # assert "aBc09-._~".to_percent_encoding == "aBc09-._~"
899 # assert "%()< >".to_percent_encoding == "%25%28%29%3c%20%3e"
900 # assert ".com/post?e=asdf&f=123".to_percent_encoding == ".com%2fpost%3fe%3dasdf%26f%3d123"
901 # assert "éあいう".to_percent_encoding == "%c3%a9%e3%81%82%e3%81%84%e3%81%86"
903 fun to_percent_encoding
: String
907 for i
in [0..length
[ do
909 if (c
>= '0' and c
<= '9') or
910 (c
>= 'a' and c
<= 'z') or
911 (c
>= 'A' and c
<= 'Z') or
912 c
== '-' or c
== '.' or
917 var bytes
= c
.to_s
.bytes
918 for b
in bytes
do buf
.append
"%{b.to_i.to_hex}"
925 # Decode `self` from percent (or URL) encoding to a clear string
927 # Invalid '%' are not decoded.
930 # assert "aBc09-._~".from_percent_encoding == "aBc09-._~"
931 # assert "%25%28%29%3c%20%3e".from_percent_encoding == "%()< >"
932 # assert ".com%2fpost%3fe%3dasdf%26f%3d123".from_percent_encoding == ".com/post?e=asdf&f=123"
933 # assert "%25%28%29%3C%20%3E".from_percent_encoding == "%()< >"
934 # assert "incomplete %".from_percent_encoding == "incomplete %"
935 # assert "invalid % usage".from_percent_encoding == "invalid % usage"
936 # assert "%c3%a9%e3%81%82%e3%81%84%e3%81%86".from_percent_encoding == "éあいう"
937 # assert "%1 %A %C3%A9A9".from_percent_encoding == "%1 %A éA9"
939 fun from_percent_encoding
: String
941 var len
= byte_length
942 var has_percent
= false
950 # If no transformation is needed, return self as a string
951 if not has_percent
then return to_s
953 var buf
= new CString(len
)
959 if i
+ 2 >= length
then
960 # What follows % has been cut off
964 var hex_s
= substring
(i
, 2)
966 var hex_i
= hex_s
.to_hex
970 # What follows a % is not Hex
975 else buf
[l
] = c
.code_point
981 return buf
.to_s_unsafe
(l
, copy
=false)
984 # Escape the characters `<`, `>`, `&`, `"`, `'` and `/` as HTML/XML entity references.
987 # assert "a&b-<>\"x\"/'".html_escape == "a&b-<>"x"/'"
990 # 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>
991 fun html_escape
: String
993 var buf
: nullable Buffer = null
995 for i
in [0..length
[ do
1000 else if c
== '<' then
1002 else if c
== '>' then
1004 else if c
== '"' then
1006 else if c
== '\'' then
1008 else if c == '/' then
1011 if buf != null then buf.add c
1016 for j in [0..i[ do buf.add chars[j]
1021 if buf == null then return self.to_s
1026 # Two pieces of text are equals if thez have the same characters in the same order.
1029 # assert "hello" == "hello"
1030 # assert "hello" != "HELLO"
1031 # assert "hello" == "hel"+"lo"
1034 # Things that are not Text are not equal.
1038 # assert "9" != ['9']
1041 # assert "9".chars.first == '9' # equality of Char
1042 # assert "9".chars == ['9'] # equality of Sequence
1043 # assert "9".to_i == 9 # equality of Int
1047 if o == null then return false
1048 if not o isa Text then return false
1049 if self.is_same_instance(o) then return true
1050 if self.length != o.length then return false
1051 return self.chars == o.chars
1054 # Lexicographical comparaison
1057 # assert "abc" < "xy"
1058 # assert "ABC" < "abc"
1062 var self_chars = self.chars.iterator
1063 var other_chars = other.chars.iterator
1065 while self_chars.is_ok and other_chars.is_ok do
1066 if self_chars.item < other_chars.item then return true
1067 if self_chars.item > other_chars.item then return false
1072 if self_chars.is_ok then
1079 # Escape string used in labels for graphviz
1082 # assert ">><<".escape_to_dot == "\\>\\>\\<\\<"
1084 fun escape_to_dot: String
1086 return escape_more_to_c("|\{\}<>")
1089 private var hash_cache: nullable Int = null
1093 if hash_cache == null then
1094 # djb2 hash algorithm
1097 for i in [0..length[ do
1099 h = (h << 5) + h + char.code_point
1104 return hash_cache.as(not null)
1107 # Format `self` by replacing each `%n` with the `n`th item of `args`
1109 # The character `%` followed by something other than a number are left as is.
1110 # To represent a `%` followed by a number, double the `%`, as in `%%7`.
1113 # assert "This %0 is a %1.".format("String", "formatted String") == "This String is a formatted String."
1114 # assert "Do not escape % nor %%1".format("unused") == "Do not escape % nor %1"
1116 fun format(args: Object...): String do
1117 var s = new Array[Text]
1121 if self[i] == '%' then
1125 while i < length and self[i].is_numeric do
1129 var ciph_len = i - ciph_st
1130 if ciph_len == 0 then
1131 # What follows '%' is not a number.
1132 s.push substring(curr_st, i - curr_st)
1133 if i < length and self[i] == '%' then
1141 var arg_index = substring(ciph_st, ciph_len).to_i
1142 if arg_index >= args.length then continue
1144 s.push substring(curr_st, fmt_st - curr_st)
1145 s.push args[arg_index].to_s
1152 s.push substring(curr_st, length - curr_st)
1156 # Return the Levenshtein distance between two strings
1159 # assert "abcd".levenshtein_distance("abcd") == 0
1160 # assert "".levenshtein_distance("abcd") == 4
1161 # assert "abcd".levenshtein_distance("") == 4
1162 # assert "abcd".levenshtein_distance("xyz") == 4
1163 # assert "abcd".levenshtein_distance("xbdy") == 3
1165 fun levenshtein_distance(other: String): Int
1167 var slen = self.length
1168 var olen = other.length
1171 if slen == 0 then return olen
1172 if olen == 0 then return slen
1173 if self == other then return 0
1175 # previous row of distances
1176 var v0 = new Array[Int].with_capacity(olen+1)
1178 # current row of distances
1179 var v1 = new Array[Int].with_capacity(olen+1)
1181 for j in [0..olen] do
1182 # prefix insert cost
1186 for i in [0..slen[ do
1188 # prefix delete cost
1191 for j in [0..olen[ do
1193 var cost1 = v1[j] + 1
1195 var cost2 = v0[j + 1] + 1
1196 # same char cost (+0)
1199 if self[i] != other[j] then cost3 += 1
1201 v1[j+1] = cost1.min(cost2).min(cost3)
1205 # * v1 become v0 in the next iteration
1206 # * old v0 is reused as the new v1
1215 # Copies `n` bytes from `self` at `src_offset` into `dest` starting at `dest_offset`
1217 # Basically a high-level synonym of CString::copy_to
1219 # REQUIRE: `n` must be large enough to contain `len` bytes
1222 # var ns = new CString(8)
1223 # "Text is String".copy_to_native(ns, 8, 2, 0)
1224 # assert ns.to_s_with_length(8) == "xt is St"
1226 fun copy_to_native(dest: CString, n, src_offset, dest_offset: Int) do
1227 var mypos = src_offset
1228 var itspos = dest_offset
1230 dest[itspos] = self.bytes[mypos]
1237 # Packs the content of a string in packs of `ln` chars.
1238 # This variant ensures that only the last element might be smaller than `ln`
1241 # var s = "abcdefghijklmnopqrstuvwxyz"
1242 # assert s.pack_l(4) == ["abcd","efgh","ijkl","mnop","qrst","uvwx","yz"]
1244 fun pack_l(ln: Int): Array[Text] do
1246 var retarr = new Array[Text].with_capacity(length / ln + length % ln)
1247 while st < length do
1248 retarr.add(substring(st, ln))
1254 # Packs the content of a string in packs of `ln` chars.
1255 # This variant ensures that only the first element might be smaller than `ln`
1258 # var s = "abcdefghijklmnopqrstuvwxyz"
1259 # assert s.pack_r(4) == ["ab","cdef","ghij","klmn","opqr","stuv","wxyz"]
1261 fun pack_r(ln: Int): Array[Text] do
1263 var retarr = new Array[Text].with_capacity(length / ln + length % ln)
1265 retarr.add(substring(st - ln, ln))
1268 return retarr.reversed
1271 # Concatenates self `i` times
1274 # assert "abc" * 4 == "abcabcabcabc"
1275 # assert "abc" * 1 == "abc"
1276 # assert "abc" * 0 == ""
1277 # var b = new Buffer
1280 # assert b == "天地天地天地天地"
1282 fun *(i: Int): SELFTYPE is abstract
1284 # Insert `s` at `pos`.
1287 # assert "helloworld".insert_at(" ", 5) == "hello world"
1288 # var b = new Buffer
1289 # b.append("Hello世界")
1290 # b = b.insert_at(" beautiful ", 5)
1291 # assert b == "Hello beautiful 世界"
1293 fun insert_at(s: String, pos: Int): SELFTYPE is abstract
1295 # Returns a reversed version of self
1298 # assert "hello".reversed == "olleh"
1299 # assert "bob".reversed == "bob"
1300 # assert "".reversed == ""
1302 fun reversed: SELFTYPE is abstract
1304 # A upper case version of `self`
1307 # assert "Hello World!".to_upper == "HELLO WORLD!"
1309 fun to_upper: SELFTYPE is abstract
1311 # A lower case version of `self`
1314 # assert "Hello World!".to_lower == "hello world!"
1316 fun to_lower : SELFTYPE is abstract
1318 # Takes a camel case `self` and converts it to snake case
1321 # assert "randomMethodId".to_snake_case == "random_method_id"
1324 # The rules are the following:
1326 # An uppercase is always converted to a lowercase
1329 # assert "HELLO_WORLD".to_snake_case == "hello_world"
1332 # An uppercase that follows a lowercase is prefixed with an underscore
1335 # assert "HelloTheWORLD".to_snake_case == "hello_the_world"
1338 # An uppercase that follows an uppercase and is followed by a lowercase, is prefixed with an underscore
1341 # assert "HelloTHEWorld".to_snake_case == "hello_the_world"
1344 # All other characters are kept as is; `self` does not need to be a proper CamelCased string.
1347 # assert "=-_H3ll0Th3W0rld_-=".to_snake_case == "=-_h3ll0th3w0rld_-="
1349 fun to_snake_case: SELFTYPE is abstract
1351 # Takes a snake case `self` and converts it to camel case
1354 # assert "random_method_id".to_camel_case == "randomMethodId"
1357 # If the identifier is prefixed by an underscore, the underscore is ignored
1360 # assert "_private_field".to_camel_case == "_privateField"
1363 # If `self` is upper, it is returned unchanged
1366 # assert "RANDOM_ID".to_camel_case == "RANDOM_ID"
1369 # If there are several consecutive underscores, they are considered as a single one
1372 # assert "random__method_id".to_camel_case == "randomMethodId"
1374 fun to_camel_case: SELFTYPE is abstract
1376 # Returns a capitalized `self`
1378 # Letters that follow a letter are lowercased
1379 # Letters that follow a non-letter are upcased.
1381 # If `keep_upper = true`, already uppercase letters are not lowercased.
1383 # SEE : `Char::is_letter` for the definition of letter.
1386 # assert "jAVASCRIPT".capitalized == "Javascript"
1387 # assert "i am root".capitalized == "I Am Root"
1388 # assert "ab_c -ab0c ab\nc".capitalized == "Ab_C -Ab0C Ab\nC"
1389 # assert "preserve my ACRONYMS".capitalized(keep_upper=true) == "Preserve My ACRONYMS"
1391 fun capitalized(keep_upper: nullable Bool): SELFTYPE do
1392 if length == 0 then return self
1394 var buf = new Buffer.with_cap(length)
1395 buf.capitalize(keep_upper=keep_upper, src=self)
1400 # All kinds of array-based text representations.
1401 abstract class FlatText
1404 # Underlying CString (`char*`)
1406 # Warning: Might be void in some subclasses, be sure to check
1407 # if set before using it.
1408 var items: CString is noinit
1410 # Returns a char* starting at position `first_byte`
1412 # WARNING: If you choose to use this service, be careful of the following.
1414 # Strings and CString are *ideally* always allocated through a Garbage Collector.
1415 # Since the GC tracks the use of the pointer for the beginning of the char*, it may be
1416 # deallocated at any moment, rendering the pointer returned by this function invalid.
1417 # Any access to freed memory may very likely cause undefined behaviour or a crash.
1418 # (Failure to do so will most certainly result in long and painful debugging hours)
1420 # The only safe use of this pointer is if it is ephemeral (e.g. read in a C function
1421 # then immediately return).
1423 # As always, do not modify the content of the String in C code, if this is what you want
1424 # copy locally the char* as Nit Strings are immutable.
1425 fun fast_cstring: CString is abstract
1427 redef var length = 0
1429 redef var byte_length = 0
1440 redef fun copy_to_native(dest, n, src_offset, dest_offset) do
1441 items.copy_to(dest, n, src_offset, dest_offset)
1445 # Abstract class for the SequenceRead compatible
1446 # views on the chars of any Text
1447 private abstract class StringCharView
1448 super SequenceRead[Char]
1452 var target: SELFTYPE
1454 redef fun is_empty do return target.is_empty
1456 redef fun length do return target.length
1458 redef fun iterator: IndexedIterator[Char] do return self.iterator_from(0)
1460 redef fun reverse_iterator do return self.reverse_iterator_from(self.length - 1)
1463 # Abstract class for the SequenceRead compatible
1464 # views on the bytes of any Text
1465 private abstract class StringByteView
1466 super SequenceRead[Int]
1470 var target: SELFTYPE
1472 redef fun is_empty do return target.is_empty
1474 redef fun length do return target.byte_length
1476 redef fun iterator do return self.iterator_from(0)
1478 redef fun reverse_iterator do return self.reverse_iterator_from(target.byte_length - 1)
1481 # Immutable sequence of characters.
1483 # String objects may be created using literals.
1486 # assert "Hello World!" isa String
1488 abstract class String
1491 redef type SELFTYPE: String is fixed
1493 redef fun to_s do return self
1495 redef fun clone do return self
1497 redef fun to_buffer do return new Buffer.from_text(self)
1499 redef fun to_camel_case do
1500 if self.is_upper then return self
1502 var new_str = new Buffer.with_cap(length)
1508 redef fun to_snake_case do
1509 if self.is_lower then return self
1511 var new_str = new Buffer.with_cap(self.length)
1518 # A mutable sequence of characters.
1519 abstract class Buffer
1522 # Returns an arbitrary subclass of `Buffer` with default parameters
1525 # Returns an instance of a subclass of `Buffer` with `i` base capacity
1526 new with_cap(i: Int) is abstract
1528 # Returns an instance of a subclass of `Buffer` with `t` as content
1529 new from_text(t: Text) do
1530 var ret = new Buffer.with_cap(t.byte_length)
1535 redef type SELFTYPE: Buffer is fixed
1537 # Copy-On-Write flag
1539 # If the `Buffer` was to_s'd
, the next in-place altering
1540 # operation will cause the current `Buffer` to be re-allocated.
1542 # The flag will then be set at `false`.
1543 protected var written
= false
1545 # Modifies the char contained at pos `index`
1546 fun []=(index
: Int, item
: Char) is abstract
1548 redef fun to_buffer
do return clone
1551 # var b = new Buffer
1552 # b.append("Buffer!")
1557 var cln
= new Buffer.with_cap
(byte_length
)
1562 # Adds a char `c` at the end of self
1563 fun add
(c
: Char) is abstract
1568 # var b = new Buffer
1570 # assert not b.is_empty
1574 fun clear
is abstract
1576 # Enlarges the subsequent array containing the chars of self
1577 fun enlarge
(cap
: Int) is abstract
1579 # Adds the content of text `s` at the end of self
1582 # var b = new Buffer
1585 # assert b == "helloworld"
1587 fun append
(s
: Text) is abstract
1589 # `self` is appended in such a way that `self` is repeated `r` times
1592 # var b = new Buffer
1595 # assert b == "hellohellohello"
1597 fun times
(r
: Int) is abstract
1599 # Reverses itself in-place
1602 # var b = new Buffer
1605 # assert b == "olleh"
1607 fun reverse
is abstract
1609 # Changes each lower-case char in `self` by its upper-case variant
1612 # var b = new Buffer
1613 # b.append("Hello World!")
1615 # assert b == "HELLO WORLD!"
1617 fun upper
is abstract
1619 # Changes each upper-case char in `self` by its lower-case variant
1622 # var b = new Buffer
1623 # b.append("Hello World!")
1625 # assert b == "hello world!"
1627 fun lower
is abstract
1629 # Capitalizes each word in `self`
1631 # Letters that follow a letter are lowercased
1632 # Letters that follow a non-letter are upcased.
1634 # If `keep_upper = true`, uppercase letters are not lowercased.
1636 # When `src` is specified, this method reads from `src` instead of `self`
1637 # but it still writes the result to the beginning of `self`.
1638 # This requires `self` to have the capacity to receive all of the
1639 # capitalized content of `src`.
1641 # SEE: `Char::is_letter` for the definition of a letter.
1644 # var b = new FlatBuffer.from("jAVAsCriPt")
1646 # assert b == "Javascript"
1647 # b = new FlatBuffer.from("i am root")
1649 # assert b == "I Am Root"
1650 # b = new FlatBuffer.from("ab_c -ab0c ab\nc")
1652 # assert b == "Ab_C -Ab0C Ab\nC"
1654 # b = new FlatBuffer.from("12345")
1655 # b.capitalize(src="foo")
1656 # assert b == "Foo45"
1658 # b = new FlatBuffer.from("preserve my ACRONYMS")
1659 # b.capitalize(keep_upper=true)
1660 # assert b == "Preserve My ACRONYMS"
1662 fun capitalize
(keep_upper
: nullable Bool, src
: nullable Text) do
1663 src
= src
or else self
1664 var length
= src
.length
1665 if length
== 0 then return
1666 keep_upper
= keep_upper
or else false
1668 var c
= src
[0].to_upper
1671 for i
in [1 .. length
[ do
1674 if prev
.is_letter
then
1678 self[i
] = c
.to_lower
1681 self[i
] = c
.to_upper
1686 # In Buffers, the internal sequence of character is mutable
1687 # Thus, `chars` can be used to modify the buffer.
1688 redef fun chars
: Sequence[Char] is abstract
1690 # Appends `length` chars from `s` starting at index `from`
1693 # var b = new Buffer
1694 # b.append_substring("abcde", 1, 2)
1696 # b.append_substring("vwxyz", 2, 3)
1697 # assert b == "bcxyz"
1698 # b.append_substring("ABCDE", 4, 300)
1699 # assert b == "bcxyzE"
1700 # b.append_substring("VWXYZ", 400, 1)
1701 # assert b == "bcxyzE"
1703 fun append_substring
(s
: Text, from
, length
: Int) do
1709 if (length
+ from
) > ln
then length
= ln
- from
1710 if length
<= 0 then return
1711 append_substring_impl
(s
, from
, length
)
1714 # Unsafe version of `append_substring` for performance
1716 # NOTE: Use only if sure about `from` and `length`, no checks
1717 # or bound recalculation is done
1718 fun append_substring_impl
(s
: Text, from
, length
: Int) do
1719 var max
= from
+ length
1720 for i
in [from
.. max
[ do add s
[i
]
1724 var ret
= new Buffer.with_cap
(byte_length
* i
)
1725 for its
in [0 .. i
[ do ret
.append
self
1729 redef fun insert_at
(s
, pos
) do
1730 var obuf
= new Buffer.with_cap
(byte_length
+ s
.byte_length
)
1731 obuf
.append_substring
(self, 0, pos
)
1733 obuf
.append_substring
(self, pos
, length
- pos
)
1737 # Inserts `s` at position `pos`
1740 # var b = new Buffer
1742 # b.insert(" nit ", 3)
1743 # assert b == "美しい nit 世界"
1745 fun insert
(s
: Text, pos
: Int) is abstract
1747 # Inserts `c` at position `pos`
1750 # var b = new Buffer
1752 # b.insert_char(' ', 3)
1753 # assert b == "美しい 世界"
1755 fun insert_char
(c
: Char, pos
: Int) is abstract
1757 # Removes a substring from `self` at position `pos`
1759 # NOTE: `length` defaults to 1, expressed in chars
1762 # var b = new Buffer
1763 # b.append("美しい 世界")
1765 # assert b == "美しい世界"
1769 fun remove_at
(pos
: Int, length
: nullable Int) is abstract
1771 redef fun reversed
do
1777 redef fun to_upper
do
1783 redef fun to_lower
do
1789 redef fun to_snake_case
do
1795 # Takes a camel case `self` and converts it to snake case
1797 # SEE: `to_snake_case`
1799 if self.is_lower
then return
1800 var prev_is_lower
= false
1801 var prev_is_upper
= false
1806 if char
.is_lower
then
1807 prev_is_lower
= true
1808 prev_is_upper
= false
1809 else if char
.is_upper
then
1810 if prev_is_lower
then
1813 else if prev_is_upper
and i
+ 1 < length
and self[i
+ 1].is_lower
then
1817 self[i
] = char
.to_lower
1818 prev_is_lower
= false
1819 prev_is_upper
= true
1821 prev_is_lower
= false
1822 prev_is_upper
= false
1828 redef fun to_camel_case
1835 # Takes a snake case `self` and converts it to camel case
1837 # SEE: `to_camel_case`
1839 if is_upper
then return
1841 var underscore_count
= 0
1844 while pos
< length
do
1845 var char
= self[pos
]
1847 underscore_count
+= 1
1848 else if underscore_count
> 0 then
1849 pos
-= underscore_count
1850 remove_at
(pos
, underscore_count
)
1851 self[pos
] = char
.to_upper
1852 underscore_count
= 0
1856 if underscore_count
> 0 then remove_at
(pos
- underscore_count
- 1, underscore_count
)
1859 redef fun capitalized
(keep_upper
) do
1860 if length
== 0 then return self
1862 var buf
= new Buffer.with_cap
(byte_length
)
1863 buf
.capitalize
(keep_upper
=keep_upper
, src
=self)
1868 # View for chars on Buffer objects, extends Sequence
1869 # for mutation operations
1870 private abstract class BufferCharView
1871 super StringCharView
1872 super Sequence[Char]
1874 redef type SELFTYPE: Buffer
1878 # View for bytes on Buffer objects, extends Sequence
1879 # for mutation operations
1880 private abstract class BufferByteView
1881 super StringByteView
1883 redef type SELFTYPE: Buffer
1887 # User readable representation of `self`.
1888 fun to_s
: String do return inspect
1890 # The class name of the object in CString format.
1891 private fun native_class_name
: CString is intern
1893 # The class name of the object.
1896 # assert 5.class_name == "Int"
1898 fun class_name
: String do return native_class_name
.to_s
1900 # Developer readable representation of `self`.
1901 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
1904 return "<{inspect_head}>"
1907 # Return "CLASSNAME:#OBJECTID".
1908 # This function is mainly used with the redefinition of the inspect method
1909 protected fun inspect_head
: String
1911 return "{class_name}:#{object_id.to_hex}"
1917 # assert true.to_s == "true"
1918 # assert false.to_s == "false"
1931 # C function to calculate the length of the `CString` to receive `self`
1932 private fun byte_to_s_len
: Int `{
1933 return snprintf(NULL, 0, "0x%02x", self);
1936 # C function to convert an nit Int to a CString (char*)
1937 private fun native_byte_to_s
(nstr
: CString, strlen
: Int) `{
1938 snprintf(nstr, strlen, "0x%02x", self);
1941 # Displayable byte in its hexadecimal form (0x..)
1944 # assert 1.to_b.to_s == "0x01"
1945 # assert (-123).to_b.to_s == "0x85"
1948 var nslen
= byte_to_s_len
1949 var ns
= new CString(nslen
+ 1)
1951 native_byte_to_s
(ns
, nslen
+ 1)
1952 return ns
.to_s_unsafe
(nslen
, copy
=false, clean
=false)
1958 # Wrapper of strerror C function
1959 private fun strerror_ext
: CString `{ return strerror((int)self); `}
1961 # Returns a string describing error number
1962 fun strerror: String do return strerror_ext.to_s
1964 # Fill `s
` with the digits in base `base
` of `self` (and with the '-' sign if negative).
1965 # assume < to_c max const of char
1966 private fun fill_buffer(s: Buffer, base: Int)
1973 else if self == 0 then
1980 var pos = digit_count(base) - 1
1981 while pos >= 0 and n > 0 do
1982 s.chars[pos] = (n % base).to_c
1988 # C function to calculate the length of the `CString` to receive `self`
1989 private fun int_to_s_len: Int `{
1990 return snprintf
(NULL, 0, "%ld", self);
1993 # C function to convert an nit Int to a CString (char*)
1994 private fun native_int_to_s(nstr: CString, strlen: Int) `{
1995 snprintf
(nstr
, strlen
, "%ld", self);
1998 # String representation of `self` in the given `base
`
2001 # assert 15.to_base(10) == "15"
2002 # assert 15.to_base(16) == "f"
2003 # assert 15.to_base(2) == "1111"
2004 # assert (-10).to_base(3) == "-101"
2006 fun to_base(base: Int): String
2008 var l = digit_count(base)
2011 for x in [0..l[ do s.add(' ')
2012 fill_buffer(s, base)
2017 # return displayable int in hexadecimal
2020 # assert 1.to_hex == "1"
2021 # assert (-255).to_hex == "-ff"
2023 fun to_hex: String do return to_base(16)
2027 # Pretty representation of `self`, with decimals as needed from 1 to a maximum of 3
2030 # assert 12.34.to_s == "12.34"
2031 # assert (-0120.030).to_s == "-120.03"
2032 # assert (-inf).to_s == "-inf"
2033 # assert (nan).to_s == "nan"
2036 # see `to_precision
` for a custom precision.
2038 var str = to_precision(3)
2039 return adapt_number_of_decimal(str, false)
2042 # Return the representation of `self`, with scientific notation
2044 # Adpat the number of decimals as needed from 1 to a maximum of 6
2046 # assert 12.34.to_sci == "1.234e+01"
2047 # assert 123.45.to_sci.to_f.to_sci == "1.2345e+02"
2048 # assert 0.001234.to_sci == "1.234e-03"
2049 # assert (inf).to_sci == "inf"
2050 # assert (nan).to_sci == "nan"
2054 var is_inf_or_nan = check_inf_or_nan
2055 if is_inf_or_nan != null then return is_inf_or_nan
2056 return adapt_number_of_decimal(return_from_specific_format("%e".to_cstring), true)
2059 # Return the `string_number
` with the adapted number of decimal (i.e the fonction remove the useless `0`)
2060 # `is_expo
` it's here to specifi if the given `string_number
` is in scientific notation
2061 private fun adapt_number_of_decimal(string_number: String, is_expo: Bool): String
2063 # check if `self` does not need an adaptation of the decimal
2064 if is_inf != 0 or is_nan then return string_number
2065 var len = string_number.length
2067 var numeric_value = ""
2068 for i in [0..len-1] do
2070 var c = string_number.chars[j]
2074 else if c == '.' then
2075 numeric_value = string_number.substring( 0, j + 2)
2078 numeric_value = string_number.substring( 0, j + 1)
2081 else if c == 'e' then
2082 expo_value = string_number.substring( j, len - 1 )
2086 return numeric_value + expo_value
2089 # Return a string representation of `self` in fonction if it is not a number or infinity.
2090 # Return `null` if `self` is not a not a number or an infinity
2091 private fun check_inf_or_nan: nullable String
2093 if is_nan then return "nan"
2095 var isinf = self.is_inf
2098 else if isinf == -1 then
2104 # `String` representation of `self` with the given number of `decimals
`
2107 # assert 12.345.to_precision(0) == "12"
2108 # assert 12.345.to_precision(3) == "12.345"
2109 # assert (-12.345).to_precision(3) == "-12.345"
2110 # assert (-0.123).to_precision(3) == "-0.123"
2111 # assert 0.999.to_precision(2) == "1.00"
2112 # assert 0.999.to_precision(4) == "0.9990"
2114 fun to_precision(decimals: Int): String
2116 var is_inf_or_nan = check_inf_or_nan
2117 if is_inf_or_nan != null then return is_inf_or_nan
2118 return return_from_specific_format("%.{decimals}f".to_cstring)
2121 # Returns the hexadecimal (`String`) representation of `self` in exponential notation
2124 # assert 12.345.to_hexa_exponential_notation == "0x1.8b0a3d70a3d71p+3"
2125 # assert 12.345.to_hexa_exponential_notation.to_f == 12.345
2127 fun to_hexa_exponential_notation: String
2129 return return_from_specific_format("%a".to_cstring)
2132 # Return the representation of `self`, with the specific given c `format
`.
2133 private fun return_from_specific_format(format: CString): String
2135 var size = to_precision_size_with_format(format)
2136 var cstr = new CString(size + 1)
2137 to_precision_fill_with_format(format, size + 1, cstr)
2138 return cstr.to_s_unsafe(byte_length = size, copy = false)
2141 # The lenght of `self` in the specific given c `format
`
2142 private fun to_precision_size_with_format(format: CString): Int`{
2143 return snprintf
(NULL, 0, format
, self);
2146 # Fill `cstr
` with `self` in the specific given c `format
`
2147 private fun to_precision_fill_with_format(format: CString, size: Int, cstr: CString) `{
2148 snprintf
(cstr
, size
, format
, self);
2154 # Returns a sequence with the UTF-8 bytes of `self`
2157 # assert 'a'.bytes == [0x61]
2158 # assert 'ま'.bytes == [0xE3, 0x81, 0xBE]
2160 fun bytes: SequenceRead[Int] do return to_s.bytes
2162 # Is `self` an UTF-16 surrogate pair ?
2163 fun is_surrogate: Bool do
2165 return cp >= 0xD800 and cp <= 0xDFFF
2168 # Is `self` a UTF-16 high surrogate ?
2169 fun is_hi_surrogate: Bool do
2171 return cp >= 0xD800 and cp <= 0xDBFF
2174 # Is `self` a UTF-16 low surrogate ?
2175 fun is_lo_surrogate: Bool do
2177 return cp >= 0xDC00 and cp <= 0xDFFF
2180 # Length of `self` in a UTF-8 String
2181 fun u8char_len: Int do
2182 var c = self.code_point
2183 if c < 0x80 then return 1
2184 if c <= 0x7FF then return 2
2185 if c <= 0xFFFF then return 3
2186 if c <= 0x10FFFF then return 4
2187 # Bad character format
2192 # assert 'x'.to_s == "x"
2196 var ns = new CString(ln + 1)
2198 return ns.to_s_unsafe(ln, copy=false, clean=false)
2201 # Returns `self` escaped to UTF-16
2203 # i.e. Represents `self`.`code_point
` using UTF-16 codets escaped
2207 # assert 'A'.escape_to_utf16 == "\\u0041"
2208 # assert 'è'.escape_to_utf16 == "\\u00e8"
2209 # assert 'あ'.escape_to_utf16 == "\\u3042"
2210 # assert '𐏓'.escape_to_utf16 == "\\ud800\\udfd3"
2212 fun escape_to_utf16: String do
2215 if cp < 0xD800 or (cp >= 0xE000 and cp <= 0xFFFF) then
2216 buf = new Buffer.with_cap(6)
2217 buf.append("\\u0000")
2220 for i in hx.chars.reverse_iterator do
2225 buf = new Buffer.with_cap(12)
2226 buf.append("\\u0000\\u0000")
2227 var lo = (((cp - 0x10000) & 0x3FF) + 0xDC00).to_hex
2228 var hi = ((((cp - 0x10000) & 0xFFC00) >> 10) + 0xD800).to_hex
2243 private fun u8char_tos(r: CString, len: Int) `{
2250 r
[0] = 0xC0 | ((self & 0x7C0) >> 6);
2251 r
[1] = 0x80 | (self & 0x3F);
2254 r
[0] = 0xE0 | ((self & 0xF000) >> 12);
2255 r
[1] = 0x80 | ((self & 0xFC0) >> 6);
2256 r
[2] = 0x80 | (self & 0x3F);
2259 r
[0] = 0xF0 | ((self & 0x1C0000) >> 18);
2260 r
[1] = 0x80 | ((self & 0x3F000) >> 12);
2261 r
[2] = 0x80 | ((self & 0xFC0) >> 6);
2262 r
[3] = 0x80 | (self & 0x3F);
2267 # Returns true if the char is a numerical digit
2270 # assert '0'.is_numeric
2271 # assert '9'.is_numeric
2272 # assert not 'a'.is_numeric
2273 # assert not '?'.is_numeric
2276 # FIXME: Works on ASCII-range only
2277 fun is_numeric: Bool
2279 return self >= '0' and self <= '9'
2282 # Returns true if the char is an alpha digit
2285 # assert 'a'.is_alpha
2286 # assert 'Z'.is_alpha
2287 # assert not '0'.is_alpha
2288 # assert not '?'.is_alpha
2291 # FIXME: Works on ASCII-range only
2294 return (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z')
2297 # Is `self` an hexadecimal digit ?
2300 # assert 'A'.is_hexdigit
2301 # assert not 'G'.is_hexdigit
2302 # assert 'a'.is_hexdigit
2303 # assert not 'g'.is_hexdigit
2304 # assert '5'.is_hexdigit
2306 fun is_hexdigit: Bool do return (self >= '0' and self <= '9') or (self >= 'A' and self <= 'F') or
2307 (self >= 'a' and self <= 'f')
2309 # Returns true if the char is an alpha or a numeric digit
2312 # assert 'a'.is_alphanumeric
2313 # assert 'Z'.is_alphanumeric
2314 # assert '0'.is_alphanumeric
2315 # assert '9'.is_alphanumeric
2316 # assert not '?'.is_alphanumeric
2319 # FIXME: Works on ASCII-range only
2320 fun is_alphanumeric: Bool
2322 return self.is_numeric or self.is_alpha
2325 # Returns `self` to its int value
2327 # REQUIRE: `is_hexdigit
`
2328 fun from_hex: Int do
2329 if self >= '0' and self <= '9' then return code_point - 0x30
2330 if self >= 'A' and self <= 'F' then return code_point - 0x37
2331 if self >= 'a' and self <= 'f' then return code_point - 0x57
2332 # Happens if self is not a hexdigit
2333 assert self.is_hexdigit
2334 # To make flow analysis happy
2339 redef class Collection[E]
2340 # String representation of the content of the collection.
2342 # The standard representation is the list of elements separated with commas.
2345 # assert [1,2,3].to_s == "[1,2,3]"
2346 # assert [1..3].to_s == "[1,2,3]"
2347 # assert (new Array[Int]).to_s == "[]" # empty collection
2350 # Subclasses may return a more specific string representation.
2353 return "[" + join(",") + "]"
2356 # Concatenate elements without separators
2359 # assert [1,2,3].plain_to_s == "123"
2360 # assert [11..13].plain_to_s == "111213"
2361 # assert (new Array[Int]).plain_to_s == "" # empty collection
2363 fun plain_to_s: String
2366 for e in self do if e != null then s.append(e.to_s)
2370 # Concatenate and separate each elements with `separator
`.
2372 # Only concatenate if `separator
== null`.
2375 # assert [1, 2, 3].join(":") == "1:2:3"
2376 # assert [1..3].join(":") == "1:2:3"
2377 # assert [1..3].join == "123"
2380 # if `last_separator
` is given, then it is used to separate the last element.
2383 # assert [1, 2, 3, 4].join(", ", " and ") == "1, 2, 3 and 4"
2385 fun join(separator: nullable Text, last_separator: nullable Text): String
2387 if is_empty then return ""
2389 var s = new Buffer # Result
2394 if e != null then s.append(e.to_s)
2396 if last_separator == null then last_separator = separator
2398 # Concat other items
2404 if separator != null then s.append(separator)
2406 if last_separator != null then s.append(last_separator)
2408 if e != null then s.append(e.to_s)
2414 redef class Map[K,V]
2415 # Concatenate couples of key value.
2416 # Key and value are separated by `couple_sep
`.
2417 # Couples are separated by `sep
`.
2420 # var m = new HashMap[Int, String]
2423 # assert m.join("; ", "=") == "1=one; 10=ten"
2425 fun join(sep, couple_sep: String): String is abstract
2429 private var args_cache: nullable Sequence[String] = null
2431 # The arguments of the program as given by the OS
2432 fun program_args: Sequence[String]
2434 if _args_cache == null then init_args
2435 return _args_cache.as(not null)
2438 # The name of the program as given by the OS
2439 fun program_name: String
2441 return native_argv(0).to_s
2444 # Initialize `program_args
` with the contents of `native_argc
` and `native_argv
`.
2445 private fun init_args
2447 var argc = native_argc
2448 var args = new Array[String].with_capacity(0)
2451 args[i-1] = native_argv(i).to_s
2457 # First argument of the main C function.
2458 private fun native_argc: Int is intern
2460 # Second argument of the main C function.
2461 private fun native_argv(i: Int): CString is intern
2464 # Comparator that efficienlty use `to_s
` to compare things
2466 # The comparaison call `to_s
` on object and use the result to order things.
2469 # var a = [1, 2, 3, 10, 20]
2470 # (new CachedAlphaComparator).sort(a)
2471 # assert a == [1, 10, 2, 20, 3]
2474 # Internally the result of `to_s
` is cached in a HashMap to counter
2475 # uneficient implementation of `to_s
`.
2477 # Note: it caching is not usefull, see `alpha_comparator
`
2478 class CachedAlphaComparator
2480 redef type COMPARED: Object
2482 private var cache = new HashMap[Object, String]
2484 private fun do_to_s(a: Object): String do
2485 if cache.has_key(a) then return cache[a]
2491 redef fun compare(a, b) do
2492 return do_to_s(a) <=> do_to_s(b)
2496 # see `alpha_comparator
`
2497 private class AlphaComparator
2499 redef fun compare(a, b) do
2500 if a == b then return 0
2501 if a == null then return -1
2502 if b == null then return 1
2503 return a.to_s <=> b.to_s
2507 # Stateless comparator that naively use `to_s
` to compare things.
2509 # Note: the result of `to_s
` is not cached, thus can be invoked a lot
2510 # on a single instace. See `CachedAlphaComparator` as an alternative.
2513 # var a = [1, 2, 3, 10, 20]
2514 # alpha_comparator.sort(a)
2515 # assert a == [1, 10, 2, 20, 3]
2517 fun alpha_comparator: Comparator do return once new AlphaComparator
2519 # The arguments of the program as given by the OS
2520 fun args: Sequence[String]
2522 return sys.program_args
2527 # Get a `String` from the data at `self` (with unsafe options)
2529 # The default behavior is the safest and equivalent to `to_s
`.
2533 # * Set `byte_length
` to the number of bytes to use as data.
2534 # Otherwise, this method searches for a terminating null byte.
2536 # * Set `char_length
` to the number of Unicode character in the string.
2537 # Otherwise, the data is read to count the characters.
2538 # Ignored if `clean
== true`.
2540 # * If `copy
== true`, the default, copies the data at `self` in the
2541 # Nit GC allocated memory. Otherwise, the return may still point to
2542 # the data at `self`.
2544 # * If `clean
== true`, the default, the string is cleaned of invalid UTF-8
2545 # characters. If cleaning is necessary, the data is copied into Nit GC
2546 # managed memory, whether or not `copy
== true`.
2547 # Don't clean only when the data has already been verified as valid UTF-8,
2548 # other library services rely on UTF-8 compliant characters.
2549 fun to_s_unsafe(byte_length, char_length: nullable Int, copy, clean: nullable Bool): String is abstract
2551 # Retro-compatibility service use by execution engines
2553 # TODO remove this method at the next c_src regen.
2554 private fun to_s_full(byte_length, char_length: Int): String do return to_s_unsafe(byte_length, char_length, false, false)
2556 # Copies the content of `src
` to `self`
2558 # NOTE: `self` must be large enough to contain `self.byte_length
` bytes
2559 fun fill_from(src: Text) do src.copy_to_native(self, src.byte_length, 0, 0)
2562 redef class NativeArray[E]
2563 # Join all the elements using `to_s
`
2565 # REQUIRE: `self isa NativeArray[String]`
2566 # REQUIRE: all elements are initialized
2567 fun native_to_s: String is abstract