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