638efff972e4b8cf7fe04c8dbe42ee9e15f2eb38
[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 end
1181
1182 private class FlatBufferReverseIterator
1183 super IndexedIterator[Char]
1184
1185 var target: FlatBuffer
1186
1187 var target_items: NativeString
1188
1189 var curr_pos: Int
1190
1191 init with_pos(tgt: FlatBuffer, pos: Int)
1192 do
1193 target = tgt
1194 target_items = tgt.items
1195 curr_pos = pos
1196 end
1197
1198 redef fun index do return curr_pos
1199
1200 redef fun is_ok do return curr_pos >= 0
1201
1202 redef fun item do return target_items[curr_pos]
1203
1204 redef fun next do curr_pos -= 1
1205
1206 end
1207
1208 private class FlatBufferCharView
1209 super BufferCharView
1210 super StringCapable
1211
1212 redef type SELFTYPE: FlatBuffer
1213
1214 redef fun [](index) do return target.items[index]
1215
1216 redef fun []=(index, item)
1217 do
1218 assert index >= 0 and index <= length
1219 if index == length then
1220 add(item)
1221 return
1222 end
1223 target.items[index] = item
1224 end
1225
1226 redef fun push(c)
1227 do
1228 target.add(c)
1229 end
1230
1231 redef fun add(c)
1232 do
1233 target.add(c)
1234 end
1235
1236 fun enlarge(cap: Int)
1237 do
1238 target.enlarge(cap)
1239 end
1240
1241 redef fun append(s)
1242 do
1243 var my_items = target.items
1244 var s_length = s.length
1245 if target.capacity < s.length then enlarge(s_length + target.length)
1246 end
1247
1248 redef fun iterator_from(pos) do return new FlatBufferIterator.with_pos(target, pos)
1249
1250 redef fun reverse_iterator_from(pos) do return new FlatBufferReverseIterator.with_pos(target, pos)
1251
1252 end
1253
1254 private class FlatBufferIterator
1255 super IndexedIterator[Char]
1256
1257 var target: FlatBuffer
1258
1259 var target_items: NativeString
1260
1261 var curr_pos: Int
1262
1263 init with_pos(tgt: FlatBuffer, pos: Int)
1264 do
1265 target = tgt
1266 target_items = tgt.items
1267 curr_pos = pos
1268 end
1269
1270 redef fun index do return curr_pos
1271
1272 redef fun is_ok do return curr_pos < target.length
1273
1274 redef fun item do return target_items[curr_pos]
1275
1276 redef fun next do curr_pos += 1
1277
1278 end
1279
1280 ###############################################################################
1281 # Refinement #
1282 ###############################################################################
1283
1284 redef class Object
1285 # User readable representation of `self`.
1286 fun to_s: String do return inspect
1287
1288 # The class name of the object in NativeString format.
1289 private fun native_class_name: NativeString is intern
1290
1291 # The class name of the object.
1292 #
1293 # assert 5.class_name == "Int"
1294 fun class_name: String do return native_class_name.to_s
1295
1296 # Developer readable representation of `self`.
1297 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
1298 fun inspect: String
1299 do
1300 return "<{inspect_head}>"
1301 end
1302
1303 # Return "CLASSNAME:#OBJECTID".
1304 # This function is mainly used with the redefinition of the inspect method
1305 protected fun inspect_head: String
1306 do
1307 return "{class_name}:#{object_id.to_hex}"
1308 end
1309
1310 protected fun args: Sequence[String]
1311 do
1312 return sys.args
1313 end
1314 end
1315
1316 redef class Bool
1317 # assert true.to_s == "true"
1318 # assert false.to_s == "false"
1319 redef fun to_s
1320 do
1321 if self then
1322 return once "true"
1323 else
1324 return once "false"
1325 end
1326 end
1327 end
1328
1329 redef class Int
1330
1331 # Wrapper of strerror C function
1332 private fun strerror_ext: NativeString is extern `{
1333 return strerror(recv);
1334 `}
1335
1336 # Returns a string describing error number
1337 fun strerror: String do return strerror_ext.to_s
1338
1339 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
1340 # assume < to_c max const of char
1341 private fun fill_buffer(s: Buffer, base: Int, signed: Bool)
1342 do
1343 var n: Int
1344 # Sign
1345 if self < 0 then
1346 n = - self
1347 s.chars[0] = '-'
1348 else if self == 0 then
1349 s.chars[0] = '0'
1350 return
1351 else
1352 n = self
1353 end
1354 # Fill digits
1355 var pos = digit_count(base) - 1
1356 while pos >= 0 and n > 0 do
1357 s.chars[pos] = (n % base).to_c
1358 n = n / base # /
1359 pos -= 1
1360 end
1361 end
1362
1363 # C function to convert an nit Int to a NativeString (char*)
1364 private fun native_int_to_s(len: Int): NativeString is extern "native_int_to_s"
1365
1366 # return displayable int in base 10 and signed
1367 #
1368 # assert 1.to_s == "1"
1369 # assert (-123).to_s == "-123"
1370 redef fun to_s do
1371 var len = digit_count(10)
1372 return native_int_to_s(len).to_s_with_length(len)
1373 end
1374
1375 # return displayable int in hexadecimal
1376 #
1377 # assert 1.to_hex == "1"
1378 # assert (-255).to_hex == "-ff"
1379 fun to_hex: String do return to_base(16,false)
1380
1381 # return displayable int in base base and signed
1382 fun to_base(base: Int, signed: Bool): String
1383 do
1384 var l = digit_count(base)
1385 var s = new FlatBuffer.from(" " * l)
1386 fill_buffer(s, base, signed)
1387 return s.to_s
1388 end
1389 end
1390
1391 redef class Float
1392 # Pretty print self, print needoed decimals up to a max of 3.
1393 #
1394 # assert 12.34.to_s == "12.34"
1395 # assert (-0120.03450).to_s == "-120.035"
1396 #
1397 # see `to_precision` for a different precision.
1398 redef fun to_s do
1399 var str = to_precision( 3 )
1400 if is_inf != 0 or is_nan then return str
1401 var len = str.length
1402 for i in [0..len-1] do
1403 var j = len-1-i
1404 var c = str.chars[j]
1405 if c == '0' then
1406 continue
1407 else if c == '.' then
1408 return str.substring( 0, j+2 )
1409 else
1410 return str.substring( 0, j+1 )
1411 end
1412 end
1413 return str
1414 end
1415
1416 # `self` representation with `nb` digits after the '.'.
1417 #
1418 # assert 12.345.to_precision(1) == "12.3"
1419 # assert 12.345.to_precision(2) == "12.35"
1420 # assert 12.345.to_precision(3) == "12.345"
1421 # assert 12.345.to_precision(4) == "12.3450"
1422 fun to_precision(nb: Int): String
1423 do
1424 if is_nan then return "nan"
1425
1426 var isinf = self.is_inf
1427 if isinf == 1 then
1428 return "inf"
1429 else if isinf == -1 then
1430 return "-inf"
1431 end
1432
1433 if nb == 0 then return self.to_i.to_s
1434 var f = self
1435 for i in [0..nb[ do f = f * 10.0
1436 if self > 0.0 then
1437 f = f + 0.5
1438 else
1439 f = f - 0.5
1440 end
1441 var i = f.to_i
1442 if i == 0 then return "0.0"
1443 var s = i.to_s
1444 var sl = s.length
1445 if sl > nb then
1446 var p1 = s.substring(0, s.length-nb)
1447 var p2 = s.substring(s.length-nb, nb)
1448 return p1 + "." + p2
1449 else
1450 return "0." + ("0"*(nb-sl)) + s
1451 end
1452 end
1453
1454 # `self` representation with `nb` digits after the '.'.
1455 #
1456 # assert 12.345.to_precision_native(1) == "12.3"
1457 # assert 12.345.to_precision_native(2) == "12.35"
1458 # assert 12.345.to_precision_native(3) == "12.345"
1459 # assert 12.345.to_precision_native(4) == "12.3450"
1460 fun to_precision_native(nb: Int): String import NativeString.to_s `{
1461 int size;
1462 char *str;
1463
1464 size = snprintf(NULL, 0, "%.*f", (int)nb, recv);
1465 str = malloc(size + 1);
1466 sprintf(str, "%.*f", (int)nb, recv );
1467
1468 return NativeString_to_s( str );
1469 `}
1470 end
1471
1472 redef class Char
1473 # assert 'x'.to_s == "x"
1474 redef fun to_s
1475 do
1476 var s = new FlatBuffer.with_capacity(1)
1477 s.chars[0] = self
1478 return s.to_s
1479 end
1480
1481 # Returns true if the char is a numerical digit
1482 #
1483 # assert '0'.is_numeric
1484 # assert '9'.is_numeric
1485 # assert not 'a'.is_numeric
1486 # assert not '?'.is_numeric
1487 fun is_numeric: Bool
1488 do
1489 return self >= '0' and self <= '9'
1490 end
1491
1492 # Returns true if the char is an alpha digit
1493 #
1494 # assert 'a'.is_alpha
1495 # assert 'Z'.is_alpha
1496 # assert not '0'.is_alpha
1497 # assert not '?'.is_alpha
1498 fun is_alpha: Bool
1499 do
1500 return (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z')
1501 end
1502
1503 # Returns true if the char is an alpha or a numeric digit
1504 #
1505 # assert 'a'.is_alphanumeric
1506 # assert 'Z'.is_alphanumeric
1507 # assert '0'.is_alphanumeric
1508 # assert '9'.is_alphanumeric
1509 # assert not '?'.is_alphanumeric
1510 fun is_alphanumeric: Bool
1511 do
1512 return self.is_numeric or self.is_alpha
1513 end
1514 end
1515
1516 redef class Collection[E]
1517 # Concatenate elements.
1518 redef fun to_s
1519 do
1520 var s = new FlatBuffer
1521 for e in self do if e != null then s.append(e.to_s)
1522 return s.to_s
1523 end
1524
1525 # Concatenate and separate each elements with `sep`.
1526 #
1527 # assert [1, 2, 3].join(":") == "1:2:3"
1528 # assert [1..3].join(":") == "1:2:3"
1529 fun join(sep: Text): String
1530 do
1531 if is_empty then return ""
1532
1533 var s = new FlatBuffer # Result
1534
1535 # Concat first item
1536 var i = iterator
1537 var e = i.item
1538 if e != null then s.append(e.to_s)
1539
1540 # Concat other items
1541 i.next
1542 while i.is_ok do
1543 s.append(sep)
1544 e = i.item
1545 if e != null then s.append(e.to_s)
1546 i.next
1547 end
1548 return s.to_s
1549 end
1550 end
1551
1552 redef class Array[E]
1553 # Fast implementation
1554 redef fun to_s
1555 do
1556 var s = new FlatBuffer
1557 var i = 0
1558 var l = length
1559 while i < l do
1560 var e = self[i]
1561 if e != null then s.append(e.to_s)
1562 i += 1
1563 end
1564 return s.to_s
1565 end
1566 end
1567
1568 redef class Map[K,V]
1569 # Concatenate couple of 'key value'.
1570 # key and value are separated by `couple_sep`.
1571 # each couple is separated each couple with `sep`.
1572 #
1573 # var m = new ArrayMap[Int, String]
1574 # m[1] = "one"
1575 # m[10] = "ten"
1576 # assert m.join("; ", "=") == "1=one; 10=ten"
1577 fun join(sep: String, couple_sep: String): String
1578 do
1579 if is_empty then return ""
1580
1581 var s = new FlatBuffer # Result
1582
1583 # Concat first item
1584 var i = iterator
1585 var k = i.key
1586 var e = i.item
1587 s.append("{k}{couple_sep}{e or else "<null>"}")
1588
1589 # Concat other items
1590 i.next
1591 while i.is_ok do
1592 s.append(sep)
1593 k = i.key
1594 e = i.item
1595 s.append("{k}{couple_sep}{e or else "<null>"}")
1596 i.next
1597 end
1598 return s.to_s
1599 end
1600 end
1601
1602 ###############################################################################
1603 # Native classes #
1604 ###############################################################################
1605
1606 # Native strings are simple C char *
1607 class NativeString
1608 super StringCapable
1609
1610 fun [](index: Int): Char is intern
1611 fun []=(index: Int, item: Char) is intern
1612 fun copy_to(dest: NativeString, length: Int, from: Int, to: Int) is intern
1613
1614 # Position of the first nul character.
1615 fun cstring_length: Int
1616 do
1617 var l = 0
1618 while self[l] != '\0' do l += 1
1619 return l
1620 end
1621 fun atoi: Int is intern
1622 fun atof: Float is extern "atof"
1623
1624 redef fun to_s
1625 do
1626 return to_s_with_length(cstring_length)
1627 end
1628
1629 fun to_s_with_length(length: Int): FlatString
1630 do
1631 assert length >= 0
1632 return new FlatString.with_infos(self, length, 0, length - 1)
1633 end
1634
1635 fun to_s_with_copy: FlatString
1636 do
1637 var length = cstring_length
1638 var new_self = calloc_string(length + 1)
1639 copy_to(new_self, length, 0, 0)
1640 return new FlatString.with_infos(new_self, length, 0, length - 1)
1641 end
1642
1643 end
1644
1645 # StringCapable objects can create native strings
1646 interface StringCapable
1647 protected fun calloc_string(size: Int): NativeString is intern
1648 end
1649
1650 redef class Sys
1651 var _args_cache: nullable Sequence[String]
1652
1653 redef fun args: Sequence[String]
1654 do
1655 if _args_cache == null then init_args
1656 return _args_cache.as(not null)
1657 end
1658
1659 # The name of the program as given by the OS
1660 fun program_name: String
1661 do
1662 return native_argv(0).to_s
1663 end
1664
1665 # Initialize `args` with the contents of `native_argc` and `native_argv`.
1666 private fun init_args
1667 do
1668 var argc = native_argc
1669 var args = new Array[String].with_capacity(0)
1670 var i = 1
1671 while i < argc do
1672 args[i-1] = native_argv(i).to_s
1673 i += 1
1674 end
1675 _args_cache = args
1676 end
1677
1678 # First argument of the main C function.
1679 private fun native_argc: Int is intern
1680
1681 # Second argument of the main C function.
1682 private fun native_argv(i: Int): NativeString is intern
1683 end
1684
1685 # Comparator that efficienlty use `to_s` to compare things
1686 #
1687 # The comparaison call `to_s` on object and use the result to order things.
1688 #
1689 # var a = [1, 2, 3, 10, 20]
1690 # (new CachedAlphaComparator).sort(a)
1691 # assert a == [1, 10, 2, 20, 3]
1692 #
1693 # Internally the result of `to_s` is cached in a HashMap to counter
1694 # uneficient implementation of `to_s`.
1695 #
1696 # Note: it caching is not usefull, see `alpha_comparator`
1697 class CachedAlphaComparator
1698 super Comparator[Object]
1699
1700 private var cache = new HashMap[Object, String]
1701
1702 private fun do_to_s(a: Object): String do
1703 if cache.has_key(a) then return cache[a]
1704 var res = a.to_s
1705 cache[a] = res
1706 return res
1707 end
1708
1709 redef fun compare(a, b) do
1710 return do_to_s(a) <=> do_to_s(b)
1711 end
1712 end
1713
1714 # see `alpha_comparator`
1715 private class AlphaComparator
1716 super Comparator[Object]
1717 redef fun compare(a, b) do return a.to_s <=> b.to_s
1718 end
1719
1720 # Stateless comparator that naively use `to_s` to compare things.
1721 #
1722 # Note: the result of `to_s` is not cached, thus can be invoked a lot
1723 # on a single instace. See `CachedAlphaComparator` as an alternative.
1724 #
1725 # var a = [1, 2, 3, 10, 20]
1726 # alpha_comparator.sort(a)
1727 # assert a == [1, 10, 2, 20, 3]
1728 fun alpha_comparator: Comparator[Object] do return once new AlphaComparator