409b4ed55233f81384cb8e962478fedc5bb46541
[nit.git] / lib / standard / string.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
5 #
6 # This file is free software, which comes along with NIT. This software is
7 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
10 # is kept unaltered, and a notification of the changes is added.
11 # You are allowed to redistribute it and sell it, alone or is a part of
12 # another product.
13
14 # Basic manipulations of strings of characters
15 module string
16
17 import math
18 import collection
19 intrude import collection::array
20
21 `{
22 #include <stdio.h>
23 #include <string.h>
24 `}
25
26 ###############################################################################
27 # String #
28 ###############################################################################
29
30 # High-level abstraction for all text representations
31 abstract class Text
32 super Comparable
33
34 redef type OTHER: Text
35
36 # Type of self (used for factorization of several methods, ex : substring_from, empty...)
37 type SELFTYPE: Text
38
39 # Gets a view on the chars of the Text object
40 #
41 # assert "hello".chars.to_a == ['h', 'e', 'l', 'l', 'o']
42 fun chars: SequenceRead[Char] is abstract
43
44 # Number of characters contained in self.
45 #
46 # assert "12345".length == 5
47 # assert "".length == 0
48 fun length: Int is abstract
49
50 # Create a substring.
51 #
52 # assert "abcd".substring(1, 2) == "bc"
53 # assert "abcd".substring(-1, 2) == "a"
54 # assert "abcd".substring(1, 0) == ""
55 # assert "abcd".substring(2, 5) == "cd"
56 #
57 # A `from` index < 0 will be replaced by 0.
58 # Unless a `count` value is > 0 at the same time.
59 # In this case, `from += count` and `count -= from`.
60 fun substring(from: Int, count: Int): SELFTYPE is abstract
61
62 # Iterates on the substrings of self if any
63 fun substrings: Iterator[Text] is abstract
64
65 # Is the current Text empty (== "")
66 #
67 # assert "".is_empty
68 # assert not "foo".is_empty
69 fun is_empty: Bool do return self.length == 0
70
71 # Returns an empty Text of the right type
72 #
73 # This method is used internally to get the right
74 # implementation of an empty string.
75 protected fun empty: SELFTYPE is abstract
76
77 # Gets the first char of the Text
78 #
79 # DEPRECATED : Use self.chars.first instead
80 fun first: Char do return self.chars[0]
81
82 # Access a character at `index` in the string.
83 #
84 # assert "abcd"[2] == 'c'
85 #
86 # DEPRECATED : Use self.chars.[] instead
87 fun [](index: Int): Char do return self.chars[index]
88
89 # Gets the index of the first occurence of 'c'
90 #
91 # Returns -1 if not found
92 #
93 # DEPRECATED : Use self.chars.index_of instead
94 fun index_of(c: Char): Int
95 do
96 return index_of_from(c, 0)
97 end
98
99 # Gets the last char of self
100 #
101 # DEPRECATED : Use self.chars.last instead
102 fun last: Char do return self.chars[length-1]
103
104 # Gets the index of the first occurence of ´c´ starting from ´pos´
105 #
106 # Returns -1 if not found
107 #
108 # DEPRECATED : Use self.chars.index_of_from instead
109 fun index_of_from(c: Char, pos: Int): Int
110 do
111 var iter = self.chars.iterator_from(pos)
112 while iter.is_ok do
113 if iter.item == c then return iter.index
114 iter.next
115 end
116 return -1
117 end
118
119 # Gets the last index of char ´c´
120 #
121 # Returns -1 if not found
122 #
123 # DEPRECATED : Use self.chars.last_index_of instead
124 fun last_index_of(c: Char): Int
125 do
126 return last_index_of_from(c, length - 1)
127 end
128
129 # Return a null terminated char *
130 fun to_cstring: NativeString do return flatten.to_cstring
131
132 # The index of the last occurrence of an element starting from pos (in reverse order).
133 #
134 # var s = "/etc/bin/test/test.nit"
135 # assert s.last_index_of_from('/', s.length-1) == 13
136 # assert s.last_index_of_from('/', 12) == 8
137 #
138 # Returns -1 if not found
139 #
140 # DEPRECATED : Use self.chars.last_index_of_from instead
141 fun last_index_of_from(item: Char, pos: Int): Int
142 do
143 var iter = self.chars.reverse_iterator_from(pos)
144 while iter.is_ok do
145 if iter.item == item then return iter.index
146 iter.next
147 end
148 return -1
149 end
150
151 # Gets an iterator on the chars of self
152 #
153 # DEPRECATED : Use self.chars.iterator instead
154 fun iterator: Iterator[Char]
155 do
156 return self.chars.iterator
157 end
158
159
160 # Gets an Array containing the chars of self
161 #
162 # DEPRECATED : Use self.chars.to_a instead
163 fun to_a: Array[Char] do return chars.to_a
164
165 # Create a substring from `self` beginning at the `from` position
166 #
167 # assert "abcd".substring_from(1) == "bcd"
168 # assert "abcd".substring_from(-1) == "abcd"
169 # assert "abcd".substring_from(2) == "cd"
170 #
171 # As with substring, a `from` index < 0 will be replaced by 0
172 fun substring_from(from: Int): SELFTYPE
173 do
174 if from >= self.length then return empty
175 if from < 0 then from = 0
176 return substring(from, length - from)
177 end
178
179 # Does self have a substring `str` starting from position `pos`?
180 #
181 # assert "abcd".has_substring("bc",1) == true
182 # assert "abcd".has_substring("bc",2) == false
183 #
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...
188 #
189 # This means that all characters of `str` need to be inside `self`.
190 #
191 # assert "abcd".has_substring("xab", -1) == false
192 # assert "abcd".has_substring("cdx", 2) == false
193 #
194 # And that the empty string is always a valid substring.
195 #
196 # assert "abcd".has_substring("", 2) == true
197 # assert "abcd".has_substring("", 200) == true
198 fun has_substring(str: String, pos: Int): Bool
199 do
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
206 myiter.next
207 itsiter.next
208 end
209 if itsiter.is_ok then return false
210 return true
211 end
212
213 # Is this string prefixed by `prefix`?
214 #
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)
219
220 # Is this string suffixed by `suffix`?
221 #
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)
225
226 # If `self` contains only digits, return the corresponding integer
227 #
228 # assert "123".to_i == 123
229 # assert "-1".to_i == -1
230 fun to_i: Int
231 do
232 # Shortcut
233 return to_s.to_cstring.atoi
234 end
235
236 # If `self` contains a float, return the corresponding float
237 #
238 # assert "123".to_f == 123.0
239 # assert "-1".to_f == -1.0
240 # assert "-1.2e-3".to_f == -0.0012
241 fun to_f: Float
242 do
243 # Shortcut
244 return to_s.to_cstring.atof
245 end
246
247 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
248 #
249 # assert "ff".to_hex == 255
250 fun to_hex: Int do return a_to(16)
251
252 # If `self` contains only digits and letters, return the corresponding integer in a given base
253 #
254 # assert "120".a_to(3) == 15
255 fun a_to(base: Int) : Int
256 do
257 var i = 0
258 var neg = false
259
260 for j in [0..length[ do
261 var c = chars[j]
262 var v = c.to_i
263 if v > base then
264 if neg then
265 return -i
266 else
267 return i
268 end
269 else if v < 0 then
270 neg = true
271 else
272 i = i * base + v
273 end
274 end
275 if neg then
276 return -i
277 else
278 return i
279 end
280 end
281
282 # Returns `true` if the string contains only Numeric values (and one "," or one "." character)
283 #
284 # assert "123".is_numeric == true
285 # assert "1.2".is_numeric == true
286 # assert "1,2".is_numeric == true
287 # assert "1..2".is_numeric == false
288 fun is_numeric: Bool
289 do
290 var has_point_or_comma = false
291 for i in [0..length[ do
292 var c = chars[i]
293 if not c.is_numeric then
294 if (c == '.' or c == ',') and not has_point_or_comma then
295 has_point_or_comma = true
296 else
297 return false
298 end
299 end
300 end
301 return true
302 end
303
304 # Returns `true` if the string contains only Hex chars
305 #
306 # assert "048bf".is_hex == true
307 # assert "ABCDEF".is_hex == true
308 # assert "0G".is_hex == false
309 fun is_hex: Bool
310 do
311 for i in [0..length[ do
312 var c = chars[i]
313 if not (c >= 'a' and c <= 'f') and
314 not (c >= 'A' and c <= 'F') and
315 not (c >= '0' and c <= '9') then return false
316 end
317 return true
318 end
319
320 # Are all letters in `self` upper-case ?
321 #
322 # assert "HELLO WORLD".is_upper == true
323 # assert "%$&%!".is_upper == true
324 # assert "hello world".is_upper == false
325 # assert "Hello World".is_upper == false
326 fun is_upper: Bool
327 do
328 for i in [0..length[ do
329 var char = chars[i]
330 if char.is_lower then return false
331 end
332 return true
333 end
334
335 # Are all letters in `self` lower-case ?
336 #
337 # assert "hello world".is_lower == true
338 # assert "%$&%!".is_lower == true
339 # assert "Hello World".is_lower == false
340 fun is_lower: Bool
341 do
342 for i in [0..length[ do
343 var char = chars[i]
344 if char.is_upper then return false
345 end
346 return true
347 end
348
349 # Removes the whitespaces at the beginning of self
350 #
351 # assert " \n\thello \n\t".l_trim == "hello \n\t"
352 #
353 # `Char::is_whitespace` determines what is a whitespace.
354 fun l_trim: SELFTYPE
355 do
356 var iter = self.chars.iterator
357 while iter.is_ok do
358 if not iter.item.is_whitespace then break
359 iter.next
360 end
361 if iter.index == length then return self.empty
362 return self.substring_from(iter.index)
363 end
364
365 # Removes the whitespaces at the end of self
366 #
367 # assert " \n\thello \n\t".r_trim == " \n\thello"
368 #
369 # `Char::is_whitespace` determines what is a whitespace.
370 fun r_trim: SELFTYPE
371 do
372 var iter = self.chars.reverse_iterator
373 while iter.is_ok do
374 if not iter.item.is_whitespace then break
375 iter.next
376 end
377 if iter.index < 0 then return self.empty
378 return self.substring(0, iter.index + 1)
379 end
380
381 # Trims trailing and preceding white spaces
382 #
383 # assert " Hello World ! ".trim == "Hello World !"
384 # assert "\na\nb\tc\t".trim == "a\nb\tc"
385 #
386 # `Char::is_whitespace` determines what is a whitespace.
387 fun trim: SELFTYPE do return (self.l_trim).r_trim
388
389 # Is the string non-empty but only made of whitespaces?
390 #
391 # assert " \n\t ".is_whitespace == true
392 # assert " hello ".is_whitespace == false
393 # assert "".is_whitespace == false
394 #
395 # `Char::is_whitespace` determines what is a whitespace.
396 fun is_whitespace: Bool
397 do
398 if is_empty then return false
399 for c in self.chars do
400 if not c.is_whitespace then return false
401 end
402 return true
403 end
404
405 # Returns `self` removed from its last line terminator (if any).
406 #
407 # assert "Hello\n".chomp == "Hello"
408 # assert "Hello".chomp == "Hello"
409 #
410 # assert "\n".chomp == ""
411 # assert "".chomp == ""
412 #
413 # Line terminators are `"\n"`, `"\r\n"` and `"\r"`.
414 # A single line terminator, the last one, is removed.
415 #
416 # assert "\r\n".chomp == ""
417 # assert "\r\n\n".chomp == "\r\n"
418 # assert "\r\n\r\n".chomp == "\r\n"
419 # assert "\r\n\r".chomp == "\r\n"
420 #
421 # Note: unlike with most IO methods like `Reader::read_line`,
422 # a single `\r` is considered here to be a line terminator and will be removed.
423 fun chomp: SELFTYPE
424 do
425 var len = length
426 if len == 0 then return self
427 var l = self.chars.last
428 if l == '\r' then
429 return substring(0, len-1)
430 else if l != '\n' then
431 return self
432 else if len > 1 and self.chars[len-2] == '\r' then
433 return substring(0, len-2)
434 else
435 return substring(0, len-1)
436 end
437 end
438
439 # Justify a self in a space of `length`
440 #
441 # `left` is the space ratio on the left side.
442 # * 0.0 for left-justified (no space at the left)
443 # * 1.0 for right-justified (all spaces at the left)
444 # * 0.5 for centered (half the spaces at the left)
445 #
446 # Examples
447 #
448 # assert "hello".justify(10, 0.0) == "hello "
449 # assert "hello".justify(10, 1.0) == " hello"
450 # assert "hello".justify(10, 0.5) == " hello "
451 #
452 # If `length` is not enough, `self` is returned as is.
453 #
454 # assert "hello".justify(2, 0.0) == "hello"
455 #
456 # REQUIRE: `left >= 0.0 and left <= 1.0`
457 # ENSURE: `self.length <= length implies result.length == length`
458 # ENSURE: `self.length >= length implies result == self`
459 fun justify(length: Int, left: Float): String
460 do
461 var diff = length - self.length
462 if diff <= 0 then return to_s
463 assert left >= 0.0 and left <= 1.0
464 var before = (diff.to_f * left).to_i
465 return " " * before + self + " " * (diff-before)
466 end
467
468 # Mangle a string to be a unique string only made of alphanumeric characters and underscores.
469 #
470 # This method is injective (two different inputs never produce the same
471 # output) and the returned string always respect the following rules:
472 #
473 # * Contains only US-ASCII letters, digits and underscores.
474 # * Never starts with a digit.
475 # * Never ends with an underscore.
476 # * Never contains two contiguous underscores.
477 #
478 # assert "42_is/The answer!".to_cmangle == "_52d2_is_47dThe_32danswer_33d"
479 # assert "__".to_cmangle == "_95d_95d"
480 # assert "__d".to_cmangle == "_95d_d"
481 # assert "_d_".to_cmangle == "_d_95d"
482 # assert "_42".to_cmangle == "_95d42"
483 # assert "foo".to_cmangle == "foo"
484 # assert "".to_cmangle == ""
485 fun to_cmangle: String
486 do
487 if is_empty then return ""
488 var res = new FlatBuffer
489 var underscore = false
490 var start = 0
491 var c = chars[0]
492
493 if c >= '0' and c <= '9' then
494 res.add('_')
495 res.append(c.ascii.to_s)
496 res.add('d')
497 start = 1
498 end
499 for i in [start..length[ do
500 c = chars[i]
501 if (c >= 'a' and c <= 'z') or (c >='A' and c <= 'Z') then
502 res.add(c)
503 underscore = false
504 continue
505 end
506 if underscore then
507 res.append('_'.ascii.to_s)
508 res.add('d')
509 end
510 if c >= '0' and c <= '9' then
511 res.add(c)
512 underscore = false
513 else if c == '_' then
514 res.add(c)
515 underscore = true
516 else
517 res.add('_')
518 res.append(c.ascii.to_s)
519 res.add('d')
520 underscore = false
521 end
522 end
523 if underscore then
524 res.append('_'.ascii.to_s)
525 res.add('d')
526 end
527 return res.to_s
528 end
529
530 # Escape " \ ' and non printable characters using the rules of literal C strings and characters
531 #
532 # assert "abAB12<>&".escape_to_c == "abAB12<>&"
533 # assert "\n\"'\\".escape_to_c == "\\n\\\"\\'\\\\"
534 #
535 # Most non-printable characters (bellow ASCII 32) are escaped to an octal form `\nnn`.
536 # Three digits are always used to avoid following digits to be interpreted as an element
537 # of the octal sequence.
538 #
539 # assert "{0.ascii}{1.ascii}{8.ascii}{31.ascii}{32.ascii}".escape_to_c == "\\000\\001\\010\\037 "
540 #
541 # The exceptions are the common `\t` and `\n`.
542 fun escape_to_c: String
543 do
544 var b = new FlatBuffer
545 for i in [0..length[ do
546 var c = chars[i]
547 if c == '\n' then
548 b.append("\\n")
549 else if c == '\t' then
550 b.append("\\t")
551 else if c == '\0' then
552 b.append("\\000")
553 else if c == '"' then
554 b.append("\\\"")
555 else if c == '\'' then
556 b.append("\\\'")
557 else if c == '\\' then
558 b.append("\\\\")
559 else if c.ascii < 32 then
560 b.add('\\')
561 var oct = c.ascii.to_base(8, false)
562 # Force 3 octal digits since it is the
563 # maximum allowed in the C specification
564 if oct.length == 1 then
565 b.add('0')
566 b.add('0')
567 else if oct.length == 2 then
568 b.add('0')
569 end
570 b.append(oct)
571 else
572 b.add(c)
573 end
574 end
575 return b.to_s
576 end
577
578 # Escape additionnal characters
579 # The result might no be legal in C but be used in other languages
580 #
581 # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
582 fun escape_more_to_c(chars: String): String
583 do
584 var b = new FlatBuffer
585 for c in escape_to_c.chars do
586 if chars.chars.has(c) then
587 b.add('\\')
588 end
589 b.add(c)
590 end
591 return b.to_s
592 end
593
594 # Escape to C plus braces
595 #
596 # assert "\n\"'\\\{\}".escape_to_nit == "\\n\\\"\\'\\\\\\\{\\\}"
597 fun escape_to_nit: String do return escape_more_to_c("\{\}")
598
599 # Escape to POSIX Shell (sh).
600 #
601 # Abort if the text contains a null byte.
602 #
603 # assert "\n\"'\\\{\}0".escape_to_sh == "'\n\"'\\''\\\{\}0'"
604 fun escape_to_sh: String do
605 var b = new FlatBuffer
606 b.chars.add '\''
607 for i in [0..length[ do
608 var c = chars[i]
609 if c == '\'' then
610 b.append("'\\''")
611 else
612 assert without_null_byte: c != '\0'
613 b.add(c)
614 end
615 end
616 b.chars.add '\''
617 return b.to_s
618 end
619
620 # Escape to include in a Makefile
621 #
622 # Unfortunately, some characters are not escapable in Makefile.
623 # These characters are `;`, `|`, `\`, and the non-printable ones.
624 # They will be rendered as `"?{hex}"`.
625 fun escape_to_mk: String do
626 var b = new FlatBuffer
627 for i in [0..length[ do
628 var c = chars[i]
629 if c == '$' then
630 b.append("$$")
631 else if c == ':' or c == ' ' or c == '#' then
632 b.add('\\')
633 b.add(c)
634 else if c.ascii < 32 or c == ';' or c == '|' or c == '\\' or c == '=' then
635 b.append("?{c.ascii.to_base(16, false)}")
636 else
637 b.add(c)
638 end
639 end
640 return b.to_s
641 end
642
643 # Return a string where Nit escape sequences are transformed.
644 #
645 # var s = "\\n"
646 # assert s.length == 2
647 # var u = s.unescape_nit
648 # assert u.length == 1
649 # assert u.chars[0].ascii == 10 # (the ASCII value of the "new line" character)
650 fun unescape_nit: String
651 do
652 var res = new FlatBuffer.with_capacity(self.length)
653 var was_slash = false
654 for i in [0..length[ do
655 var c = chars[i]
656 if not was_slash then
657 if c == '\\' then
658 was_slash = true
659 else
660 res.add(c)
661 end
662 continue
663 end
664 was_slash = false
665 if c == 'n' then
666 res.add('\n')
667 else if c == 'r' then
668 res.add('\r')
669 else if c == 't' then
670 res.add('\t')
671 else if c == '0' then
672 res.add('\0')
673 else
674 res.add(c)
675 end
676 end
677 return res.to_s
678 end
679
680 # Encode `self` to percent (or URL) encoding
681 #
682 # assert "aBc09-._~".to_percent_encoding == "aBc09-._~"
683 # assert "%()< >".to_percent_encoding == "%25%28%29%3c%20%3e"
684 # assert ".com/post?e=asdf&f=123".to_percent_encoding == ".com%2fpost%3fe%3dasdf%26f%3d123"
685 fun to_percent_encoding: String
686 do
687 var buf = new FlatBuffer
688
689 for i in [0..length[ do
690 var c = chars[i]
691 if (c >= '0' and c <= '9') or
692 (c >= 'a' and c <= 'z') or
693 (c >= 'A' and c <= 'Z') or
694 c == '-' or c == '.' or
695 c == '_' or c == '~'
696 then
697 buf.add c
698 else buf.append "%{c.ascii.to_hex}"
699 end
700
701 return buf.to_s
702 end
703
704 # Decode `self` from percent (or URL) encoding to a clear string
705 #
706 # Replace invalid use of '%' with '?'.
707 #
708 # assert "aBc09-._~".from_percent_encoding == "aBc09-._~"
709 # assert "%25%28%29%3c%20%3e".from_percent_encoding == "%()< >"
710 # assert ".com%2fpost%3fe%3dasdf%26f%3d123".from_percent_encoding == ".com/post?e=asdf&f=123"
711 # assert "%25%28%29%3C%20%3E".from_percent_encoding == "%()< >"
712 # assert "incomplete %".from_percent_encoding == "incomplete ?"
713 # assert "invalid % usage".from_percent_encoding == "invalid ? usage"
714 fun from_percent_encoding: String
715 do
716 var buf = new FlatBuffer
717
718 var i = 0
719 while i < length do
720 var c = chars[i]
721 if c == '%' then
722 if i + 2 >= length then
723 # What follows % has been cut off
724 buf.add '?'
725 else
726 i += 1
727 var hex_s = substring(i, 2)
728 if hex_s.is_hex then
729 var hex_i = hex_s.to_hex
730 buf.add hex_i.ascii
731 i += 1
732 else
733 # What follows a % is not Hex
734 buf.add '?'
735 i -= 1
736 end
737 end
738 else buf.add c
739
740 i += 1
741 end
742
743 return buf.to_s
744 end
745
746 # Escape the characters `<`, `>`, `&`, `"`, `'` and `/` as HTML/XML entity references.
747 #
748 # assert "a&b-<>\"x\"/'".html_escape == "a&amp;b-&lt;&gt;&#34;x&#34;&#47;&#39;"
749 #
750 # 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>
751 fun html_escape: String
752 do
753 var buf = new FlatBuffer
754
755 for i in [0..length[ do
756 var c = chars[i]
757 if c == '&' then
758 buf.append "&amp;"
759 else if c == '<' then
760 buf.append "&lt;"
761 else if c == '>' then
762 buf.append "&gt;"
763 else if c == '"' then
764 buf.append "&#34;"
765 else if c == '\'' then
766 buf.append "&#39;"
767 else if c == '/' then
768 buf.append "&#47;"
769 else buf.add c
770 end
771
772 return buf.to_s
773 end
774
775 # Equality of text
776 # Two pieces of text are equals if thez have the same characters in the same order.
777 #
778 # assert "hello" == "hello"
779 # assert "hello" != "HELLO"
780 # assert "hello" == "hel"+"lo"
781 #
782 # Things that are not Text are not equal.
783 #
784 # assert "9" != '9'
785 # assert "9" != ['9']
786 # assert "9" != 9
787 #
788 # assert "9".chars.first == '9' # equality of Char
789 # assert "9".chars == ['9'] # equality of Sequence
790 # assert "9".to_i == 9 # equality of Int
791 redef fun ==(o)
792 do
793 if o == null then return false
794 if not o isa Text then return false
795 if self.is_same_instance(o) then return true
796 if self.length != o.length then return false
797 return self.chars == o.chars
798 end
799
800 # Lexicographical comparaison
801 #
802 # assert "abc" < "xy"
803 # assert "ABC" < "abc"
804 redef fun <(other)
805 do
806 var self_chars = self.chars.iterator
807 var other_chars = other.chars.iterator
808
809 while self_chars.is_ok and other_chars.is_ok do
810 if self_chars.item < other_chars.item then return true
811 if self_chars.item > other_chars.item then return false
812 self_chars.next
813 other_chars.next
814 end
815
816 if self_chars.is_ok then
817 return false
818 else
819 return true
820 end
821 end
822
823 # Escape string used in labels for graphviz
824 #
825 # assert ">><<".escape_to_dot == "\\>\\>\\<\\<"
826 fun escape_to_dot: String
827 do
828 return escape_more_to_c("|\{\}<>")
829 end
830
831 # Flat representation of self
832 fun flatten: FlatText is abstract
833
834 private var hash_cache: nullable Int = null
835
836 redef fun hash
837 do
838 if hash_cache == null then
839 # djb2 hash algorithm
840 var h = 5381
841
842 for i in [0..length[ do
843 var char = chars[i]
844 h = h.lshift(5) + h + char.ascii
845 end
846
847 hash_cache = h
848 end
849 return hash_cache.as(not null)
850 end
851
852 end
853
854 # All kinds of array-based text representations.
855 abstract class FlatText
856 super Text
857
858 # Underlying C-String (`char*`)
859 #
860 # Warning : Might be void in some subclasses, be sure to check
861 # if set before using it.
862 private var items: NativeString is noinit
863
864 # Real items, used as cache for to_cstring is called
865 private var real_items: nullable NativeString = null
866
867 # Returns a char* starting at position `index_from`
868 #
869 # WARNING: If you choose to use this service, be careful of the following.
870 #
871 # Strings and NativeString are *ideally* always allocated through a Garbage Collector.
872 # Since the GC tracks the use of the pointer for the beginning of the char*, it may be
873 # deallocated at any moment, rendering the pointer returned by this function invalid.
874 # Any access to freed memory may very likely cause undefined behaviour or a crash.
875 # (Failure to do so will most certainly result in long and painful debugging hours)
876 #
877 # The only safe use of this pointer is if it is ephemeral (e.g. read in a C function
878 # then immediately return).
879 #
880 # As always, do not modify the content of the String in C code, if this is what you want
881 # copy locally the char* as Nit Strings are immutable.
882 private fun fast_cstring: NativeString is abstract
883
884 redef var length: Int = 0
885
886 redef fun output
887 do
888 var i = 0
889 while i < length do
890 items[i].output
891 i += 1
892 end
893 end
894
895 redef fun flatten do return self
896 end
897
898 # Abstract class for the SequenceRead compatible
899 # views on String and Buffer objects
900 private abstract class StringCharView
901 super SequenceRead[Char]
902
903 type SELFTYPE: Text
904
905 var target: SELFTYPE
906
907 redef fun is_empty do return target.is_empty
908
909 redef fun length do return target.length
910
911 redef fun iterator: IndexedIterator[Char] do return self.iterator_from(0)
912
913 redef fun reverse_iterator do return self.reverse_iterator_from(self.length - 1)
914 end
915
916 # View on Buffer objects, extends Sequence
917 # for mutation operations
918 private abstract class BufferCharView
919 super StringCharView
920 super Sequence[Char]
921
922 redef type SELFTYPE: Buffer
923
924 end
925
926 # A `String` holds and manipulates an arbitrary sequence of characters.
927 #
928 # String objects may be created using literals.
929 #
930 # assert "Hello World!" isa String
931 abstract class String
932 super Text
933
934 redef type SELFTYPE: String is fixed
935
936 redef fun to_s do return self
937
938 # Concatenates `o` to `self`
939 #
940 # assert "hello" + "world" == "helloworld"
941 # assert "" + "hello" + "" == "hello"
942 fun +(o: Text): SELFTYPE is abstract
943
944 # Concatenates self `i` times
945 #
946 # assert "abc" * 4 == "abcabcabcabc"
947 # assert "abc" * 1 == "abc"
948 # assert "abc" * 0 == ""
949 fun *(i: Int): SELFTYPE is abstract
950
951 # Insert `s` at `pos`.
952 #
953 # assert "helloworld".insert_at(" ", 5) == "hello world"
954 fun insert_at(s: String, pos: Int): SELFTYPE is abstract
955
956 redef fun substrings: Iterator[String] is abstract
957
958 # Returns a reversed version of self
959 #
960 # assert "hello".reversed == "olleh"
961 # assert "bob".reversed == "bob"
962 # assert "".reversed == ""
963 fun reversed: SELFTYPE is abstract
964
965 # A upper case version of `self`
966 #
967 # assert "Hello World!".to_upper == "HELLO WORLD!"
968 fun to_upper: SELFTYPE is abstract
969
970 # A lower case version of `self`
971 #
972 # assert "Hello World!".to_lower == "hello world!"
973 fun to_lower : SELFTYPE is abstract
974
975 # Takes a camel case `self` and converts it to snake case
976 #
977 # assert "randomMethodId".to_snake_case == "random_method_id"
978 #
979 # If `self` is upper, it is returned unchanged
980 #
981 # assert "RANDOM_METHOD_ID".to_snake_case == "RANDOM_METHOD_ID"
982 #
983 # If the identifier is prefixed by an underscore, the underscore is ignored
984 #
985 # assert "_privateField".to_snake_case == "_private_field"
986 fun to_snake_case: SELFTYPE
987 do
988 if self.is_upper then return self
989
990 var new_str = new FlatBuffer.with_capacity(self.length)
991 var is_first_char = true
992
993 for i in [0..length[ do
994 var char = chars[i]
995 if is_first_char then
996 new_str.add(char.to_lower)
997 is_first_char = false
998 else if char.is_upper then
999 new_str.add('_')
1000 new_str.add(char.to_lower)
1001 else
1002 new_str.add(char)
1003 end
1004 end
1005
1006 return new_str.to_s
1007 end
1008
1009 # Takes a snake case `self` and converts it to camel case
1010 #
1011 # assert "random_method_id".to_camel_case == "randomMethodId"
1012 #
1013 # If the identifier is prefixed by an underscore, the underscore is ignored
1014 #
1015 # assert "_private_field".to_camel_case == "_privateField"
1016 #
1017 # If `self` is upper, it is returned unchanged
1018 #
1019 # assert "RANDOM_ID".to_camel_case == "RANDOM_ID"
1020 #
1021 # If there are several consecutive underscores, they are considered as a single one
1022 #
1023 # assert "random__method_id".to_camel_case == "randomMethodId"
1024 fun to_camel_case: SELFTYPE
1025 do
1026 if self.is_upper then return self
1027
1028 var new_str = new FlatBuffer
1029 var is_first_char = true
1030 var follows_us = false
1031
1032 for i in [0..length[ do
1033 var char = chars[i]
1034 if is_first_char then
1035 new_str.add(char)
1036 is_first_char = false
1037 else if char == '_' then
1038 follows_us = true
1039 else if follows_us then
1040 new_str.add(char.to_upper)
1041 follows_us = false
1042 else
1043 new_str.add(char)
1044 end
1045 end
1046
1047 return new_str.to_s
1048 end
1049
1050 # Returns a capitalized `self`
1051 #
1052 # Letters that follow a letter are lowercased
1053 # Letters that follow a non-letter are upcased.
1054 #
1055 # SEE : `Char::is_letter` for the definition of letter.
1056 #
1057 # assert "jAVASCRIPT".capitalized == "Javascript"
1058 # assert "i am root".capitalized == "I Am Root"
1059 # assert "ab_c -ab0c ab\nc".capitalized == "Ab_C -Ab0C Ab\nC"
1060 fun capitalized: SELFTYPE do
1061 if length == 0 then return self
1062
1063 var buf = new FlatBuffer.with_capacity(length)
1064
1065 var curr = chars[0].to_upper
1066 var prev = curr
1067 buf[0] = curr
1068
1069 for i in [1 .. length[ do
1070 prev = curr
1071 curr = self[i]
1072 if prev.is_letter then
1073 buf[i] = curr.to_lower
1074 else
1075 buf[i] = curr.to_upper
1076 end
1077 end
1078
1079 return buf.to_s
1080 end
1081 end
1082
1083 private class FlatSubstringsIter
1084 super Iterator[FlatText]
1085
1086 var tgt: nullable FlatText
1087
1088 redef fun item do
1089 assert is_ok
1090 return tgt.as(not null)
1091 end
1092
1093 redef fun is_ok do return tgt != null
1094
1095 redef fun next do tgt = null
1096 end
1097
1098 # Immutable strings of characters.
1099 class FlatString
1100 super FlatText
1101 super String
1102
1103 # Index in _items of the start of the string
1104 private var index_from: Int is noinit
1105
1106 # Indes in _items of the last item of the string
1107 private var index_to: Int is noinit
1108
1109 redef var chars: SequenceRead[Char] = new FlatStringCharView(self) is lazy
1110
1111 redef fun [](index)
1112 do
1113 # Check that the index (+ index_from) is not larger than indexTo
1114 # In other terms, if the index is valid
1115 assert index >= 0
1116 assert (index + index_from) <= index_to
1117 return items[index + index_from]
1118 end
1119
1120 ################################################
1121 # AbstractString specific methods #
1122 ################################################
1123
1124 redef fun reversed
1125 do
1126 var native = new NativeString(self.length + 1)
1127 var length = self.length
1128 var items = self.items
1129 var pos = 0
1130 var ipos = length-1
1131 while pos < length do
1132 native[pos] = items[ipos]
1133 pos += 1
1134 ipos -= 1
1135 end
1136 return native.to_s_with_length(self.length)
1137 end
1138
1139 redef fun fast_cstring do return items.fast_cstring(index_from)
1140
1141 redef fun substring(from, count)
1142 do
1143 assert count >= 0
1144
1145 if from < 0 then
1146 count += from
1147 if count < 0 then count = 0
1148 from = 0
1149 end
1150
1151 var new_from = index_from + from
1152
1153 if (new_from + count) > index_to then
1154 var new_len = index_to - new_from + 1
1155 if new_len <= 0 then return empty
1156 return new FlatString.with_infos(items, new_len, new_from, index_to)
1157 end
1158
1159 if count <= 0 then return empty
1160
1161 var to = new_from + count - 1
1162
1163 return new FlatString.with_infos(items, to - new_from + 1, new_from, to)
1164 end
1165
1166 redef fun empty do return "".as(FlatString)
1167
1168 redef fun to_upper
1169 do
1170 var outstr = new NativeString(self.length + 1)
1171 var out_index = 0
1172
1173 var myitems = self.items
1174 var index_from = self.index_from
1175 var max = self.index_to
1176
1177 while index_from <= max do
1178 outstr[out_index] = myitems[index_from].to_upper
1179 out_index += 1
1180 index_from += 1
1181 end
1182
1183 outstr[self.length] = '\0'
1184
1185 return outstr.to_s_with_length(self.length)
1186 end
1187
1188 redef fun to_lower
1189 do
1190 var outstr = new NativeString(self.length + 1)
1191 var out_index = 0
1192
1193 var myitems = self.items
1194 var index_from = self.index_from
1195 var max = self.index_to
1196
1197 while index_from <= max do
1198 outstr[out_index] = myitems[index_from].to_lower
1199 out_index += 1
1200 index_from += 1
1201 end
1202
1203 outstr[self.length] = '\0'
1204
1205 return outstr.to_s_with_length(self.length)
1206 end
1207
1208 redef fun output
1209 do
1210 var i = self.index_from
1211 var imax = self.index_to
1212 while i <= imax do
1213 items[i].output
1214 i += 1
1215 end
1216 end
1217
1218 ##################################################
1219 # String Specific Methods #
1220 ##################################################
1221
1222 # Low-level creation of a new string with given data.
1223 #
1224 # `items` will be used as is, without copy, to retrieve the characters of the string.
1225 # Aliasing issues is the responsibility of the caller.
1226 private init with_infos(items: NativeString, length: Int, from: Int, to: Int)
1227 do
1228 self.items = items
1229 self.length = length
1230 index_from = from
1231 index_to = to
1232 end
1233
1234 redef fun to_cstring: NativeString
1235 do
1236 if real_items != null then
1237 return real_items.as(not null)
1238 else
1239 var newItems = new NativeString(length + 1)
1240 self.items.copy_to(newItems, length, index_from, 0)
1241 newItems[length] = '\0'
1242 self.real_items = newItems
1243 return newItems
1244 end
1245 end
1246
1247 redef fun ==(other)
1248 do
1249 if not other isa FlatString then return super
1250
1251 if self.object_id == other.object_id then return true
1252
1253 var my_length = length
1254
1255 if other.length != my_length then return false
1256
1257 var my_index = index_from
1258 var its_index = other.index_from
1259
1260 var last_iteration = my_index + my_length
1261
1262 var itsitems = other.items
1263 var myitems = self.items
1264
1265 while my_index < last_iteration do
1266 if myitems[my_index] != itsitems[its_index] then return false
1267 my_index += 1
1268 its_index += 1
1269 end
1270
1271 return true
1272 end
1273
1274 redef fun <(other)
1275 do
1276 if not other isa FlatString then return super
1277
1278 if self.object_id == other.object_id then return false
1279
1280 var my_curr_char : Char
1281 var its_curr_char : Char
1282
1283 var curr_id_self = self.index_from
1284 var curr_id_other = other.index_from
1285
1286 var my_items = self.items
1287 var its_items = other.items
1288
1289 var my_length = self.length
1290 var its_length = other.length
1291
1292 var max_iterations = curr_id_self + my_length
1293
1294 while curr_id_self < max_iterations do
1295 my_curr_char = my_items[curr_id_self]
1296 its_curr_char = its_items[curr_id_other]
1297
1298 if my_curr_char != its_curr_char then
1299 if my_curr_char < its_curr_char then return true
1300 return false
1301 end
1302
1303 curr_id_self += 1
1304 curr_id_other += 1
1305 end
1306
1307 return my_length < its_length
1308 end
1309
1310 redef fun +(s)
1311 do
1312 var my_length = self.length
1313 var its_length = s.length
1314
1315 var total_length = my_length + its_length
1316
1317 var target_string = new NativeString(my_length + its_length + 1)
1318
1319 self.items.copy_to(target_string, my_length, index_from, 0)
1320 if s isa FlatString then
1321 s.items.copy_to(target_string, its_length, s.index_from, my_length)
1322 else if s isa FlatBuffer then
1323 s.items.copy_to(target_string, its_length, 0, my_length)
1324 else
1325 var curr_pos = my_length
1326 for i in [0..s.length[ do
1327 var c = s.chars[i]
1328 target_string[curr_pos] = c
1329 curr_pos += 1
1330 end
1331 end
1332
1333 target_string[total_length] = '\0'
1334
1335 return target_string.to_s_with_length(total_length)
1336 end
1337
1338 redef fun *(i)
1339 do
1340 assert i >= 0
1341
1342 var my_length = self.length
1343
1344 var final_length = my_length * i
1345
1346 var my_items = self.items
1347
1348 var target_string = new NativeString(final_length + 1)
1349
1350 target_string[final_length] = '\0'
1351
1352 var current_last = 0
1353
1354 for iteration in [1 .. i] do
1355 my_items.copy_to(target_string, my_length, 0, current_last)
1356 current_last += my_length
1357 end
1358
1359 return target_string.to_s_with_length(final_length)
1360 end
1361
1362 redef fun hash
1363 do
1364 if hash_cache == null then
1365 # djb2 hash algorithm
1366 var h = 5381
1367 var i = index_from
1368
1369 var myitems = items
1370
1371 while i <= index_to do
1372 h = h.lshift(5) + h + myitems[i].ascii
1373 i += 1
1374 end
1375
1376 hash_cache = h
1377 end
1378
1379 return hash_cache.as(not null)
1380 end
1381
1382 redef fun substrings do return new FlatSubstringsIter(self)
1383 end
1384
1385 private class FlatStringReverseIterator
1386 super IndexedIterator[Char]
1387
1388 var target: FlatString
1389
1390 var target_items: NativeString
1391
1392 var curr_pos: Int
1393
1394 init with_pos(tgt: FlatString, pos: Int)
1395 do
1396 target = tgt
1397 target_items = tgt.items
1398 curr_pos = pos + tgt.index_from
1399 end
1400
1401 redef fun is_ok do return curr_pos >= target.index_from
1402
1403 redef fun item do return target_items[curr_pos]
1404
1405 redef fun next do curr_pos -= 1
1406
1407 redef fun index do return curr_pos - target.index_from
1408
1409 end
1410
1411 private class FlatStringIterator
1412 super IndexedIterator[Char]
1413
1414 var target: FlatString
1415
1416 var target_items: NativeString
1417
1418 var curr_pos: Int
1419
1420 init with_pos(tgt: FlatString, pos: Int)
1421 do
1422 target = tgt
1423 target_items = tgt.items
1424 curr_pos = pos + target.index_from
1425 end
1426
1427 redef fun is_ok do return curr_pos <= target.index_to
1428
1429 redef fun item do return target_items[curr_pos]
1430
1431 redef fun next do curr_pos += 1
1432
1433 redef fun index do return curr_pos - target.index_from
1434
1435 end
1436
1437 private class FlatStringCharView
1438 super StringCharView
1439
1440 redef type SELFTYPE: FlatString
1441
1442 redef fun [](index)
1443 do
1444 # Check that the index (+ index_from) is not larger than indexTo
1445 # In other terms, if the index is valid
1446 assert index >= 0
1447 var target = self.target
1448 assert (index + target.index_from) <= target.index_to
1449 return target.items[index + target.index_from]
1450 end
1451
1452 redef fun iterator_from(start) do return new FlatStringIterator.with_pos(target, start)
1453
1454 redef fun reverse_iterator_from(start) do return new FlatStringReverseIterator.with_pos(target, start)
1455
1456 end
1457
1458 # A mutable sequence of characters.
1459 abstract class Buffer
1460 super Text
1461
1462 redef type SELFTYPE: Buffer is fixed
1463
1464 # Specific implementations MUST set this to `true` in order to invalidate caches
1465 protected var is_dirty = true
1466
1467 # Copy-On-Write flag
1468 #
1469 # If the `Buffer` was to_s'd, the next in-place altering
1470 # operation will cause the current `Buffer` to be re-allocated.
1471 #
1472 # The flag will then be set at `false`.
1473 protected var written = false
1474
1475 # Modifies the char contained at pos `index`
1476 #
1477 # DEPRECATED : Use self.chars.[]= instead
1478 fun []=(index: Int, item: Char) is abstract
1479
1480 # Adds a char `c` at the end of self
1481 #
1482 # DEPRECATED : Use self.chars.add instead
1483 fun add(c: Char) is abstract
1484
1485 # Clears the buffer
1486 #
1487 # var b = new FlatBuffer
1488 # b.append "hello"
1489 # assert not b.is_empty
1490 # b.clear
1491 # assert b.is_empty
1492 fun clear is abstract
1493
1494 # Enlarges the subsequent array containing the chars of self
1495 fun enlarge(cap: Int) is abstract
1496
1497 # Adds the content of text `s` at the end of self
1498 #
1499 # var b = new FlatBuffer
1500 # b.append "hello"
1501 # b.append "world"
1502 # assert b == "helloworld"
1503 fun append(s: Text) is abstract
1504
1505 # `self` is appended in such a way that `self` is repeated `r` times
1506 #
1507 # var b = new FlatBuffer
1508 # b.append "hello"
1509 # b.times 3
1510 # assert b == "hellohellohello"
1511 fun times(r: Int) is abstract
1512
1513 # Reverses itself in-place
1514 #
1515 # var b = new FlatBuffer
1516 # b.append("hello")
1517 # b.reverse
1518 # assert b == "olleh"
1519 fun reverse is abstract
1520
1521 # Changes each lower-case char in `self` by its upper-case variant
1522 #
1523 # var b = new FlatBuffer
1524 # b.append("Hello World!")
1525 # b.upper
1526 # assert b == "HELLO WORLD!"
1527 fun upper is abstract
1528
1529 # Changes each upper-case char in `self` by its lower-case variant
1530 #
1531 # var b = new FlatBuffer
1532 # b.append("Hello World!")
1533 # b.lower
1534 # assert b == "hello world!"
1535 fun lower is abstract
1536
1537 # Capitalizes each word in `self`
1538 #
1539 # Letters that follow a letter are lowercased
1540 # Letters that follow a non-letter are upcased.
1541 #
1542 # SEE: `Char::is_letter` for the definition of a letter.
1543 #
1544 # var b = new FlatBuffer.from("jAVAsCriPt")
1545 # b.capitalize
1546 # assert b == "Javascript"
1547 # b = new FlatBuffer.from("i am root")
1548 # b.capitalize
1549 # assert b == "I Am Root"
1550 # b = new FlatBuffer.from("ab_c -ab0c ab\nc")
1551 # b.capitalize
1552 # assert b == "Ab_C -Ab0C Ab\nC"
1553 fun capitalize do
1554 if length == 0 then return
1555 var c = self[0].to_upper
1556 self[0] = c
1557 var prev = c
1558 for i in [1 .. length[ do
1559 prev = c
1560 c = self[i]
1561 if prev.is_letter then
1562 self[i] = c.to_lower
1563 else
1564 self[i] = c.to_upper
1565 end
1566 end
1567 end
1568
1569 redef fun hash
1570 do
1571 if is_dirty then hash_cache = null
1572 return super
1573 end
1574
1575 # In Buffers, the internal sequence of character is mutable
1576 # Thus, `chars` can be used to modify the buffer.
1577 redef fun chars: Sequence[Char] is abstract
1578 end
1579
1580 # Mutable strings of characters.
1581 class FlatBuffer
1582 super FlatText
1583 super Buffer
1584
1585 redef var chars: Sequence[Char] = new FlatBufferCharView(self) is lazy
1586
1587 private var capacity: Int = 0
1588
1589 redef fun fast_cstring do return items.fast_cstring(0)
1590
1591 redef fun substrings do return new FlatSubstringsIter(self)
1592
1593 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
1594 #
1595 # This happens when an operation modifies the current `Buffer` and
1596 # the Copy-On-Write flag `written` is set at true.
1597 private fun reset do
1598 var nns = new NativeString(capacity)
1599 items.copy_to(nns, length, 0, 0)
1600 items = nns
1601 written = false
1602 end
1603
1604 redef fun [](index)
1605 do
1606 assert index >= 0
1607 assert index < length
1608 return items[index]
1609 end
1610
1611 redef fun []=(index, item)
1612 do
1613 is_dirty = true
1614 if index == length then
1615 add(item)
1616 return
1617 end
1618 if written then reset
1619 assert index >= 0 and index < length
1620 items[index] = item
1621 end
1622
1623 redef fun add(c)
1624 do
1625 is_dirty = true
1626 if capacity <= length then enlarge(length + 5)
1627 items[length] = c
1628 length += 1
1629 end
1630
1631 redef fun clear do
1632 is_dirty = true
1633 if written then reset
1634 length = 0
1635 end
1636
1637 redef fun empty do return new FlatBuffer
1638
1639 redef fun enlarge(cap)
1640 do
1641 var c = capacity
1642 if cap <= c then return
1643 while c <= cap do c = c * 2 + 2
1644 # The COW flag can be set at false here, since
1645 # it does a copy of the current `Buffer`
1646 written = false
1647 var a = new NativeString(c+1)
1648 if length > 0 then items.copy_to(a, length, 0, 0)
1649 items = a
1650 capacity = c
1651 end
1652
1653 redef fun to_s: String
1654 do
1655 written = true
1656 if length == 0 then items = new NativeString(1)
1657 return new FlatString.with_infos(items, length, 0, length - 1)
1658 end
1659
1660 redef fun to_cstring
1661 do
1662 if is_dirty then
1663 var new_native = new NativeString(length + 1)
1664 new_native[length] = '\0'
1665 if length > 0 then items.copy_to(new_native, length, 0, 0)
1666 real_items = new_native
1667 is_dirty = false
1668 end
1669 return real_items.as(not null)
1670 end
1671
1672 # Create a new empty string.
1673 init do end
1674
1675 # Low-level creation a new buffer with given data.
1676 #
1677 # `items` will be used as is, without copy, to store the characters of the buffer.
1678 # Aliasing issues is the responsibility of the caller.
1679 #
1680 # If `items` is shared, `written` should be set to true after the creation
1681 # so that a modification will do a copy-on-write.
1682 private init with_infos(items: NativeString, capacity, length: Int)
1683 do
1684 self.items = items
1685 self.length = length
1686 self.capacity = capacity
1687 end
1688
1689 # Create a new string copied from `s`.
1690 init from(s: Text)
1691 do
1692 capacity = s.length + 1
1693 length = s.length
1694 items = new NativeString(capacity)
1695 if s isa FlatString then
1696 s.items.copy_to(items, length, s.index_from, 0)
1697 else if s isa FlatBuffer then
1698 s.items.copy_to(items, length, 0, 0)
1699 else
1700 var curr_pos = 0
1701 for i in [0..s.length[ do
1702 var c = s.chars[i]
1703 items[curr_pos] = c
1704 curr_pos += 1
1705 end
1706 end
1707 end
1708
1709 # Create a new empty string with a given capacity.
1710 init with_capacity(cap: Int)
1711 do
1712 assert cap >= 0
1713 items = new NativeString(cap+1)
1714 capacity = cap
1715 length = 0
1716 end
1717
1718 redef fun append(s)
1719 do
1720 if s.is_empty then return
1721 is_dirty = true
1722 var sl = s.length
1723 if capacity < length + sl then enlarge(length + sl)
1724 if s isa FlatString then
1725 s.items.copy_to(items, sl, s.index_from, length)
1726 else if s isa FlatBuffer then
1727 s.items.copy_to(items, sl, 0, length)
1728 else
1729 var curr_pos = self.length
1730 for i in [0..s.length[ do
1731 var c = s.chars[i]
1732 items[curr_pos] = c
1733 curr_pos += 1
1734 end
1735 end
1736 length += sl
1737 end
1738
1739 # Copies the content of self in `dest`
1740 fun copy(start: Int, len: Int, dest: Buffer, new_start: Int)
1741 do
1742 var self_chars = self.chars
1743 var dest_chars = dest.chars
1744 for i in [0..len-1] do
1745 dest_chars[new_start+i] = self_chars[start+i]
1746 end
1747 end
1748
1749 redef fun substring(from, count)
1750 do
1751 assert count >= 0
1752 count += from
1753 if from < 0 then from = 0
1754 if count > length then count = length
1755 if from < count then
1756 var len = count - from
1757 var r_items = new NativeString(len)
1758 items.copy_to(r_items, len, from, 0)
1759 var r = new FlatBuffer.with_infos(r_items, len, len)
1760 return r
1761 else
1762 return new FlatBuffer
1763 end
1764 end
1765
1766 redef fun reverse
1767 do
1768 written = false
1769 var ns = new NativeString(capacity)
1770 var si = length - 1
1771 var ni = 0
1772 var it = items
1773 while si >= 0 do
1774 ns[ni] = it[si]
1775 ni += 1
1776 si -= 1
1777 end
1778 items = ns
1779 end
1780
1781 redef fun times(repeats)
1782 do
1783 var x = new FlatString.with_infos(items, length, 0, length - 1)
1784 for i in [1..repeats[ do
1785 append(x)
1786 end
1787 end
1788
1789 redef fun upper
1790 do
1791 if written then reset
1792 var it = items
1793 var id = length - 1
1794 while id >= 0 do
1795 it[id] = it[id].to_upper
1796 id -= 1
1797 end
1798 end
1799
1800 redef fun lower
1801 do
1802 if written then reset
1803 var it = items
1804 var id = length - 1
1805 while id >= 0 do
1806 it[id] = it[id].to_lower
1807 id -= 1
1808 end
1809 end
1810 end
1811
1812 private class FlatBufferReverseIterator
1813 super IndexedIterator[Char]
1814
1815 var target: FlatBuffer
1816
1817 var target_items: NativeString
1818
1819 var curr_pos: Int
1820
1821 init with_pos(tgt: FlatBuffer, pos: Int)
1822 do
1823 target = tgt
1824 if tgt.length > 0 then target_items = tgt.items
1825 curr_pos = pos
1826 end
1827
1828 redef fun index do return curr_pos
1829
1830 redef fun is_ok do return curr_pos >= 0
1831
1832 redef fun item do return target_items[curr_pos]
1833
1834 redef fun next do curr_pos -= 1
1835
1836 end
1837
1838 private class FlatBufferCharView
1839 super BufferCharView
1840
1841 redef type SELFTYPE: FlatBuffer
1842
1843 redef fun [](index) do return target.items[index]
1844
1845 redef fun []=(index, item)
1846 do
1847 assert index >= 0 and index <= length
1848 if index == length then
1849 add(item)
1850 return
1851 end
1852 target.items[index] = item
1853 end
1854
1855 redef fun push(c)
1856 do
1857 target.add(c)
1858 end
1859
1860 redef fun add(c)
1861 do
1862 target.add(c)
1863 end
1864
1865 fun enlarge(cap: Int)
1866 do
1867 target.enlarge(cap)
1868 end
1869
1870 redef fun append(s)
1871 do
1872 var s_length = s.length
1873 if target.capacity < s.length then enlarge(s_length + target.length)
1874 end
1875
1876 redef fun iterator_from(pos) do return new FlatBufferIterator.with_pos(target, pos)
1877
1878 redef fun reverse_iterator_from(pos) do return new FlatBufferReverseIterator.with_pos(target, pos)
1879
1880 end
1881
1882 private class FlatBufferIterator
1883 super IndexedIterator[Char]
1884
1885 var target: FlatBuffer
1886
1887 var target_items: NativeString
1888
1889 var curr_pos: Int
1890
1891 init with_pos(tgt: FlatBuffer, pos: Int)
1892 do
1893 target = tgt
1894 if tgt.length > 0 then target_items = tgt.items
1895 curr_pos = pos
1896 end
1897
1898 redef fun index do return curr_pos
1899
1900 redef fun is_ok do return curr_pos < target.length
1901
1902 redef fun item do return target_items[curr_pos]
1903
1904 redef fun next do curr_pos += 1
1905
1906 end
1907
1908 ###############################################################################
1909 # Refinement #
1910 ###############################################################################
1911
1912 redef class Object
1913 # User readable representation of `self`.
1914 fun to_s: String do return inspect
1915
1916 # The class name of the object in NativeString format.
1917 private fun native_class_name: NativeString is intern
1918
1919 # The class name of the object.
1920 #
1921 # assert 5.class_name == "Int"
1922 fun class_name: String do return native_class_name.to_s
1923
1924 # Developer readable representation of `self`.
1925 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
1926 fun inspect: String
1927 do
1928 return "<{inspect_head}>"
1929 end
1930
1931 # Return "CLASSNAME:#OBJECTID".
1932 # This function is mainly used with the redefinition of the inspect method
1933 protected fun inspect_head: String
1934 do
1935 return "{class_name}:#{object_id.to_hex}"
1936 end
1937 end
1938
1939 redef class Bool
1940 # assert true.to_s == "true"
1941 # assert false.to_s == "false"
1942 redef fun to_s
1943 do
1944 if self then
1945 return once "true"
1946 else
1947 return once "false"
1948 end
1949 end
1950 end
1951
1952 redef class Int
1953
1954 # Wrapper of strerror C function
1955 private fun strerror_ext: NativeString is extern `{
1956 return strerror(recv);
1957 `}
1958
1959 # Returns a string describing error number
1960 fun strerror: String do return strerror_ext.to_s
1961
1962 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
1963 # assume < to_c max const of char
1964 private fun fill_buffer(s: Buffer, base: Int, signed: Bool)
1965 do
1966 var n: Int
1967 # Sign
1968 if self < 0 then
1969 n = - self
1970 s.chars[0] = '-'
1971 else if self == 0 then
1972 s.chars[0] = '0'
1973 return
1974 else
1975 n = self
1976 end
1977 # Fill digits
1978 var pos = digit_count(base) - 1
1979 while pos >= 0 and n > 0 do
1980 s.chars[pos] = (n % base).to_c
1981 n = n / base # /
1982 pos -= 1
1983 end
1984 end
1985
1986 # C function to calculate the length of the `NativeString` to receive `self`
1987 private fun int_to_s_len: Int is extern "native_int_length_str"
1988
1989 # C function to convert an nit Int to a NativeString (char*)
1990 private fun native_int_to_s(nstr: NativeString, strlen: Int) is extern "native_int_to_s"
1991
1992 # return displayable int in base 10 and signed
1993 #
1994 # assert 1.to_s == "1"
1995 # assert (-123).to_s == "-123"
1996 redef fun to_s do
1997 # Fast case for common numbers
1998 if self == 0 then return "0"
1999 if self == 1 then return "1"
2000
2001 var nslen = int_to_s_len
2002 var ns = new NativeString(nslen + 1)
2003 ns[nslen] = '\0'
2004 native_int_to_s(ns, nslen + 1)
2005 return ns.to_s_with_length(nslen)
2006 end
2007
2008 # return displayable int in hexadecimal
2009 #
2010 # assert 1.to_hex == "1"
2011 # assert (-255).to_hex == "-ff"
2012 fun to_hex: String do return to_base(16,false)
2013
2014 # return displayable int in base base and signed
2015 fun to_base(base: Int, signed: Bool): String
2016 do
2017 var l = digit_count(base)
2018 var s = new FlatBuffer.from(" " * l)
2019 fill_buffer(s, base, signed)
2020 return s.to_s
2021 end
2022 end
2023
2024 redef class Float
2025 # Pretty representation of `self`, with decimals as needed from 1 to a maximum of 3
2026 #
2027 # assert 12.34.to_s == "12.34"
2028 # assert (-0120.030).to_s == "-120.03"
2029 #
2030 # see `to_precision` for a custom precision.
2031 redef fun to_s do
2032 var str = to_precision( 3 )
2033 if is_inf != 0 or is_nan then return str
2034 var len = str.length
2035 for i in [0..len-1] do
2036 var j = len-1-i
2037 var c = str.chars[j]
2038 if c == '0' then
2039 continue
2040 else if c == '.' then
2041 return str.substring( 0, j+2 )
2042 else
2043 return str.substring( 0, j+1 )
2044 end
2045 end
2046 return str
2047 end
2048
2049 # `String` representation of `self` with the given number of `decimals`
2050 #
2051 # assert 12.345.to_precision(0) == "12"
2052 # assert 12.345.to_precision(3) == "12.345"
2053 # assert (-12.345).to_precision(3) == "-12.345"
2054 # assert (-0.123).to_precision(3) == "-0.123"
2055 # assert 0.999.to_precision(2) == "1.00"
2056 # assert 0.999.to_precision(4) == "0.9990"
2057 fun to_precision(decimals: Int): String
2058 do
2059 if is_nan then return "nan"
2060
2061 var isinf = self.is_inf
2062 if isinf == 1 then
2063 return "inf"
2064 else if isinf == -1 then
2065 return "-inf"
2066 end
2067
2068 if decimals == 0 then return self.to_i.to_s
2069 var f = self
2070 for i in [0..decimals[ do f = f * 10.0
2071 if self > 0.0 then
2072 f = f + 0.5
2073 else
2074 f = f - 0.5
2075 end
2076 var i = f.to_i
2077 if i == 0 then return "0." + "0"*decimals
2078
2079 # Prepare both parts of the float, before and after the "."
2080 var s = i.abs.to_s
2081 var sl = s.length
2082 var p1
2083 var p2
2084 if sl > decimals then
2085 # Has something before the "."
2086 p1 = s.substring(0, sl-decimals)
2087 p2 = s.substring(sl-decimals, decimals)
2088 else
2089 p1 = "0"
2090 p2 = "0"*(decimals-sl) + s
2091 end
2092
2093 if i < 0 then p1 = "-" + p1
2094
2095 return p1 + "." + p2
2096 end
2097 end
2098
2099 redef class Char
2100 # assert 'x'.to_s == "x"
2101 redef fun to_s
2102 do
2103 var s = new FlatBuffer.with_capacity(1)
2104 s.chars[0] = self
2105 return s.to_s
2106 end
2107
2108 # Returns true if the char is a numerical digit
2109 #
2110 # assert '0'.is_numeric
2111 # assert '9'.is_numeric
2112 # assert not 'a'.is_numeric
2113 # assert not '?'.is_numeric
2114 fun is_numeric: Bool
2115 do
2116 return self >= '0' and self <= '9'
2117 end
2118
2119 # Returns true if the char is an alpha digit
2120 #
2121 # assert 'a'.is_alpha
2122 # assert 'Z'.is_alpha
2123 # assert not '0'.is_alpha
2124 # assert not '?'.is_alpha
2125 fun is_alpha: Bool
2126 do
2127 return (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z')
2128 end
2129
2130 # Returns true if the char is an alpha or a numeric digit
2131 #
2132 # assert 'a'.is_alphanumeric
2133 # assert 'Z'.is_alphanumeric
2134 # assert '0'.is_alphanumeric
2135 # assert '9'.is_alphanumeric
2136 # assert not '?'.is_alphanumeric
2137 fun is_alphanumeric: Bool
2138 do
2139 return self.is_numeric or self.is_alpha
2140 end
2141 end
2142
2143 redef class Collection[E]
2144 # Concatenate elements.
2145 redef fun to_s
2146 do
2147 var s = new FlatBuffer
2148 for e in self do if e != null then s.append(e.to_s)
2149 return s.to_s
2150 end
2151
2152 # Concatenate and separate each elements with `sep`.
2153 #
2154 # assert [1, 2, 3].join(":") == "1:2:3"
2155 # assert [1..3].join(":") == "1:2:3"
2156 fun join(sep: Text): String
2157 do
2158 if is_empty then return ""
2159
2160 var s = new FlatBuffer # Result
2161
2162 # Concat first item
2163 var i = iterator
2164 var e = i.item
2165 if e != null then s.append(e.to_s)
2166
2167 # Concat other items
2168 i.next
2169 while i.is_ok do
2170 s.append(sep)
2171 e = i.item
2172 if e != null then s.append(e.to_s)
2173 i.next
2174 end
2175 return s.to_s
2176 end
2177 end
2178
2179 redef class Array[E]
2180
2181 # Fast implementation
2182 redef fun to_s
2183 do
2184 var l = length
2185 if l == 0 then return ""
2186 if l == 1 then if self[0] == null then return "" else return self[0].to_s
2187 var its = _items
2188 var na = new NativeArray[String](l)
2189 var i = 0
2190 var sl = 0
2191 var mypos = 0
2192 while i < l do
2193 var itsi = its[i]
2194 if itsi == null then
2195 i += 1
2196 continue
2197 end
2198 var tmp = itsi.to_s
2199 sl += tmp.length
2200 na[mypos] = tmp
2201 i += 1
2202 mypos += 1
2203 end
2204 var ns = new NativeString(sl + 1)
2205 ns[sl] = '\0'
2206 i = 0
2207 var off = 0
2208 while i < mypos do
2209 var tmp = na[i]
2210 var tpl = tmp.length
2211 if tmp isa FlatString then
2212 tmp.items.copy_to(ns, tpl, tmp.index_from, off)
2213 off += tpl
2214 else
2215 for j in tmp.substrings do
2216 var s = j.as(FlatString)
2217 var slen = s.length
2218 s.items.copy_to(ns, slen, s.index_from, off)
2219 off += slen
2220 end
2221 end
2222 i += 1
2223 end
2224 return ns.to_s_with_length(sl)
2225 end
2226 end
2227
2228 redef class NativeArray[E]
2229 # Join all the elements using `to_s`
2230 #
2231 # REQUIRE: `self isa NativeArray[String]`
2232 # REQUIRE: all elements are initialized
2233 fun native_to_s: String
2234 do
2235 assert self isa NativeArray[String]
2236 var l = length
2237 var na = self
2238 var i = 0
2239 var sl = 0
2240 var mypos = 0
2241 while i < l do
2242 sl += na[i].length
2243 i += 1
2244 mypos += 1
2245 end
2246 var ns = new NativeString(sl + 1)
2247 ns[sl] = '\0'
2248 i = 0
2249 var off = 0
2250 while i < mypos do
2251 var tmp = na[i]
2252 var tpl = tmp.length
2253 if tmp isa FlatString then
2254 tmp.items.copy_to(ns, tpl, tmp.index_from, off)
2255 off += tpl
2256 else
2257 for j in tmp.substrings do
2258 var s = j.as(FlatString)
2259 var slen = s.length
2260 s.items.copy_to(ns, slen, s.index_from, off)
2261 off += slen
2262 end
2263 end
2264 i += 1
2265 end
2266 return ns.to_s_with_length(sl)
2267 end
2268 end
2269
2270 redef class Map[K,V]
2271 # Concatenate couple of 'key value'.
2272 # key and value are separated by `couple_sep`.
2273 # each couple is separated each couple with `sep`.
2274 #
2275 # var m = new ArrayMap[Int, String]
2276 # m[1] = "one"
2277 # m[10] = "ten"
2278 # assert m.join("; ", "=") == "1=one; 10=ten"
2279 fun join(sep: String, couple_sep: String): String
2280 do
2281 if is_empty then return ""
2282
2283 var s = new FlatBuffer # Result
2284
2285 # Concat first item
2286 var i = iterator
2287 var k = i.key
2288 var e = i.item
2289 s.append("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
2290
2291 # Concat other items
2292 i.next
2293 while i.is_ok do
2294 s.append(sep)
2295 k = i.key
2296 e = i.item
2297 s.append("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
2298 i.next
2299 end
2300 return s.to_s
2301 end
2302 end
2303
2304 ###############################################################################
2305 # Native classes #
2306 ###############################################################################
2307
2308 # Native strings are simple C char *
2309 extern class NativeString `{ char* `}
2310 # Creates a new NativeString with a capacity of `length`
2311 new(length: Int) is intern
2312
2313 # Returns a char* starting at `index`.
2314 #
2315 # WARNING: Unsafe for extern code, use only for temporary
2316 # pointer manipulation purposes (e.g. write to file or such)
2317 fun fast_cstring(index: Int): NativeString is intern
2318
2319 # Get char at `index`.
2320 fun [](index: Int): Char is intern
2321
2322 # Set char `item` at index.
2323 fun []=(index: Int, item: Char) is intern
2324
2325 # Copy `self` to `dest`.
2326 fun copy_to(dest: NativeString, length: Int, from: Int, to: Int) is intern
2327
2328 # Position of the first nul character.
2329 fun cstring_length: Int
2330 do
2331 var l = 0
2332 while self[l] != '\0' do l += 1
2333 return l
2334 end
2335
2336 # Parse `self` as an Int.
2337 fun atoi: Int is intern
2338
2339 # Parse `self` as a Float.
2340 fun atof: Float is extern "atof"
2341
2342 redef fun to_s
2343 do
2344 return to_s_with_length(cstring_length)
2345 end
2346
2347 # Returns `self` as a String of `length`.
2348 fun to_s_with_length(length: Int): FlatString
2349 do
2350 assert length >= 0
2351 var str = new FlatString.with_infos(self, length, 0, length - 1)
2352 return str
2353 end
2354
2355 # Returns `self` as a new String.
2356 fun to_s_with_copy: FlatString
2357 do
2358 var length = cstring_length
2359 var new_self = new NativeString(length + 1)
2360 copy_to(new_self, length, 0, 0)
2361 var str = new FlatString.with_infos(new_self, length, 0, length - 1)
2362 new_self[length] = '\0'
2363 str.real_items = new_self
2364 return str
2365 end
2366 end
2367
2368 redef class Sys
2369 private var args_cache: nullable Sequence[String]
2370
2371 # The arguments of the program as given by the OS
2372 fun program_args: Sequence[String]
2373 do
2374 if _args_cache == null then init_args
2375 return _args_cache.as(not null)
2376 end
2377
2378 # The name of the program as given by the OS
2379 fun program_name: String
2380 do
2381 return native_argv(0).to_s
2382 end
2383
2384 # Initialize `program_args` with the contents of `native_argc` and `native_argv`.
2385 private fun init_args
2386 do
2387 var argc = native_argc
2388 var args = new Array[String].with_capacity(0)
2389 var i = 1
2390 while i < argc do
2391 args[i-1] = native_argv(i).to_s
2392 i += 1
2393 end
2394 _args_cache = args
2395 end
2396
2397 # First argument of the main C function.
2398 private fun native_argc: Int is intern
2399
2400 # Second argument of the main C function.
2401 private fun native_argv(i: Int): NativeString is intern
2402 end
2403
2404 # Comparator that efficienlty use `to_s` to compare things
2405 #
2406 # The comparaison call `to_s` on object and use the result to order things.
2407 #
2408 # var a = [1, 2, 3, 10, 20]
2409 # (new CachedAlphaComparator).sort(a)
2410 # assert a == [1, 10, 2, 20, 3]
2411 #
2412 # Internally the result of `to_s` is cached in a HashMap to counter
2413 # uneficient implementation of `to_s`.
2414 #
2415 # Note: it caching is not usefull, see `alpha_comparator`
2416 class CachedAlphaComparator
2417 super Comparator
2418 redef type COMPARED: Object
2419
2420 private var cache = new HashMap[Object, String]
2421
2422 private fun do_to_s(a: Object): String do
2423 if cache.has_key(a) then return cache[a]
2424 var res = a.to_s
2425 cache[a] = res
2426 return res
2427 end
2428
2429 redef fun compare(a, b) do
2430 return do_to_s(a) <=> do_to_s(b)
2431 end
2432 end
2433
2434 # see `alpha_comparator`
2435 private class AlphaComparator
2436 super Comparator
2437 redef fun compare(a, b) do return a.to_s <=> b.to_s
2438 end
2439
2440 # Stateless comparator that naively use `to_s` to compare things.
2441 #
2442 # Note: the result of `to_s` is not cached, thus can be invoked a lot
2443 # on a single instace. See `CachedAlphaComparator` as an alternative.
2444 #
2445 # var a = [1, 2, 3, 10, 20]
2446 # alpha_comparator.sort(a)
2447 # assert a == [1, 10, 2, 20, 3]
2448 fun alpha_comparator: Comparator do return once new AlphaComparator
2449
2450 # The arguments of the program as given by the OS
2451 fun args: Sequence[String]
2452 do
2453 return sys.program_args
2454 end