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