src: Added complete FlatString generation from compiler
[nit.git] / lib / core / text / abstract_text.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
9 # another product.
10
11 # Abstract class for manipulation of sequences of characters
12 module abstract_text
13
14 import native
15 import math
16 import collection
17 intrude import collection::array
18
19 in "C" `{
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 `}
24
25 # High-level abstraction for all text representations
26 abstract class Text
27 super Comparable
28
29 redef type OTHER: Text
30
31 # Type of self (used for factorization of several methods, ex : substring_from, empty...)
32 type SELFTYPE: Text
33
34 # Gets a view on the chars of the Text object
35 #
36 # assert "hello".chars.to_a == ['h', 'e', 'l', 'l', 'o']
37 fun chars: SequenceRead[Char] is abstract
38
39 # Gets a view on the bytes of the Text object
40 #
41 # assert "hello".bytes.to_a == [104u8, 101u8, 108u8, 108u8, 111u8]
42 fun bytes: SequenceRead[Byte] is abstract
43
44 # Number of characters contained in self.
45 #
46 # assert "12345".length == 5
47 # assert "".length == 0
48 # assert "あいうえお".length == 5
49 fun length: Int is abstract
50
51 # Number of bytes in `self`
52 #
53 # assert "12345".bytelen == 5
54 # assert "あいうえお".bytelen == 15
55 fun bytelen: Int is abstract
56
57 # Create a substring.
58 #
59 # assert "abcd".substring(1, 2) == "bc"
60 # assert "abcd".substring(-1, 2) == "a"
61 # assert "abcd".substring(1, 0) == ""
62 # assert "abcd".substring(2, 5) == "cd"
63 # assert "あいうえお".substring(1,3) == "いうえ"
64 #
65 # A `from` index < 0 will be replaced by 0.
66 # Unless a `count` value is > 0 at the same time.
67 # In this case, `from += count` and `count -= from`.
68 fun substring(from: Int, count: Int): SELFTYPE is abstract
69
70 # Iterates on the substrings of self if any
71 fun substrings: Iterator[FlatText] is abstract
72
73 # Is the current Text empty (== "")
74 #
75 # assert "".is_empty
76 # assert not "foo".is_empty
77 fun is_empty: Bool do return self.length == 0
78
79 # Returns an empty Text of the right type
80 #
81 # This method is used internally to get the right
82 # implementation of an empty string.
83 protected fun empty: SELFTYPE is abstract
84
85 # Gets the first char of the Text
86 #
87 # DEPRECATED : Use self.chars.first instead
88 fun first: Char do return self.chars[0]
89
90 # Access a character at `index` in the string.
91 #
92 # assert "abcd"[2] == 'c'
93 #
94 # DEPRECATED : Use self.chars.[] instead
95 fun [](index: Int): Char do return self.chars[index]
96
97 # Gets the index of the first occurence of 'c'
98 #
99 # Returns -1 if not found
100 #
101 # DEPRECATED : Use self.chars.index_of instead
102 fun index_of(c: Char): Int
103 do
104 return index_of_from(c, 0)
105 end
106
107 # Gets the last char of self
108 #
109 # DEPRECATED : Use self.chars.last instead
110 fun last: Char do return self.chars[length-1]
111
112 # Gets the index of the first occurence of ´c´ starting from ´pos´
113 #
114 # Returns -1 if not found
115 #
116 # DEPRECATED : Use self.chars.index_of_from instead
117 fun index_of_from(c: Char, pos: Int): Int
118 do
119 var iter = self.chars.iterator_from(pos)
120 while iter.is_ok do
121 if iter.item == c then return iter.index
122 iter.next
123 end
124 return -1
125 end
126
127 # Gets the last index of char ´c´
128 #
129 # Returns -1 if not found
130 #
131 # DEPRECATED : Use self.chars.last_index_of instead
132 fun last_index_of(c: Char): Int
133 do
134 return last_index_of_from(c, length - 1)
135 end
136
137 # Return a null terminated char *
138 fun to_cstring: NativeString is abstract
139
140 # The index of the last occurrence of an element starting from pos (in reverse order).
141 #
142 # var s = "/etc/bin/test/test.nit"
143 # assert s.last_index_of_from('/', s.length-1) == 13
144 # assert s.last_index_of_from('/', 12) == 8
145 #
146 # Returns -1 if not found
147 #
148 # DEPRECATED : Use self.chars.last_index_of_from instead
149 fun last_index_of_from(item: Char, pos: Int): Int
150 do
151 var iter = self.chars.reverse_iterator_from(pos)
152 while iter.is_ok do
153 if iter.item == item then return iter.index
154 iter.next
155 end
156 return -1
157 end
158
159 # Gets an iterator on the chars of self
160 #
161 # DEPRECATED : Use self.chars.iterator instead
162 fun iterator: Iterator[Char]
163 do
164 return self.chars.iterator
165 end
166
167
168 # Gets an Array containing the chars of self
169 #
170 # DEPRECATED : Use self.chars.to_a instead
171 fun to_a: Array[Char] do return chars.to_a
172
173 # Create a substring from `self` beginning at the `from` position
174 #
175 # assert "abcd".substring_from(1) == "bcd"
176 # assert "abcd".substring_from(-1) == "abcd"
177 # assert "abcd".substring_from(2) == "cd"
178 #
179 # As with substring, a `from` index < 0 will be replaced by 0
180 fun substring_from(from: Int): SELFTYPE
181 do
182 if from >= self.length then return empty
183 if from < 0 then from = 0
184 return substring(from, length - from)
185 end
186
187 # Does self have a substring `str` starting from position `pos`?
188 #
189 # assert "abcd".has_substring("bc",1) == true
190 # assert "abcd".has_substring("bc",2) == false
191 #
192 # Returns true iff all characters of `str` are presents
193 # at the expected index in `self.`
194 # The first character of `str` being at `pos`, the second
195 # character being at `pos+1` and so on...
196 #
197 # This means that all characters of `str` need to be inside `self`.
198 #
199 # assert "abcd".has_substring("xab", -1) == false
200 # assert "abcd".has_substring("cdx", 2) == false
201 #
202 # And that the empty string is always a valid substring.
203 #
204 # assert "abcd".has_substring("", 2) == true
205 # assert "abcd".has_substring("", 200) == true
206 fun has_substring(str: String, pos: Int): Bool
207 do
208 if str.is_empty then return true
209 if pos < 0 or pos + str.length > length then return false
210 var myiter = self.chars.iterator_from(pos)
211 var itsiter = str.chars.iterator
212 while myiter.is_ok and itsiter.is_ok do
213 if myiter.item != itsiter.item then return false
214 myiter.next
215 itsiter.next
216 end
217 if itsiter.is_ok then return false
218 return true
219 end
220
221 # Is this string prefixed by `prefix`?
222 #
223 # assert "abcd".has_prefix("ab") == true
224 # assert "abcbc".has_prefix("bc") == false
225 # assert "ab".has_prefix("abcd") == false
226 fun has_prefix(prefix: String): Bool do return has_substring(prefix,0)
227
228 # Is this string suffixed by `suffix`?
229 #
230 # assert "abcd".has_suffix("abc") == false
231 # assert "abcd".has_suffix("bcd") == true
232 fun has_suffix(suffix: String): Bool do return has_substring(suffix, length - suffix.length)
233
234 # Returns a copy of `self` minus all occurences of `c`
235 #
236 # assert "__init__".remove_all('_') == "init"
237 fun remove_all(c: Char): String do
238 var b = new Buffer
239 for i in chars do if i != c then b.add i
240 return b.to_s
241 end
242
243 # Returns `self` as the corresponding integer
244 #
245 # assert "123".to_i == 123
246 # assert "-1".to_i == -1
247 # assert "0x64".to_i == 100
248 # assert "0b1100_0011".to_i== 195
249 # assert "--12".to_i == 12
250 #
251 # REQUIRE: `self`.`is_int`
252 fun to_i: Int is abstract
253
254 # If `self` contains a float, return the corresponding float
255 #
256 # assert "123".to_f == 123.0
257 # assert "-1".to_f == -1.0
258 # assert "-1.2e-3".to_f == -0.0012
259 fun to_f: Float
260 do
261 # Shortcut
262 return to_s.to_cstring.atof
263 end
264
265 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
266 #
267 # assert "ff".to_hex == 255
268 fun to_hex: Int do return a_to(16)
269
270 # If `self` contains only digits <= '7', return the corresponding integer.
271 #
272 # assert "714".to_oct == 460
273 fun to_oct: Int do return a_to(8)
274
275 # If `self` contains only '0' et '1', return the corresponding integer.
276 #
277 # assert "101101".to_bin == 45
278 fun to_bin: Int do return a_to(2)
279
280 # If `self` contains only digits '0' .. '9', return the corresponding integer.
281 #
282 # assert "108".to_dec == 108
283 fun to_dec: Int do return a_to(10)
284
285 # If `self` contains only digits and letters, return the corresponding integer in a given base
286 #
287 # assert "120".a_to(3) == 15
288 fun a_to(base: Int) : Int
289 do
290 var i = 0
291 var neg = false
292
293 for j in [0..length[ do
294 var c = chars[j]
295 var v = c.to_i
296 if v > base then
297 if neg then
298 return -i
299 else
300 return i
301 end
302 else if v < 0 then
303 neg = true
304 else
305 i = i * base + v
306 end
307 end
308 if neg then
309 return -i
310 else
311 return i
312 end
313 end
314
315 # Returns `true` if the string contains only Numeric values (and one "," or one "." character)
316 #
317 # assert "123".is_numeric == true
318 # assert "1.2".is_numeric == true
319 # assert "1,2".is_numeric == true
320 # assert "1..2".is_numeric == false
321 fun is_numeric: Bool
322 do
323 var has_point_or_comma = false
324 for i in [0..length[ do
325 var c = chars[i]
326 if not c.is_numeric then
327 if (c == '.' or c == ',') and not has_point_or_comma then
328 has_point_or_comma = true
329 else
330 return false
331 end
332 end
333 end
334 return true
335 end
336
337 # Returns `true` if the string contains only Hex chars
338 #
339 # assert "048bf".is_hex == true
340 # assert "ABCDEF".is_hex == true
341 # assert "0G".is_hex == false
342 fun is_hex: Bool
343 do
344 for i in [0..length[ do
345 var c = chars[i]
346 if not (c >= 'a' and c <= 'f') and
347 not (c >= 'A' and c <= 'F') and
348 not (c >= '0' and c <= '9') then return false
349 end
350 return true
351 end
352
353 # Returns `true` if the string contains only Binary digits
354 #
355 # assert "1101100".is_bin == true
356 # assert "1101020".is_bin == false
357 fun is_bin: Bool do
358 for i in chars do if i != '0' and i != '1' then return false
359 return true
360 end
361
362 # Returns `true` if the string contains only Octal digits
363 #
364 # assert "213453".is_oct == true
365 # assert "781".is_oct == false
366 fun is_oct: Bool do
367 for i in chars do if i < '0' or i > '7' then return false
368 return true
369 end
370
371 # Returns `true` if the string contains only Decimal digits
372 #
373 # assert "10839".is_dec == true
374 # assert "164F".is_dec == false
375 fun is_dec: Bool do
376 for i in chars do if i < '0' or i > '9' then return false
377 return true
378 end
379
380 # Are all letters in `self` upper-case ?
381 #
382 # assert "HELLO WORLD".is_upper == true
383 # assert "%$&%!".is_upper == true
384 # assert "hello world".is_upper == false
385 # assert "Hello World".is_upper == false
386 fun is_upper: Bool
387 do
388 for i in [0..length[ do
389 var char = chars[i]
390 if char.is_lower then return false
391 end
392 return true
393 end
394
395 # Are all letters in `self` lower-case ?
396 #
397 # assert "hello world".is_lower == true
398 # assert "%$&%!".is_lower == true
399 # assert "Hello World".is_lower == false
400 fun is_lower: Bool
401 do
402 for i in [0..length[ do
403 var char = chars[i]
404 if char.is_upper then return false
405 end
406 return true
407 end
408
409 # Removes the whitespaces at the beginning of self
410 #
411 # assert " \n\thello \n\t".l_trim == "hello \n\t"
412 #
413 # `Char::is_whitespace` determines what is a whitespace.
414 fun l_trim: SELFTYPE
415 do
416 var iter = self.chars.iterator
417 while iter.is_ok do
418 if not iter.item.is_whitespace then break
419 iter.next
420 end
421 if iter.index == length then return self.empty
422 return self.substring_from(iter.index)
423 end
424
425 # Removes the whitespaces at the end of self
426 #
427 # assert " \n\thello \n\t".r_trim == " \n\thello"
428 #
429 # `Char::is_whitespace` determines what is a whitespace.
430 fun r_trim: SELFTYPE
431 do
432 var iter = self.chars.reverse_iterator
433 while iter.is_ok do
434 if not iter.item.is_whitespace then break
435 iter.next
436 end
437 if iter.index < 0 then return self.empty
438 return self.substring(0, iter.index + 1)
439 end
440
441 # Trims trailing and preceding white spaces
442 #
443 # assert " Hello World ! ".trim == "Hello World !"
444 # assert "\na\nb\tc\t".trim == "a\nb\tc"
445 #
446 # `Char::is_whitespace` determines what is a whitespace.
447 fun trim: SELFTYPE do return (self.l_trim).r_trim
448
449 # Is the string non-empty but only made of whitespaces?
450 #
451 # assert " \n\t ".is_whitespace == true
452 # assert " hello ".is_whitespace == false
453 # assert "".is_whitespace == false
454 #
455 # `Char::is_whitespace` determines what is a whitespace.
456 fun is_whitespace: Bool
457 do
458 if is_empty then return false
459 for c in self.chars do
460 if not c.is_whitespace then return false
461 end
462 return true
463 end
464
465 # Returns `self` removed from its last line terminator (if any).
466 #
467 # assert "Hello\n".chomp == "Hello"
468 # assert "Hello".chomp == "Hello"
469 #
470 # assert "\n".chomp == ""
471 # assert "".chomp == ""
472 #
473 # Line terminators are `"\n"`, `"\r\n"` and `"\r"`.
474 # A single line terminator, the last one, is removed.
475 #
476 # assert "\r\n".chomp == ""
477 # assert "\r\n\n".chomp == "\r\n"
478 # assert "\r\n\r\n".chomp == "\r\n"
479 # assert "\r\n\r".chomp == "\r\n"
480 #
481 # Note: unlike with most IO methods like `Reader::read_line`,
482 # a single `\r` is considered here to be a line terminator and will be removed.
483 fun chomp: SELFTYPE
484 do
485 var len = length
486 if len == 0 then return self
487 var l = self.chars.last
488 if l == '\r' then
489 return substring(0, len-1)
490 else if l != '\n' then
491 return self
492 else if len > 1 and self.chars[len-2] == '\r' then
493 return substring(0, len-2)
494 else
495 return substring(0, len-1)
496 end
497 end
498
499 # Justify a self in a space of `length`
500 #
501 # `left` is the space ratio on the left side.
502 # * 0.0 for left-justified (no space at the left)
503 # * 1.0 for right-justified (all spaces at the left)
504 # * 0.5 for centered (half the spaces at the left)
505 #
506 # Examples
507 #
508 # assert "hello".justify(10, 0.0) == "hello "
509 # assert "hello".justify(10, 1.0) == " hello"
510 # assert "hello".justify(10, 0.5) == " hello "
511 #
512 # If `length` is not enough, `self` is returned as is.
513 #
514 # assert "hello".justify(2, 0.0) == "hello"
515 #
516 # REQUIRE: `left >= 0.0 and left <= 1.0`
517 # ENSURE: `self.length <= length implies result.length == length`
518 # ENSURE: `self.length >= length implies result == self`
519 fun justify(length: Int, left: Float): String
520 do
521 var diff = length - self.length
522 if diff <= 0 then return to_s
523 assert left >= 0.0 and left <= 1.0
524 var before = (diff.to_f * left).to_i
525 return " " * before + self + " " * (diff-before)
526 end
527
528 # Mangle a string to be a unique string only made of alphanumeric characters and underscores.
529 #
530 # This method is injective (two different inputs never produce the same
531 # output) and the returned string always respect the following rules:
532 #
533 # * Contains only US-ASCII letters, digits and underscores.
534 # * Never starts with a digit.
535 # * Never ends with an underscore.
536 # * Never contains two contiguous underscores.
537 #
538 # assert "42_is/The answer!".to_cmangle == "_52d2_is_47dThe_32danswer_33d"
539 # assert "__".to_cmangle == "_95d_95d"
540 # assert "__d".to_cmangle == "_95d_d"
541 # assert "_d_".to_cmangle == "_d_95d"
542 # assert "_42".to_cmangle == "_95d42"
543 # assert "foo".to_cmangle == "foo"
544 # assert "".to_cmangle == ""
545 fun to_cmangle: String
546 do
547 if is_empty then return ""
548 var res = new Buffer
549 var underscore = false
550 var start = 0
551 var c = chars[0]
552
553 if c >= '0' and c <= '9' then
554 res.add('_')
555 res.append(c.ascii.to_s)
556 res.add('d')
557 start = 1
558 end
559 for i in [start..length[ do
560 c = chars[i]
561 if (c >= 'a' and c <= 'z') or (c >='A' and c <= 'Z') then
562 res.add(c)
563 underscore = false
564 continue
565 end
566 if underscore then
567 res.append('_'.ascii.to_s)
568 res.add('d')
569 end
570 if c >= '0' and c <= '9' then
571 res.add(c)
572 underscore = false
573 else if c == '_' then
574 res.add(c)
575 underscore = true
576 else
577 res.add('_')
578 res.append(c.ascii.to_s)
579 res.add('d')
580 underscore = false
581 end
582 end
583 if underscore then
584 res.append('_'.ascii.to_s)
585 res.add('d')
586 end
587 return res.to_s
588 end
589
590 # Escape " \ ' and non printable characters using the rules of literal C strings and characters
591 #
592 # assert "abAB12<>&".escape_to_c == "abAB12<>&"
593 # assert "\n\"'\\".escape_to_c == "\\n\\\"\\'\\\\"
594 #
595 # Most non-printable characters (bellow ASCII 32) are escaped to an octal form `\nnn`.
596 # Three digits are always used to avoid following digits to be interpreted as an element
597 # of the octal sequence.
598 #
599 # assert "{0.ascii}{1.ascii}{8.ascii}{31.ascii}{32.ascii}".escape_to_c == "\\000\\001\\010\\037 "
600 #
601 # The exceptions are the common `\t` and `\n`.
602 fun escape_to_c: String
603 do
604 var b = new Buffer
605 for i in [0..length[ do
606 var c = chars[i]
607 if c == '\n' then
608 b.append("\\n")
609 else if c == '\t' then
610 b.append("\\t")
611 else if c == '\0' then
612 b.append("\\000")
613 else if c == '"' then
614 b.append("\\\"")
615 else if c == '\'' then
616 b.append("\\\'")
617 else if c == '\\' then
618 b.append("\\\\")
619 else if c.ascii < 32 then
620 b.add('\\')
621 var oct = c.ascii.to_base(8, false)
622 # Force 3 octal digits since it is the
623 # maximum allowed in the C specification
624 if oct.length == 1 then
625 b.add('0')
626 b.add('0')
627 else if oct.length == 2 then
628 b.add('0')
629 end
630 b.append(oct)
631 else
632 b.add(c)
633 end
634 end
635 return b.to_s
636 end
637
638 # Escape additionnal characters
639 # The result might no be legal in C but be used in other languages
640 #
641 # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
642 fun escape_more_to_c(chars: String): String
643 do
644 var b = new Buffer
645 for c in escape_to_c.chars do
646 if chars.chars.has(c) then
647 b.add('\\')
648 end
649 b.add(c)
650 end
651 return b.to_s
652 end
653
654 # Escape to C plus braces
655 #
656 # assert "\n\"'\\\{\}".escape_to_nit == "\\n\\\"\\'\\\\\\\{\\\}"
657 fun escape_to_nit: String do return escape_more_to_c("\{\}")
658
659 # Escape to POSIX Shell (sh).
660 #
661 # Abort if the text contains a null byte.
662 #
663 # assert "\n\"'\\\{\}0".escape_to_sh == "'\n\"'\\''\\\{\}0'"
664 fun escape_to_sh: String do
665 var b = new Buffer
666 b.chars.add '\''
667 for i in [0..length[ do
668 var c = chars[i]
669 if c == '\'' then
670 b.append("'\\''")
671 else
672 assert without_null_byte: c != '\0'
673 b.add(c)
674 end
675 end
676 b.chars.add '\''
677 return b.to_s
678 end
679
680 # Escape to include in a Makefile
681 #
682 # Unfortunately, some characters are not escapable in Makefile.
683 # These characters are `;`, `|`, `\`, and the non-printable ones.
684 # They will be rendered as `"?{hex}"`.
685 fun escape_to_mk: String do
686 var b = new Buffer
687 for i in [0..length[ do
688 var c = chars[i]
689 if c == '$' then
690 b.append("$$")
691 else if c == ':' or c == ' ' or c == '#' then
692 b.add('\\')
693 b.add(c)
694 else if c.ascii < 32 or c == ';' or c == '|' or c == '\\' or c == '=' then
695 b.append("?{c.ascii.to_base(16, false)}")
696 else
697 b.add(c)
698 end
699 end
700 return b.to_s
701 end
702
703 # Return a string where Nit escape sequences are transformed.
704 #
705 # var s = "\\n"
706 # assert s.length == 2
707 # var u = s.unescape_nit
708 # assert u.length == 1
709 # assert u.chars[0].ascii == 10 # (the ASCII value of the "new line" character)
710 fun unescape_nit: String
711 do
712 var res = new Buffer.with_cap(self.length)
713 var was_slash = false
714 for i in [0..length[ do
715 var c = chars[i]
716 if not was_slash then
717 if c == '\\' then
718 was_slash = true
719 else
720 res.add(c)
721 end
722 continue
723 end
724 was_slash = false
725 if c == 'n' then
726 res.add('\n')
727 else if c == 'r' then
728 res.add('\r')
729 else if c == 't' then
730 res.add('\t')
731 else if c == '0' then
732 res.add('\0')
733 else
734 res.add(c)
735 end
736 end
737 return res.to_s
738 end
739
740 # Encode `self` to percent (or URL) encoding
741 #
742 # assert "aBc09-._~".to_percent_encoding == "aBc09-._~"
743 # assert "%()< >".to_percent_encoding == "%25%28%29%3c%20%3e"
744 # assert ".com/post?e=asdf&f=123".to_percent_encoding == ".com%2fpost%3fe%3dasdf%26f%3d123"
745 fun to_percent_encoding: String
746 do
747 var buf = new Buffer
748
749 for i in [0..length[ do
750 var c = chars[i]
751 if (c >= '0' and c <= '9') or
752 (c >= 'a' and c <= 'z') or
753 (c >= 'A' and c <= 'Z') or
754 c == '-' or c == '.' or
755 c == '_' or c == '~'
756 then
757 buf.add c
758 else buf.append "%{c.ascii.to_hex}"
759 end
760
761 return buf.to_s
762 end
763
764 # Decode `self` from percent (or URL) encoding to a clear string
765 #
766 # Replace invalid use of '%' with '?'.
767 #
768 # assert "aBc09-._~".from_percent_encoding == "aBc09-._~"
769 # assert "%25%28%29%3c%20%3e".from_percent_encoding == "%()< >"
770 # assert ".com%2fpost%3fe%3dasdf%26f%3d123".from_percent_encoding == ".com/post?e=asdf&f=123"
771 # assert "%25%28%29%3C%20%3E".from_percent_encoding == "%()< >"
772 # assert "incomplete %".from_percent_encoding == "incomplete ?"
773 # assert "invalid % usage".from_percent_encoding == "invalid ? usage"
774 fun from_percent_encoding: String
775 do
776 var buf = new Buffer
777
778 var i = 0
779 while i < length do
780 var c = chars[i]
781 if c == '%' then
782 if i + 2 >= length then
783 # What follows % has been cut off
784 buf.add '?'
785 else
786 i += 1
787 var hex_s = substring(i, 2)
788 if hex_s.is_hex then
789 var hex_i = hex_s.to_hex
790 buf.add hex_i.ascii
791 i += 1
792 else
793 # What follows a % is not Hex
794 buf.add '?'
795 i -= 1
796 end
797 end
798 else buf.add c
799
800 i += 1
801 end
802
803 return buf.to_s
804 end
805
806 # Escape the characters `<`, `>`, `&`, `"`, `'` and `/` as HTML/XML entity references.
807 #
808 # assert "a&b-<>\"x\"/'".html_escape == "a&amp;b-&lt;&gt;&#34;x&#34;&#47;&#39;"
809 #
810 # 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>
811 fun html_escape: String
812 do
813 var buf = new Buffer
814
815 for i in [0..length[ do
816 var c = chars[i]
817 if c == '&' then
818 buf.append "&amp;"
819 else if c == '<' then
820 buf.append "&lt;"
821 else if c == '>' then
822 buf.append "&gt;"
823 else if c == '"' then
824 buf.append "&#34;"
825 else if c == '\'' then
826 buf.append "&#39;"
827 else if c == '/' then
828 buf.append "&#47;"
829 else buf.add c
830 end
831
832 return buf.to_s
833 end
834
835 # Equality of text
836 # Two pieces of text are equals if thez have the same characters in the same order.
837 #
838 # assert "hello" == "hello"
839 # assert "hello" != "HELLO"
840 # assert "hello" == "hel"+"lo"
841 #
842 # Things that are not Text are not equal.
843 #
844 # assert "9" != '9'
845 # assert "9" != ['9']
846 # assert "9" != 9
847 #
848 # assert "9".chars.first == '9' # equality of Char
849 # assert "9".chars == ['9'] # equality of Sequence
850 # assert "9".to_i == 9 # equality of Int
851 redef fun ==(o)
852 do
853 if o == null then return false
854 if not o isa Text then return false
855 if self.is_same_instance(o) then return true
856 if self.length != o.length then return false
857 return self.chars == o.chars
858 end
859
860 # Lexicographical comparaison
861 #
862 # assert "abc" < "xy"
863 # assert "ABC" < "abc"
864 redef fun <(other)
865 do
866 var self_chars = self.chars.iterator
867 var other_chars = other.chars.iterator
868
869 while self_chars.is_ok and other_chars.is_ok do
870 if self_chars.item < other_chars.item then return true
871 if self_chars.item > other_chars.item then return false
872 self_chars.next
873 other_chars.next
874 end
875
876 if self_chars.is_ok then
877 return false
878 else
879 return true
880 end
881 end
882
883 # Escape string used in labels for graphviz
884 #
885 # assert ">><<".escape_to_dot == "\\>\\>\\<\\<"
886 fun escape_to_dot: String
887 do
888 return escape_more_to_c("|\{\}<>")
889 end
890
891 private var hash_cache: nullable Int = null
892
893 redef fun hash
894 do
895 if hash_cache == null then
896 # djb2 hash algorithm
897 var h = 5381
898
899 for i in [0..length[ do
900 var char = chars[i]
901 h = (h << 5) + h + char.ascii
902 end
903
904 hash_cache = h
905 end
906 return hash_cache.as(not null)
907 end
908
909 # Gives the formatted string back as a Nit string with `args` in place
910 #
911 # assert "This %1 is a %2.".format("String", "formatted String") == "This String is a formatted String."
912 # assert "\\%1 This string".format("String") == "\\%1 This string"
913 fun format(args: Object...): String do
914 var s = new Array[Text]
915 var curr_st = 0
916 var i = 0
917 while i < length do
918 # Skip escaped characters
919 if self[i] == '\\' then
920 i += 1
921 # In case of format
922 else if self[i] == '%' then
923 var fmt_st = i
924 i += 1
925 var ciph_st = i
926 while i < length and self[i].is_numeric do
927 i += 1
928 end
929 i -= 1
930 var fmt_end = i
931 var ciph_len = fmt_end - ciph_st + 1
932
933 var arg_index = substring(ciph_st, ciph_len).to_i - 1
934 if arg_index >= args.length then continue
935
936 s.push substring(curr_st, fmt_st - curr_st)
937 s.push args[arg_index].to_s
938 curr_st = i + 1
939 end
940 i += 1
941 end
942 s.push substring(curr_st, length - curr_st)
943 return s.plain_to_s
944 end
945
946 # Copies `n` bytes from `self` at `src_offset` into `dest` starting at `dest_offset`
947 #
948 # Basically a high-level synonym of NativeString::copy_to
949 #
950 # REQUIRE: `n` must be large enough to contain `len` bytes
951 #
952 # var ns = new NativeString(8)
953 # "Text is String".copy_to_native(ns, 8, 2, 0)
954 # assert ns.to_s_with_length(8) == "xt is St"
955 #
956 fun copy_to_native(dest: NativeString, n, src_offset, dest_offset: Int) do
957 var mypos = src_offset
958 var itspos = dest_offset
959 while n > 0 do
960 dest[itspos] = self.bytes[mypos]
961 itspos += 1
962 mypos += 1
963 n -= 1
964 end
965 end
966
967 end
968
969 # All kinds of array-based text representations.
970 abstract class FlatText
971 super Text
972
973 # Underlying C-String (`char*`)
974 #
975 # Warning : Might be void in some subclasses, be sure to check
976 # if set before using it.
977 private var items: NativeString is noinit
978
979 # Real items, used as cache for to_cstring is called
980 private var real_items: nullable NativeString = null
981
982 # Returns a char* starting at position `first_byte`
983 #
984 # WARNING: If you choose to use this service, be careful of the following.
985 #
986 # Strings and NativeString are *ideally* always allocated through a Garbage Collector.
987 # Since the GC tracks the use of the pointer for the beginning of the char*, it may be
988 # deallocated at any moment, rendering the pointer returned by this function invalid.
989 # Any access to freed memory may very likely cause undefined behaviour or a crash.
990 # (Failure to do so will most certainly result in long and painful debugging hours)
991 #
992 # The only safe use of this pointer is if it is ephemeral (e.g. read in a C function
993 # then immediately return).
994 #
995 # As always, do not modify the content of the String in C code, if this is what you want
996 # copy locally the char* as Nit Strings are immutable.
997 private fun fast_cstring: NativeString is abstract
998
999 redef var length = 0
1000
1001 redef var bytelen = 0
1002
1003 redef fun output
1004 do
1005 var i = 0
1006 while i < length do
1007 items[i].output
1008 i += 1
1009 end
1010 end
1011
1012 redef fun copy_to_native(dest, n, src_offset, dest_offset) do
1013 items.copy_to(dest, n, src_offset, dest_offset)
1014 end
1015 end
1016
1017 # Abstract class for the SequenceRead compatible
1018 # views on the chars of any Text
1019 private abstract class StringCharView
1020 super SequenceRead[Char]
1021
1022 type SELFTYPE: Text
1023
1024 var target: SELFTYPE
1025
1026 redef fun is_empty do return target.is_empty
1027
1028 redef fun length do return target.length
1029
1030 redef fun iterator: IndexedIterator[Char] do return self.iterator_from(0)
1031
1032 redef fun reverse_iterator do return self.reverse_iterator_from(self.length - 1)
1033 end
1034
1035 # Abstract class for the SequenceRead compatible
1036 # views on the bytes of any Text
1037 private abstract class StringByteView
1038 super SequenceRead[Byte]
1039
1040 type SELFTYPE: Text
1041
1042 var target: SELFTYPE
1043
1044 redef fun is_empty do return target.is_empty
1045
1046 redef fun length do return target.length
1047
1048 redef fun iterator do return self.iterator_from(0)
1049
1050 redef fun reverse_iterator do return self.reverse_iterator_from(target.bytelen - 1)
1051 end
1052
1053 # Immutable sequence of characters.
1054 #
1055 # String objects may be created using literals.
1056 #
1057 # assert "Hello World!" isa String
1058 abstract class String
1059 super Text
1060
1061 redef type SELFTYPE: String is fixed
1062
1063 redef fun to_s do return self
1064
1065 # Concatenates `o` to `self`
1066 #
1067 # assert "hello" + "world" == "helloworld"
1068 # assert "" + "hello" + "" == "hello"
1069 fun +(o: Text): SELFTYPE is abstract
1070
1071 # Concatenates self `i` times
1072 #
1073 # assert "abc" * 4 == "abcabcabcabc"
1074 # assert "abc" * 1 == "abc"
1075 # assert "abc" * 0 == ""
1076 fun *(i: Int): SELFTYPE is abstract
1077
1078 # Insert `s` at `pos`.
1079 #
1080 # assert "helloworld".insert_at(" ", 5) == "hello world"
1081 fun insert_at(s: String, pos: Int): SELFTYPE is abstract
1082
1083 redef fun substrings is abstract
1084
1085 # Returns a reversed version of self
1086 #
1087 # assert "hello".reversed == "olleh"
1088 # assert "bob".reversed == "bob"
1089 # assert "".reversed == ""
1090 fun reversed: SELFTYPE is abstract
1091
1092 # A upper case version of `self`
1093 #
1094 # assert "Hello World!".to_upper == "HELLO WORLD!"
1095 fun to_upper: SELFTYPE is abstract
1096
1097 # A lower case version of `self`
1098 #
1099 # assert "Hello World!".to_lower == "hello world!"
1100 fun to_lower : SELFTYPE is abstract
1101
1102 # Takes a camel case `self` and converts it to snake case
1103 #
1104 # assert "randomMethodId".to_snake_case == "random_method_id"
1105 #
1106 # The rules are the following:
1107 #
1108 # An uppercase is always converted to a lowercase
1109 #
1110 # assert "HELLO_WORLD".to_snake_case == "hello_world"
1111 #
1112 # An uppercase that follows a lowercase is prefixed with an underscore
1113 #
1114 # assert "HelloTheWORLD".to_snake_case == "hello_the_world"
1115 #
1116 # An uppercase that follows an uppercase and is followed by a lowercase, is prefixed with an underscore
1117 #
1118 # assert "HelloTHEWorld".to_snake_case == "hello_the_world"
1119 #
1120 # All other characters are kept as is; `self` does not need to be a proper CamelCased string.
1121 #
1122 # assert "=-_H3ll0Th3W0rld_-=".to_snake_case == "=-_h3ll0th3w0rld_-="
1123 fun to_snake_case: SELFTYPE
1124 do
1125 if self.is_lower then return self
1126
1127 var new_str = new Buffer.with_cap(self.length)
1128 var prev_is_lower = false
1129 var prev_is_upper = false
1130
1131 for i in [0..length[ do
1132 var char = chars[i]
1133 if char.is_lower then
1134 new_str.add(char)
1135 prev_is_lower = true
1136 prev_is_upper = false
1137 else if char.is_upper then
1138 if prev_is_lower then
1139 new_str.add('_')
1140 else if prev_is_upper and i+1 < length and chars[i+1].is_lower then
1141 new_str.add('_')
1142 end
1143 new_str.add(char.to_lower)
1144 prev_is_lower = false
1145 prev_is_upper = true
1146 else
1147 new_str.add(char)
1148 prev_is_lower = false
1149 prev_is_upper = false
1150 end
1151 end
1152
1153 return new_str.to_s
1154 end
1155
1156 # Takes a snake case `self` and converts it to camel case
1157 #
1158 # assert "random_method_id".to_camel_case == "randomMethodId"
1159 #
1160 # If the identifier is prefixed by an underscore, the underscore is ignored
1161 #
1162 # assert "_private_field".to_camel_case == "_privateField"
1163 #
1164 # If `self` is upper, it is returned unchanged
1165 #
1166 # assert "RANDOM_ID".to_camel_case == "RANDOM_ID"
1167 #
1168 # If there are several consecutive underscores, they are considered as a single one
1169 #
1170 # assert "random__method_id".to_camel_case == "randomMethodId"
1171 fun to_camel_case: SELFTYPE
1172 do
1173 if self.is_upper then return self
1174
1175 var new_str = new Buffer
1176 var is_first_char = true
1177 var follows_us = false
1178
1179 for i in [0..length[ do
1180 var char = chars[i]
1181 if is_first_char then
1182 new_str.add(char)
1183 is_first_char = false
1184 else if char == '_' then
1185 follows_us = true
1186 else if follows_us then
1187 new_str.add(char.to_upper)
1188 follows_us = false
1189 else
1190 new_str.add(char)
1191 end
1192 end
1193
1194 return new_str.to_s
1195 end
1196
1197 # Returns a capitalized `self`
1198 #
1199 # Letters that follow a letter are lowercased
1200 # Letters that follow a non-letter are upcased.
1201 #
1202 # SEE : `Char::is_letter` for the definition of letter.
1203 #
1204 # assert "jAVASCRIPT".capitalized == "Javascript"
1205 # assert "i am root".capitalized == "I Am Root"
1206 # assert "ab_c -ab0c ab\nc".capitalized == "Ab_C -Ab0C Ab\nC"
1207 fun capitalized: SELFTYPE do
1208 if length == 0 then return self
1209
1210 var buf = new Buffer.with_cap(length)
1211
1212 var curr = chars[0].to_upper
1213 var prev = curr
1214 buf[0] = curr
1215
1216 for i in [1 .. length[ do
1217 prev = curr
1218 curr = self[i]
1219 if prev.is_letter then
1220 buf[i] = curr.to_lower
1221 else
1222 buf[i] = curr.to_upper
1223 end
1224 end
1225
1226 return buf.to_s
1227 end
1228 end
1229
1230 # A mutable sequence of characters.
1231 abstract class Buffer
1232 super Text
1233
1234 # Returns an arbitrary subclass of `Buffer` with default parameters
1235 new is abstract
1236
1237 # Returns an instance of a subclass of `Buffer` with `i` base capacity
1238 new with_cap(i: Int) is abstract
1239
1240 redef type SELFTYPE: Buffer is fixed
1241
1242 # Specific implementations MUST set this to `true` in order to invalidate caches
1243 protected var is_dirty = true
1244
1245 # Copy-On-Write flag
1246 #
1247 # If the `Buffer` was to_s'd, the next in-place altering
1248 # operation will cause the current `Buffer` to be re-allocated.
1249 #
1250 # The flag will then be set at `false`.
1251 protected var written = false
1252
1253 # Modifies the char contained at pos `index`
1254 #
1255 # DEPRECATED : Use self.chars.[]= instead
1256 fun []=(index: Int, item: Char) is abstract
1257
1258 # Adds a char `c` at the end of self
1259 #
1260 # DEPRECATED : Use self.chars.add instead
1261 fun add(c: Char) is abstract
1262
1263 # Clears the buffer
1264 #
1265 # var b = new Buffer
1266 # b.append "hello"
1267 # assert not b.is_empty
1268 # b.clear
1269 # assert b.is_empty
1270 fun clear is abstract
1271
1272 # Enlarges the subsequent array containing the chars of self
1273 fun enlarge(cap: Int) is abstract
1274
1275 # Adds the content of text `s` at the end of self
1276 #
1277 # var b = new Buffer
1278 # b.append "hello"
1279 # b.append "world"
1280 # assert b == "helloworld"
1281 fun append(s: Text) is abstract
1282
1283 # `self` is appended in such a way that `self` is repeated `r` times
1284 #
1285 # var b = new Buffer
1286 # b.append "hello"
1287 # b.times 3
1288 # assert b == "hellohellohello"
1289 fun times(r: Int) is abstract
1290
1291 # Reverses itself in-place
1292 #
1293 # var b = new Buffer
1294 # b.append("hello")
1295 # b.reverse
1296 # assert b == "olleh"
1297 fun reverse is abstract
1298
1299 # Changes each lower-case char in `self` by its upper-case variant
1300 #
1301 # var b = new Buffer
1302 # b.append("Hello World!")
1303 # b.upper
1304 # assert b == "HELLO WORLD!"
1305 fun upper is abstract
1306
1307 # Changes each upper-case char in `self` by its lower-case variant
1308 #
1309 # var b = new Buffer
1310 # b.append("Hello World!")
1311 # b.lower
1312 # assert b == "hello world!"
1313 fun lower is abstract
1314
1315 # Capitalizes each word in `self`
1316 #
1317 # Letters that follow a letter are lowercased
1318 # Letters that follow a non-letter are upcased.
1319 #
1320 # SEE: `Char::is_letter` for the definition of a letter.
1321 #
1322 # var b = new FlatBuffer.from("jAVAsCriPt")
1323 # b.capitalize
1324 # assert b == "Javascript"
1325 # b = new FlatBuffer.from("i am root")
1326 # b.capitalize
1327 # assert b == "I Am Root"
1328 # b = new FlatBuffer.from("ab_c -ab0c ab\nc")
1329 # b.capitalize
1330 # assert b == "Ab_C -Ab0C Ab\nC"
1331 fun capitalize do
1332 if length == 0 then return
1333 var c = self[0].to_upper
1334 self[0] = c
1335 var prev = c
1336 for i in [1 .. length[ do
1337 prev = c
1338 c = self[i]
1339 if prev.is_letter then
1340 self[i] = c.to_lower
1341 else
1342 self[i] = c.to_upper
1343 end
1344 end
1345 end
1346
1347 redef fun hash
1348 do
1349 if is_dirty then hash_cache = null
1350 return super
1351 end
1352
1353 # In Buffers, the internal sequence of character is mutable
1354 # Thus, `chars` can be used to modify the buffer.
1355 redef fun chars: Sequence[Char] is abstract
1356 end
1357
1358 # View for chars on Buffer objects, extends Sequence
1359 # for mutation operations
1360 private abstract class BufferCharView
1361 super StringCharView
1362 super Sequence[Char]
1363
1364 redef type SELFTYPE: Buffer
1365
1366 end
1367
1368 # View for bytes on Buffer objects, extends Sequence
1369 # for mutation operations
1370 private abstract class BufferByteView
1371 super StringByteView
1372
1373 redef type SELFTYPE: Buffer
1374 end
1375
1376 redef class Object
1377 # User readable representation of `self`.
1378 fun to_s: String do return inspect
1379
1380 # The class name of the object in NativeString format.
1381 private fun native_class_name: NativeString is intern
1382
1383 # The class name of the object.
1384 #
1385 # assert 5.class_name == "Int"
1386 fun class_name: String do return native_class_name.to_s
1387
1388 # Developer readable representation of `self`.
1389 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
1390 fun inspect: String
1391 do
1392 return "<{inspect_head}>"
1393 end
1394
1395 # Return "CLASSNAME:#OBJECTID".
1396 # This function is mainly used with the redefinition of the inspect method
1397 protected fun inspect_head: String
1398 do
1399 return "{class_name}:#{object_id.to_hex}"
1400 end
1401 end
1402
1403 redef class Bool
1404 # assert true.to_s == "true"
1405 # assert false.to_s == "false"
1406 redef fun to_s
1407 do
1408 if self then
1409 return once "true"
1410 else
1411 return once "false"
1412 end
1413 end
1414 end
1415
1416 redef class Byte
1417 # C function to calculate the length of the `NativeString` to receive `self`
1418 private fun byte_to_s_len: Int `{
1419 return snprintf(NULL, 0, "0x%02x", self);
1420 `}
1421
1422 # C function to convert an nit Int to a NativeString (char*)
1423 private fun native_byte_to_s(nstr: NativeString, strlen: Int) `{
1424 snprintf(nstr, strlen, "0x%02x", self);
1425 `}
1426
1427 # Displayable byte in its hexadecimal form (0x..)
1428 #
1429 # assert 1.to_b.to_s == "0x01"
1430 # assert (-123).to_b.to_s == "0x85"
1431 redef fun to_s do
1432 var nslen = byte_to_s_len
1433 var ns = new NativeString(nslen + 1)
1434 ns[nslen] = 0u8
1435 native_byte_to_s(ns, nslen + 1)
1436 return ns.to_s_with_length(nslen)
1437 end
1438 end
1439
1440 redef class Int
1441
1442 # Wrapper of strerror C function
1443 private fun strerror_ext: NativeString `{ return strerror(self); `}
1444
1445 # Returns a string describing error number
1446 fun strerror: String do return strerror_ext.to_s
1447
1448 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
1449 # assume < to_c max const of char
1450 private fun fill_buffer(s: Buffer, base: Int, signed: Bool)
1451 do
1452 var n: Int
1453 # Sign
1454 if self < 0 then
1455 n = - self
1456 s.chars[0] = '-'
1457 else if self == 0 then
1458 s.chars[0] = '0'
1459 return
1460 else
1461 n = self
1462 end
1463 # Fill digits
1464 var pos = digit_count(base) - 1
1465 while pos >= 0 and n > 0 do
1466 s.chars[pos] = (n % base).to_c
1467 n = n / base # /
1468 pos -= 1
1469 end
1470 end
1471
1472 # C function to calculate the length of the `NativeString` to receive `self`
1473 private fun int_to_s_len: Int `{
1474 return snprintf(NULL, 0, "%ld", self);
1475 `}
1476
1477 # C function to convert an nit Int to a NativeString (char*)
1478 private fun native_int_to_s(nstr: NativeString, strlen: Int) `{
1479 snprintf(nstr, strlen, "%ld", self);
1480 `}
1481
1482 # return displayable int in base base and signed
1483 fun to_base(base: Int, signed: Bool): String is abstract
1484
1485 # return displayable int in hexadecimal
1486 #
1487 # assert 1.to_hex == "1"
1488 # assert (-255).to_hex == "-ff"
1489 fun to_hex: String do return to_base(16,false)
1490 end
1491
1492 redef class Float
1493 # Pretty representation of `self`, with decimals as needed from 1 to a maximum of 3
1494 #
1495 # assert 12.34.to_s == "12.34"
1496 # assert (-0120.030).to_s == "-120.03"
1497 #
1498 # see `to_precision` for a custom precision.
1499 redef fun to_s do
1500 var str = to_precision( 3 )
1501 if is_inf != 0 or is_nan then return str
1502 var len = str.length
1503 for i in [0..len-1] do
1504 var j = len-1-i
1505 var c = str.chars[j]
1506 if c == '0' then
1507 continue
1508 else if c == '.' then
1509 return str.substring( 0, j+2 )
1510 else
1511 return str.substring( 0, j+1 )
1512 end
1513 end
1514 return str
1515 end
1516
1517 # `String` representation of `self` with the given number of `decimals`
1518 #
1519 # assert 12.345.to_precision(0) == "12"
1520 # assert 12.345.to_precision(3) == "12.345"
1521 # assert (-12.345).to_precision(3) == "-12.345"
1522 # assert (-0.123).to_precision(3) == "-0.123"
1523 # assert 0.999.to_precision(2) == "1.00"
1524 # assert 0.999.to_precision(4) == "0.9990"
1525 fun to_precision(decimals: Int): String
1526 do
1527 if is_nan then return "nan"
1528
1529 var isinf = self.is_inf
1530 if isinf == 1 then
1531 return "inf"
1532 else if isinf == -1 then
1533 return "-inf"
1534 end
1535
1536 if decimals == 0 then return self.to_i.to_s
1537 var f = self
1538 for i in [0..decimals[ do f = f * 10.0
1539 if self > 0.0 then
1540 f = f + 0.5
1541 else
1542 f = f - 0.5
1543 end
1544 var i = f.to_i
1545 if i == 0 then return "0." + "0"*decimals
1546
1547 # Prepare both parts of the float, before and after the "."
1548 var s = i.abs.to_s
1549 var sl = s.length
1550 var p1
1551 var p2
1552 if sl > decimals then
1553 # Has something before the "."
1554 p1 = s.substring(0, sl-decimals)
1555 p2 = s.substring(sl-decimals, decimals)
1556 else
1557 p1 = "0"
1558 p2 = "0"*(decimals-sl) + s
1559 end
1560
1561 if i < 0 then p1 = "-" + p1
1562
1563 return p1 + "." + p2
1564 end
1565 end
1566
1567 redef class Char
1568
1569 # Length of `self` in a UTF-8 String
1570 private fun u8char_len: Int do
1571 var c = self.ascii
1572 if c < 0x80 then return 1
1573 if c <= 0x7FF then return 2
1574 if c <= 0xFFFF then return 3
1575 if c <= 0x10FFFF then return 4
1576 # Bad character format
1577 return 1
1578 end
1579
1580 # assert 'x'.to_s == "x"
1581 redef fun to_s do
1582 var ln = u8char_len
1583 var ns = new NativeString(ln + 1)
1584 u8char_tos(ns, ln)
1585 return ns.to_s_with_length(ln)
1586 end
1587
1588 private fun u8char_tos(r: NativeString, len: Int) `{
1589 r[len] = '\0';
1590 switch(len){
1591 case 1:
1592 r[0] = self;
1593 break;
1594 case 2:
1595 r[0] = 0xC0 | ((self & 0x7C0) >> 6);
1596 r[1] = 0x80 | (self & 0x3F);
1597 break;
1598 case 3:
1599 r[0] = 0xE0 | ((self & 0xF000) >> 12);
1600 r[1] = 0x80 | ((self & 0xFC0) >> 6);
1601 r[2] = 0x80 | (self & 0x3F);
1602 break;
1603 case 4:
1604 r[0] = 0xF0 | ((self & 0x1C0000) >> 18);
1605 r[1] = 0x80 | ((self & 0x3F000) >> 12);
1606 r[2] = 0x80 | ((self & 0xFC0) >> 6);
1607 r[3] = 0x80 | (self & 0x3F);
1608 break;
1609 }
1610 `}
1611
1612 # Returns true if the char is a numerical digit
1613 #
1614 # assert '0'.is_numeric
1615 # assert '9'.is_numeric
1616 # assert not 'a'.is_numeric
1617 # assert not '?'.is_numeric
1618 #
1619 # FIXME: Works on ASCII-range only
1620 fun is_numeric: Bool
1621 do
1622 return self >= '0' and self <= '9'
1623 end
1624
1625 # Returns true if the char is an alpha digit
1626 #
1627 # assert 'a'.is_alpha
1628 # assert 'Z'.is_alpha
1629 # assert not '0'.is_alpha
1630 # assert not '?'.is_alpha
1631 #
1632 # FIXME: Works on ASCII-range only
1633 fun is_alpha: Bool
1634 do
1635 return (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z')
1636 end
1637
1638 # Returns true if the char is an alpha or a numeric digit
1639 #
1640 # assert 'a'.is_alphanumeric
1641 # assert 'Z'.is_alphanumeric
1642 # assert '0'.is_alphanumeric
1643 # assert '9'.is_alphanumeric
1644 # assert not '?'.is_alphanumeric
1645 #
1646 # FIXME: Works on ASCII-range only
1647 fun is_alphanumeric: Bool
1648 do
1649 return self.is_numeric or self.is_alpha
1650 end
1651 end
1652
1653 redef class Collection[E]
1654 # String representation of the content of the collection.
1655 #
1656 # The standard representation is the list of elements separated with commas.
1657 #
1658 # ~~~
1659 # assert [1,2,3].to_s == "[1,2,3]"
1660 # assert [1..3].to_s == "[1,2,3]"
1661 # assert (new Array[Int]).to_s == "[]" # empty collection
1662 # ~~~
1663 #
1664 # Subclasses may return a more specific string representation.
1665 redef fun to_s
1666 do
1667 return "[" + join(",") + "]"
1668 end
1669
1670 # Concatenate elements without separators
1671 #
1672 # ~~~
1673 # assert [1,2,3].plain_to_s == "123"
1674 # assert [11..13].plain_to_s == "111213"
1675 # assert (new Array[Int]).plain_to_s == "" # empty collection
1676 # ~~~
1677 fun plain_to_s: String
1678 do
1679 var s = new Buffer
1680 for e in self do if e != null then s.append(e.to_s)
1681 return s.to_s
1682 end
1683
1684 # Concatenate and separate each elements with `separator`.
1685 #
1686 # Only concatenate if `separator == null`.
1687 #
1688 # assert [1, 2, 3].join(":") == "1:2:3"
1689 # assert [1..3].join(":") == "1:2:3"
1690 # assert [1..3].join == "123"
1691 fun join(separator: nullable Text): String
1692 do
1693 if is_empty then return ""
1694
1695 var s = new Buffer # Result
1696
1697 # Concat first item
1698 var i = iterator
1699 var e = i.item
1700 if e != null then s.append(e.to_s)
1701
1702 # Concat other items
1703 i.next
1704 while i.is_ok do
1705 if separator != null then s.append(separator)
1706 e = i.item
1707 if e != null then s.append(e.to_s)
1708 i.next
1709 end
1710 return s.to_s
1711 end
1712 end
1713
1714 redef class Map[K,V]
1715 # Concatenate couples of key value.
1716 # Key and value are separated by `couple_sep`.
1717 # Couples are separated by `sep`.
1718 #
1719 # var m = new HashMap[Int, String]
1720 # m[1] = "one"
1721 # m[10] = "ten"
1722 # assert m.join("; ", "=") == "1=one; 10=ten"
1723 fun join(sep, couple_sep: String): String is abstract
1724 end
1725
1726 redef class Sys
1727 private var args_cache: nullable Sequence[String] = null
1728
1729 # The arguments of the program as given by the OS
1730 fun program_args: Sequence[String]
1731 do
1732 if _args_cache == null then init_args
1733 return _args_cache.as(not null)
1734 end
1735
1736 # The name of the program as given by the OS
1737 fun program_name: String
1738 do
1739 return native_argv(0).to_s
1740 end
1741
1742 # Initialize `program_args` with the contents of `native_argc` and `native_argv`.
1743 private fun init_args
1744 do
1745 var argc = native_argc
1746 var args = new Array[String].with_capacity(0)
1747 var i = 1
1748 while i < argc do
1749 args[i-1] = native_argv(i).to_s
1750 i += 1
1751 end
1752 _args_cache = args
1753 end
1754
1755 # First argument of the main C function.
1756 private fun native_argc: Int is intern
1757
1758 # Second argument of the main C function.
1759 private fun native_argv(i: Int): NativeString is intern
1760 end
1761
1762 # Comparator that efficienlty use `to_s` to compare things
1763 #
1764 # The comparaison call `to_s` on object and use the result to order things.
1765 #
1766 # var a = [1, 2, 3, 10, 20]
1767 # (new CachedAlphaComparator).sort(a)
1768 # assert a == [1, 10, 2, 20, 3]
1769 #
1770 # Internally the result of `to_s` is cached in a HashMap to counter
1771 # uneficient implementation of `to_s`.
1772 #
1773 # Note: it caching is not usefull, see `alpha_comparator`
1774 class CachedAlphaComparator
1775 super Comparator
1776 redef type COMPARED: Object
1777
1778 private var cache = new HashMap[Object, String]
1779
1780 private fun do_to_s(a: Object): String do
1781 if cache.has_key(a) then return cache[a]
1782 var res = a.to_s
1783 cache[a] = res
1784 return res
1785 end
1786
1787 redef fun compare(a, b) do
1788 return do_to_s(a) <=> do_to_s(b)
1789 end
1790 end
1791
1792 # see `alpha_comparator`
1793 private class AlphaComparator
1794 super Comparator
1795 redef fun compare(a, b) do return a.to_s <=> b.to_s
1796 end
1797
1798 # Stateless comparator that naively use `to_s` to compare things.
1799 #
1800 # Note: the result of `to_s` is not cached, thus can be invoked a lot
1801 # on a single instace. See `CachedAlphaComparator` as an alternative.
1802 #
1803 # var a = [1, 2, 3, 10, 20]
1804 # alpha_comparator.sort(a)
1805 # assert a == [1, 10, 2, 20, 3]
1806 fun alpha_comparator: Comparator do return once new AlphaComparator
1807
1808 # The arguments of the program as given by the OS
1809 fun args: Sequence[String]
1810 do
1811 return sys.program_args
1812 end
1813
1814 redef class NativeString
1815 # Returns `self` as a new String.
1816 fun to_s_with_copy: String is abstract
1817
1818 # Returns `self` as a String of `length`.
1819 fun to_s_with_length(length: Int): String is abstract
1820
1821 # Returns `self` as a String with `bytelen` and `length` set
1822 #
1823 # SEE: `abstract_text::Text` for more infos on the difference
1824 # between `Text::bytelen` and `Text::length`
1825 fun to_s_full(bytelen, unilen: Int): String is abstract
1826 end
1827
1828 redef class NativeArray[E]
1829 # Join all the elements using `to_s`
1830 #
1831 # REQUIRE: `self isa NativeArray[String]`
1832 # REQUIRE: all elements are initialized
1833 fun native_to_s: String is abstract
1834 end