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