tests: add base_with.nit
[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 fun escape_to_c: String
535 do
536 var b = new FlatBuffer
537 for i in [0..length[ do
538 var c = chars[i]
539 if c == '\n' then
540 b.append("\\n")
541 else if c == '\0' then
542 b.append("\\0")
543 else if c == '"' then
544 b.append("\\\"")
545 else if c == '\'' then
546 b.append("\\\'")
547 else if c == '\\' then
548 b.append("\\\\")
549 else if c.ascii < 32 then
550 b.append("\\{c.ascii.to_base(8, false)}")
551 else
552 b.add(c)
553 end
554 end
555 return b.to_s
556 end
557
558 # Escape additionnal characters
559 # The result might no be legal in C but be used in other languages
560 #
561 # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
562 fun escape_more_to_c(chars: String): String
563 do
564 var b = new FlatBuffer
565 for c in escape_to_c.chars do
566 if chars.chars.has(c) then
567 b.add('\\')
568 end
569 b.add(c)
570 end
571 return b.to_s
572 end
573
574 # Escape to C plus braces
575 #
576 # assert "\n\"'\\\{\}".escape_to_nit == "\\n\\\"\\'\\\\\\\{\\\}"
577 fun escape_to_nit: String do return escape_more_to_c("\{\}")
578
579 # Escape to POSIX Shell (sh).
580 #
581 # Abort if the text contains a null byte.
582 #
583 # assert "\n\"'\\\{\}0".escape_to_sh == "'\n\"'\\''\\\{\}0'"
584 fun escape_to_sh: String do
585 var b = new FlatBuffer
586 b.chars.add '\''
587 for i in [0..length[ do
588 var c = chars[i]
589 if c == '\'' then
590 b.append("'\\''")
591 else
592 assert without_null_byte: c != '\0'
593 b.add(c)
594 end
595 end
596 b.chars.add '\''
597 return b.to_s
598 end
599
600 # Escape to include in a Makefile
601 #
602 # Unfortunately, some characters are not escapable in Makefile.
603 # These characters are `;`, `|`, `\`, and the non-printable ones.
604 # They will be rendered as `"?{hex}"`.
605 fun escape_to_mk: String do
606 var b = new FlatBuffer
607 for i in [0..length[ do
608 var c = chars[i]
609 if c == '$' then
610 b.append("$$")
611 else if c == ':' or c == ' ' or c == '#' then
612 b.add('\\')
613 b.add(c)
614 else if c.ascii < 32 or c == ';' or c == '|' or c == '\\' or c == '=' then
615 b.append("?{c.ascii.to_base(16, false)}")
616 else
617 b.add(c)
618 end
619 end
620 return b.to_s
621 end
622
623 # Return a string where Nit escape sequences are transformed.
624 #
625 # var s = "\\n"
626 # assert s.length == 2
627 # var u = s.unescape_nit
628 # assert u.length == 1
629 # assert u.chars[0].ascii == 10 # (the ASCII value of the "new line" character)
630 fun unescape_nit: String
631 do
632 var res = new FlatBuffer.with_capacity(self.length)
633 var was_slash = false
634 for i in [0..length[ do
635 var c = chars[i]
636 if not was_slash then
637 if c == '\\' then
638 was_slash = true
639 else
640 res.add(c)
641 end
642 continue
643 end
644 was_slash = false
645 if c == 'n' then
646 res.add('\n')
647 else if c == 'r' then
648 res.add('\r')
649 else if c == 't' then
650 res.add('\t')
651 else if c == '0' then
652 res.add('\0')
653 else
654 res.add(c)
655 end
656 end
657 return res.to_s
658 end
659
660 # Encode `self` to percent (or URL) encoding
661 #
662 # assert "aBc09-._~".to_percent_encoding == "aBc09-._~"
663 # assert "%()< >".to_percent_encoding == "%25%28%29%3c%20%3e"
664 # assert ".com/post?e=asdf&f=123".to_percent_encoding == ".com%2fpost%3fe%3dasdf%26f%3d123"
665 fun to_percent_encoding: String
666 do
667 var buf = new FlatBuffer
668
669 for i in [0..length[ do
670 var c = chars[i]
671 if (c >= '0' and c <= '9') or
672 (c >= 'a' and c <= 'z') or
673 (c >= 'A' and c <= 'Z') or
674 c == '-' or c == '.' or
675 c == '_' or c == '~'
676 then
677 buf.add c
678 else buf.append "%{c.ascii.to_hex}"
679 end
680
681 return buf.to_s
682 end
683
684 # Decode `self` from percent (or URL) encoding to a clear string
685 #
686 # Replace invalid use of '%' with '?'.
687 #
688 # assert "aBc09-._~".from_percent_encoding == "aBc09-._~"
689 # assert "%25%28%29%3c%20%3e".from_percent_encoding == "%()< >"
690 # assert ".com%2fpost%3fe%3dasdf%26f%3d123".from_percent_encoding == ".com/post?e=asdf&f=123"
691 # assert "%25%28%29%3C%20%3E".from_percent_encoding == "%()< >"
692 # assert "incomplete %".from_percent_encoding == "incomplete ?"
693 # assert "invalid % usage".from_percent_encoding == "invalid ? usage"
694 fun from_percent_encoding: String
695 do
696 var buf = new FlatBuffer
697
698 var i = 0
699 while i < length do
700 var c = chars[i]
701 if c == '%' then
702 if i + 2 >= length then
703 # What follows % has been cut off
704 buf.add '?'
705 else
706 i += 1
707 var hex_s = substring(i, 2)
708 if hex_s.is_hex then
709 var hex_i = hex_s.to_hex
710 buf.add hex_i.ascii
711 i += 1
712 else
713 # What follows a % is not Hex
714 buf.add '?'
715 i -= 1
716 end
717 end
718 else buf.add c
719
720 i += 1
721 end
722
723 return buf.to_s
724 end
725
726 # Escape the characters `<`, `>`, `&`, `"`, `'` and `/` as HTML/XML entity references.
727 #
728 # assert "a&b-<>\"x\"/'".html_escape == "a&amp;b-&lt;&gt;&#34;x&#34;&#47;&#39;"
729 #
730 # 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>
731 fun html_escape: String
732 do
733 var buf = new FlatBuffer
734
735 for i in [0..length[ do
736 var c = chars[i]
737 if c == '&' then
738 buf.append "&amp;"
739 else if c == '<' then
740 buf.append "&lt;"
741 else if c == '>' then
742 buf.append "&gt;"
743 else if c == '"' then
744 buf.append "&#34;"
745 else if c == '\'' then
746 buf.append "&#39;"
747 else if c == '/' then
748 buf.append "&#47;"
749 else buf.add c
750 end
751
752 return buf.to_s
753 end
754
755 # Equality of text
756 # Two pieces of text are equals if thez have the same characters in the same order.
757 #
758 # assert "hello" == "hello"
759 # assert "hello" != "HELLO"
760 # assert "hello" == "hel"+"lo"
761 #
762 # Things that are not Text are not equal.
763 #
764 # assert "9" != '9'
765 # assert "9" != ['9']
766 # assert "9" != 9
767 #
768 # assert "9".chars.first == '9' # equality of Char
769 # assert "9".chars == ['9'] # equality of Sequence
770 # assert "9".to_i == 9 # equality of Int
771 redef fun ==(o)
772 do
773 if o == null then return false
774 if not o isa Text then return false
775 if self.is_same_instance(o) then return true
776 if self.length != o.length then return false
777 return self.chars == o.chars
778 end
779
780 # Lexicographical comparaison
781 #
782 # assert "abc" < "xy"
783 # assert "ABC" < "abc"
784 redef fun <(other)
785 do
786 var self_chars = self.chars.iterator
787 var other_chars = other.chars.iterator
788
789 while self_chars.is_ok and other_chars.is_ok do
790 if self_chars.item < other_chars.item then return true
791 if self_chars.item > other_chars.item then return false
792 self_chars.next
793 other_chars.next
794 end
795
796 if self_chars.is_ok then
797 return false
798 else
799 return true
800 end
801 end
802
803 # Escape string used in labels for graphviz
804 #
805 # assert ">><<".escape_to_dot == "\\>\\>\\<\\<"
806 fun escape_to_dot: String
807 do
808 return escape_more_to_c("|\{\}<>")
809 end
810
811 # Flat representation of self
812 fun flatten: FlatText is abstract
813
814 private var hash_cache: nullable Int = null
815
816 redef fun hash
817 do
818 if hash_cache == null then
819 # djb2 hash algorithm
820 var h = 5381
821
822 for i in [0..length[ do
823 var char = chars[i]
824 h = h.lshift(5) + h + char.ascii
825 end
826
827 hash_cache = h
828 end
829 return hash_cache.as(not null)
830 end
831
832 end
833
834 # All kinds of array-based text representations.
835 abstract class FlatText
836 super Text
837
838 # Underlying C-String (`char*`)
839 #
840 # Warning : Might be void in some subclasses, be sure to check
841 # if set before using it.
842 private var items: NativeString is noinit
843
844 # Real items, used as cache for to_cstring is called
845 private var real_items: nullable NativeString = null
846
847 redef var length: Int = 0
848
849 redef fun output
850 do
851 var i = 0
852 while i < length do
853 items[i].output
854 i += 1
855 end
856 end
857
858 redef fun flatten do return self
859 end
860
861 # Abstract class for the SequenceRead compatible
862 # views on String and Buffer objects
863 private abstract class StringCharView
864 super SequenceRead[Char]
865
866 type SELFTYPE: Text
867
868 var target: SELFTYPE
869
870 redef fun is_empty do return target.is_empty
871
872 redef fun length do return target.length
873
874 redef fun iterator: IndexedIterator[Char] do return self.iterator_from(0)
875
876 redef fun reverse_iterator do return self.reverse_iterator_from(self.length - 1)
877 end
878
879 # View on Buffer objects, extends Sequence
880 # for mutation operations
881 private abstract class BufferCharView
882 super StringCharView
883 super Sequence[Char]
884
885 redef type SELFTYPE: Buffer
886
887 end
888
889 # A `String` holds and manipulates an arbitrary sequence of characters.
890 #
891 # String objects may be created using literals.
892 #
893 # assert "Hello World!" isa String
894 abstract class String
895 super Text
896
897 redef type SELFTYPE: String is fixed
898
899 redef fun to_s do return self
900
901 # Concatenates `o` to `self`
902 #
903 # assert "hello" + "world" == "helloworld"
904 # assert "" + "hello" + "" == "hello"
905 fun +(o: Text): SELFTYPE is abstract
906
907 # Concatenates self `i` times
908 #
909 # assert "abc" * 4 == "abcabcabcabc"
910 # assert "abc" * 1 == "abc"
911 # assert "abc" * 0 == ""
912 fun *(i: Int): SELFTYPE is abstract
913
914 # Insert `s` at `pos`.
915 #
916 # assert "helloworld".insert_at(" ", 5) == "hello world"
917 fun insert_at(s: String, pos: Int): SELFTYPE is abstract
918
919 redef fun substrings: Iterator[String] is abstract
920
921 # Returns a reversed version of self
922 #
923 # assert "hello".reversed == "olleh"
924 # assert "bob".reversed == "bob"
925 # assert "".reversed == ""
926 fun reversed: SELFTYPE is abstract
927
928 # A upper case version of `self`
929 #
930 # assert "Hello World!".to_upper == "HELLO WORLD!"
931 fun to_upper: SELFTYPE is abstract
932
933 # A lower case version of `self`
934 #
935 # assert "Hello World!".to_lower == "hello world!"
936 fun to_lower : SELFTYPE is abstract
937
938 # Takes a camel case `self` and converts it to snake case
939 #
940 # assert "randomMethodId".to_snake_case == "random_method_id"
941 #
942 # If `self` is upper, it is returned unchanged
943 #
944 # assert "RANDOM_METHOD_ID".to_snake_case == "RANDOM_METHOD_ID"
945 #
946 # If the identifier is prefixed by an underscore, the underscore is ignored
947 #
948 # assert "_privateField".to_snake_case == "_private_field"
949 fun to_snake_case: SELFTYPE
950 do
951 if self.is_upper then return self
952
953 var new_str = new FlatBuffer.with_capacity(self.length)
954 var is_first_char = true
955
956 for i in [0..length[ do
957 var char = chars[i]
958 if is_first_char then
959 new_str.add(char.to_lower)
960 is_first_char = false
961 else if char.is_upper then
962 new_str.add('_')
963 new_str.add(char.to_lower)
964 else
965 new_str.add(char)
966 end
967 end
968
969 return new_str.to_s
970 end
971
972 # Takes a snake case `self` and converts it to camel case
973 #
974 # assert "random_method_id".to_camel_case == "randomMethodId"
975 #
976 # If the identifier is prefixed by an underscore, the underscore is ignored
977 #
978 # assert "_private_field".to_camel_case == "_privateField"
979 #
980 # If `self` is upper, it is returned unchanged
981 #
982 # assert "RANDOM_ID".to_camel_case == "RANDOM_ID"
983 #
984 # If there are several consecutive underscores, they are considered as a single one
985 #
986 # assert "random__method_id".to_camel_case == "randomMethodId"
987 fun to_camel_case: SELFTYPE
988 do
989 if self.is_upper then return self
990
991 var new_str = new FlatBuffer
992 var is_first_char = true
993 var follows_us = false
994
995 for i in [0..length[ do
996 var char = chars[i]
997 if is_first_char then
998 new_str.add(char)
999 is_first_char = false
1000 else if char == '_' then
1001 follows_us = true
1002 else if follows_us then
1003 new_str.add(char.to_upper)
1004 follows_us = false
1005 else
1006 new_str.add(char)
1007 end
1008 end
1009
1010 return new_str.to_s
1011 end
1012
1013 # Returns a capitalized `self`
1014 #
1015 # Letters that follow a letter are lowercased
1016 # Letters that follow a non-letter are upcased.
1017 #
1018 # SEE : `Char::is_letter` for the definition of letter.
1019 #
1020 # assert "jAVASCRIPT".capitalized == "Javascript"
1021 # assert "i am root".capitalized == "I Am Root"
1022 # assert "ab_c -ab0c ab\nc".capitalized == "Ab_C -Ab0C Ab\nC"
1023 fun capitalized: SELFTYPE do
1024 if length == 0 then return self
1025
1026 var buf = new FlatBuffer.with_capacity(length)
1027
1028 var curr = chars[0].to_upper
1029 var prev = curr
1030 buf[0] = curr
1031
1032 for i in [1 .. length[ do
1033 prev = curr
1034 curr = self[i]
1035 if prev.is_letter then
1036 buf[i] = curr.to_lower
1037 else
1038 buf[i] = curr.to_upper
1039 end
1040 end
1041
1042 return buf.to_s
1043 end
1044 end
1045
1046 private class FlatSubstringsIter
1047 super Iterator[FlatText]
1048
1049 var tgt: nullable FlatText
1050
1051 redef fun item do
1052 assert is_ok
1053 return tgt.as(not null)
1054 end
1055
1056 redef fun is_ok do return tgt != null
1057
1058 redef fun next do tgt = null
1059 end
1060
1061 # Immutable strings of characters.
1062 class FlatString
1063 super FlatText
1064 super String
1065
1066 # Index in _items of the start of the string
1067 private var index_from: Int is noinit
1068
1069 # Indes in _items of the last item of the string
1070 private var index_to: Int is noinit
1071
1072 redef var chars: SequenceRead[Char] = new FlatStringCharView(self) is lazy
1073
1074 redef fun [](index)
1075 do
1076 # Check that the index (+ index_from) is not larger than indexTo
1077 # In other terms, if the index is valid
1078 assert index >= 0
1079 assert (index + index_from) <= index_to
1080 return items[index + index_from]
1081 end
1082
1083 ################################################
1084 # AbstractString specific methods #
1085 ################################################
1086
1087 redef fun reversed
1088 do
1089 var native = new NativeString(self.length + 1)
1090 var length = self.length
1091 var items = self.items
1092 var pos = 0
1093 var ipos = length-1
1094 while pos < length do
1095 native[pos] = items[ipos]
1096 pos += 1
1097 ipos -= 1
1098 end
1099 return native.to_s_with_length(self.length)
1100 end
1101
1102 redef fun substring(from, count)
1103 do
1104 assert count >= 0
1105
1106 if from < 0 then
1107 count += from
1108 if count < 0 then count = 0
1109 from = 0
1110 end
1111
1112 var new_from = index_from + from
1113
1114 if (new_from + count) > index_to then
1115 var new_len = index_to - new_from + 1
1116 if new_len <= 0 then return empty
1117 return new FlatString.with_infos(items, new_len, new_from, index_to)
1118 end
1119
1120 if count <= 0 then return empty
1121
1122 var to = new_from + count - 1
1123
1124 return new FlatString.with_infos(items, to - new_from + 1, new_from, to)
1125 end
1126
1127 redef fun empty do return "".as(FlatString)
1128
1129 redef fun to_upper
1130 do
1131 var outstr = new NativeString(self.length + 1)
1132 var out_index = 0
1133
1134 var myitems = self.items
1135 var index_from = self.index_from
1136 var max = self.index_to
1137
1138 while index_from <= max do
1139 outstr[out_index] = myitems[index_from].to_upper
1140 out_index += 1
1141 index_from += 1
1142 end
1143
1144 outstr[self.length] = '\0'
1145
1146 return outstr.to_s_with_length(self.length)
1147 end
1148
1149 redef fun to_lower
1150 do
1151 var outstr = new NativeString(self.length + 1)
1152 var out_index = 0
1153
1154 var myitems = self.items
1155 var index_from = self.index_from
1156 var max = self.index_to
1157
1158 while index_from <= max do
1159 outstr[out_index] = myitems[index_from].to_lower
1160 out_index += 1
1161 index_from += 1
1162 end
1163
1164 outstr[self.length] = '\0'
1165
1166 return outstr.to_s_with_length(self.length)
1167 end
1168
1169 redef fun output
1170 do
1171 var i = self.index_from
1172 var imax = self.index_to
1173 while i <= imax do
1174 items[i].output
1175 i += 1
1176 end
1177 end
1178
1179 ##################################################
1180 # String Specific Methods #
1181 ##################################################
1182
1183 # Low-level creation of a new string with given data.
1184 #
1185 # `items` will be used as is, without copy, to retrieve the characters of the string.
1186 # Aliasing issues is the responsibility of the caller.
1187 private init with_infos(items: NativeString, length: Int, from: Int, to: Int)
1188 do
1189 self.items = items
1190 self.length = length
1191 index_from = from
1192 index_to = to
1193 end
1194
1195 redef fun to_cstring: NativeString
1196 do
1197 if real_items != null then
1198 return real_items.as(not null)
1199 else
1200 var newItems = new NativeString(length + 1)
1201 self.items.copy_to(newItems, length, index_from, 0)
1202 newItems[length] = '\0'
1203 self.real_items = newItems
1204 return newItems
1205 end
1206 end
1207
1208 redef fun ==(other)
1209 do
1210 if not other isa FlatString then return super
1211
1212 if self.object_id == other.object_id then return true
1213
1214 var my_length = length
1215
1216 if other.length != my_length then return false
1217
1218 var my_index = index_from
1219 var its_index = other.index_from
1220
1221 var last_iteration = my_index + my_length
1222
1223 var itsitems = other.items
1224 var myitems = self.items
1225
1226 while my_index < last_iteration do
1227 if myitems[my_index] != itsitems[its_index] then return false
1228 my_index += 1
1229 its_index += 1
1230 end
1231
1232 return true
1233 end
1234
1235 redef fun <(other)
1236 do
1237 if not other isa FlatString then return super
1238
1239 if self.object_id == other.object_id then return false
1240
1241 var my_curr_char : Char
1242 var its_curr_char : Char
1243
1244 var curr_id_self = self.index_from
1245 var curr_id_other = other.index_from
1246
1247 var my_items = self.items
1248 var its_items = other.items
1249
1250 var my_length = self.length
1251 var its_length = other.length
1252
1253 var max_iterations = curr_id_self + my_length
1254
1255 while curr_id_self < max_iterations do
1256 my_curr_char = my_items[curr_id_self]
1257 its_curr_char = its_items[curr_id_other]
1258
1259 if my_curr_char != its_curr_char then
1260 if my_curr_char < its_curr_char then return true
1261 return false
1262 end
1263
1264 curr_id_self += 1
1265 curr_id_other += 1
1266 end
1267
1268 return my_length < its_length
1269 end
1270
1271 redef fun +(s)
1272 do
1273 var my_length = self.length
1274 var its_length = s.length
1275
1276 var total_length = my_length + its_length
1277
1278 var target_string = new NativeString(my_length + its_length + 1)
1279
1280 self.items.copy_to(target_string, my_length, index_from, 0)
1281 if s isa FlatString then
1282 s.items.copy_to(target_string, its_length, s.index_from, my_length)
1283 else if s isa FlatBuffer then
1284 s.items.copy_to(target_string, its_length, 0, my_length)
1285 else
1286 var curr_pos = my_length
1287 for i in [0..s.length[ do
1288 var c = s.chars[i]
1289 target_string[curr_pos] = c
1290 curr_pos += 1
1291 end
1292 end
1293
1294 target_string[total_length] = '\0'
1295
1296 return target_string.to_s_with_length(total_length)
1297 end
1298
1299 redef fun *(i)
1300 do
1301 assert i >= 0
1302
1303 var my_length = self.length
1304
1305 var final_length = my_length * i
1306
1307 var my_items = self.items
1308
1309 var target_string = new NativeString(final_length + 1)
1310
1311 target_string[final_length] = '\0'
1312
1313 var current_last = 0
1314
1315 for iteration in [1 .. i] do
1316 my_items.copy_to(target_string, my_length, 0, current_last)
1317 current_last += my_length
1318 end
1319
1320 return target_string.to_s_with_length(final_length)
1321 end
1322
1323 redef fun hash
1324 do
1325 if hash_cache == null then
1326 # djb2 hash algorithm
1327 var h = 5381
1328 var i = index_from
1329
1330 var myitems = items
1331
1332 while i <= index_to do
1333 h = h.lshift(5) + h + myitems[i].ascii
1334 i += 1
1335 end
1336
1337 hash_cache = h
1338 end
1339
1340 return hash_cache.as(not null)
1341 end
1342
1343 redef fun substrings do return new FlatSubstringsIter(self)
1344 end
1345
1346 private class FlatStringReverseIterator
1347 super IndexedIterator[Char]
1348
1349 var target: FlatString
1350
1351 var target_items: NativeString
1352
1353 var curr_pos: Int
1354
1355 init with_pos(tgt: FlatString, pos: Int)
1356 do
1357 target = tgt
1358 target_items = tgt.items
1359 curr_pos = pos + tgt.index_from
1360 end
1361
1362 redef fun is_ok do return curr_pos >= target.index_from
1363
1364 redef fun item do return target_items[curr_pos]
1365
1366 redef fun next do curr_pos -= 1
1367
1368 redef fun index do return curr_pos - target.index_from
1369
1370 end
1371
1372 private class FlatStringIterator
1373 super IndexedIterator[Char]
1374
1375 var target: FlatString
1376
1377 var target_items: NativeString
1378
1379 var curr_pos: Int
1380
1381 init with_pos(tgt: FlatString, pos: Int)
1382 do
1383 target = tgt
1384 target_items = tgt.items
1385 curr_pos = pos + target.index_from
1386 end
1387
1388 redef fun is_ok do return curr_pos <= target.index_to
1389
1390 redef fun item do return target_items[curr_pos]
1391
1392 redef fun next do curr_pos += 1
1393
1394 redef fun index do return curr_pos - target.index_from
1395
1396 end
1397
1398 private class FlatStringCharView
1399 super StringCharView
1400
1401 redef type SELFTYPE: FlatString
1402
1403 redef fun [](index)
1404 do
1405 # Check that the index (+ index_from) is not larger than indexTo
1406 # In other terms, if the index is valid
1407 assert index >= 0
1408 var target = self.target
1409 assert (index + target.index_from) <= target.index_to
1410 return target.items[index + target.index_from]
1411 end
1412
1413 redef fun iterator_from(start) do return new FlatStringIterator.with_pos(target, start)
1414
1415 redef fun reverse_iterator_from(start) do return new FlatStringReverseIterator.with_pos(target, start)
1416
1417 end
1418
1419 # A mutable sequence of characters.
1420 abstract class Buffer
1421 super Text
1422
1423 redef type SELFTYPE: Buffer is fixed
1424
1425 # Specific implementations MUST set this to `true` in order to invalidate caches
1426 protected var is_dirty = true
1427
1428 # Copy-On-Write flag
1429 #
1430 # If the `Buffer` was to_s'd, the next in-place altering
1431 # operation will cause the current `Buffer` to be re-allocated.
1432 #
1433 # The flag will then be set at `false`.
1434 protected var written = false
1435
1436 # Modifies the char contained at pos `index`
1437 #
1438 # DEPRECATED : Use self.chars.[]= instead
1439 fun []=(index: Int, item: Char) is abstract
1440
1441 # Adds a char `c` at the end of self
1442 #
1443 # DEPRECATED : Use self.chars.add instead
1444 fun add(c: Char) is abstract
1445
1446 # Clears the buffer
1447 #
1448 # var b = new FlatBuffer
1449 # b.append "hello"
1450 # assert not b.is_empty
1451 # b.clear
1452 # assert b.is_empty
1453 fun clear is abstract
1454
1455 # Enlarges the subsequent array containing the chars of self
1456 fun enlarge(cap: Int) is abstract
1457
1458 # Adds the content of text `s` at the end of self
1459 #
1460 # var b = new FlatBuffer
1461 # b.append "hello"
1462 # b.append "world"
1463 # assert b == "helloworld"
1464 fun append(s: Text) is abstract
1465
1466 # `self` is appended in such a way that `self` is repeated `r` times
1467 #
1468 # var b = new FlatBuffer
1469 # b.append "hello"
1470 # b.times 3
1471 # assert b == "hellohellohello"
1472 fun times(r: Int) is abstract
1473
1474 # Reverses itself in-place
1475 #
1476 # var b = new FlatBuffer
1477 # b.append("hello")
1478 # b.reverse
1479 # assert b == "olleh"
1480 fun reverse is abstract
1481
1482 # Changes each lower-case char in `self` by its upper-case variant
1483 #
1484 # var b = new FlatBuffer
1485 # b.append("Hello World!")
1486 # b.upper
1487 # assert b == "HELLO WORLD!"
1488 fun upper is abstract
1489
1490 # Changes each upper-case char in `self` by its lower-case variant
1491 #
1492 # var b = new FlatBuffer
1493 # b.append("Hello World!")
1494 # b.lower
1495 # assert b == "hello world!"
1496 fun lower is abstract
1497
1498 # Capitalizes each word in `self`
1499 #
1500 # Letters that follow a letter are lowercased
1501 # Letters that follow a non-letter are upcased.
1502 #
1503 # SEE: `Char::is_letter` for the definition of a letter.
1504 #
1505 # var b = new FlatBuffer.from("jAVAsCriPt")
1506 # b.capitalize
1507 # assert b == "Javascript"
1508 # b = new FlatBuffer.from("i am root")
1509 # b.capitalize
1510 # assert b == "I Am Root"
1511 # b = new FlatBuffer.from("ab_c -ab0c ab\nc")
1512 # b.capitalize
1513 # assert b == "Ab_C -Ab0C Ab\nC"
1514 fun capitalize do
1515 if length == 0 then return
1516 var c = self[0].to_upper
1517 self[0] = c
1518 var prev = c
1519 for i in [1 .. length[ do
1520 prev = c
1521 c = self[i]
1522 if prev.is_letter then
1523 self[i] = c.to_lower
1524 else
1525 self[i] = c.to_upper
1526 end
1527 end
1528 end
1529
1530 redef fun hash
1531 do
1532 if is_dirty then hash_cache = null
1533 return super
1534 end
1535
1536 # In Buffers, the internal sequence of character is mutable
1537 # Thus, `chars` can be used to modify the buffer.
1538 redef fun chars: Sequence[Char] is abstract
1539 end
1540
1541 # Mutable strings of characters.
1542 class FlatBuffer
1543 super FlatText
1544 super Buffer
1545
1546 redef var chars: Sequence[Char] = new FlatBufferCharView(self) is lazy
1547
1548 private var capacity: Int = 0
1549
1550 redef fun substrings do return new FlatSubstringsIter(self)
1551
1552 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
1553 #
1554 # This happens when an operation modifies the current `Buffer` and
1555 # the Copy-On-Write flag `written` is set at true.
1556 private fun reset do
1557 var nns = new NativeString(capacity)
1558 items.copy_to(nns, length, 0, 0)
1559 items = nns
1560 written = false
1561 end
1562
1563 redef fun [](index)
1564 do
1565 assert index >= 0
1566 assert index < length
1567 return items[index]
1568 end
1569
1570 redef fun []=(index, item)
1571 do
1572 is_dirty = true
1573 if index == length then
1574 add(item)
1575 return
1576 end
1577 if written then reset
1578 assert index >= 0 and index < length
1579 items[index] = item
1580 end
1581
1582 redef fun add(c)
1583 do
1584 is_dirty = true
1585 if capacity <= length then enlarge(length + 5)
1586 items[length] = c
1587 length += 1
1588 end
1589
1590 redef fun clear do
1591 is_dirty = true
1592 if written then reset
1593 length = 0
1594 end
1595
1596 redef fun empty do return new FlatBuffer
1597
1598 redef fun enlarge(cap)
1599 do
1600 var c = capacity
1601 if cap <= c then return
1602 while c <= cap do c = c * 2 + 2
1603 # The COW flag can be set at false here, since
1604 # it does a copy of the current `Buffer`
1605 written = false
1606 var a = new NativeString(c+1)
1607 if length > 0 then items.copy_to(a, length, 0, 0)
1608 items = a
1609 capacity = c
1610 end
1611
1612 redef fun to_s: String
1613 do
1614 written = true
1615 if length == 0 then items = new NativeString(1)
1616 return new FlatString.with_infos(items, length, 0, length - 1)
1617 end
1618
1619 redef fun to_cstring
1620 do
1621 if is_dirty then
1622 var new_native = new NativeString(length + 1)
1623 new_native[length] = '\0'
1624 if length > 0 then items.copy_to(new_native, length, 0, 0)
1625 real_items = new_native
1626 is_dirty = false
1627 end
1628 return real_items.as(not null)
1629 end
1630
1631 # Create a new empty string.
1632 init do end
1633
1634 # Low-level creation a new buffer with given data.
1635 #
1636 # `items` will be used as is, without copy, to store the characters of the buffer.
1637 # Aliasing issues is the responsibility of the caller.
1638 #
1639 # If `items` is shared, `written` should be set to true after the creation
1640 # so that a modification will do a copy-on-write.
1641 private init with_infos(items: NativeString, capacity, length: Int)
1642 do
1643 self.items = items
1644 self.length = length
1645 self.capacity = capacity
1646 end
1647
1648 # Create a new string copied from `s`.
1649 init from(s: Text)
1650 do
1651 capacity = s.length + 1
1652 length = s.length
1653 items = new NativeString(capacity)
1654 if s isa FlatString then
1655 s.items.copy_to(items, length, s.index_from, 0)
1656 else if s isa FlatBuffer then
1657 s.items.copy_to(items, length, 0, 0)
1658 else
1659 var curr_pos = 0
1660 for i in [0..s.length[ do
1661 var c = s.chars[i]
1662 items[curr_pos] = c
1663 curr_pos += 1
1664 end
1665 end
1666 end
1667
1668 # Create a new empty string with a given capacity.
1669 init with_capacity(cap: Int)
1670 do
1671 assert cap >= 0
1672 items = new NativeString(cap+1)
1673 capacity = cap
1674 length = 0
1675 end
1676
1677 redef fun append(s)
1678 do
1679 if s.is_empty then return
1680 is_dirty = true
1681 var sl = s.length
1682 if capacity < length + sl then enlarge(length + sl)
1683 if s isa FlatString then
1684 s.items.copy_to(items, sl, s.index_from, length)
1685 else if s isa FlatBuffer then
1686 s.items.copy_to(items, sl, 0, length)
1687 else
1688 var curr_pos = self.length
1689 for i in [0..s.length[ do
1690 var c = s.chars[i]
1691 items[curr_pos] = c
1692 curr_pos += 1
1693 end
1694 end
1695 length += sl
1696 end
1697
1698 # Copies the content of self in `dest`
1699 fun copy(start: Int, len: Int, dest: Buffer, new_start: Int)
1700 do
1701 var self_chars = self.chars
1702 var dest_chars = dest.chars
1703 for i in [0..len-1] do
1704 dest_chars[new_start+i] = self_chars[start+i]
1705 end
1706 end
1707
1708 redef fun substring(from, count)
1709 do
1710 assert count >= 0
1711 count += from
1712 if from < 0 then from = 0
1713 if count > length then count = length
1714 if from < count then
1715 var len = count - from
1716 var r_items = new NativeString(len)
1717 items.copy_to(r_items, len, from, 0)
1718 var r = new FlatBuffer.with_infos(r_items, len, len)
1719 return r
1720 else
1721 return new FlatBuffer
1722 end
1723 end
1724
1725 redef fun reverse
1726 do
1727 written = false
1728 var ns = new NativeString(capacity)
1729 var si = length - 1
1730 var ni = 0
1731 var it = items
1732 while si >= 0 do
1733 ns[ni] = it[si]
1734 ni += 1
1735 si -= 1
1736 end
1737 items = ns
1738 end
1739
1740 redef fun times(repeats)
1741 do
1742 var x = new FlatString.with_infos(items, length, 0, length - 1)
1743 for i in [1..repeats[ do
1744 append(x)
1745 end
1746 end
1747
1748 redef fun upper
1749 do
1750 if written then reset
1751 var it = items
1752 var id = length - 1
1753 while id >= 0 do
1754 it[id] = it[id].to_upper
1755 id -= 1
1756 end
1757 end
1758
1759 redef fun lower
1760 do
1761 if written then reset
1762 var it = items
1763 var id = length - 1
1764 while id >= 0 do
1765 it[id] = it[id].to_lower
1766 id -= 1
1767 end
1768 end
1769 end
1770
1771 private class FlatBufferReverseIterator
1772 super IndexedIterator[Char]
1773
1774 var target: FlatBuffer
1775
1776 var target_items: NativeString
1777
1778 var curr_pos: Int
1779
1780 init with_pos(tgt: FlatBuffer, pos: Int)
1781 do
1782 target = tgt
1783 if tgt.length > 0 then target_items = tgt.items
1784 curr_pos = pos
1785 end
1786
1787 redef fun index do return curr_pos
1788
1789 redef fun is_ok do return curr_pos >= 0
1790
1791 redef fun item do return target_items[curr_pos]
1792
1793 redef fun next do curr_pos -= 1
1794
1795 end
1796
1797 private class FlatBufferCharView
1798 super BufferCharView
1799
1800 redef type SELFTYPE: FlatBuffer
1801
1802 redef fun [](index) do return target.items[index]
1803
1804 redef fun []=(index, item)
1805 do
1806 assert index >= 0 and index <= length
1807 if index == length then
1808 add(item)
1809 return
1810 end
1811 target.items[index] = item
1812 end
1813
1814 redef fun push(c)
1815 do
1816 target.add(c)
1817 end
1818
1819 redef fun add(c)
1820 do
1821 target.add(c)
1822 end
1823
1824 fun enlarge(cap: Int)
1825 do
1826 target.enlarge(cap)
1827 end
1828
1829 redef fun append(s)
1830 do
1831 var s_length = s.length
1832 if target.capacity < s.length then enlarge(s_length + target.length)
1833 end
1834
1835 redef fun iterator_from(pos) do return new FlatBufferIterator.with_pos(target, pos)
1836
1837 redef fun reverse_iterator_from(pos) do return new FlatBufferReverseIterator.with_pos(target, pos)
1838
1839 end
1840
1841 private class FlatBufferIterator
1842 super IndexedIterator[Char]
1843
1844 var target: FlatBuffer
1845
1846 var target_items: NativeString
1847
1848 var curr_pos: Int
1849
1850 init with_pos(tgt: FlatBuffer, pos: Int)
1851 do
1852 target = tgt
1853 if tgt.length > 0 then target_items = tgt.items
1854 curr_pos = pos
1855 end
1856
1857 redef fun index do return curr_pos
1858
1859 redef fun is_ok do return curr_pos < target.length
1860
1861 redef fun item do return target_items[curr_pos]
1862
1863 redef fun next do curr_pos += 1
1864
1865 end
1866
1867 ###############################################################################
1868 # Refinement #
1869 ###############################################################################
1870
1871 redef class Object
1872 # User readable representation of `self`.
1873 fun to_s: String do return inspect
1874
1875 # The class name of the object in NativeString format.
1876 private fun native_class_name: NativeString is intern
1877
1878 # The class name of the object.
1879 #
1880 # assert 5.class_name == "Int"
1881 fun class_name: String do return native_class_name.to_s
1882
1883 # Developer readable representation of `self`.
1884 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
1885 fun inspect: String
1886 do
1887 return "<{inspect_head}>"
1888 end
1889
1890 # Return "CLASSNAME:#OBJECTID".
1891 # This function is mainly used with the redefinition of the inspect method
1892 protected fun inspect_head: String
1893 do
1894 return "{class_name}:#{object_id.to_hex}"
1895 end
1896 end
1897
1898 redef class Bool
1899 # assert true.to_s == "true"
1900 # assert false.to_s == "false"
1901 redef fun to_s
1902 do
1903 if self then
1904 return once "true"
1905 else
1906 return once "false"
1907 end
1908 end
1909 end
1910
1911 redef class Int
1912
1913 # Wrapper of strerror C function
1914 private fun strerror_ext: NativeString is extern `{
1915 return strerror(recv);
1916 `}
1917
1918 # Returns a string describing error number
1919 fun strerror: String do return strerror_ext.to_s
1920
1921 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
1922 # assume < to_c max const of char
1923 private fun fill_buffer(s: Buffer, base: Int, signed: Bool)
1924 do
1925 var n: Int
1926 # Sign
1927 if self < 0 then
1928 n = - self
1929 s.chars[0] = '-'
1930 else if self == 0 then
1931 s.chars[0] = '0'
1932 return
1933 else
1934 n = self
1935 end
1936 # Fill digits
1937 var pos = digit_count(base) - 1
1938 while pos >= 0 and n > 0 do
1939 s.chars[pos] = (n % base).to_c
1940 n = n / base # /
1941 pos -= 1
1942 end
1943 end
1944
1945 # C function to calculate the length of the `NativeString` to receive `self`
1946 private fun int_to_s_len: Int is extern "native_int_length_str"
1947
1948 # C function to convert an nit Int to a NativeString (char*)
1949 private fun native_int_to_s(nstr: NativeString, strlen: Int) is extern "native_int_to_s"
1950
1951 # return displayable int in base 10 and signed
1952 #
1953 # assert 1.to_s == "1"
1954 # assert (-123).to_s == "-123"
1955 redef fun to_s do
1956 # Fast case for common numbers
1957 if self == 0 then return "0"
1958 if self == 1 then return "1"
1959
1960 var nslen = int_to_s_len
1961 var ns = new NativeString(nslen + 1)
1962 ns[nslen] = '\0'
1963 native_int_to_s(ns, nslen + 1)
1964 return ns.to_s_with_length(nslen)
1965 end
1966
1967 # return displayable int in hexadecimal
1968 #
1969 # assert 1.to_hex == "1"
1970 # assert (-255).to_hex == "-ff"
1971 fun to_hex: String do return to_base(16,false)
1972
1973 # return displayable int in base base and signed
1974 fun to_base(base: Int, signed: Bool): String
1975 do
1976 var l = digit_count(base)
1977 var s = new FlatBuffer.from(" " * l)
1978 fill_buffer(s, base, signed)
1979 return s.to_s
1980 end
1981 end
1982
1983 redef class Float
1984 # Pretty representation of `self`, with decimals as needed from 1 to a maximum of 3
1985 #
1986 # assert 12.34.to_s == "12.34"
1987 # assert (-0120.030).to_s == "-120.03"
1988 #
1989 # see `to_precision` for a custom precision.
1990 redef fun to_s do
1991 var str = to_precision( 3 )
1992 if is_inf != 0 or is_nan then return str
1993 var len = str.length
1994 for i in [0..len-1] do
1995 var j = len-1-i
1996 var c = str.chars[j]
1997 if c == '0' then
1998 continue
1999 else if c == '.' then
2000 return str.substring( 0, j+2 )
2001 else
2002 return str.substring( 0, j+1 )
2003 end
2004 end
2005 return str
2006 end
2007
2008 # `String` representation of `self` with the given number of `decimals`
2009 #
2010 # assert 12.345.to_precision(0) == "12"
2011 # assert 12.345.to_precision(3) == "12.345"
2012 # assert (-12.345).to_precision(3) == "-12.345"
2013 # assert (-0.123).to_precision(3) == "-0.123"
2014 # assert 0.999.to_precision(2) == "1.00"
2015 # assert 0.999.to_precision(4) == "0.9990"
2016 fun to_precision(decimals: Int): String
2017 do
2018 if is_nan then return "nan"
2019
2020 var isinf = self.is_inf
2021 if isinf == 1 then
2022 return "inf"
2023 else if isinf == -1 then
2024 return "-inf"
2025 end
2026
2027 if decimals == 0 then return self.to_i.to_s
2028 var f = self
2029 for i in [0..decimals[ do f = f * 10.0
2030 if self > 0.0 then
2031 f = f + 0.5
2032 else
2033 f = f - 0.5
2034 end
2035 var i = f.to_i
2036 if i == 0 then return "0." + "0"*decimals
2037
2038 # Prepare both parts of the float, before and after the "."
2039 var s = i.abs.to_s
2040 var sl = s.length
2041 var p1
2042 var p2
2043 if sl > decimals then
2044 # Has something before the "."
2045 p1 = s.substring(0, sl-decimals)
2046 p2 = s.substring(sl-decimals, decimals)
2047 else
2048 p1 = "0"
2049 p2 = "0"*(decimals-sl) + s
2050 end
2051
2052 if i < 0 then p1 = "-" + p1
2053
2054 return p1 + "." + p2
2055 end
2056 end
2057
2058 redef class Char
2059 # assert 'x'.to_s == "x"
2060 redef fun to_s
2061 do
2062 var s = new FlatBuffer.with_capacity(1)
2063 s.chars[0] = self
2064 return s.to_s
2065 end
2066
2067 # Returns true if the char is a numerical digit
2068 #
2069 # assert '0'.is_numeric
2070 # assert '9'.is_numeric
2071 # assert not 'a'.is_numeric
2072 # assert not '?'.is_numeric
2073 fun is_numeric: Bool
2074 do
2075 return self >= '0' and self <= '9'
2076 end
2077
2078 # Returns true if the char is an alpha digit
2079 #
2080 # assert 'a'.is_alpha
2081 # assert 'Z'.is_alpha
2082 # assert not '0'.is_alpha
2083 # assert not '?'.is_alpha
2084 fun is_alpha: Bool
2085 do
2086 return (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z')
2087 end
2088
2089 # Returns true if the char is an alpha or a numeric digit
2090 #
2091 # assert 'a'.is_alphanumeric
2092 # assert 'Z'.is_alphanumeric
2093 # assert '0'.is_alphanumeric
2094 # assert '9'.is_alphanumeric
2095 # assert not '?'.is_alphanumeric
2096 fun is_alphanumeric: Bool
2097 do
2098 return self.is_numeric or self.is_alpha
2099 end
2100 end
2101
2102 redef class Collection[E]
2103 # Concatenate elements.
2104 redef fun to_s
2105 do
2106 var s = new FlatBuffer
2107 for e in self do if e != null then s.append(e.to_s)
2108 return s.to_s
2109 end
2110
2111 # Concatenate and separate each elements with `sep`.
2112 #
2113 # assert [1, 2, 3].join(":") == "1:2:3"
2114 # assert [1..3].join(":") == "1:2:3"
2115 fun join(sep: Text): String
2116 do
2117 if is_empty then return ""
2118
2119 var s = new FlatBuffer # Result
2120
2121 # Concat first item
2122 var i = iterator
2123 var e = i.item
2124 if e != null then s.append(e.to_s)
2125
2126 # Concat other items
2127 i.next
2128 while i.is_ok do
2129 s.append(sep)
2130 e = i.item
2131 if e != null then s.append(e.to_s)
2132 i.next
2133 end
2134 return s.to_s
2135 end
2136 end
2137
2138 redef class Array[E]
2139
2140 # Fast implementation
2141 redef fun to_s
2142 do
2143 var l = length
2144 if l == 0 then return ""
2145 if l == 1 then if self[0] == null then return "" else return self[0].to_s
2146 var its = _items
2147 var na = new NativeArray[String](l)
2148 var i = 0
2149 var sl = 0
2150 var mypos = 0
2151 while i < l do
2152 var itsi = its[i]
2153 if itsi == null then
2154 i += 1
2155 continue
2156 end
2157 var tmp = itsi.to_s
2158 sl += tmp.length
2159 na[mypos] = tmp
2160 i += 1
2161 mypos += 1
2162 end
2163 var ns = new NativeString(sl + 1)
2164 ns[sl] = '\0'
2165 i = 0
2166 var off = 0
2167 while i < mypos do
2168 var tmp = na[i]
2169 var tpl = tmp.length
2170 if tmp isa FlatString then
2171 tmp.items.copy_to(ns, tpl, tmp.index_from, off)
2172 off += tpl
2173 else
2174 for j in tmp.substrings do
2175 var s = j.as(FlatString)
2176 var slen = s.length
2177 s.items.copy_to(ns, slen, s.index_from, off)
2178 off += slen
2179 end
2180 end
2181 i += 1
2182 end
2183 return ns.to_s_with_length(sl)
2184 end
2185 end
2186
2187 redef class NativeArray[E]
2188 # Join all the elements using `to_s`
2189 #
2190 # REQUIRE: `self isa NativeArray[String]`
2191 # REQUIRE: all elements are initialized
2192 fun native_to_s: String
2193 do
2194 assert self isa NativeArray[String]
2195 var l = length
2196 var na = self
2197 var i = 0
2198 var sl = 0
2199 var mypos = 0
2200 while i < l do
2201 sl += na[i].length
2202 i += 1
2203 mypos += 1
2204 end
2205 var ns = new NativeString(sl + 1)
2206 ns[sl] = '\0'
2207 i = 0
2208 var off = 0
2209 while i < mypos do
2210 var tmp = na[i]
2211 var tpl = tmp.length
2212 if tmp isa FlatString then
2213 tmp.items.copy_to(ns, tpl, tmp.index_from, off)
2214 off += tpl
2215 else
2216 for j in tmp.substrings do
2217 var s = j.as(FlatString)
2218 var slen = s.length
2219 s.items.copy_to(ns, slen, s.index_from, off)
2220 off += slen
2221 end
2222 end
2223 i += 1
2224 end
2225 return ns.to_s_with_length(sl)
2226 end
2227 end
2228
2229 redef class Map[K,V]
2230 # Concatenate couple of 'key value'.
2231 # key and value are separated by `couple_sep`.
2232 # each couple is separated each couple with `sep`.
2233 #
2234 # var m = new ArrayMap[Int, String]
2235 # m[1] = "one"
2236 # m[10] = "ten"
2237 # assert m.join("; ", "=") == "1=one; 10=ten"
2238 fun join(sep: String, couple_sep: String): String
2239 do
2240 if is_empty then return ""
2241
2242 var s = new FlatBuffer # Result
2243
2244 # Concat first item
2245 var i = iterator
2246 var k = i.key
2247 var e = i.item
2248 s.append("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
2249
2250 # Concat other items
2251 i.next
2252 while i.is_ok do
2253 s.append(sep)
2254 k = i.key
2255 e = i.item
2256 s.append("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
2257 i.next
2258 end
2259 return s.to_s
2260 end
2261 end
2262
2263 ###############################################################################
2264 # Native classes #
2265 ###############################################################################
2266
2267 # Native strings are simple C char *
2268 extern class NativeString `{ char* `}
2269 # Creates a new NativeString with a capacity of `length`
2270 new(length: Int) is intern
2271
2272 # Get char at `index`.
2273 fun [](index: Int): Char is intern
2274
2275 # Set char `item` at index.
2276 fun []=(index: Int, item: Char) is intern
2277
2278 # Copy `self` to `dest`.
2279 fun copy_to(dest: NativeString, length: Int, from: Int, to: Int) is intern
2280
2281 # Position of the first nul character.
2282 fun cstring_length: Int
2283 do
2284 var l = 0
2285 while self[l] != '\0' do l += 1
2286 return l
2287 end
2288
2289 # Parse `self` as an Int.
2290 fun atoi: Int is intern
2291
2292 # Parse `self` as a Float.
2293 fun atof: Float is extern "atof"
2294
2295 redef fun to_s
2296 do
2297 return to_s_with_length(cstring_length)
2298 end
2299
2300 # Returns `self` as a String of `length`.
2301 fun to_s_with_length(length: Int): FlatString
2302 do
2303 assert length >= 0
2304 var str = new FlatString.with_infos(self, length, 0, length - 1)
2305 return str
2306 end
2307
2308 # Returns `self` as a new String.
2309 fun to_s_with_copy: FlatString
2310 do
2311 var length = cstring_length
2312 var new_self = new NativeString(length + 1)
2313 copy_to(new_self, length, 0, 0)
2314 var str = new FlatString.with_infos(new_self, length, 0, length - 1)
2315 new_self[length] = '\0'
2316 str.real_items = new_self
2317 return str
2318 end
2319 end
2320
2321 redef class Sys
2322 private var args_cache: nullable Sequence[String]
2323
2324 # The arguments of the program as given by the OS
2325 fun program_args: Sequence[String]
2326 do
2327 if _args_cache == null then init_args
2328 return _args_cache.as(not null)
2329 end
2330
2331 # The name of the program as given by the OS
2332 fun program_name: String
2333 do
2334 return native_argv(0).to_s
2335 end
2336
2337 # Initialize `program_args` with the contents of `native_argc` and `native_argv`.
2338 private fun init_args
2339 do
2340 var argc = native_argc
2341 var args = new Array[String].with_capacity(0)
2342 var i = 1
2343 while i < argc do
2344 args[i-1] = native_argv(i).to_s
2345 i += 1
2346 end
2347 _args_cache = args
2348 end
2349
2350 # First argument of the main C function.
2351 private fun native_argc: Int is intern
2352
2353 # Second argument of the main C function.
2354 private fun native_argv(i: Int): NativeString is intern
2355 end
2356
2357 # Comparator that efficienlty use `to_s` to compare things
2358 #
2359 # The comparaison call `to_s` on object and use the result to order things.
2360 #
2361 # var a = [1, 2, 3, 10, 20]
2362 # (new CachedAlphaComparator).sort(a)
2363 # assert a == [1, 10, 2, 20, 3]
2364 #
2365 # Internally the result of `to_s` is cached in a HashMap to counter
2366 # uneficient implementation of `to_s`.
2367 #
2368 # Note: it caching is not usefull, see `alpha_comparator`
2369 class CachedAlphaComparator
2370 super Comparator
2371 redef type COMPARED: Object
2372
2373 private var cache = new HashMap[Object, String]
2374
2375 private fun do_to_s(a: Object): String do
2376 if cache.has_key(a) then return cache[a]
2377 var res = a.to_s
2378 cache[a] = res
2379 return res
2380 end
2381
2382 redef fun compare(a, b) do
2383 return do_to_s(a) <=> do_to_s(b)
2384 end
2385 end
2386
2387 # see `alpha_comparator`
2388 private class AlphaComparator
2389 super Comparator
2390 redef fun compare(a, b) do return a.to_s <=> b.to_s
2391 end
2392
2393 # Stateless comparator that naively use `to_s` to compare things.
2394 #
2395 # Note: the result of `to_s` is not cached, thus can be invoked a lot
2396 # on a single instace. See `CachedAlphaComparator` as an alternative.
2397 #
2398 # var a = [1, 2, 3, 10, 20]
2399 # alpha_comparator.sort(a)
2400 # assert a == [1, 10, 2, 20, 3]
2401 fun alpha_comparator: Comparator do return once new AlphaComparator
2402
2403 # The arguments of the program as given by the OS
2404 fun args: Sequence[String]
2405 do
2406 return sys.program_args
2407 end