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