lib/core: Made `Text` cloneable
[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 super Cloneable
29
30 redef type OTHER: Text
31
32 # Type of self (used for factorization of several methods, ex : substring_from, empty...)
33 type SELFTYPE: Text
34
35 # Gets a view on the chars of the Text object
36 #
37 # assert "hello".chars.to_a == ['h', 'e', 'l', 'l', 'o']
38 fun chars: SequenceRead[Char] is abstract
39
40 # Gets a view on the bytes of the Text object
41 #
42 # assert "hello".bytes.to_a == [104u8, 101u8, 108u8, 108u8, 111u8]
43 fun bytes: SequenceRead[Byte] is abstract
44
45 # Number of characters contained in self.
46 #
47 # assert "12345".length == 5
48 # assert "".length == 0
49 # assert "あいうえお".length == 5
50 fun length: Int is abstract
51
52 # Number of bytes in `self`
53 #
54 # assert "12345".byte_length == 5
55 # assert "あいうえお".byte_length == 15
56 fun byte_length: Int is abstract
57
58 # Create a substring.
59 #
60 # assert "abcd".substring(1, 2) == "bc"
61 # assert "abcd".substring(-1, 2) == "a"
62 # assert "abcd".substring(1, 0) == ""
63 # assert "abcd".substring(2, 5) == "cd"
64 # assert "あいうえお".substring(1,3) == "いうえ"
65 #
66 # A `from` index < 0 will be replaced by 0.
67 # Unless a `count` value is > 0 at the same time.
68 # In this case, `from += count` and `count -= from`.
69 fun substring(from: Int, count: Int): SELFTYPE is abstract
70
71 # Iterates on the substrings of self if any
72 private fun substrings: Iterator[FlatText] is abstract
73
74 # Is the current Text empty (== "")
75 #
76 # assert "".is_empty
77 # assert not "foo".is_empty
78 fun is_empty: Bool do return self.length == 0
79
80 # Returns an empty Text of the right type
81 #
82 # This method is used internally to get the right
83 # implementation of an empty string.
84 protected fun empty: SELFTYPE is abstract
85
86 # Gets the first char of the Text
87 #
88 # DEPRECATED : Use self.chars.first instead
89 fun first: Char do return self.chars[0]
90
91 # Access a character at `index` in the string.
92 #
93 # assert "abcd"[2] == 'c'
94 #
95 # DEPRECATED : Use self.chars.[] instead
96 fun [](index: Int): Char do return self.chars[index]
97
98 # Gets the index of the first occurence of 'c'
99 #
100 # Returns -1 if not found
101 #
102 # DEPRECATED : Use self.chars.index_of instead
103 fun index_of(c: Char): Int
104 do
105 return index_of_from(c, 0)
106 end
107
108 # Gets the last char of self
109 #
110 # DEPRECATED : Use self.chars.last instead
111 fun last: Char do return self.chars[length-1]
112
113 # Gets the index of the first occurence of ´c´ starting from ´pos´
114 #
115 # Returns -1 if not found
116 #
117 # DEPRECATED : Use self.chars.index_of_from instead
118 fun index_of_from(c: Char, pos: Int): Int
119 do
120 var iter = self.chars.iterator_from(pos)
121 while iter.is_ok do
122 if iter.item == c then return iter.index
123 iter.next
124 end
125 return -1
126 end
127
128 # Gets the last index of char ´c´
129 #
130 # Returns -1 if not found
131 #
132 # DEPRECATED : Use self.chars.last_index_of instead
133 fun last_index_of(c: Char): Int
134 do
135 return last_index_of_from(c, length - 1)
136 end
137
138 # Return a null terminated char *
139 fun to_cstring: NativeString is abstract
140
141 # The index of the last occurrence of an element starting from pos (in reverse order).
142 #
143 # var s = "/etc/bin/test/test.nit"
144 # assert s.last_index_of_from('/', s.length-1) == 13
145 # assert s.last_index_of_from('/', 12) == 8
146 #
147 # Returns -1 if not found
148 #
149 # DEPRECATED : Use self.chars.last_index_of_from instead
150 fun last_index_of_from(item: Char, pos: Int): Int do return chars.last_index_of_from(item, pos)
151
152 # Gets an iterator on the chars of self
153 #
154 # DEPRECATED : Use self.chars.iterator instead
155 fun iterator: Iterator[Char]
156 do
157 return self.chars.iterator
158 end
159
160
161 # Gets an Array containing the chars of self
162 #
163 # DEPRECATED : Use self.chars.to_a instead
164 fun to_a: Array[Char] do return chars.to_a
165
166 # Create a substring from `self` beginning at the `from` position
167 #
168 # assert "abcd".substring_from(1) == "bcd"
169 # assert "abcd".substring_from(-1) == "abcd"
170 # assert "abcd".substring_from(2) == "cd"
171 #
172 # As with substring, a `from` index < 0 will be replaced by 0
173 fun substring_from(from: Int): SELFTYPE
174 do
175 if from >= self.length then return empty
176 if from < 0 then from = 0
177 return substring(from, length - from)
178 end
179
180 # Does self have a substring `str` starting from position `pos`?
181 #
182 # assert "abcd".has_substring("bc",1) == true
183 # assert "abcd".has_substring("bc",2) == false
184 #
185 # Returns true iff all characters of `str` are presents
186 # at the expected index in `self.`
187 # The first character of `str` being at `pos`, the second
188 # character being at `pos+1` and so on...
189 #
190 # This means that all characters of `str` need to be inside `self`.
191 #
192 # assert "abcd".has_substring("xab", -1) == false
193 # assert "abcd".has_substring("cdx", 2) == false
194 #
195 # And that the empty string is always a valid substring.
196 #
197 # assert "abcd".has_substring("", 2) == true
198 # assert "abcd".has_substring("", 200) == true
199 fun has_substring(str: String, pos: Int): Bool
200 do
201 if str.is_empty then return true
202 if pos < 0 or pos + str.length > length then return false
203 var myiter = self.chars.iterator_from(pos)
204 var itsiter = str.chars.iterator
205 while myiter.is_ok and itsiter.is_ok do
206 if myiter.item != itsiter.item then return false
207 myiter.next
208 itsiter.next
209 end
210 if itsiter.is_ok then return false
211 return true
212 end
213
214 # Is this string prefixed by `prefix`?
215 #
216 # assert "abcd".has_prefix("ab") == true
217 # assert "abcbc".has_prefix("bc") == false
218 # assert "ab".has_prefix("abcd") == false
219 fun has_prefix(prefix: String): Bool do return has_substring(prefix,0)
220
221 # Is this string suffixed by `suffix`?
222 #
223 # assert "abcd".has_suffix("abc") == false
224 # assert "abcd".has_suffix("bcd") == true
225 fun has_suffix(suffix: String): Bool do return has_substring(suffix, length - suffix.length)
226
227 # Returns `self` as the corresponding integer
228 #
229 # assert "123".to_i == 123
230 # assert "-1".to_i == -1
231 # assert "0x64".to_i == 100
232 # assert "0b1100_0011".to_i== 195
233 # assert "--12".to_i == 12
234 #
235 # REQUIRE: `self`.`is_int`
236 fun to_i: Int is abstract
237
238 # If `self` contains a float, return the corresponding float
239 #
240 # assert "123".to_f == 123.0
241 # assert "-1".to_f == -1.0
242 # assert "-1.2e-3".to_f == -0.0012
243 fun to_f: Float
244 do
245 # Shortcut
246 return to_s.to_cstring.atof
247 end
248
249 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
250 #
251 # assert "ff".to_hex == 255
252 fun to_hex(pos, ln: nullable Int): Int do
253 var res = 0
254 if pos == null then pos = 0
255 if ln == null then ln = length - pos
256 var max = pos + ln
257 for i in [pos .. max[ do
258 res <<= 4
259 res += self[i].from_hex
260 end
261 return res
262 end
263
264 # If `self` contains only digits <= '7', return the corresponding integer.
265 #
266 # assert "714".to_oct == 460
267 fun to_oct: Int do return a_to(8)
268
269 # If `self` contains only '0' et '1', return the corresponding integer.
270 #
271 # assert "101101".to_bin == 45
272 fun to_bin: Int do return a_to(2)
273
274 # If `self` contains only digits '0' .. '9', return the corresponding integer.
275 #
276 # assert "108".to_dec == 108
277 fun to_dec: Int do return a_to(10)
278
279 # If `self` contains only digits and letters, return the corresponding integer in a given base
280 #
281 # assert "120".a_to(3) == 15
282 fun a_to(base: Int) : Int
283 do
284 var i = 0
285 var neg = false
286
287 for j in [0..length[ do
288 var c = chars[j]
289 var v = c.to_i
290 if v > base then
291 if neg then
292 return -i
293 else
294 return i
295 end
296 else if v < 0 then
297 neg = true
298 else
299 i = i * base + v
300 end
301 end
302 if neg then
303 return -i
304 else
305 return i
306 end
307 end
308
309 # Is this string in a valid numeric format compatible with `to_f`?
310 #
311 # assert "123".is_numeric == true
312 # assert "1.2".is_numeric == true
313 # assert "-1.2".is_numeric == true
314 # assert "-1.23e-2".is_numeric == true
315 # assert "1..2".is_numeric == false
316 # assert "".is_numeric == false
317 fun is_numeric: Bool
318 do
319 var has_point = false
320 var e_index = -1
321 for i in [0..length[ do
322 var c = chars[i]
323 if not c.is_numeric then
324 if c == '.' and not has_point then
325 has_point = true
326 else if c == 'e' and e_index == -1 and i > 0 and i < length - 1 and chars[i-1] != '-' then
327 e_index = i
328 else if c == '-' and i == e_index + 1 and i < length - 1 then
329 else
330 return false
331 end
332 end
333 end
334 return not is_empty
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 `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 # `char`, or `' '` by default, is repeated to pad the empty space.
507 #
508 # Examples
509 #
510 # assert "hello".justify(10, 0.0) == "hello "
511 # assert "hello".justify(10, 1.0) == " hello"
512 # assert "hello".justify(10, 0.5) == " hello "
513 # assert "hello".justify(10, 0.5, '.') == "..hello..."
514 #
515 # If `length` is not enough, `self` is returned as is.
516 #
517 # assert "hello".justify(2, 0.0) == "hello"
518 #
519 # REQUIRE: `left >= 0.0 and left <= 1.0`
520 # ENSURE: `self.length <= length implies result.length == length`
521 # ENSURE: `self.length >= length implies result == self`
522 fun justify(length: Int, left: Float, char: nullable Char): String
523 do
524 var pad = (char or else ' ').to_s
525 var diff = length - self.length
526 if diff <= 0 then return to_s
527 assert left >= 0.0 and left <= 1.0
528 var before = (diff.to_f * left).to_i
529 return pad * before + self + pad * (diff-before)
530 end
531
532 # Mangle a string to be a unique string only made of alphanumeric characters and underscores.
533 #
534 # This method is injective (two different inputs never produce the same
535 # output) and the returned string always respect the following rules:
536 #
537 # * Contains only US-ASCII letters, digits and underscores.
538 # * Never starts with a digit.
539 # * Never ends with an underscore.
540 # * Never contains two contiguous underscores.
541 #
542 # assert "42_is/The answer!".to_cmangle == "_52d2_is_47dThe_32danswer_33d"
543 # assert "__".to_cmangle == "_95d_95d"
544 # assert "__d".to_cmangle == "_95d_d"
545 # assert "_d_".to_cmangle == "_d_95d"
546 # assert "_42".to_cmangle == "_95d42"
547 # assert "foo".to_cmangle == "foo"
548 # assert "".to_cmangle == ""
549 fun to_cmangle: String
550 do
551 if is_empty then return ""
552 var res = new Buffer
553 var underscore = false
554 var start = 0
555 var c = self[0]
556
557 if c >= '0' and c <= '9' then
558 res.add('_')
559 res.append(c.code_point.to_s)
560 res.add('d')
561 start = 1
562 end
563 for i in [start..length[ do
564 c = self[i]
565 if (c >= 'a' and c <= 'z') or (c >='A' and c <= 'Z') then
566 res.add(c)
567 underscore = false
568 continue
569 end
570 if underscore then
571 res.append('_'.code_point.to_s)
572 res.add('d')
573 end
574 if c >= '0' and c <= '9' then
575 res.add(c)
576 underscore = false
577 else if c == '_' then
578 res.add(c)
579 underscore = true
580 else
581 res.add('_')
582 res.append(c.code_point.to_s)
583 res.add('d')
584 underscore = false
585 end
586 end
587 if underscore then
588 res.append('_'.code_point.to_s)
589 res.add('d')
590 end
591 return res.to_s
592 end
593
594 # Escape `"` `\` `'`, trigraphs and non printable characters using the rules of literal C strings and characters
595 #
596 # assert "abAB12<>&".escape_to_c == "abAB12<>&"
597 # assert "\n\"'\\".escape_to_c == "\\n\\\"\\'\\\\"
598 # assert "allo???!".escape_to_c == "allo??\\?!"
599 # assert "??=??/??'??(??)".escape_to_c == "?\\?=?\\?/??\\'?\\?(?\\?)"
600 # assert "??!??<??>??-".escape_to_c == "?\\?!?\\?<?\\?>?\\?-"
601 #
602 # Most non-printable characters (bellow ASCII 32) are escaped to an octal form `\nnn`.
603 # Three digits are always used to avoid following digits to be interpreted as an element
604 # of the octal sequence.
605 #
606 # assert "{0.code_point}{1.code_point}{8.code_point}{31.code_point}{32.code_point}".escape_to_c == "\\000\\001\\010\\037 "
607 #
608 # The exceptions are the common `\t` and `\n`.
609 fun escape_to_c: String
610 do
611 var b = new Buffer
612 for i in [0..length[ do
613 var c = chars[i]
614 if c == '\n' then
615 b.append("\\n")
616 else if c == '\t' then
617 b.append("\\t")
618 else if c == '"' then
619 b.append("\\\"")
620 else if c == '\'' then
621 b.append("\\\'")
622 else if c == '\\' then
623 b.append("\\\\")
624 else if c == '?' then
625 # Escape if it is the last question mark of a ANSI C trigraph.
626 var j = i + 1
627 if j < length then
628 var next = chars[j]
629 # We ignore `??'` because it will be escaped as `??\'`.
630 if
631 next == '!' or
632 next == '(' or
633 next == ')' or
634 next == '-' or
635 next == '/' or
636 next == '<' or
637 next == '=' or
638 next == '>'
639 then b.add('\\')
640 end
641 b.add('?')
642 else if c.code_point < 32 then
643 b.add('\\')
644 var oct = c.code_point.to_base(8)
645 # Force 3 octal digits since it is the
646 # maximum allowed in the C specification
647 if oct.length == 1 then
648 b.add('0')
649 b.add('0')
650 else if oct.length == 2 then
651 b.add('0')
652 end
653 b.append(oct)
654 else
655 b.add(c)
656 end
657 end
658 return b.to_s
659 end
660
661 # Escape additionnal characters
662 # The result might no be legal in C but be used in other languages
663 #
664 # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
665 # assert "allo???!".escape_more_to_c("") == "allo??\\?!"
666 fun escape_more_to_c(chars: String): String
667 do
668 var b = new Buffer
669 for c in escape_to_c.chars do
670 if chars.chars.has(c) then
671 b.add('\\')
672 end
673 b.add(c)
674 end
675 return b.to_s
676 end
677
678 # Escape to C plus braces
679 #
680 # assert "\n\"'\\\{\}".escape_to_nit == "\\n\\\"\\'\\\\\\\{\\\}"
681 fun escape_to_nit: String do return escape_more_to_c("\{\}")
682
683 # Escape to POSIX Shell (sh).
684 #
685 # Abort if the text contains a null byte.
686 #
687 # assert "\n\"'\\\{\}0".escape_to_sh == "'\n\"'\\''\\\{\}0'"
688 fun escape_to_sh: String do
689 var b = new Buffer
690 b.chars.add '\''
691 for i in [0..length[ do
692 var c = chars[i]
693 if c == '\'' then
694 b.append("'\\''")
695 else
696 assert without_null_byte: c != '\0'
697 b.add(c)
698 end
699 end
700 b.chars.add '\''
701 return b.to_s
702 end
703
704 # Escape to include in a Makefile
705 #
706 # Unfortunately, some characters are not escapable in Makefile.
707 # These characters are `;`, `|`, `\`, and the non-printable ones.
708 # They will be rendered as `"?{hex}"`.
709 fun escape_to_mk: String do
710 var b = new Buffer
711 for i in [0..length[ do
712 var c = chars[i]
713 if c == '$' then
714 b.append("$$")
715 else if c == ':' or c == ' ' or c == '#' then
716 b.add('\\')
717 b.add(c)
718 else if c.code_point < 32 or c == ';' or c == '|' or c == '\\' or c == '=' then
719 b.append("?{c.code_point.to_base(16)}")
720 else
721 b.add(c)
722 end
723 end
724 return b.to_s
725 end
726
727 # Return a string where Nit escape sequences are transformed.
728 #
729 # var s = "\\n"
730 # assert s.length == 2
731 # var u = s.unescape_nit
732 # assert u.length == 1
733 # assert u.chars[0].code_point == 10 # (the ASCII value of the "new line" character)
734 fun unescape_nit: String
735 do
736 var res = new Buffer.with_cap(self.length)
737 var was_slash = false
738 for i in [0..length[ do
739 var c = chars[i]
740 if not was_slash then
741 if c == '\\' then
742 was_slash = true
743 else
744 res.add(c)
745 end
746 continue
747 end
748 was_slash = false
749 if c == 'n' then
750 res.add('\n')
751 else if c == 'r' then
752 res.add('\r')
753 else if c == 't' then
754 res.add('\t')
755 else if c == '0' then
756 res.add('\0')
757 else
758 res.add(c)
759 end
760 end
761 return res.to_s
762 end
763
764 # Returns `self` with all characters escaped with their UTF-16 representation
765 #
766 # assert "Aèあ𐏓".escape_to_utf16 == "\\u0041\\u00e8\\u3042\\ud800\\udfd3"
767 fun escape_to_utf16: String do
768 var buf = new Buffer
769 for i in chars do buf.append i.escape_to_utf16
770 return buf.to_s
771 end
772
773 # Returns the Unicode char escaped by `self`
774 #
775 # assert "\\u0041".from_utf16_escape == 'A'
776 # assert "\\ud800\\udfd3".from_utf16_escape == '𐏓'
777 # assert "\\u00e8".from_utf16_escape == 'è'
778 # assert "\\u3042".from_utf16_escape == 'あ'
779 fun from_utf16_escape(pos, ln: nullable Int): Char do
780 if pos == null then pos = 0
781 if ln == null then ln = length - pos
782 if ln < 6 then return 0xFFFD.code_point
783 var cp = from_utf16_digit(pos + 2)
784 if cp < 0xD800 then return cp.code_point
785 if cp > 0xDFFF then return cp.code_point
786 if cp > 0xDBFF then return 0xFFFD.code_point
787 if ln == 6 then return 0xFFFD.code_point
788 if ln < 12 then return 0xFFFD.code_point
789 cp <<= 16
790 cp += from_utf16_digit(pos + 8)
791 var cplo = cp & 0xFFFF
792 if cplo < 0xDC00 then return 0xFFFD.code_point
793 if cplo > 0xDFFF then return 0xFFFD.code_point
794 return cp.from_utf16_surr.code_point
795 end
796
797 # Returns a UTF-16 escape value
798 #
799 # var s = "\\ud800\\udfd3"
800 # assert s.from_utf16_digit(2) == 0xD800
801 # assert s.from_utf16_digit(8) == 0xDFD3
802 fun from_utf16_digit(pos: nullable Int): Int do
803 if pos == null then pos = 0
804 return to_hex(pos, 4)
805 end
806
807 # Encode `self` to percent (or URL) encoding
808 #
809 # assert "aBc09-._~".to_percent_encoding == "aBc09-._~"
810 # assert "%()< >".to_percent_encoding == "%25%28%29%3c%20%3e"
811 # assert ".com/post?e=asdf&f=123".to_percent_encoding == ".com%2fpost%3fe%3dasdf%26f%3d123"
812 # assert "éあいう".to_percent_encoding == "%c3%a9%e3%81%82%e3%81%84%e3%81%86"
813 fun to_percent_encoding: String
814 do
815 var buf = new Buffer
816
817 for i in [0..length[ do
818 var c = chars[i]
819 if (c >= '0' and c <= '9') or
820 (c >= 'a' and c <= 'z') or
821 (c >= 'A' and c <= 'Z') or
822 c == '-' or c == '.' or
823 c == '_' or c == '~'
824 then
825 buf.add c
826 else
827 var bytes = c.to_s.bytes
828 for b in bytes do buf.append "%{b.to_i.to_hex}"
829 end
830 end
831
832 return buf.to_s
833 end
834
835 # Decode `self` from percent (or URL) encoding to a clear string
836 #
837 # Replace invalid use of '%' with '?'.
838 #
839 # assert "aBc09-._~".from_percent_encoding == "aBc09-._~"
840 # assert "%25%28%29%3c%20%3e".from_percent_encoding == "%()< >"
841 # assert ".com%2fpost%3fe%3dasdf%26f%3d123".from_percent_encoding == ".com/post?e=asdf&f=123"
842 # assert "%25%28%29%3C%20%3E".from_percent_encoding == "%()< >"
843 # assert "incomplete %".from_percent_encoding == "incomplete ?"
844 # assert "invalid % usage".from_percent_encoding == "invalid ? usage"
845 # assert "%c3%a9%e3%81%82%e3%81%84%e3%81%86".from_percent_encoding == "éあいう"
846 fun from_percent_encoding: String
847 do
848 var len = byte_length
849 var has_percent = false
850 for c in chars do
851 if c == '%' then
852 len -= 2
853 has_percent = true
854 end
855 end
856
857 # If no transformation is needed, return self as a string
858 if not has_percent then return to_s
859
860 var buf = new NativeString(len)
861 var i = 0
862 var l = 0
863 while i < length do
864 var c = chars[i]
865 if c == '%' then
866 if i + 2 >= length then
867 # What follows % has been cut off
868 buf[l] = '?'.ascii
869 else
870 i += 1
871 var hex_s = substring(i, 2)
872 if hex_s.is_hex then
873 var hex_i = hex_s.to_hex
874 buf[l] = hex_i.to_b
875 i += 1
876 else
877 # What follows a % is not Hex
878 buf[l] = '?'.ascii
879 i -= 1
880 end
881 end
882 else buf[l] = c.ascii
883
884 i += 1
885 l += 1
886 end
887
888 return buf.to_s_unsafe(l)
889 end
890
891 # Escape the characters `<`, `>`, `&`, `"`, `'` and `/` as HTML/XML entity references.
892 #
893 # assert "a&b-<>\"x\"/'".html_escape == "a&amp;b-&lt;&gt;&#34;x&#34;&#47;&#39;"
894 #
895 # 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>
896 fun html_escape: String
897 do
898 var buf = new Buffer
899
900 for i in [0..length[ do
901 var c = chars[i]
902 if c == '&' then
903 buf.append "&amp;"
904 else if c == '<' then
905 buf.append "&lt;"
906 else if c == '>' then
907 buf.append "&gt;"
908 else if c == '"' then
909 buf.append "&#34;"
910 else if c == '\'' then
911 buf.append "&#39;"
912 else if c == '/' then
913 buf.append "&#47;"
914 else buf.add c
915 end
916
917 return buf.to_s
918 end
919
920 # Equality of text
921 # Two pieces of text are equals if thez have the same characters in the same order.
922 #
923 # assert "hello" == "hello"
924 # assert "hello" != "HELLO"
925 # assert "hello" == "hel"+"lo"
926 #
927 # Things that are not Text are not equal.
928 #
929 # assert "9" != '9'
930 # assert "9" != ['9']
931 # assert "9" != 9
932 #
933 # assert "9".chars.first == '9' # equality of Char
934 # assert "9".chars == ['9'] # equality of Sequence
935 # assert "9".to_i == 9 # equality of Int
936 redef fun ==(o)
937 do
938 if o == null then return false
939 if not o isa Text then return false
940 if self.is_same_instance(o) then return true
941 if self.length != o.length then return false
942 return self.chars == o.chars
943 end
944
945 # Lexicographical comparaison
946 #
947 # assert "abc" < "xy"
948 # assert "ABC" < "abc"
949 redef fun <(other)
950 do
951 var self_chars = self.chars.iterator
952 var other_chars = other.chars.iterator
953
954 while self_chars.is_ok and other_chars.is_ok do
955 if self_chars.item < other_chars.item then return true
956 if self_chars.item > other_chars.item then return false
957 self_chars.next
958 other_chars.next
959 end
960
961 if self_chars.is_ok then
962 return false
963 else
964 return true
965 end
966 end
967
968 # Escape string used in labels for graphviz
969 #
970 # assert ">><<".escape_to_dot == "\\>\\>\\<\\<"
971 fun escape_to_dot: String
972 do
973 return escape_more_to_c("|\{\}<>")
974 end
975
976 private var hash_cache: nullable Int = null
977
978 redef fun hash
979 do
980 if hash_cache == null then
981 # djb2 hash algorithm
982 var h = 5381
983
984 for i in [0..length[ do
985 var char = chars[i]
986 h = (h << 5) + h + char.code_point
987 end
988
989 hash_cache = h
990 end
991 return hash_cache.as(not null)
992 end
993
994 # Format `self` by replacing each `%n` with the `n`th item of `args`
995 #
996 # The character `%` followed by something other than a number are left as is.
997 # To represent a `%` followed by a number, double the `%`, as in `%%7`.
998 #
999 # assert "This %0 is a %1.".format("String", "formatted String") == "This String is a formatted String."
1000 # assert "Do not escape % nor %%1".format("unused") == "Do not escape % nor %1"
1001 fun format(args: Object...): String do
1002 var s = new Array[Text]
1003 var curr_st = 0
1004 var i = 0
1005 while i < length do
1006 if self[i] == '%' then
1007 var fmt_st = i
1008 i += 1
1009 var ciph_st = i
1010 while i < length and self[i].is_numeric do
1011 i += 1
1012 end
1013
1014 var ciph_len = i - ciph_st
1015 if ciph_len == 0 then
1016 # What follows '%' is not a number.
1017 s.push substring(curr_st, i - curr_st)
1018 if i < length and self[i] == '%' then
1019 # Skip the next `%`
1020 i += 1
1021 end
1022 curr_st = i
1023 continue
1024 end
1025
1026 var arg_index = substring(ciph_st, ciph_len).to_i
1027 if arg_index >= args.length then continue
1028
1029 s.push substring(curr_st, fmt_st - curr_st)
1030 s.push args[arg_index].to_s
1031
1032 curr_st = i
1033 i -= 1
1034 end
1035 i += 1
1036 end
1037 s.push substring(curr_st, length - curr_st)
1038 return s.plain_to_s
1039 end
1040
1041 # Return the Levenshtein distance between two strings
1042 #
1043 # ~~~
1044 # assert "abcd".levenshtein_distance("abcd") == 0
1045 # assert "".levenshtein_distance("abcd") == 4
1046 # assert "abcd".levenshtein_distance("") == 4
1047 # assert "abcd".levenshtein_distance("xyz") == 4
1048 # assert "abcd".levenshtein_distance("xbdy") == 3
1049 # ~~~
1050 fun levenshtein_distance(other: String): Int
1051 do
1052 var slen = self.length
1053 var olen = other.length
1054
1055 # fast cases
1056 if slen == 0 then return olen
1057 if olen == 0 then return slen
1058 if self == other then return 0
1059
1060 # previous row of distances
1061 var v0 = new Array[Int].with_capacity(olen+1)
1062
1063 # current row of distances
1064 var v1 = new Array[Int].with_capacity(olen+1)
1065
1066 for j in [0..olen] do
1067 # prefix insert cost
1068 v0[j] = j
1069 end
1070
1071 for i in [0..slen[ do
1072
1073 # prefix delete cost
1074 v1[0] = i + 1
1075
1076 for j in [0..olen[ do
1077 # delete cost
1078 var cost1 = v1[j] + 1
1079 # insert cost
1080 var cost2 = v0[j + 1] + 1
1081 # same char cost (+0)
1082 var cost3 = v0[j]
1083 # change cost
1084 if self[i] != other[j] then cost3 += 1
1085 # keep the min
1086 v1[j+1] = cost1.min(cost2).min(cost3)
1087 end
1088
1089 # Switch columns:
1090 # * v1 become v0 in the next iteration
1091 # * old v0 is reused as the new v1
1092 var tmp = v1
1093 v1 = v0
1094 v0 = tmp
1095 end
1096
1097 return v0[olen]
1098 end
1099
1100 # Copies `n` bytes from `self` at `src_offset` into `dest` starting at `dest_offset`
1101 #
1102 # Basically a high-level synonym of NativeString::copy_to
1103 #
1104 # REQUIRE: `n` must be large enough to contain `len` bytes
1105 #
1106 # var ns = new NativeString(8)
1107 # "Text is String".copy_to_native(ns, 8, 2, 0)
1108 # assert ns.to_s_unsafe(8) == "xt is St"
1109 #
1110 fun copy_to_native(dest: NativeString, n, src_offset, dest_offset: Int) do
1111 var mypos = src_offset
1112 var itspos = dest_offset
1113 while n > 0 do
1114 dest[itspos] = self.bytes[mypos]
1115 itspos += 1
1116 mypos += 1
1117 n -= 1
1118 end
1119 end
1120
1121 # Packs the content of a string in packs of `ln` chars.
1122 # This variant ensures that only the last element might be smaller than `ln`
1123 #
1124 # ~~~nit
1125 # var s = "abcdefghijklmnopqrstuvwxyz"
1126 # assert s.pack_l(4) == ["abcd","efgh","ijkl","mnop","qrst","uvwx","yz"]
1127 # ~~~
1128 fun pack_l(ln: Int): Array[Text] do
1129 var st = 0
1130 var retarr = new Array[Text].with_capacity(length / ln + length % ln)
1131 while st < length do
1132 retarr.add(substring(st, ln))
1133 st += ln
1134 end
1135 return retarr
1136 end
1137
1138 # Packs the content of a string in packs of `ln` chars.
1139 # This variant ensures that only the first element might be smaller than `ln`
1140 #
1141 # ~~~nit
1142 # var s = "abcdefghijklmnopqrstuvwxyz"
1143 # assert s.pack_r(4) == ["ab","cdef","ghij","klmn","opqr","stuv","wxyz"]
1144 # ~~~
1145 fun pack_r(ln: Int): Array[Text] do
1146 var st = length
1147 var retarr = new Array[Text].with_capacity(length / ln + length % ln)
1148 while st >= 0 do
1149 retarr.add(substring(st - ln, ln))
1150 st -= ln
1151 end
1152 return retarr.reversed
1153 end
1154 end
1155
1156 # All kinds of array-based text representations.
1157 abstract class FlatText
1158 super Text
1159
1160 # Underlying C-String (`char*`)
1161 #
1162 # Warning : Might be void in some subclasses, be sure to check
1163 # if set before using it.
1164 var items: NativeString is noinit
1165
1166 # Returns a char* starting at position `first_byte`
1167 #
1168 # WARNING: If you choose to use this service, be careful of the following.
1169 #
1170 # Strings and NativeString are *ideally* always allocated through a Garbage Collector.
1171 # Since the GC tracks the use of the pointer for the beginning of the char*, it may be
1172 # deallocated at any moment, rendering the pointer returned by this function invalid.
1173 # Any access to freed memory may very likely cause undefined behaviour or a crash.
1174 # (Failure to do so will most certainly result in long and painful debugging hours)
1175 #
1176 # The only safe use of this pointer is if it is ephemeral (e.g. read in a C function
1177 # then immediately return).
1178 #
1179 # As always, do not modify the content of the String in C code, if this is what you want
1180 # copy locally the char* as Nit Strings are immutable.
1181 fun fast_cstring: NativeString is abstract
1182
1183 redef var length = 0
1184
1185 redef var byte_length = 0
1186
1187 redef fun output
1188 do
1189 var i = 0
1190 while i < length do
1191 items[i].output
1192 i += 1
1193 end
1194 end
1195
1196 redef fun copy_to_native(dest, n, src_offset, dest_offset) do
1197 items.copy_to(dest, n, src_offset, dest_offset)
1198 end
1199 end
1200
1201 # Abstract class for the SequenceRead compatible
1202 # views on the chars of any Text
1203 private abstract class StringCharView
1204 super SequenceRead[Char]
1205
1206 type SELFTYPE: Text
1207
1208 var target: SELFTYPE
1209
1210 redef fun is_empty do return target.is_empty
1211
1212 redef fun length do return target.length
1213
1214 redef fun iterator: IndexedIterator[Char] do return self.iterator_from(0)
1215
1216 redef fun reverse_iterator do return self.reverse_iterator_from(self.length - 1)
1217 end
1218
1219 # Abstract class for the SequenceRead compatible
1220 # views on the bytes of any Text
1221 private abstract class StringByteView
1222 super SequenceRead[Byte]
1223
1224 type SELFTYPE: Text
1225
1226 var target: SELFTYPE
1227
1228 redef fun is_empty do return target.is_empty
1229
1230 redef fun length do return target.byte_length
1231
1232 redef fun iterator do return self.iterator_from(0)
1233
1234 redef fun reverse_iterator do return self.reverse_iterator_from(target.byte_length - 1)
1235 end
1236
1237 # Immutable sequence of characters.
1238 #
1239 # String objects may be created using literals.
1240 #
1241 # assert "Hello World!" isa String
1242 abstract class String
1243 super Text
1244
1245 redef type SELFTYPE: String is fixed
1246
1247 redef fun to_s do return self
1248
1249 redef fun clone do return self
1250 # Concatenates `o` to `self`
1251 #
1252 # assert "hello" + "world" == "helloworld"
1253 # assert "" + "hello" + "" == "hello"
1254 fun +(o: Text): SELFTYPE is abstract
1255
1256 # Concatenates self `i` times
1257 #
1258 # assert "abc" * 4 == "abcabcabcabc"
1259 # assert "abc" * 1 == "abc"
1260 # assert "abc" * 0 == ""
1261 fun *(i: Int): SELFTYPE is abstract
1262
1263 # Insert `s` at `pos`.
1264 #
1265 # assert "helloworld".insert_at(" ", 5) == "hello world"
1266 fun insert_at(s: String, pos: Int): SELFTYPE is abstract
1267
1268 redef fun substrings is abstract
1269
1270 # Returns a reversed version of self
1271 #
1272 # assert "hello".reversed == "olleh"
1273 # assert "bob".reversed == "bob"
1274 # assert "".reversed == ""
1275 fun reversed: SELFTYPE is abstract
1276
1277 # A upper case version of `self`
1278 #
1279 # assert "Hello World!".to_upper == "HELLO WORLD!"
1280 fun to_upper: SELFTYPE is abstract
1281
1282 # A lower case version of `self`
1283 #
1284 # assert "Hello World!".to_lower == "hello world!"
1285 fun to_lower : SELFTYPE is abstract
1286
1287 # Takes a camel case `self` and converts it to snake case
1288 #
1289 # assert "randomMethodId".to_snake_case == "random_method_id"
1290 #
1291 # The rules are the following:
1292 #
1293 # An uppercase is always converted to a lowercase
1294 #
1295 # assert "HELLO_WORLD".to_snake_case == "hello_world"
1296 #
1297 # An uppercase that follows a lowercase is prefixed with an underscore
1298 #
1299 # assert "HelloTheWORLD".to_snake_case == "hello_the_world"
1300 #
1301 # An uppercase that follows an uppercase and is followed by a lowercase, is prefixed with an underscore
1302 #
1303 # assert "HelloTHEWorld".to_snake_case == "hello_the_world"
1304 #
1305 # All other characters are kept as is; `self` does not need to be a proper CamelCased string.
1306 #
1307 # assert "=-_H3ll0Th3W0rld_-=".to_snake_case == "=-_h3ll0th3w0rld_-="
1308 fun to_snake_case: SELFTYPE
1309 do
1310 if self.is_lower then return self
1311
1312 var new_str = new Buffer.with_cap(self.length)
1313 var prev_is_lower = false
1314 var prev_is_upper = false
1315
1316 for i in [0..length[ do
1317 var char = chars[i]
1318 if char.is_lower then
1319 new_str.add(char)
1320 prev_is_lower = true
1321 prev_is_upper = false
1322 else if char.is_upper then
1323 if prev_is_lower then
1324 new_str.add('_')
1325 else if prev_is_upper and i+1 < length and chars[i+1].is_lower then
1326 new_str.add('_')
1327 end
1328 new_str.add(char.to_lower)
1329 prev_is_lower = false
1330 prev_is_upper = true
1331 else
1332 new_str.add(char)
1333 prev_is_lower = false
1334 prev_is_upper = false
1335 end
1336 end
1337
1338 return new_str.to_s
1339 end
1340
1341 # Takes a snake case `self` and converts it to camel case
1342 #
1343 # assert "random_method_id".to_camel_case == "randomMethodId"
1344 #
1345 # If the identifier is prefixed by an underscore, the underscore is ignored
1346 #
1347 # assert "_private_field".to_camel_case == "_privateField"
1348 #
1349 # If `self` is upper, it is returned unchanged
1350 #
1351 # assert "RANDOM_ID".to_camel_case == "RANDOM_ID"
1352 #
1353 # If there are several consecutive underscores, they are considered as a single one
1354 #
1355 # assert "random__method_id".to_camel_case == "randomMethodId"
1356 fun to_camel_case: SELFTYPE
1357 do
1358 if self.is_upper then return self
1359
1360 var new_str = new Buffer
1361 var is_first_char = true
1362 var follows_us = false
1363
1364 for i in [0..length[ do
1365 var char = chars[i]
1366 if is_first_char then
1367 new_str.add(char)
1368 is_first_char = false
1369 else if char == '_' then
1370 follows_us = true
1371 else if follows_us then
1372 new_str.add(char.to_upper)
1373 follows_us = false
1374 else
1375 new_str.add(char)
1376 end
1377 end
1378
1379 return new_str.to_s
1380 end
1381
1382 # Returns a capitalized `self`
1383 #
1384 # Letters that follow a letter are lowercased
1385 # Letters that follow a non-letter are upcased.
1386 #
1387 # If `keep_upper = true`, already uppercase letters are not lowercased.
1388 #
1389 # SEE : `Char::is_letter` for the definition of letter.
1390 #
1391 # assert "jAVASCRIPT".capitalized == "Javascript"
1392 # assert "i am root".capitalized == "I Am Root"
1393 # assert "ab_c -ab0c ab\nc".capitalized == "Ab_C -Ab0C Ab\nC"
1394 # assert "preserve my ACRONYMS".capitalized(keep_upper=true) == "Preserve My ACRONYMS"
1395 fun capitalized(keep_upper: nullable Bool): SELFTYPE do
1396 if length == 0 then return self
1397
1398 var buf = new Buffer.with_cap(length)
1399 buf.capitalize(keep_upper=keep_upper, src=self)
1400 return buf.to_s
1401 end
1402 end
1403
1404 # A mutable sequence of characters.
1405 abstract class Buffer
1406 super Text
1407
1408 # Returns an arbitrary subclass of `Buffer` with default parameters
1409 new is abstract
1410
1411 # Returns an instance of a subclass of `Buffer` with `i` base capacity
1412 new with_cap(i: Int) is abstract
1413
1414 redef type SELFTYPE: Buffer is fixed
1415
1416 # Copy-On-Write flag
1417 #
1418 # If the `Buffer` was to_s'd, the next in-place altering
1419 # operation will cause the current `Buffer` to be re-allocated.
1420 #
1421 # The flag will then be set at `false`.
1422 protected var written = false
1423
1424 # Modifies the char contained at pos `index`
1425 #
1426 # DEPRECATED : Use self.chars.[]= instead
1427 fun []=(index: Int, item: Char) is abstract
1428
1429 #~~~nit
1430 # var b = new Buffer
1431 # b.append("Buffer!")
1432 # var c = b.clone
1433 # assert b == c
1434 #~~~
1435 redef fun clone do
1436 var cln = new Buffer.with_cap(byte_length)
1437 cln.append self
1438 return cln
1439 end
1440
1441 # Adds a char `c` at the end of self
1442 #
1443 # DEPRECATED : Use self.chars.add instead
1444 fun add(c: Char) is abstract
1445
1446 # Clears the buffer
1447 #
1448 # var b = new Buffer
1449 # b.append "hello"
1450 # assert not b.is_empty
1451 # b.clear
1452 # assert b.is_empty
1453 fun clear is abstract
1454
1455 # Enlarges the subsequent array containing the chars of self
1456 fun enlarge(cap: Int) is abstract
1457
1458 # Adds the content of text `s` at the end of self
1459 #
1460 # var b = new Buffer
1461 # b.append "hello"
1462 # b.append "world"
1463 # assert b == "helloworld"
1464 fun append(s: Text) is abstract
1465
1466 # `self` is appended in such a way that `self` is repeated `r` times
1467 #
1468 # var b = new Buffer
1469 # b.append "hello"
1470 # b.times 3
1471 # assert b == "hellohellohello"
1472 fun times(r: Int) is abstract
1473
1474 # Reverses itself in-place
1475 #
1476 # var b = new Buffer
1477 # b.append("hello")
1478 # b.reverse
1479 # assert b == "olleh"
1480 fun reverse is abstract
1481
1482 # Changes each lower-case char in `self` by its upper-case variant
1483 #
1484 # var b = new Buffer
1485 # b.append("Hello World!")
1486 # b.upper
1487 # assert b == "HELLO WORLD!"
1488 fun upper is abstract
1489
1490 # Changes each upper-case char in `self` by its lower-case variant
1491 #
1492 # var b = new Buffer
1493 # b.append("Hello World!")
1494 # b.lower
1495 # assert b == "hello world!"
1496 fun lower is abstract
1497
1498 # Capitalizes each word in `self`
1499 #
1500 # Letters that follow a letter are lowercased
1501 # Letters that follow a non-letter are upcased.
1502 #
1503 # If `keep_upper = true`, uppercase letters are not lowercased.
1504 #
1505 # When `src` is specified, this method reads from `src` instead of `self`
1506 # but it still writes the result to the beginning of `self`.
1507 # This requires `self` to have the capacity to receive all of the
1508 # capitalized content of `src`.
1509 #
1510 # SEE: `Char::is_letter` for the definition of a letter.
1511 #
1512 # var b = new FlatBuffer.from("jAVAsCriPt")
1513 # b.capitalize
1514 # assert b == "Javascript"
1515 # b = new FlatBuffer.from("i am root")
1516 # b.capitalize
1517 # assert b == "I Am Root"
1518 # b = new FlatBuffer.from("ab_c -ab0c ab\nc")
1519 # b.capitalize
1520 # assert b == "Ab_C -Ab0C Ab\nC"
1521 #
1522 # b = new FlatBuffer.from("12345")
1523 # b.capitalize(src="foo")
1524 # assert b == "Foo45"
1525 #
1526 # b = new FlatBuffer.from("preserve my ACRONYMS")
1527 # b.capitalize(keep_upper=true)
1528 # assert b == "Preserve My ACRONYMS"
1529 fun capitalize(keep_upper: nullable Bool, src: nullable Text) do
1530 src = src or else self
1531 var length = src.length
1532 if length == 0 then return
1533 keep_upper = keep_upper or else false
1534
1535 var c = src[0].to_upper
1536 self[0] = c
1537 var prev = c
1538 for i in [1 .. length[ do
1539 prev = c
1540 c = src[i]
1541 if prev.is_letter then
1542 if keep_upper then
1543 self[i] = c
1544 else
1545 self[i] = c.to_lower
1546 end
1547 else
1548 self[i] = c.to_upper
1549 end
1550 end
1551 end
1552
1553 # In Buffers, the internal sequence of character is mutable
1554 # Thus, `chars` can be used to modify the buffer.
1555 redef fun chars: Sequence[Char] is abstract
1556
1557 # Appends `length` chars from `s` starting at index `from`
1558 #
1559 # ~~~nit
1560 # var b = new Buffer
1561 # b.append_substring("abcde", 1, 2)
1562 # assert b == "bc"
1563 # b.append_substring("vwxyz", 2, 3)
1564 # assert b == "bcxyz"
1565 # b.append_substring("ABCDE", 4, 300)
1566 # assert b == "bcxyzE"
1567 # b.append_substring("VWXYZ", 400, 1)
1568 # assert b == "bcxyzE"
1569 # ~~~
1570 fun append_substring(s: Text, from, length: Int) do
1571 if from < 0 then
1572 length += from
1573 from = 0
1574 end
1575 var ln = s.length
1576 if (length + from) > ln then length = ln - from
1577 if length <= 0 then return
1578 append_substring_impl(s, from, length)
1579 end
1580
1581 # Unsafe version of `append_substring` for performance
1582 #
1583 # NOTE: Use only if sure about `from` and `length`, no checks
1584 # or bound recalculation is done
1585 fun append_substring_impl(s: Text, from, length: Int) do
1586 var pos = from
1587 for i in [0 .. length[ do
1588 self.add s[pos]
1589 pos += 1
1590 end
1591 end
1592 end
1593
1594 # View for chars on Buffer objects, extends Sequence
1595 # for mutation operations
1596 private abstract class BufferCharView
1597 super StringCharView
1598 super Sequence[Char]
1599
1600 redef type SELFTYPE: Buffer
1601
1602 end
1603
1604 # View for bytes on Buffer objects, extends Sequence
1605 # for mutation operations
1606 private abstract class BufferByteView
1607 super StringByteView
1608
1609 redef type SELFTYPE: Buffer
1610 end
1611
1612 redef class Object
1613 # User readable representation of `self`.
1614 fun to_s: String do return inspect
1615
1616 # The class name of the object in NativeString format.
1617 private fun native_class_name: NativeString is intern
1618
1619 # The class name of the object.
1620 #
1621 # assert 5.class_name == "Int"
1622 fun class_name: String do return native_class_name.to_s
1623
1624 # Developer readable representation of `self`.
1625 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
1626 fun inspect: String
1627 do
1628 return "<{inspect_head}>"
1629 end
1630
1631 # Return "CLASSNAME:#OBJECTID".
1632 # This function is mainly used with the redefinition of the inspect method
1633 protected fun inspect_head: String
1634 do
1635 return "{class_name}:#{object_id.to_hex}"
1636 end
1637 end
1638
1639 redef class Bool
1640 # assert true.to_s == "true"
1641 # assert false.to_s == "false"
1642 redef fun to_s
1643 do
1644 if self then
1645 return once "true"
1646 else
1647 return once "false"
1648 end
1649 end
1650 end
1651
1652 redef class Byte
1653 # C function to calculate the length of the `NativeString` to receive `self`
1654 private fun byte_to_s_len: Int `{
1655 return snprintf(NULL, 0, "0x%02x", self);
1656 `}
1657
1658 # C function to convert an nit Int to a NativeString (char*)
1659 private fun native_byte_to_s(nstr: NativeString, strlen: Int) `{
1660 snprintf(nstr, strlen, "0x%02x", self);
1661 `}
1662
1663 # Displayable byte in its hexadecimal form (0x..)
1664 #
1665 # assert 1.to_b.to_s == "0x01"
1666 # assert (-123).to_b.to_s == "0x85"
1667 redef fun to_s do
1668 var nslen = byte_to_s_len
1669 var ns = new NativeString(nslen + 1)
1670 ns[nslen] = 0u8
1671 native_byte_to_s(ns, nslen + 1)
1672 return ns.to_s_unsafe(nslen)
1673 end
1674 end
1675
1676 redef class Int
1677
1678 # Wrapper of strerror C function
1679 private fun strerror_ext: NativeString `{ return strerror((int)self); `}
1680
1681 # Returns a string describing error number
1682 fun strerror: String do return strerror_ext.to_s
1683
1684 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if negative).
1685 # assume < to_c max const of char
1686 private fun fill_buffer(s: Buffer, base: Int)
1687 do
1688 var n: Int
1689 # Sign
1690 if self < 0 then
1691 n = - self
1692 s.chars[0] = '-'
1693 else if self == 0 then
1694 s.chars[0] = '0'
1695 return
1696 else
1697 n = self
1698 end
1699 # Fill digits
1700 var pos = digit_count(base) - 1
1701 while pos >= 0 and n > 0 do
1702 s.chars[pos] = (n % base).to_c
1703 n = n / base # /
1704 pos -= 1
1705 end
1706 end
1707
1708 # C function to calculate the length of the `NativeString` to receive `self`
1709 private fun int_to_s_len: Int `{
1710 return snprintf(NULL, 0, "%ld", self);
1711 `}
1712
1713 # C function to convert an nit Int to a NativeString (char*)
1714 private fun native_int_to_s(nstr: NativeString, strlen: Int) `{
1715 snprintf(nstr, strlen, "%ld", self);
1716 `}
1717
1718 # String representation of `self` in the given `base`
1719 #
1720 # ~~~
1721 # assert 15.to_base(10) == "15"
1722 # assert 15.to_base(16) == "f"
1723 # assert 15.to_base(2) == "1111"
1724 # assert (-10).to_base(3) == "-101"
1725 # ~~~
1726 fun to_base(base: Int): String
1727 do
1728 var l = digit_count(base)
1729 var s = new Buffer
1730 s.enlarge(l)
1731 for x in [0..l[ do s.add(' ')
1732 fill_buffer(s, base)
1733 return s.to_s
1734 end
1735
1736
1737 # return displayable int in hexadecimal
1738 #
1739 # assert 1.to_hex == "1"
1740 # assert (-255).to_hex == "-ff"
1741 fun to_hex: String do return to_base(16)
1742 end
1743
1744 redef class Float
1745 # Pretty representation of `self`, with decimals as needed from 1 to a maximum of 3
1746 #
1747 # assert 12.34.to_s == "12.34"
1748 # assert (-0120.030).to_s == "-120.03"
1749 #
1750 # see `to_precision` for a custom precision.
1751 redef fun to_s do
1752 var str = to_precision( 3 )
1753 if is_inf != 0 or is_nan then return str
1754 var len = str.length
1755 for i in [0..len-1] do
1756 var j = len-1-i
1757 var c = str.chars[j]
1758 if c == '0' then
1759 continue
1760 else if c == '.' then
1761 return str.substring( 0, j+2 )
1762 else
1763 return str.substring( 0, j+1 )
1764 end
1765 end
1766 return str
1767 end
1768
1769 # `String` representation of `self` with the given number of `decimals`
1770 #
1771 # assert 12.345.to_precision(0) == "12"
1772 # assert 12.345.to_precision(3) == "12.345"
1773 # assert (-12.345).to_precision(3) == "-12.345"
1774 # assert (-0.123).to_precision(3) == "-0.123"
1775 # assert 0.999.to_precision(2) == "1.00"
1776 # assert 0.999.to_precision(4) == "0.9990"
1777 fun to_precision(decimals: Int): String
1778 do
1779 if is_nan then return "nan"
1780
1781 var isinf = self.is_inf
1782 if isinf == 1 then
1783 return "inf"
1784 else if isinf == -1 then
1785 return "-inf"
1786 end
1787
1788 if decimals == 0 then return self.to_i.to_s
1789 var f = self
1790 for i in [0..decimals[ do f = f * 10.0
1791 if self > 0.0 then
1792 f = f + 0.5
1793 else
1794 f = f - 0.5
1795 end
1796 var i = f.to_i
1797 if i == 0 then return "0." + "0"*decimals
1798
1799 # Prepare both parts of the float, before and after the "."
1800 var s = i.abs.to_s
1801 var sl = s.length
1802 var p1
1803 var p2
1804 if sl > decimals then
1805 # Has something before the "."
1806 p1 = s.substring(0, sl-decimals)
1807 p2 = s.substring(sl-decimals, decimals)
1808 else
1809 p1 = "0"
1810 p2 = "0"*(decimals-sl) + s
1811 end
1812
1813 if i < 0 then p1 = "-" + p1
1814
1815 return p1 + "." + p2
1816 end
1817 end
1818
1819 redef class Char
1820
1821 # Returns a sequence with the UTF-8 bytes of `self`
1822 #
1823 # assert 'a'.bytes == [0x61u8]
1824 # assert 'ま'.bytes == [0xE3u8, 0x81u8, 0xBEu8]
1825 fun bytes: SequenceRead[Byte] do return to_s.bytes
1826
1827 # Is `self` an UTF-16 surrogate pair ?
1828 fun is_surrogate: Bool do
1829 var cp = code_point
1830 return cp >= 0xD800 and cp <= 0xDFFF
1831 end
1832
1833 # Is `self` a UTF-16 high surrogate ?
1834 fun is_hi_surrogate: Bool do
1835 var cp = code_point
1836 return cp >= 0xD800 and cp <= 0xDBFF
1837 end
1838
1839 # Is `self` a UTF-16 low surrogate ?
1840 fun is_lo_surrogate: Bool do
1841 var cp = code_point
1842 return cp >= 0xDC00 and cp <= 0xDFFF
1843 end
1844
1845 # Length of `self` in a UTF-8 String
1846 fun u8char_len: Int do
1847 var c = self.code_point
1848 if c < 0x80 then return 1
1849 if c <= 0x7FF then return 2
1850 if c <= 0xFFFF then return 3
1851 if c <= 0x10FFFF then return 4
1852 # Bad character format
1853 return 1
1854 end
1855
1856 # assert 'x'.to_s == "x"
1857 redef fun to_s do
1858 var ln = u8char_len
1859 var ns = new NativeString(ln + 1)
1860 u8char_tos(ns, ln)
1861 return ns.to_s_unsafe(ln)
1862 end
1863
1864 # Returns `self` escaped to UTF-16
1865 #
1866 # i.e. Represents `self`.`code_point` using UTF-16 codets escaped
1867 # with a `\u`
1868 #
1869 # assert 'A'.escape_to_utf16 == "\\u0041"
1870 # assert 'è'.escape_to_utf16 == "\\u00e8"
1871 # assert 'あ'.escape_to_utf16 == "\\u3042"
1872 # assert '𐏓'.escape_to_utf16 == "\\ud800\\udfd3"
1873 fun escape_to_utf16: String do
1874 var cp = code_point
1875 var buf: Buffer
1876 if cp < 0xD800 or (cp >= 0xE000 and cp <= 0xFFFF) then
1877 buf = new Buffer.with_cap(6)
1878 buf.append("\\u0000")
1879 var hx = cp.to_hex
1880 var outid = 5
1881 for i in hx.chars.reverse_iterator do
1882 buf[outid] = i
1883 outid -= 1
1884 end
1885 else
1886 buf = new Buffer.with_cap(12)
1887 buf.append("\\u0000\\u0000")
1888 var lo = (((cp - 0x10000) & 0x3FF) + 0xDC00).to_hex
1889 var hi = ((((cp - 0x10000) & 0xFFC00) >> 10) + 0xD800).to_hex
1890 var out = 2
1891 for i in hi do
1892 buf[out] = i
1893 out += 1
1894 end
1895 out = 8
1896 for i in lo do
1897 buf[out] = i
1898 out += 1
1899 end
1900 end
1901 return buf.to_s
1902 end
1903
1904 private fun u8char_tos(r: NativeString, len: Int) `{
1905 r[len] = '\0';
1906 switch(len){
1907 case 1:
1908 r[0] = self;
1909 break;
1910 case 2:
1911 r[0] = 0xC0 | ((self & 0x7C0) >> 6);
1912 r[1] = 0x80 | (self & 0x3F);
1913 break;
1914 case 3:
1915 r[0] = 0xE0 | ((self & 0xF000) >> 12);
1916 r[1] = 0x80 | ((self & 0xFC0) >> 6);
1917 r[2] = 0x80 | (self & 0x3F);
1918 break;
1919 case 4:
1920 r[0] = 0xF0 | ((self & 0x1C0000) >> 18);
1921 r[1] = 0x80 | ((self & 0x3F000) >> 12);
1922 r[2] = 0x80 | ((self & 0xFC0) >> 6);
1923 r[3] = 0x80 | (self & 0x3F);
1924 break;
1925 }
1926 `}
1927
1928 # Returns true if the char is a numerical digit
1929 #
1930 # assert '0'.is_numeric
1931 # assert '9'.is_numeric
1932 # assert not 'a'.is_numeric
1933 # assert not '?'.is_numeric
1934 #
1935 # FIXME: Works on ASCII-range only
1936 fun is_numeric: Bool
1937 do
1938 return self >= '0' and self <= '9'
1939 end
1940
1941 # Returns true if the char is an alpha digit
1942 #
1943 # assert 'a'.is_alpha
1944 # assert 'Z'.is_alpha
1945 # assert not '0'.is_alpha
1946 # assert not '?'.is_alpha
1947 #
1948 # FIXME: Works on ASCII-range only
1949 fun is_alpha: Bool
1950 do
1951 return (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z')
1952 end
1953
1954 # Is `self` an hexadecimal digit ?
1955 #
1956 # assert 'A'.is_hexdigit
1957 # assert not 'G'.is_hexdigit
1958 # assert 'a'.is_hexdigit
1959 # assert not 'g'.is_hexdigit
1960 # assert '5'.is_hexdigit
1961 fun is_hexdigit: Bool do return (self >= '0' and self <= '9') or (self >= 'A' and self <= 'F') or
1962 (self >= 'a' and self <= 'f')
1963
1964 # Returns true if the char is an alpha or a numeric digit
1965 #
1966 # assert 'a'.is_alphanumeric
1967 # assert 'Z'.is_alphanumeric
1968 # assert '0'.is_alphanumeric
1969 # assert '9'.is_alphanumeric
1970 # assert not '?'.is_alphanumeric
1971 #
1972 # FIXME: Works on ASCII-range only
1973 fun is_alphanumeric: Bool
1974 do
1975 return self.is_numeric or self.is_alpha
1976 end
1977
1978 # Returns `self` to its int value
1979 #
1980 # REQUIRE: `is_hexdigit`
1981 fun from_hex: Int do
1982 if self >= '0' and self <= '9' then return code_point - 0x30
1983 if self >= 'A' and self <= 'F' then return code_point - 0x37
1984 if self >= 'a' and self <= 'f' then return code_point - 0x57
1985 # Happens if self is not a hexdigit
1986 assert self.is_hexdigit
1987 # To make flow analysis happy
1988 abort
1989 end
1990 end
1991
1992 redef class Collection[E]
1993 # String representation of the content of the collection.
1994 #
1995 # The standard representation is the list of elements separated with commas.
1996 #
1997 # ~~~
1998 # assert [1,2,3].to_s == "[1,2,3]"
1999 # assert [1..3].to_s == "[1,2,3]"
2000 # assert (new Array[Int]).to_s == "[]" # empty collection
2001 # ~~~
2002 #
2003 # Subclasses may return a more specific string representation.
2004 redef fun to_s
2005 do
2006 return "[" + join(",") + "]"
2007 end
2008
2009 # Concatenate elements without separators
2010 #
2011 # ~~~
2012 # assert [1,2,3].plain_to_s == "123"
2013 # assert [11..13].plain_to_s == "111213"
2014 # assert (new Array[Int]).plain_to_s == "" # empty collection
2015 # ~~~
2016 fun plain_to_s: String
2017 do
2018 var s = new Buffer
2019 for e in self do if e != null then s.append(e.to_s)
2020 return s.to_s
2021 end
2022
2023 # Concatenate and separate each elements with `separator`.
2024 #
2025 # Only concatenate if `separator == null`.
2026 #
2027 # assert [1, 2, 3].join(":") == "1:2:3"
2028 # assert [1..3].join(":") == "1:2:3"
2029 # assert [1..3].join == "123"
2030 #
2031 # if `last_separator` is given, then it is used to separate the last element.
2032 #
2033 # assert [1, 2, 3, 4].join(", ", " and ") == "1, 2, 3 and 4"
2034 fun join(separator: nullable Text, last_separator: nullable Text): String
2035 do
2036 if is_empty then return ""
2037
2038 var s = new Buffer # Result
2039
2040 # Concat first item
2041 var i = iterator
2042 var e = i.item
2043 if e != null then s.append(e.to_s)
2044
2045 if last_separator == null then last_separator = separator
2046
2047 # Concat other items
2048 i.next
2049 while i.is_ok do
2050 e = i.item
2051 i.next
2052 if i.is_ok then
2053 if separator != null then s.append(separator)
2054 else
2055 if last_separator != null then s.append(last_separator)
2056 end
2057 if e != null then s.append(e.to_s)
2058 end
2059 return s.to_s
2060 end
2061 end
2062
2063 redef class Map[K,V]
2064 # Concatenate couples of key value.
2065 # Key and value are separated by `couple_sep`.
2066 # Couples are separated by `sep`.
2067 #
2068 # var m = new HashMap[Int, String]
2069 # m[1] = "one"
2070 # m[10] = "ten"
2071 # assert m.join("; ", "=") == "1=one; 10=ten"
2072 fun join(sep, couple_sep: String): String is abstract
2073 end
2074
2075 redef class Sys
2076 private var args_cache: nullable Sequence[String] = null
2077
2078 # The arguments of the program as given by the OS
2079 fun program_args: Sequence[String]
2080 do
2081 if _args_cache == null then init_args
2082 return _args_cache.as(not null)
2083 end
2084
2085 # The name of the program as given by the OS
2086 fun program_name: String
2087 do
2088 return native_argv(0).to_s
2089 end
2090
2091 # Initialize `program_args` with the contents of `native_argc` and `native_argv`.
2092 private fun init_args
2093 do
2094 var argc = native_argc
2095 var args = new Array[String].with_capacity(0)
2096 var i = 1
2097 while i < argc do
2098 args[i-1] = native_argv(i).to_s
2099 i += 1
2100 end
2101 _args_cache = args
2102 end
2103
2104 # First argument of the main C function.
2105 private fun native_argc: Int is intern
2106
2107 # Second argument of the main C function.
2108 private fun native_argv(i: Int): NativeString is intern
2109 end
2110
2111 # Comparator that efficienlty use `to_s` to compare things
2112 #
2113 # The comparaison call `to_s` on object and use the result to order things.
2114 #
2115 # var a = [1, 2, 3, 10, 20]
2116 # (new CachedAlphaComparator).sort(a)
2117 # assert a == [1, 10, 2, 20, 3]
2118 #
2119 # Internally the result of `to_s` is cached in a HashMap to counter
2120 # uneficient implementation of `to_s`.
2121 #
2122 # Note: it caching is not usefull, see `alpha_comparator`
2123 class CachedAlphaComparator
2124 super Comparator
2125 redef type COMPARED: Object
2126
2127 private var cache = new HashMap[Object, String]
2128
2129 private fun do_to_s(a: Object): String do
2130 if cache.has_key(a) then return cache[a]
2131 var res = a.to_s
2132 cache[a] = res
2133 return res
2134 end
2135
2136 redef fun compare(a, b) do
2137 return do_to_s(a) <=> do_to_s(b)
2138 end
2139 end
2140
2141 # see `alpha_comparator`
2142 private class AlphaComparator
2143 super Comparator
2144 redef fun compare(a, b) do
2145 if a == b then return 0
2146 if a == null then return -1
2147 if b == null then return 1
2148 return a.to_s <=> b.to_s
2149 end
2150 end
2151
2152 # Stateless comparator that naively use `to_s` to compare things.
2153 #
2154 # Note: the result of `to_s` is not cached, thus can be invoked a lot
2155 # on a single instace. See `CachedAlphaComparator` as an alternative.
2156 #
2157 # var a = [1, 2, 3, 10, 20]
2158 # alpha_comparator.sort(a)
2159 # assert a == [1, 10, 2, 20, 3]
2160 fun alpha_comparator: Comparator do return once new AlphaComparator
2161
2162 # The arguments of the program as given by the OS
2163 fun args: Sequence[String]
2164 do
2165 return sys.program_args
2166 end
2167
2168 redef class NativeString
2169 # Get a `String` from the data at `self` copied into Nit memory
2170 #
2171 # Require: `self` is a null-terminated string.
2172 fun to_s_with_copy: String is abstract
2173
2174 # Get a `String` from `length` bytes at `self`
2175 #
2176 # The result may point to the data at `self` or
2177 # it may make a copy in Nit controlled memory.
2178 # This method should only be used when `self` was allocated by the Nit GC,
2179 # or when manually controlling the deallocation of `self`.
2180 fun to_s_with_length(length: Int): String is abstract
2181
2182 # Get a `String` from the raw `length` bytes at `self`
2183 #
2184 # The default value of `length` is the number of bytes before
2185 # the first null character.
2186 #
2187 # The created `String` points to the data at `self`.
2188 # This method should be used when `self` was allocated by the Nit GC,
2189 # or when manually controlling the deallocation of `self`.
2190 #
2191 # /!\: This service does not clean the items for compliance with UTF-8,
2192 # use only when the data has already been verified as valid UTF-8.
2193 fun to_s_unsafe(length: nullable Int): String is abstract
2194
2195 # Get a `String` from the raw `byte_length` bytes at `self` with `unilen` Unicode characters
2196 #
2197 # The created `String` points to the data at `self`.
2198 # This method should be used when `self` was allocated by the Nit GC,
2199 # or when manually controlling the deallocation of `self`.
2200 #
2201 # /!\: This service does not clean the items for compliance with UTF-8,
2202 # use only when the data has already been verified as valid UTF-8.
2203 #
2204 # SEE: `abstract_text::Text` for more info on the difference
2205 # between `Text::byte_length` and `Text::length`.
2206 fun to_s_full(byte_length, unilen: Int): String is abstract
2207
2208 # Copies the content of `src` to `self`
2209 #
2210 # NOTE: `self` must be large enough to withold `self.byte_length` bytes
2211 fun fill_from(src: Text) do src.copy_to_native(self, src.byte_length, 0, 0)
2212 end
2213
2214 redef class NativeArray[E]
2215 # Join all the elements using `to_s`
2216 #
2217 # REQUIRE: `self isa NativeArray[String]`
2218 # REQUIRE: all elements are initialized
2219 fun native_to_s: String is abstract
2220 end