ecf7cf1f544ef6de39a7f6ea8b0f315f8f219ee9
[nit.git] / lib / standard / string.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
5 #
6 # This file is free software, which comes along with NIT. This software is
7 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
10 # is kept unaltered, and a notification of the changes is added.
11 # You are allowed to redistribute it and sell it, alone or is a part of
12 # another product.
13
14 # Basic manipulations of strings of characters
15 module string
16
17 import math
18 intrude import collection # FIXME should be collection::array
19
20 `{
21 #include <stdio.h>
22 `}
23
24 ###############################################################################
25 # String #
26 ###############################################################################
27
28 # High-level abstraction for all text representations
29 abstract class Text
30 super Comparable
31 super StringCapable
32
33 redef type OTHER: Text
34
35 # Type of the view on self (.chars)
36 type SELFVIEW: StringCharView
37
38 # Type of self (used for factorization of several methods, ex : substring_from, empty...)
39 type SELFTYPE: Text
40
41 # Gets a view on the chars of the Text object
42 fun chars: SELFVIEW is abstract
43
44 # Number of characters contained in self.
45 fun length: Int is abstract
46
47 # Create a substring.
48 #
49 # assert "abcd".substring(1, 2) == "bc"
50 # assert "abcd".substring(-1, 2) == "a"
51 # assert "abcd".substring(1, 0) == ""
52 # assert "abcd".substring(2, 5) == "cd"
53 #
54 # A `from` index < 0 will be replaced by 0.
55 # Unless a `count` value is > 0 at the same time.
56 # In this case, `from += count` and `count -= from`.
57 fun substring(from: Int, count: Int): SELFTYPE is abstract
58
59 # Concatenates `o` to `self`
60 fun +(o: Text): SELFTYPE is abstract
61
62 # Auto-concatenates self `i` times
63 fun *(i: Int): SELFTYPE is abstract
64
65 # Is the current Text empty (== "")
66 # assert "".is_empty
67 # assert not "foo".is_empty
68 fun is_empty: Bool do return self.length == 0
69
70 # Returns an empty Text of the right type
71 fun empty: SELFTYPE is abstract
72
73 # Gets the first char of the Text
74 #
75 # DEPRECATED : Use self.chars.first instead
76 fun first: Char do return self.chars[0]
77
78 # Access a character at `index` in the string.
79 #
80 # assert "abcd"[2] == 'c'
81 #
82 # DEPRECATED : Use self.chars.[] instead
83 fun [](index: Int): Char do return self.chars[index]
84
85 # Gets the index of the first occurence of 'c'
86 #
87 # Returns -1 if not found
88 #
89 # DEPRECATED : Use self.chars.index_of instead
90 fun index_of(c: Char): Int
91 do
92 return index_of_from(c, 0)
93 end
94
95 # Gets the last char of self
96 #
97 # DEPRECATED : Use self.chars.last instead
98 fun last: Char do return self.chars[length-1]
99
100 # Gets the index of the first occurence of ´c´ starting from ´pos´
101 #
102 # Returns -1 if not found
103 #
104 # DEPRECATED : Use self.chars.index_of_from instead
105 fun index_of_from(c: Char, pos: Int): Int
106 do
107 var iter = self.chars.iterator_from(pos)
108 while iter.is_ok do
109 if iter.item == c then return iter.index
110 end
111 return -1
112 end
113
114 # Gets the last index of char ´c´
115 #
116 # Returns -1 if not found
117 #
118 # DEPRECATED : Use self.chars.last_index_of instead
119 fun last_index_of(c: Char): Int
120 do
121 return last_index_of_from(c, length - 1)
122 end
123
124 # Return a null terminated char *
125 fun to_cstring: NativeString do return flatten.to_cstring
126
127 # The index of the last occurrence of an element starting from pos (in reverse order).
128 # Example :
129 # assert "/etc/bin/test/test.nit".last_index_of_from('/', length-1) == 13
130 # assert "/etc/bin/test/test.nit".last_index_of_from('/', 12) == 8
131 #
132 # Returns -1 if not found
133 #
134 # DEPRECATED : Use self.chars.last_index_of_from instead
135 fun last_index_of_from(item: Char, pos: Int): Int
136 do
137 var iter = self.chars.reverse_iterator_from(pos)
138 while iter.is_ok do
139 if iter.item == item then return iter.index
140 iter.next
141 end
142 return -1
143 end
144
145 # Gets an iterator on the chars of self
146 #
147 # DEPRECATED : Use self.chars.iterator instead
148 fun iterator: Iterator[Char]
149 do
150 return self.chars.iterator
151 end
152
153 # Is 'c' contained in self ?
154 #
155 # DEPRECATED : Use self.chars.has instead
156 fun has(c: Char): Bool
157 do
158 return self.chars.has(c)
159 end
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 # Returns a reversed version of self
181 fun reversed: SELFTYPE is abstract
182
183 # Does self have a substring `str` starting from position `pos`?
184 #
185 # assert "abcd".has_substring("bc",1) == true
186 # assert "abcd".has_substring("bc",2) == false
187 fun has_substring(str: String, pos: Int): Bool
188 do
189 var myiter = self.chars.iterator_from(pos)
190 var itsiter = str.iterator
191 while myiter.is_ok and itsiter.is_ok do
192 if myiter.item != itsiter.item then return false
193 myiter.next
194 itsiter.next
195 end
196 if itsiter.is_ok then return false
197 return true
198 end
199
200 # Is this string prefixed by `prefix`?
201 #
202 # assert "abcd".has_prefix("ab") == true
203 # assert "abcbc".has_prefix("bc") == false
204 # assert "ab".has_prefix("abcd") == false
205 fun has_prefix(prefix: String): Bool do return has_substring(prefix,0)
206
207 # Is this string suffixed by `suffix`?
208 #
209 # assert "abcd".has_suffix("abc") == false
210 # assert "abcd".has_suffix("bcd") == true
211 fun has_suffix(suffix: String): Bool do return has_substring(suffix, length - suffix.length)
212
213 # If `self` contains only digits, return the corresponding integer
214 #
215 # assert "123".to_i == 123
216 # assert "-1".to_i == -1
217 fun to_i: Int
218 do
219 # Shortcut
220 return to_s.to_cstring.atoi
221 end
222
223 # If `self` contains a float, return the corresponding float
224 #
225 # assert "123".to_f == 123.0
226 # assert "-1".to_f == -1.0
227 # assert "-1.2e-3".to_f == -0.0012
228 fun to_f: Float
229 do
230 # Shortcut
231 return to_s.to_cstring.atof
232 end
233
234 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
235 fun to_hex: Int do return a_to(16)
236
237 # If `self` contains only digits and letters, return the corresponding integer in a given base
238 #
239 # assert "120".a_to(3) == 15
240 fun a_to(base: Int) : Int
241 do
242 var i = 0
243 var neg = false
244
245 for c in self.chars
246 do
247 var v = c.to_i
248 if v > base then
249 if neg then
250 return -i
251 else
252 return i
253 end
254 else if v < 0 then
255 neg = true
256 else
257 i = i * base + v
258 end
259 end
260 if neg then
261 return -i
262 else
263 return i
264 end
265 end
266
267 # Returns `true` if the string contains only Numeric values (and one "," or one "." character)
268 #
269 # assert "123".is_numeric == true
270 # assert "1.2".is_numeric == true
271 # assert "1,2".is_numeric == true
272 # assert "1..2".is_numeric == false
273 fun is_numeric: Bool
274 do
275 var has_point_or_comma = false
276 for i in self.chars
277 do
278 if not i.is_numeric
279 then
280 if (i == '.' or i == ',') and not has_point_or_comma
281 then
282 has_point_or_comma = true
283 else
284 return false
285 end
286 end
287 end
288 return true
289 end
290
291 # A upper case version of `self`
292 #
293 # assert "Hello World!".to_upper == "HELLO WORLD!"
294 fun to_upper: SELFTYPE is abstract
295
296 # A lower case version of `self`
297 #
298 # assert "Hello World!".to_lower == "hello world!"
299 fun to_lower : SELFTYPE is abstract
300
301 # Removes the whitespaces at the beginning of self
302 fun l_trim: SELFTYPE
303 do
304 var iter = self.chars.iterator
305 while iter.is_ok do
306 if iter.item.ascii > 32 then break
307 iter.next
308 end
309 if iter.index == length then return self.empty
310 return self.substring_from(iter.index)
311 end
312
313 # Removes the whitespaces at the end of self
314 fun r_trim: SELFTYPE
315 do
316 var iter = self.chars.reverse_iterator
317 while iter.is_ok do
318 if iter.item.ascii > 32 then break
319 iter.next
320 end
321 if iter.index == length then return self.empty
322 return self.substring(0, iter.index + 1)
323 end
324
325 # Trims trailing and preceding white spaces
326 # A whitespace is defined as any character which ascii value is less than or equal to 32
327 #
328 # assert " Hello World ! ".trim == "Hello World !"
329 # assert "\na\nb\tc\t".trim == "a\nb\tc"
330 fun trim: SELFTYPE do return (self.l_trim).r_trim
331
332 # Mangle a string to be a unique string only made of alphanumeric characters
333 fun to_cmangle: String
334 do
335 var res = new FlatBuffer
336 var underscore = false
337 for c in self.chars do
338 if (c >= 'a' and c <= 'z') or (c >='A' and c <= 'Z') then
339 res.add(c)
340 underscore = false
341 continue
342 end
343 if underscore then
344 res.append('_'.ascii.to_s)
345 res.add('d')
346 end
347 if c >= '0' and c <= '9' then
348 res.add(c)
349 underscore = false
350 else if c == '_' then
351 res.add(c)
352 underscore = true
353 else
354 res.add('_')
355 res.append(c.ascii.to_s)
356 res.add('d')
357 underscore = false
358 end
359 end
360 return res.to_s
361 end
362
363 # Escape " \ ' and non printable characters using the rules of literal C strings and characters
364 #
365 # assert "abAB12<>&".escape_to_c == "abAB12<>&"
366 # assert "\n\"'\\".escape_to_c == "\\n\\\"\\'\\\\"
367 fun escape_to_c: String
368 do
369 var b = new FlatBuffer
370 for c in self.chars do
371 if c == '\n' then
372 b.append("\\n")
373 else if c == '\0' then
374 b.append("\\0")
375 else if c == '"' then
376 b.append("\\\"")
377 else if c == '\'' then
378 b.append("\\\'")
379 else if c == '\\' then
380 b.append("\\\\")
381 else if c.ascii < 32 then
382 b.append("\\{c.ascii.to_base(8, false)}")
383 else
384 b.add(c)
385 end
386 end
387 return b.to_s
388 end
389
390 # Escape additionnal characters
391 # The result might no be legal in C but be used in other languages
392 #
393 # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
394 fun escape_more_to_c(chars: String): String
395 do
396 var b = new FlatBuffer
397 for c in escape_to_c do
398 if chars.chars.has(c) then
399 b.add('\\')
400 end
401 b.add(c)
402 end
403 return b.to_s
404 end
405
406 # Escape to c plus braces
407 #
408 # assert "\n\"'\\\{\}".escape_to_nit == "\\n\\\"\\'\\\\\\\{\\\}"
409 fun escape_to_nit: String do return escape_more_to_c("\{\}")
410
411 # Return a string where Nit escape sequences are transformed.
412 #
413 # Example:
414 # var s = "\\n"
415 # assert s.length == 2
416 # var u = s.unescape_nit
417 # assert u.length == 1
418 # assert u[0].ascii == 10 # (the ASCII value of the "new line" character)
419 fun unescape_nit: String
420 do
421 var res = new FlatBuffer.with_capacity(self.length)
422 var was_slash = false
423 for c in self do
424 if not was_slash then
425 if c == '\\' then
426 was_slash = true
427 else
428 res.add(c)
429 end
430 continue
431 end
432 was_slash = false
433 if c == 'n' then
434 res.add('\n')
435 else if c == 'r' then
436 res.add('\r')
437 else if c == 't' then
438 res.add('\t')
439 else if c == '0' then
440 res.add('\0')
441 else
442 res.add(c)
443 end
444 end
445 return res.to_s
446 end
447
448 redef fun ==(o)
449 do
450 if o == null then return false
451 if not o isa Text then return false
452 if self.is_same_instance(o) then return true
453 if self.length != o.length then return false
454 return self.chars == o.chars
455 end
456
457 redef fun <(o)
458 do
459 return self.chars < o.chars
460 end
461
462 # Flat representation of self
463 fun flatten: FlatText is abstract
464
465 end
466
467 # All kinds of array-based text representations.
468 abstract class FlatText
469 super Text
470
471 private var items: NativeString
472
473 # Real items, used as cache for to_cstring is called
474 private var real_items: nullable NativeString = null
475
476 redef var length: Int
477
478 init do end
479
480 redef fun output
481 do
482 var i = 0
483 while i < length do
484 items[i].output
485 i += 1
486 end
487 end
488
489 redef fun flatten do return self
490 end
491
492 # Abstract class for the SequenceRead compatible
493 # views on String and Buffer objects
494 abstract class StringCharView
495 super SequenceRead[Char]
496 super Comparable
497
498 type SELFTYPE: Text
499
500 redef type OTHER: StringCharView
501
502 private var target: SELFTYPE
503
504 private init(tgt: SELFTYPE)
505 do
506 target = tgt
507 end
508
509 redef fun is_empty do return target.is_empty
510
511 redef fun length do return target.length
512
513 redef fun iterator: IndexedIterator[Char] do return self.iterator_from(0)
514
515 # Gets a new Iterator starting at position `pos`
516 #
517 # Ex :
518 # var iter = "abcd".iterator_from(2)
519 # while iter.is_ok do
520 # printn iter.item
521 # iter.next
522 # end
523 #
524 # Outputs : cd
525 fun iterator_from(pos: Int): IndexedIterator[Char] is abstract
526
527 # Gets an iterator starting at the end and going backwards
528 #
529 # Ex :
530 # var reviter = "now step live...".reverse_iterator
531 # while reviter.is_ok do
532 # printn reviter.item
533 # reviter.next
534 # end
535 #
536 # Outputs : ...evil pets won
537 fun reverse_iterator: IndexedIterator[Char] do return self.reverse_iterator_from(self.length - 1)
538
539 # Gets an iterator on the chars of self starting from `pos`
540 #
541 # Ex :
542 # var iter = "abcd".reverse_iterator_from(1)
543 # while iter.is_ok do
544 # printn iter.item
545 # iter.next
546 # end
547 #
548 # Outputs : ba
549 fun reverse_iterator_from(pos: Int): IndexedIterator[Char] is abstract
550
551 redef fun has(c: Char): Bool
552 do
553 for i in self do
554 if i == c then return true
555 end
556 return false
557 end
558
559 redef fun ==(other)
560 do
561 if other == null then return false
562 if not other isa StringCharView then return false
563 var other_chars = other.iterator
564 for i in self do
565 if i != other_chars.item then return false
566 other_chars.next
567 end
568 return true
569 end
570
571 redef fun <(other)
572 do
573 var self_chars = self.iterator
574 var other_chars = other.iterator
575
576 while self_chars.is_ok and other_chars.is_ok do
577 if self_chars.item < other_chars.item then return true
578 if self_chars.item > other_chars.item then return false
579 self_chars.next
580 other_chars.next
581 end
582
583 if self_chars.is_ok then
584 return false
585 else
586 return true
587 end
588 end
589 end
590
591 # View on Buffer objects, extends Sequence
592 # for mutation operations
593 abstract class BufferCharView
594 super StringCharView
595 super Sequence[Char]
596
597 redef type SELFTYPE: Buffer
598
599 end
600
601 abstract class String
602 super Text
603
604 redef type SELFTYPE: String
605
606 redef fun to_s do return self
607
608 end
609
610 # Immutable strings of characters.
611 class FlatString
612 super FlatText
613 super String
614
615 redef type SELFTYPE: FlatString
616 redef type SELFVIEW: FlatStringCharView
617
618 # Index in _items of the start of the string
619 private var index_from: Int
620
621 # Indes in _items of the last item of the string
622 private var index_to: Int
623
624 redef var chars: SELFVIEW = new FlatStringCharView(self)
625
626 ################################################
627 # AbstractString specific methods #
628 ################################################
629
630 redef fun [](index) do
631 assert index >= 0
632 # Check that the index (+ index_from) is not larger than indexTo
633 # In other terms, if the index is valid
634 assert (index + index_from) <= index_to
635 return items[index + index_from]
636 end
637
638 redef fun reversed
639 do
640 var native = calloc_string(self.length + 1)
641 var reviter = chars.reverse_iterator
642 var pos = 0
643 while reviter.is_ok do
644 native[pos] = reviter.item
645 pos += 1
646 reviter.next
647 end
648 return native.to_s_with_length(self.length)
649 end
650
651 redef fun substring(from, count)
652 do
653 assert count >= 0
654
655 if from < 0 then
656 count += from
657 if count < 0 then count = 0
658 from = 0
659 end
660
661 var realFrom = index_from + from
662
663 if (realFrom + count) > index_to then return new FlatString.with_infos(items, index_to - realFrom + 1, realFrom, index_to)
664
665 if count == 0 then return empty
666
667 var to = realFrom + count - 1
668
669 return new FlatString.with_infos(items, to - realFrom + 1, realFrom, to)
670 end
671
672 redef fun empty do return "".as(FlatString)
673
674 redef fun to_upper
675 do
676 var outstr = calloc_string(self.length + 1)
677 var out_index = 0
678
679 var myitems = self.items
680 var index_from = self.index_from
681 var max = self.index_to
682
683 while index_from <= max do
684 outstr[out_index] = myitems[index_from].to_upper
685 out_index += 1
686 index_from += 1
687 end
688
689 outstr[self.length] = '\0'
690
691 return outstr.to_s_with_length(self.length)
692 end
693
694 redef fun to_lower
695 do
696 var outstr = calloc_string(self.length + 1)
697 var out_index = 0
698
699 var myitems = self.items
700 var index_from = self.index_from
701 var max = self.index_to
702
703 while index_from <= max do
704 outstr[out_index] = myitems[index_from].to_lower
705 out_index += 1
706 index_from += 1
707 end
708
709 outstr[self.length] = '\0'
710
711 return outstr.to_s_with_length(self.length)
712 end
713
714 redef fun output
715 do
716 var i = self.index_from
717 var imax = self.index_to
718 while i <= imax do
719 items[i].output
720 i += 1
721 end
722 end
723
724 ##################################################
725 # String Specific Methods #
726 ##################################################
727
728 private init with_infos(items: NativeString, len: Int, from: Int, to: Int)
729 do
730 self.items = items
731 length = len
732 index_from = from
733 index_to = to
734 end
735
736 # Return a null terminated char *
737 redef fun to_cstring: NativeString
738 do
739 if real_items != null then return real_items.as(not null)
740 if index_from > 0 or index_to != items.cstring_length - 1 then
741 var newItems = calloc_string(length + 1)
742 self.items.copy_to(newItems, length, index_from, 0)
743 newItems[length] = '\0'
744 self.real_items = newItems
745 return newItems
746 end
747 return items
748 end
749
750 redef fun ==(other)
751 do
752 if not other isa FlatString then return super
753
754 if self.object_id == other.object_id then return true
755
756 var my_length = length
757
758 if other.length != my_length then return false
759
760 var my_index = index_from
761 var its_index = other.index_from
762
763 var last_iteration = my_index + my_length
764
765 var itsitems = other.items
766 var myitems = self.items
767
768 while my_index < last_iteration do
769 if myitems[my_index] != itsitems[its_index] then return false
770 my_index += 1
771 its_index += 1
772 end
773
774 return true
775 end
776
777 # The comparison between two strings is done on a lexicographical basis
778 #
779 # assert ("aa" < "b") == true
780 redef fun <(other)
781 do
782 if not other isa FlatString then return super
783
784 if self.object_id == other.object_id then return false
785
786 var my_curr_char : Char
787 var its_curr_char : Char
788
789 var curr_id_self = self.index_from
790 var curr_id_other = other.index_from
791
792 var my_items = self.items
793 var its_items = other.items
794
795 var my_length = self.length
796 var its_length = other.length
797
798 var max_iterations = curr_id_self + my_length
799
800 while curr_id_self < max_iterations do
801 my_curr_char = my_items[curr_id_self]
802 its_curr_char = its_items[curr_id_other]
803
804 if my_curr_char != its_curr_char then
805 if my_curr_char < its_curr_char then return true
806 return false
807 end
808
809 curr_id_self += 1
810 curr_id_other += 1
811 end
812
813 return my_length < its_length
814 end
815
816 # The concatenation of `self` with `s`
817 #
818 # assert "hello " + "world!" == "hello world!"
819 redef fun +(s)
820 do
821 var my_length = self.length
822 var its_length = s.length
823
824 var total_length = my_length + its_length
825
826 var target_string = calloc_string(my_length + its_length + 1)
827
828 self.items.copy_to(target_string, my_length, index_from, 0)
829 if s isa FlatString then
830 s.items.copy_to(target_string, its_length, s.index_from, my_length)
831 else if s isa FlatBuffer then
832 s.items.copy_to(target_string, its_length, 0, my_length)
833 else
834 var curr_pos = my_length
835 for i in s.chars do
836 target_string[curr_pos] = i
837 curr_pos += 1
838 end
839 end
840
841 target_string[total_length] = '\0'
842
843 return target_string.to_s_with_length(total_length)
844 end
845
846 # assert "abc"*3 == "abcabcabc"
847 # assert "abc"*1 == "abc"
848 # assert "abc"*0 == ""
849 redef fun *(i)
850 do
851 assert i >= 0
852
853 var my_length = self.length
854
855 var final_length = my_length * i
856
857 var my_items = self.items
858
859 var target_string = calloc_string((final_length) + 1)
860
861 target_string[final_length] = '\0'
862
863 var current_last = 0
864
865 for iteration in [1 .. i] do
866 my_items.copy_to(target_string, my_length, 0, current_last)
867 current_last += my_length
868 end
869
870 return target_string.to_s_with_length(final_length)
871 end
872
873 redef fun hash
874 do
875 # djb2 hash algorythm
876 var h = 5381
877 var i = length - 1
878
879 var myitems = items
880 var strStart = index_from
881
882 i += strStart
883
884 while i >= strStart do
885 h = (h * 32) + h + self.items[i].ascii
886 i -= 1
887 end
888
889 return h
890 end
891 end
892
893 private class FlatStringReverseIterator
894 super IndexedIterator[Char]
895
896 var target: FlatString
897
898 var target_items: NativeString
899
900 var curr_pos: Int
901
902 init with_pos(tgt: FlatString, pos: Int)
903 do
904 target = tgt
905 target_items = tgt.items
906 curr_pos = pos + tgt.index_from
907 end
908
909 redef fun is_ok do return curr_pos >= 0
910
911 redef fun item do return target_items[curr_pos]
912
913 redef fun next do curr_pos -= 1
914
915 redef fun index do return curr_pos - target.index_from
916
917 end
918
919 private class FlatStringIterator
920 super IndexedIterator[Char]
921
922 var target: FlatString
923
924 var target_items: NativeString
925
926 var curr_pos: Int
927
928 init with_pos(tgt: FlatString, pos: Int)
929 do
930 target = tgt
931 target_items = tgt.items
932 curr_pos = pos + target.index_from
933 end
934
935 redef fun is_ok do return curr_pos <= target.index_to
936
937 redef fun item do return target_items[curr_pos]
938
939 redef fun next do curr_pos += 1
940
941 redef fun index do return curr_pos - target.index_from
942
943 end
944
945 private class FlatStringCharView
946 super StringCharView
947
948 redef type SELFTYPE: FlatString
949
950 redef fun [](index)
951 do
952 # Check that the index (+ index_from) is not larger than indexTo
953 # In other terms, if the index is valid
954 assert index >= 0
955 assert (index + target.index_from) <= target.index_to
956 return target.items[index + target.index_from]
957 end
958
959 redef fun iterator_from(start) do return new FlatStringIterator.with_pos(target, start)
960
961 redef fun reverse_iterator_from(start) do return new FlatStringReverseIterator.with_pos(target, start)
962
963 end
964
965 abstract class Buffer
966 super Text
967
968 redef type SELFVIEW: BufferCharView
969 redef type SELFTYPE: Buffer
970
971 # Modifies the char contained at pos `index`
972 #
973 # DEPRECATED : Use self.chars.[]= instead
974 fun []=(index: Int, item: Char) is abstract
975
976 # Adds a char `c` at the end of self
977 #
978 # DEPRECATED : Use self.chars.add instead
979 fun add(c: Char) is abstract
980
981 # Clears the buffer
982 fun clear is abstract
983
984 # Enlarges the subsequent array containing the chars of self
985 fun enlarge(cap: Int) is abstract
986
987 # Adds the content of text `s` at the end of self
988 fun append(s: Text) is abstract
989
990 end
991
992 # Mutable strings of characters.
993 class FlatBuffer
994 super FlatText
995 super Buffer
996
997 redef type SELFVIEW: FlatBufferCharView
998 redef type SELFTYPE: FlatBuffer
999
1000 redef var chars: SELFVIEW = new FlatBufferCharView(self)
1001
1002 var capacity: Int
1003
1004 redef fun []=(index, item)
1005 do
1006 if index == length then
1007 add(item)
1008 return
1009 end
1010 assert index >= 0 and index < length
1011 items[index] = item
1012 end
1013
1014 redef fun add(c)
1015 do
1016 if capacity <= length then enlarge(length + 5)
1017 items[length] = c
1018 length += 1
1019 end
1020
1021 redef fun clear do length = 0
1022
1023 redef fun empty do return new FlatBuffer
1024
1025 redef fun enlarge(cap)
1026 do
1027 var c = capacity
1028 if cap <= c then return
1029 while c <= cap do c = c * 2 + 2
1030 var a = calloc_string(c+1)
1031 items.copy_to(a, length, 0, 0)
1032 items = a
1033 capacity = c
1034 items.copy_to(a, length, 0, 0)
1035 end
1036
1037 redef fun to_s: String
1038 do
1039 var l = length
1040 var a = calloc_string(l+1)
1041 items.copy_to(a, l, 0, 0)
1042
1043 # Ensure the afterlast byte is '\0' to nul-terminated char *
1044 a[length] = '\0'
1045
1046 return a.to_s_with_length(length)
1047 end
1048
1049 redef fun to_cstring
1050 do
1051 if real_items == null then
1052 var new_native = calloc_string(length + 1)
1053 new_native[length] = '\0'
1054 items.copy_to(new_native, length, 0, 0)
1055 real_items = new_native
1056 end
1057 return real_items.as(not null)
1058 end
1059
1060 # Create a new empty string.
1061 init
1062 do
1063 with_capacity(5)
1064 end
1065
1066 init from(s: Text)
1067 do
1068 capacity = s.length + 1
1069 length = s.length
1070 items = calloc_string(capacity)
1071 if s isa FlatString then
1072 s.items.copy_to(items, length, s.index_from, 0)
1073 else if s isa FlatBuffer then
1074 s.items.copy_to(items, length, 0, 0)
1075 else
1076 var curr_pos = 0
1077 for i in s.chars do
1078 items[curr_pos] = i
1079 curr_pos += 1
1080 end
1081 end
1082 end
1083
1084 # Create a new empty string with a given capacity.
1085 init with_capacity(cap: Int)
1086 do
1087 assert cap >= 0
1088 # _items = new NativeString.calloc(cap)
1089 items = calloc_string(cap+1)
1090 capacity = cap
1091 length = 0
1092 end
1093
1094 redef fun append(s)
1095 do
1096 var sl = s.length
1097 if capacity < length + sl then enlarge(length + sl)
1098 if s isa FlatString then
1099 s.items.copy_to(items, sl, s.index_from, length)
1100 else if s isa FlatBuffer then
1101 s.items.copy_to(items, sl, 0, length)
1102 else
1103 var curr_pos = self.length
1104 for i in s.chars do
1105 items[curr_pos] = i
1106 curr_pos += 1
1107 end
1108 end
1109 length += sl
1110 end
1111
1112 # Copies the content of self in `dest`
1113 fun copy(start: Int, len: Int, dest: Buffer, new_start: Int)
1114 do
1115 var self_chars = self.chars
1116 var dest_chars = dest.chars
1117 for i in [0..len-1] do
1118 dest_chars[new_start+i] = self_chars[start+i]
1119 end
1120 end
1121
1122 redef fun substring(from, count)
1123 do
1124 assert count >= 0
1125 count += from
1126 if from < 0 then from = 0
1127 if count > length then count = length
1128 if from < count then
1129 var r = new FlatBuffer.with_capacity(count - from)
1130 while from < count do
1131 r.chars.push(items[from])
1132 from += 1
1133 end
1134 return r
1135 else
1136 return new FlatBuffer
1137 end
1138 end
1139
1140 redef fun reversed
1141 do
1142 var new_buf = new FlatBuffer.with_capacity(self.length)
1143 var reviter = self.chars.reverse_iterator
1144 while reviter.is_ok do
1145 new_buf.add(reviter.item)
1146 reviter.next
1147 end
1148 return new_buf
1149 end
1150
1151 redef fun +(other)
1152 do
1153 var new_buf = new FlatBuffer.with_capacity(self.length + other.length)
1154 new_buf.append(self)
1155 new_buf.append(other)
1156 return new_buf
1157 end
1158
1159 redef fun *(repeats)
1160 do
1161 var new_buf = new FlatBuffer.with_capacity(self.length * repeats)
1162 for i in [0..repeats[ do
1163 new_buf.append(self)
1164 end
1165 return new_buf
1166 end
1167 end
1168
1169 private class FlatBufferReverseIterator
1170 super IndexedIterator[Char]
1171
1172 var target: FlatBuffer
1173
1174 var target_items: NativeString
1175
1176 var curr_pos: Int
1177
1178 init with_pos(tgt: FlatBuffer, pos: Int)
1179 do
1180 target = tgt
1181 target_items = tgt.items
1182 curr_pos = pos
1183 end
1184
1185 redef fun index do return curr_pos
1186
1187 redef fun is_ok do return curr_pos >= 0
1188
1189 redef fun item do return target_items[curr_pos]
1190
1191 redef fun next do curr_pos -= 1
1192
1193 end
1194
1195 private class FlatBufferCharView
1196 super BufferCharView
1197 super StringCapable
1198
1199 redef type SELFTYPE: FlatBuffer
1200
1201 redef fun [](index) do return target.items[index]
1202
1203 redef fun []=(index, item)
1204 do
1205 assert index >= 0 and index <= length
1206 if index == length then
1207 add(item)
1208 return
1209 end
1210 target.items[index] = item
1211 end
1212
1213 redef fun push(c)
1214 do
1215 target.add(c)
1216 end
1217
1218 redef fun add(c)
1219 do
1220 target.add(c)
1221 end
1222
1223 fun enlarge(cap: Int)
1224 do
1225 target.enlarge(cap)
1226 end
1227
1228 redef fun append(s)
1229 do
1230 var my_items = target.items
1231 var s_length = s.length
1232 if target.capacity < s.length then enlarge(s_length + target.length)
1233 end
1234
1235 redef fun iterator_from(pos) do return new FlatBufferIterator.with_pos(target, pos)
1236
1237 redef fun reverse_iterator_from(pos) do return new FlatBufferReverseIterator.with_pos(target, pos)
1238
1239 end
1240
1241 private class FlatBufferIterator
1242 super IndexedIterator[Char]
1243
1244 var target: FlatBuffer
1245
1246 var target_items: NativeString
1247
1248 var curr_pos: Int
1249
1250 init with_pos(tgt: FlatBuffer, pos: Int)
1251 do
1252 target = tgt
1253 target_items = tgt.items
1254 curr_pos = pos
1255 end
1256
1257 redef fun index do return curr_pos
1258
1259 redef fun is_ok do return curr_pos < target.length
1260
1261 redef fun item do return target_items[curr_pos]
1262
1263 redef fun next do curr_pos += 1
1264
1265 end
1266
1267 ###############################################################################
1268 # Refinement #
1269 ###############################################################################
1270
1271 redef class Object
1272 # User readable representation of `self`.
1273 fun to_s: String do return inspect
1274
1275 # The class name of the object in NativeString format.
1276 private fun native_class_name: NativeString is intern
1277
1278 # The class name of the object.
1279 #
1280 # assert 5.class_name == "Int"
1281 fun class_name: String do return native_class_name.to_s
1282
1283 # Developer readable representation of `self`.
1284 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
1285 fun inspect: String
1286 do
1287 return "<{inspect_head}>"
1288 end
1289
1290 # Return "CLASSNAME:#OBJECTID".
1291 # This function is mainly used with the redefinition of the inspect method
1292 protected fun inspect_head: String
1293 do
1294 return "{class_name}:#{object_id.to_hex}"
1295 end
1296
1297 protected fun args: Sequence[String]
1298 do
1299 return sys.args
1300 end
1301 end
1302
1303 redef class Bool
1304 # assert true.to_s == "true"
1305 # assert false.to_s == "false"
1306 redef fun to_s
1307 do
1308 if self then
1309 return once "true"
1310 else
1311 return once "false"
1312 end
1313 end
1314 end
1315
1316 redef class Int
1317 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
1318 # assume < to_c max const of char
1319 fun fill_buffer(s: Buffer, base: Int, signed: Bool)
1320 do
1321 var n: Int
1322 # Sign
1323 if self < 0 then
1324 n = - self
1325 s.chars[0] = '-'
1326 else if self == 0 then
1327 s.chars[0] = '0'
1328 return
1329 else
1330 n = self
1331 end
1332 # Fill digits
1333 var pos = digit_count(base) - 1
1334 while pos >= 0 and n > 0 do
1335 s.chars[pos] = (n % base).to_c
1336 n = n / base # /
1337 pos -= 1
1338 end
1339 end
1340
1341 # C function to convert an nit Int to a NativeString (char*)
1342 private fun native_int_to_s(len: Int): NativeString is extern "native_int_to_s"
1343
1344 # return displayable int in base 10 and signed
1345 #
1346 # assert 1.to_s == "1"
1347 # assert (-123).to_s == "-123"
1348 redef fun to_s do
1349 var len = digit_count(10)
1350 return native_int_to_s(len).to_s_with_length(len)
1351 end
1352
1353 # return displayable int in hexadecimal (unsigned (not now))
1354 fun to_hex: String do return to_base(16,false)
1355
1356 # return displayable int in base base and signed
1357 fun to_base(base: Int, signed: Bool): String
1358 do
1359 var l = digit_count(base)
1360 var s = new FlatBuffer.from(" " * l)
1361 fill_buffer(s, base, signed)
1362 return s.to_s
1363 end
1364 end
1365
1366 redef class Float
1367 # Pretty print self, print needoed decimals up to a max of 3.
1368 redef fun to_s do
1369 var str = to_precision( 3 )
1370 if is_inf != 0 or is_nan then return str
1371 var len = str.length
1372 for i in [0..len-1] do
1373 var j = len-1-i
1374 var c = str.chars[j]
1375 if c == '0' then
1376 continue
1377 else if c == '.' then
1378 return str.substring( 0, j+2 )
1379 else
1380 return str.substring( 0, j+1 )
1381 end
1382 end
1383 return str
1384 end
1385
1386 # `self` representation with `nb` digits after the '.'.
1387 fun to_precision(nb: Int): String
1388 do
1389 if is_nan then return "nan"
1390
1391 var isinf = self.is_inf
1392 if isinf == 1 then
1393 return "inf"
1394 else if isinf == -1 then
1395 return "-inf"
1396 end
1397
1398 if nb == 0 then return self.to_i.to_s
1399 var f = self
1400 for i in [0..nb[ do f = f * 10.0
1401 if self > 0.0 then
1402 f = f + 0.5
1403 else
1404 f = f - 0.5
1405 end
1406 var i = f.to_i
1407 if i == 0 then return "0.0"
1408 var s = i.to_s
1409 var sl = s.length
1410 if sl > nb then
1411 var p1 = s.substring(0, s.length-nb)
1412 var p2 = s.substring(s.length-nb, nb)
1413 return p1 + "." + p2
1414 else
1415 return "0." + ("0"*(nb-sl)) + s
1416 end
1417 end
1418
1419 fun to_precision_native(nb: Int): String import NativeString.to_s `{
1420 int size;
1421 char *str;
1422
1423 size = snprintf(NULL, 0, "%.*f", (int)nb, recv);
1424 str = malloc(size + 1);
1425 sprintf(str, "%.*f", (int)nb, recv );
1426
1427 return NativeString_to_s( str );
1428 `}
1429 end
1430
1431 redef class Char
1432 # assert 'x'.to_s == "x"
1433 redef fun to_s
1434 do
1435 var s = new FlatBuffer.with_capacity(1)
1436 s.chars[0] = self
1437 return s.to_s
1438 end
1439
1440 # Returns true if the char is a numerical digit
1441 fun is_numeric: Bool
1442 do
1443 if self >= '0' and self <= '9'
1444 then
1445 return true
1446 end
1447 return false
1448 end
1449
1450 # Returns true if the char is an alpha digit
1451 fun is_alpha: Bool
1452 do
1453 if (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z') then return true
1454 return false
1455 end
1456
1457 # Returns true if the char is an alpha or a numeric digit
1458 fun is_alphanumeric: Bool
1459 do
1460 if self.is_numeric or self.is_alpha then return true
1461 return false
1462 end
1463 end
1464
1465 redef class Collection[E]
1466 # Concatenate elements.
1467 redef fun to_s
1468 do
1469 var s = new FlatBuffer
1470 for e in self do if e != null then s.append(e.to_s)
1471 return s.to_s
1472 end
1473
1474 # Concatenate and separate each elements with `sep`.
1475 #
1476 # assert [1, 2, 3].join(":") == "1:2:3"
1477 # assert [1..3].join(":") == "1:2:3"
1478 fun join(sep: String): String
1479 do
1480 if is_empty then return ""
1481
1482 var s = new FlatBuffer # Result
1483
1484 # Concat first item
1485 var i = iterator
1486 var e = i.item
1487 if e != null then s.append(e.to_s)
1488
1489 # Concat other items
1490 i.next
1491 while i.is_ok do
1492 s.append(sep)
1493 e = i.item
1494 if e != null then s.append(e.to_s)
1495 i.next
1496 end
1497 return s.to_s
1498 end
1499 end
1500
1501 redef class Array[E]
1502 # Fast implementation
1503 redef fun to_s
1504 do
1505 var s = new FlatBuffer
1506 var i = 0
1507 var l = length
1508 while i < l do
1509 var e = self[i]
1510 if e != null then s.append(e.to_s)
1511 i += 1
1512 end
1513 return s.to_s
1514 end
1515 end
1516
1517 redef class Map[K,V]
1518 # Concatenate couple of 'key value'.
1519 # key and value are separated by `couple_sep`.
1520 # each couple is separated each couple with `sep`.
1521 #
1522 # var m = new ArrayMap[Int, String]
1523 # m[1] = "one"
1524 # m[10] = "ten"
1525 # assert m.join("; ", "=") == "1=one; 10=ten"
1526 fun join(sep: String, couple_sep: String): String
1527 do
1528 if is_empty then return ""
1529
1530 var s = new FlatBuffer # Result
1531
1532 # Concat first item
1533 var i = iterator
1534 var k = i.key
1535 var e = i.item
1536 s.append("{k}{couple_sep}{e or else "<null>"}")
1537
1538 # Concat other items
1539 i.next
1540 while i.is_ok do
1541 s.append(sep)
1542 k = i.key
1543 e = i.item
1544 s.append("{k}{couple_sep}{e or else "<null>"}")
1545 i.next
1546 end
1547 return s.to_s
1548 end
1549 end
1550
1551 ###############################################################################
1552 # Native classes #
1553 ###############################################################################
1554
1555 # Native strings are simple C char *
1556 class NativeString
1557 super StringCapable
1558
1559 fun [](index: Int): Char is intern
1560 fun []=(index: Int, item: Char) is intern
1561 fun copy_to(dest: NativeString, length: Int, from: Int, to: Int) is intern
1562
1563 # Position of the first nul character.
1564 fun cstring_length: Int
1565 do
1566 var l = 0
1567 while self[l] != '\0' do l += 1
1568 return l
1569 end
1570 fun atoi: Int is intern
1571 fun atof: Float is extern "atof"
1572
1573 redef fun to_s
1574 do
1575 return to_s_with_length(cstring_length)
1576 end
1577
1578 fun to_s_with_length(length: Int): FlatString
1579 do
1580 assert length >= 0
1581 return new FlatString.with_infos(self, length, 0, length - 1)
1582 end
1583
1584 fun to_s_with_copy: FlatString
1585 do
1586 var length = cstring_length
1587 var new_self = calloc_string(length + 1)
1588 copy_to(new_self, length, 0, 0)
1589 return new FlatString.with_infos(new_self, length, 0, length - 1)
1590 end
1591
1592 end
1593
1594 # StringCapable objects can create native strings
1595 interface StringCapable
1596 protected fun calloc_string(size: Int): NativeString is intern
1597 end
1598
1599 redef class Sys
1600 var _args_cache: nullable Sequence[String]
1601
1602 redef fun args: Sequence[String]
1603 do
1604 if _args_cache == null then init_args
1605 return _args_cache.as(not null)
1606 end
1607
1608 # The name of the program as given by the OS
1609 fun program_name: String
1610 do
1611 return native_argv(0).to_s
1612 end
1613
1614 # Initialize `args` with the contents of `native_argc` and `native_argv`.
1615 private fun init_args
1616 do
1617 var argc = native_argc
1618 var args = new Array[String].with_capacity(0)
1619 var i = 1
1620 while i < argc do
1621 args[i-1] = native_argv(i).to_s
1622 i += 1
1623 end
1624 _args_cache = args
1625 end
1626
1627 # First argument of the main C function.
1628 private fun native_argc: Int is intern
1629
1630 # Second argument of the main C function.
1631 private fun native_argv(i: Int): NativeString is intern
1632 end
1633