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