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