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