d522714ff5aa80b08e1c7b883c0ed52777fda80f
[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 # Concatenates `o` to `self`
66 #
67 # assert "hello" + "world" == "helloworld"
68 # assert "" + "hello" + "" == "hello"
69 fun +(o: Text): SELFTYPE is abstract
70
71 # Auto-concatenates self `i` times
72 #
73 # assert "abc" * 4 == "abcabcabcabc"
74 # assert "abc" * 1 == "abc"
75 # assert "abc" * 0 == ""
76 fun *(i: Int): SELFTYPE is abstract
77
78 # Is the current Text empty (== "")
79 #
80 # assert "".is_empty
81 # assert not "foo".is_empty
82 fun is_empty: Bool do return self.length == 0
83
84 # Returns an empty Text of the right type
85 #
86 # This method is used internally to get the right
87 # implementation of an empty string.
88 protected fun empty: SELFTYPE is abstract
89
90 # Gets the first char of the Text
91 #
92 # DEPRECATED : Use self.chars.first instead
93 fun first: Char do return self.chars[0]
94
95 # Access a character at `index` in the string.
96 #
97 # assert "abcd"[2] == 'c'
98 #
99 # DEPRECATED : Use self.chars.[] instead
100 fun [](index: Int): Char do return self.chars[index]
101
102 # Gets the index of the first occurence of 'c'
103 #
104 # Returns -1 if not found
105 #
106 # DEPRECATED : Use self.chars.index_of instead
107 fun index_of(c: Char): Int
108 do
109 return index_of_from(c, 0)
110 end
111
112 # Gets the last char of self
113 #
114 # DEPRECATED : Use self.chars.last instead
115 fun last: Char do return self.chars[length-1]
116
117 # Gets the index of the first occurence of ´c´ starting from ´pos´
118 #
119 # Returns -1 if not found
120 #
121 # DEPRECATED : Use self.chars.index_of_from instead
122 fun index_of_from(c: Char, pos: Int): Int
123 do
124 var iter = self.chars.iterator_from(pos)
125 while iter.is_ok do
126 if iter.item == c then return iter.index
127 iter.next
128 end
129 return -1
130 end
131
132 # Gets the last index of char ´c´
133 #
134 # Returns -1 if not found
135 #
136 # DEPRECATED : Use self.chars.last_index_of instead
137 fun last_index_of(c: Char): Int
138 do
139 return last_index_of_from(c, length - 1)
140 end
141
142 # Return a null terminated char *
143 fun to_cstring: NativeString do return flatten.to_cstring
144
145 # The index of the last occurrence of an element starting from pos (in reverse order).
146 #
147 # var s = "/etc/bin/test/test.nit"
148 # assert s.last_index_of_from('/', s.length-1) == 13
149 # assert s.last_index_of_from('/', 12) == 8
150 #
151 # Returns -1 if not found
152 #
153 # DEPRECATED : Use self.chars.last_index_of_from instead
154 fun last_index_of_from(item: Char, pos: Int): Int
155 do
156 var iter = self.chars.reverse_iterator_from(pos)
157 while iter.is_ok do
158 if iter.item == item then return iter.index
159 iter.next
160 end
161 return -1
162 end
163
164 # Gets an iterator on the chars of self
165 #
166 # DEPRECATED : Use self.chars.iterator instead
167 fun iterator: Iterator[Char]
168 do
169 return self.chars.iterator
170 end
171
172 # Is 'c' contained in self ?
173 #
174 # DEPRECATED : Use self.chars.has instead
175 fun has(c: Char): Bool
176 do
177 return self.chars.has(c)
178 end
179
180 # Gets an Array containing the chars of self
181 #
182 # DEPRECATED : Use self.chars.to_a instead
183 fun to_a: Array[Char] do return chars.to_a
184
185 # Create a substring from `self` beginning at the `from` position
186 #
187 # assert "abcd".substring_from(1) == "bcd"
188 # assert "abcd".substring_from(-1) == "abcd"
189 # assert "abcd".substring_from(2) == "cd"
190 #
191 # As with substring, a `from` index < 0 will be replaced by 0
192 fun substring_from(from: Int): SELFTYPE
193 do
194 if from > self.length then return empty
195 if from < 0 then from = 0
196 return substring(from, length - from)
197 end
198
199 # Returns a reversed version of self
200 #
201 # assert "hello".reversed == "olleh"
202 # assert "bob".reversed == "bob"
203 # assert "".reversed == ""
204 fun reversed: SELFTYPE is abstract
205
206 # Does self have a substring `str` starting from position `pos`?
207 #
208 # assert "abcd".has_substring("bc",1) == true
209 # assert "abcd".has_substring("bc",2) == false
210 fun has_substring(str: String, pos: Int): Bool
211 do
212 var myiter = self.chars.iterator_from(pos)
213 var itsiter = str.chars.iterator
214 while myiter.is_ok and itsiter.is_ok do
215 if myiter.item != itsiter.item then return false
216 myiter.next
217 itsiter.next
218 end
219 if itsiter.is_ok then return false
220 return true
221 end
222
223 # Is this string prefixed by `prefix`?
224 #
225 # assert "abcd".has_prefix("ab") == true
226 # assert "abcbc".has_prefix("bc") == false
227 # assert "ab".has_prefix("abcd") == false
228 fun has_prefix(prefix: String): Bool do return has_substring(prefix,0)
229
230 # Is this string suffixed by `suffix`?
231 #
232 # assert "abcd".has_suffix("abc") == false
233 # assert "abcd".has_suffix("bcd") == true
234 fun has_suffix(suffix: String): Bool do return has_substring(suffix, length - suffix.length)
235
236 # If `self` contains only digits, return the corresponding integer
237 #
238 # assert "123".to_i == 123
239 # assert "-1".to_i == -1
240 fun to_i: Int
241 do
242 # Shortcut
243 return to_s.to_cstring.atoi
244 end
245
246 # If `self` contains a float, return the corresponding float
247 #
248 # assert "123".to_f == 123.0
249 # assert "-1".to_f == -1.0
250 # assert "-1.2e-3".to_f == -0.0012
251 fun to_f: Float
252 do
253 # Shortcut
254 return to_s.to_cstring.atof
255 end
256
257 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
258 #
259 # assert "ff".to_hex == 255
260 fun to_hex: Int do return a_to(16)
261
262 # If `self` contains only digits and letters, return the corresponding integer in a given base
263 #
264 # assert "120".a_to(3) == 15
265 fun a_to(base: Int) : Int
266 do
267 var i = 0
268 var neg = false
269
270 for c in self.chars
271 do
272 var v = c.to_i
273 if v > base then
274 if neg then
275 return -i
276 else
277 return i
278 end
279 else if v < 0 then
280 neg = true
281 else
282 i = i * base + v
283 end
284 end
285 if neg then
286 return -i
287 else
288 return i
289 end
290 end
291
292 # Returns `true` if the string contains only Numeric values (and one "," or one "." character)
293 #
294 # assert "123".is_numeric == true
295 # assert "1.2".is_numeric == true
296 # assert "1,2".is_numeric == true
297 # assert "1..2".is_numeric == false
298 fun is_numeric: Bool
299 do
300 var has_point_or_comma = false
301 for i in self.chars
302 do
303 if not i.is_numeric
304 then
305 if (i == '.' or i == ',') and not has_point_or_comma
306 then
307 has_point_or_comma = true
308 else
309 return false
310 end
311 end
312 end
313 return true
314 end
315
316 # A upper case version of `self`
317 #
318 # assert "Hello World!".to_upper == "HELLO WORLD!"
319 fun to_upper: SELFTYPE is abstract
320
321 # A lower case version of `self`
322 #
323 # assert "Hello World!".to_lower == "hello world!"
324 fun to_lower : SELFTYPE is abstract
325
326 # Removes the whitespaces at the beginning of self
327 #
328 # assert " \n\thello \n\t".l_trim == "hello \n\t"
329 #
330 # A whitespace is defined as any character which ascii value is less than or equal to 32
331 fun l_trim: SELFTYPE
332 do
333 var iter = self.chars.iterator
334 while iter.is_ok do
335 if iter.item.ascii > 32 then break
336 iter.next
337 end
338 if iter.index == length then return self.empty
339 return self.substring_from(iter.index)
340 end
341
342 # Removes the whitespaces at the end of self
343 #
344 # assert " \n\thello \n\t".r_trim == " \n\thello"
345 #
346 # A whitespace is defined as any character which ascii value is less than or equal to 32
347 fun r_trim: SELFTYPE
348 do
349 var iter = self.chars.reverse_iterator
350 while iter.is_ok do
351 if iter.item.ascii > 32 then break
352 iter.next
353 end
354 if iter.index == length then return self.empty
355 return self.substring(0, iter.index + 1)
356 end
357
358 # Trims trailing and preceding white spaces
359 # A whitespace is defined as any character which ascii value is less than or equal to 32
360 #
361 # assert " Hello World ! ".trim == "Hello World !"
362 # assert "\na\nb\tc\t".trim == "a\nb\tc"
363 fun trim: SELFTYPE do return (self.l_trim).r_trim
364
365 # Mangle a string to be a unique string only made of alphanumeric characters
366 fun to_cmangle: String
367 do
368 var res = new FlatBuffer
369 var underscore = false
370 for c in self.chars do
371 if (c >= 'a' and c <= 'z') or (c >='A' and c <= 'Z') then
372 res.add(c)
373 underscore = false
374 continue
375 end
376 if underscore then
377 res.append('_'.ascii.to_s)
378 res.add('d')
379 end
380 if c >= '0' and c <= '9' then
381 res.add(c)
382 underscore = false
383 else if c == '_' then
384 res.add(c)
385 underscore = true
386 else
387 res.add('_')
388 res.append(c.ascii.to_s)
389 res.add('d')
390 underscore = false
391 end
392 end
393 return res.to_s
394 end
395
396 # Escape " \ ' and non printable characters using the rules of literal C strings and characters
397 #
398 # assert "abAB12<>&".escape_to_c == "abAB12<>&"
399 # assert "\n\"'\\".escape_to_c == "\\n\\\"\\'\\\\"
400 fun escape_to_c: String
401 do
402 var b = new FlatBuffer
403 for c in self.chars do
404 if c == '\n' then
405 b.append("\\n")
406 else if c == '\0' then
407 b.append("\\0")
408 else if c == '"' then
409 b.append("\\\"")
410 else if c == '\'' then
411 b.append("\\\'")
412 else if c == '\\' then
413 b.append("\\\\")
414 else if c.ascii < 32 then
415 b.append("\\{c.ascii.to_base(8, false)}")
416 else
417 b.add(c)
418 end
419 end
420 return b.to_s
421 end
422
423 # Escape additionnal characters
424 # The result might no be legal in C but be used in other languages
425 #
426 # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
427 fun escape_more_to_c(chars: String): String
428 do
429 var b = new FlatBuffer
430 for c in escape_to_c.chars do
431 if chars.chars.has(c) then
432 b.add('\\')
433 end
434 b.add(c)
435 end
436 return b.to_s
437 end
438
439 # Escape to C plus braces
440 #
441 # assert "\n\"'\\\{\}".escape_to_nit == "\\n\\\"\\'\\\\\\\{\\\}"
442 fun escape_to_nit: String do return escape_more_to_c("\{\}")
443
444 # Return a string where Nit escape sequences are transformed.
445 #
446 # var s = "\\n"
447 # assert s.length == 2
448 # var u = s.unescape_nit
449 # assert u.length == 1
450 # assert u.chars[0].ascii == 10 # (the ASCII value of the "new line" character)
451 fun unescape_nit: String
452 do
453 var res = new FlatBuffer.with_capacity(self.length)
454 var was_slash = false
455 for c in chars do
456 if not was_slash then
457 if c == '\\' then
458 was_slash = true
459 else
460 res.add(c)
461 end
462 continue
463 end
464 was_slash = false
465 if c == 'n' then
466 res.add('\n')
467 else if c == 'r' then
468 res.add('\r')
469 else if c == 't' then
470 res.add('\t')
471 else if c == '0' then
472 res.add('\0')
473 else
474 res.add(c)
475 end
476 end
477 return res.to_s
478 end
479
480 # Equality of text
481 # Two pieces of text are equals if thez have the same characters in the same order.
482 #
483 # assert "hello" == "hello"
484 # assert "hello" != "HELLO"
485 # assert "hello" == "hel"+"lo"
486 #
487 # Things that are not Text are not equal.
488 #
489 # assert "9" != '9'
490 # assert "9" != ['9']
491 # assert "9" != 9
492 #
493 # assert "9".chars.first == '9' # equality of Char
494 # assert "9".chars == ['9'] # equality of Sequence
495 # assert "9".to_i == 9 # equality of Int
496 redef fun ==(o)
497 do
498 if o == null then return false
499 if not o isa Text then return false
500 if self.is_same_instance(o) then return true
501 if self.length != o.length then return false
502 return self.chars == o.chars
503 end
504
505 # Lexicographical comparaison
506 #
507 # assert "abc" < "xy"
508 # assert "ABC" < "abc"
509 redef fun <(other)
510 do
511 var self_chars = self.chars.iterator
512 var other_chars = other.chars.iterator
513
514 while self_chars.is_ok and other_chars.is_ok do
515 if self_chars.item < other_chars.item then return true
516 if self_chars.item > other_chars.item then return false
517 self_chars.next
518 other_chars.next
519 end
520
521 if self_chars.is_ok then
522 return false
523 else
524 return true
525 end
526 end
527
528 # Flat representation of self
529 fun flatten: FlatText is abstract
530
531 private var hash_cache: nullable Int = null
532
533 redef fun hash
534 do
535 if hash_cache == null then
536 # djb2 hash algorithm
537 var h = 5381
538
539 for char in self.chars do
540 h = h.lshift(5) + h + char.ascii
541 end
542
543 hash_cache = h
544 end
545 return hash_cache.as(not null)
546 end
547
548 end
549
550 # All kinds of array-based text representations.
551 abstract class FlatText
552 super Text
553
554 private var items: NativeString
555
556 # Real items, used as cache for to_cstring is called
557 private var real_items: nullable NativeString = null
558
559 redef var length: Int = 0
560
561 init do end
562
563 redef fun output
564 do
565 var i = 0
566 while i < length do
567 items[i].output
568 i += 1
569 end
570 end
571
572 redef fun flatten do return self
573 end
574
575 # Abstract class for the SequenceRead compatible
576 # views on String and Buffer objects
577 private abstract class StringCharView
578 super SequenceRead[Char]
579
580 type SELFTYPE: Text
581
582 private var target: SELFTYPE
583
584 private init(tgt: SELFTYPE)
585 do
586 target = tgt
587 end
588
589 redef fun is_empty do return target.is_empty
590
591 redef fun length do return target.length
592
593 redef fun iterator: IndexedIterator[Char] do return self.iterator_from(0)
594
595 redef fun reverse_iterator do return self.reverse_iterator_from(self.length - 1)
596 end
597
598 # View on Buffer objects, extends Sequence
599 # for mutation operations
600 private abstract class BufferCharView
601 super StringCharView
602 super Sequence[Char]
603
604 redef type SELFTYPE: Buffer
605
606 end
607
608 abstract class String
609 super Text
610
611 redef type SELFTYPE: String
612
613 redef fun to_s do return self
614 end
615
616 private class FlatSubstringsIter
617 super Iterator[FlatText]
618
619 var tgt: nullable FlatText
620
621 init(tgt: FlatText) do self.tgt = tgt
622
623 redef fun item do
624 assert is_ok
625 return tgt.as(not null)
626 end
627
628 redef fun is_ok do return tgt != null
629
630 redef fun next do tgt = null
631 end
632
633 # Immutable strings of characters.
634 class FlatString
635 super FlatText
636 super String
637
638 redef type SELFTYPE: FlatString
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 redef fun hash
1014 do
1015 if is_dirty then hash_cache = null
1016 return super
1017 end
1018
1019 # In Buffers, the internal sequence of character is mutable
1020 # Thus, `chars` can be used to modify the buffer.
1021 redef fun chars: Sequence[Char] is abstract
1022 end
1023
1024 # Mutable strings of characters.
1025 class FlatBuffer
1026 super FlatText
1027 super Buffer
1028
1029 redef type SELFTYPE: FlatBuffer
1030
1031 redef var chars: Sequence[Char] = new FlatBufferCharView(self)
1032
1033 private var capacity: Int = 0
1034
1035 redef fun substrings do return new FlatSubstringsIter(self)
1036
1037 redef fun []=(index, item)
1038 do
1039 is_dirty = true
1040 if index == length then
1041 add(item)
1042 return
1043 end
1044 assert index >= 0 and index < length
1045 items[index] = item
1046 end
1047
1048 redef fun add(c)
1049 do
1050 is_dirty = true
1051 if capacity <= length then enlarge(length + 5)
1052 items[length] = c
1053 length += 1
1054 end
1055
1056 redef fun clear do
1057 is_dirty = true
1058 length = 0
1059 end
1060
1061 redef fun empty do return new FlatBuffer
1062
1063 redef fun enlarge(cap)
1064 do
1065 var c = capacity
1066 if cap <= c then return
1067 while c <= cap do c = c * 2 + 2
1068 var a = calloc_string(c+1)
1069 if length > 0 then items.copy_to(a, length, 0, 0)
1070 items = a
1071 capacity = c
1072 end
1073
1074 redef fun to_s: String
1075 do
1076 return to_cstring.to_s_with_length(length)
1077 end
1078
1079 redef fun to_cstring
1080 do
1081 if is_dirty then
1082 var new_native = calloc_string(length + 1)
1083 new_native[length] = '\0'
1084 if length > 0 then items.copy_to(new_native, length, 0, 0)
1085 real_items = new_native
1086 is_dirty = false
1087 end
1088 return real_items.as(not null)
1089 end
1090
1091 # Create a new empty string.
1092 init do end
1093
1094 init from(s: Text)
1095 do
1096 capacity = s.length + 1
1097 length = s.length
1098 items = calloc_string(capacity)
1099 if s isa FlatString then
1100 s.items.copy_to(items, length, s.index_from, 0)
1101 else if s isa FlatBuffer then
1102 s.items.copy_to(items, length, 0, 0)
1103 else
1104 var curr_pos = 0
1105 for i in s.chars do
1106 items[curr_pos] = i
1107 curr_pos += 1
1108 end
1109 end
1110 end
1111
1112 # Create a new empty string with a given capacity.
1113 init with_capacity(cap: Int)
1114 do
1115 assert cap >= 0
1116 # _items = new NativeString.calloc(cap)
1117 items = calloc_string(cap+1)
1118 capacity = cap
1119 length = 0
1120 end
1121
1122 redef fun append(s)
1123 do
1124 if s.is_empty then return
1125 is_dirty = true
1126 var sl = s.length
1127 if capacity < length + sl then enlarge(length + sl)
1128 if s isa FlatString then
1129 s.items.copy_to(items, sl, s.index_from, length)
1130 else if s isa FlatBuffer then
1131 s.items.copy_to(items, sl, 0, length)
1132 else
1133 var curr_pos = self.length
1134 for i in s.chars do
1135 items[curr_pos] = i
1136 curr_pos += 1
1137 end
1138 end
1139 length += sl
1140 end
1141
1142 # Copies the content of self in `dest`
1143 fun copy(start: Int, len: Int, dest: Buffer, new_start: Int)
1144 do
1145 var self_chars = self.chars
1146 var dest_chars = dest.chars
1147 for i in [0..len-1] do
1148 dest_chars[new_start+i] = self_chars[start+i]
1149 end
1150 end
1151
1152 redef fun substring(from, count)
1153 do
1154 assert count >= 0
1155 count += from
1156 if from < 0 then from = 0
1157 if count > length then count = length
1158 if from < count then
1159 var r = new FlatBuffer.with_capacity(count - from)
1160 while from < count do
1161 r.chars.push(items[from])
1162 from += 1
1163 end
1164 return r
1165 else
1166 return new FlatBuffer
1167 end
1168 end
1169
1170 redef fun reversed
1171 do
1172 var new_buf = new FlatBuffer.with_capacity(self.length)
1173 var reviter = self.chars.reverse_iterator
1174 while reviter.is_ok do
1175 new_buf.add(reviter.item)
1176 reviter.next
1177 end
1178 return new_buf
1179 end
1180
1181 redef fun +(other)
1182 do
1183 var new_buf = new FlatBuffer.with_capacity(self.length + other.length)
1184 new_buf.append(self)
1185 new_buf.append(other)
1186 return new_buf
1187 end
1188
1189 redef fun *(repeats)
1190 do
1191 var new_buf = new FlatBuffer.with_capacity(self.length * repeats)
1192 for i in [0..repeats[ do
1193 new_buf.append(self)
1194 end
1195 return new_buf
1196 end
1197
1198 redef fun to_upper
1199 do
1200 var new_buf = new FlatBuffer.with_capacity(self.length)
1201 for i in self.chars do
1202 new_buf.add(i.to_upper)
1203 end
1204 return new_buf
1205 end
1206
1207 redef fun to_lower
1208 do
1209 var new_buf = new FlatBuffer.with_capacity(self.length)
1210 for i in self.chars do
1211 new_buf.add(i.to_lower)
1212 end
1213 return new_buf
1214 end
1215 end
1216
1217 private class FlatBufferReverseIterator
1218 super IndexedIterator[Char]
1219
1220 var target: FlatBuffer
1221
1222 var target_items: NativeString
1223
1224 var curr_pos: Int
1225
1226 init with_pos(tgt: FlatBuffer, pos: Int)
1227 do
1228 target = tgt
1229 if tgt.length > 0 then target_items = tgt.items
1230 curr_pos = pos
1231 end
1232
1233 redef fun index do return curr_pos
1234
1235 redef fun is_ok do return curr_pos >= 0
1236
1237 redef fun item do return target_items[curr_pos]
1238
1239 redef fun next do curr_pos -= 1
1240
1241 end
1242
1243 private class FlatBufferCharView
1244 super BufferCharView
1245 super StringCapable
1246
1247 redef type SELFTYPE: FlatBuffer
1248
1249 redef fun [](index) do return target.items[index]
1250
1251 redef fun []=(index, item)
1252 do
1253 assert index >= 0 and index <= length
1254 if index == length then
1255 add(item)
1256 return
1257 end
1258 target.items[index] = item
1259 end
1260
1261 redef fun push(c)
1262 do
1263 target.add(c)
1264 end
1265
1266 redef fun add(c)
1267 do
1268 target.add(c)
1269 end
1270
1271 fun enlarge(cap: Int)
1272 do
1273 target.enlarge(cap)
1274 end
1275
1276 redef fun append(s)
1277 do
1278 var my_items = target.items
1279 var s_length = s.length
1280 if target.capacity < s.length then enlarge(s_length + target.length)
1281 end
1282
1283 redef fun iterator_from(pos) do return new FlatBufferIterator.with_pos(target, pos)
1284
1285 redef fun reverse_iterator_from(pos) do return new FlatBufferReverseIterator.with_pos(target, pos)
1286
1287 end
1288
1289 private class FlatBufferIterator
1290 super IndexedIterator[Char]
1291
1292 var target: FlatBuffer
1293
1294 var target_items: NativeString
1295
1296 var curr_pos: Int
1297
1298 init with_pos(tgt: FlatBuffer, pos: Int)
1299 do
1300 target = tgt
1301 if tgt.length > 0 then target_items = tgt.items
1302 curr_pos = pos
1303 end
1304
1305 redef fun index do return curr_pos
1306
1307 redef fun is_ok do return curr_pos < target.length
1308
1309 redef fun item do return target_items[curr_pos]
1310
1311 redef fun next do curr_pos += 1
1312
1313 end
1314
1315 ###############################################################################
1316 # Refinement #
1317 ###############################################################################
1318
1319 redef class Object
1320 # User readable representation of `self`.
1321 fun to_s: String do return inspect
1322
1323 # The class name of the object in NativeString format.
1324 private fun native_class_name: NativeString is intern
1325
1326 # The class name of the object.
1327 #
1328 # assert 5.class_name == "Int"
1329 fun class_name: String do return native_class_name.to_s
1330
1331 # Developer readable representation of `self`.
1332 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
1333 fun inspect: String
1334 do
1335 return "<{inspect_head}>"
1336 end
1337
1338 # Return "CLASSNAME:#OBJECTID".
1339 # This function is mainly used with the redefinition of the inspect method
1340 protected fun inspect_head: String
1341 do
1342 return "{class_name}:#{object_id.to_hex}"
1343 end
1344
1345 protected fun args: Sequence[String]
1346 do
1347 return sys.args
1348 end
1349 end
1350
1351 redef class Bool
1352 # assert true.to_s == "true"
1353 # assert false.to_s == "false"
1354 redef fun to_s
1355 do
1356 if self then
1357 return once "true"
1358 else
1359 return once "false"
1360 end
1361 end
1362 end
1363
1364 redef class Int
1365
1366 # Wrapper of strerror C function
1367 private fun strerror_ext: NativeString is extern `{
1368 return strerror(recv);
1369 `}
1370
1371 # Returns a string describing error number
1372 fun strerror: String do return strerror_ext.to_s
1373
1374 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
1375 # assume < to_c max const of char
1376 private fun fill_buffer(s: Buffer, base: Int, signed: Bool)
1377 do
1378 var n: Int
1379 # Sign
1380 if self < 0 then
1381 n = - self
1382 s.chars[0] = '-'
1383 else if self == 0 then
1384 s.chars[0] = '0'
1385 return
1386 else
1387 n = self
1388 end
1389 # Fill digits
1390 var pos = digit_count(base) - 1
1391 while pos >= 0 and n > 0 do
1392 s.chars[pos] = (n % base).to_c
1393 n = n / base # /
1394 pos -= 1
1395 end
1396 end
1397
1398 # C function to convert an nit Int to a NativeString (char*)
1399 private fun native_int_to_s(len: Int): NativeString is extern "native_int_to_s"
1400
1401 # return displayable int in base 10 and signed
1402 #
1403 # assert 1.to_s == "1"
1404 # assert (-123).to_s == "-123"
1405 redef fun to_s do
1406 var len = digit_count(10)
1407 return native_int_to_s(len).to_s_with_length(len)
1408 end
1409
1410 # return displayable int in hexadecimal
1411 #
1412 # assert 1.to_hex == "1"
1413 # assert (-255).to_hex == "-ff"
1414 fun to_hex: String do return to_base(16,false)
1415
1416 # return displayable int in base base and signed
1417 fun to_base(base: Int, signed: Bool): String
1418 do
1419 var l = digit_count(base)
1420 var s = new FlatBuffer.from(" " * l)
1421 fill_buffer(s, base, signed)
1422 return s.to_s
1423 end
1424 end
1425
1426 redef class Float
1427 # Pretty print self, print needoed decimals up to a max of 3.
1428 #
1429 # assert 12.34.to_s == "12.34"
1430 # assert (-0120.03450).to_s == "-120.035"
1431 #
1432 # see `to_precision` for a different precision.
1433 redef fun to_s do
1434 var str = to_precision( 3 )
1435 if is_inf != 0 or is_nan then return str
1436 var len = str.length
1437 for i in [0..len-1] do
1438 var j = len-1-i
1439 var c = str.chars[j]
1440 if c == '0' then
1441 continue
1442 else if c == '.' then
1443 return str.substring( 0, j+2 )
1444 else
1445 return str.substring( 0, j+1 )
1446 end
1447 end
1448 return str
1449 end
1450
1451 # `self` representation with `nb` digits after the '.'.
1452 #
1453 # assert 12.345.to_precision(1) == "12.3"
1454 # assert 12.345.to_precision(2) == "12.35"
1455 # assert 12.345.to_precision(3) == "12.345"
1456 # assert 12.345.to_precision(4) == "12.3450"
1457 fun to_precision(nb: Int): String
1458 do
1459 if is_nan then return "nan"
1460
1461 var isinf = self.is_inf
1462 if isinf == 1 then
1463 return "inf"
1464 else if isinf == -1 then
1465 return "-inf"
1466 end
1467
1468 if nb == 0 then return self.to_i.to_s
1469 var f = self
1470 for i in [0..nb[ do f = f * 10.0
1471 if self > 0.0 then
1472 f = f + 0.5
1473 else
1474 f = f - 0.5
1475 end
1476 var i = f.to_i
1477 if i == 0 then return "0.0"
1478 var s = i.to_s
1479 var sl = s.length
1480 if sl > nb then
1481 var p1 = s.substring(0, s.length-nb)
1482 var p2 = s.substring(s.length-nb, nb)
1483 return p1 + "." + p2
1484 else
1485 return "0." + ("0"*(nb-sl)) + s
1486 end
1487 end
1488
1489 # `self` representation with `nb` digits after the '.'.
1490 #
1491 # assert 12.345.to_precision_native(1) == "12.3"
1492 # assert 12.345.to_precision_native(2) == "12.35"
1493 # assert 12.345.to_precision_native(3) == "12.345"
1494 # assert 12.345.to_precision_native(4) == "12.3450"
1495 fun to_precision_native(nb: Int): String import NativeString.to_s `{
1496 int size;
1497 char *str;
1498
1499 size = snprintf(NULL, 0, "%.*f", (int)nb, recv);
1500 str = malloc(size + 1);
1501 sprintf(str, "%.*f", (int)nb, recv );
1502
1503 return NativeString_to_s( str );
1504 `}
1505 end
1506
1507 redef class Char
1508 # assert 'x'.to_s == "x"
1509 redef fun to_s
1510 do
1511 var s = new FlatBuffer.with_capacity(1)
1512 s.chars[0] = self
1513 return s.to_s
1514 end
1515
1516 # Returns true if the char is a numerical digit
1517 #
1518 # assert '0'.is_numeric
1519 # assert '9'.is_numeric
1520 # assert not 'a'.is_numeric
1521 # assert not '?'.is_numeric
1522 fun is_numeric: Bool
1523 do
1524 return self >= '0' and self <= '9'
1525 end
1526
1527 # Returns true if the char is an alpha digit
1528 #
1529 # assert 'a'.is_alpha
1530 # assert 'Z'.is_alpha
1531 # assert not '0'.is_alpha
1532 # assert not '?'.is_alpha
1533 fun is_alpha: Bool
1534 do
1535 return (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z')
1536 end
1537
1538 # Returns true if the char is an alpha or a numeric digit
1539 #
1540 # assert 'a'.is_alphanumeric
1541 # assert 'Z'.is_alphanumeric
1542 # assert '0'.is_alphanumeric
1543 # assert '9'.is_alphanumeric
1544 # assert not '?'.is_alphanumeric
1545 fun is_alphanumeric: Bool
1546 do
1547 return self.is_numeric or self.is_alpha
1548 end
1549 end
1550
1551 redef class Collection[E]
1552 # Concatenate elements.
1553 redef fun to_s
1554 do
1555 var s = new FlatBuffer
1556 for e in self do if e != null then s.append(e.to_s)
1557 return s.to_s
1558 end
1559
1560 # Concatenate and separate each elements with `sep`.
1561 #
1562 # assert [1, 2, 3].join(":") == "1:2:3"
1563 # assert [1..3].join(":") == "1:2:3"
1564 fun join(sep: Text): String
1565 do
1566 if is_empty then return ""
1567
1568 var s = new FlatBuffer # Result
1569
1570 # Concat first item
1571 var i = iterator
1572 var e = i.item
1573 if e != null then s.append(e.to_s)
1574
1575 # Concat other items
1576 i.next
1577 while i.is_ok do
1578 s.append(sep)
1579 e = i.item
1580 if e != null then s.append(e.to_s)
1581 i.next
1582 end
1583 return s.to_s
1584 end
1585 end
1586
1587 redef class Array[E]
1588 # Fast implementation
1589 redef fun to_s
1590 do
1591 var s = new FlatBuffer
1592 var i = 0
1593 var l = length
1594 while i < l do
1595 var e = self[i]
1596 if e != null then s.append(e.to_s)
1597 i += 1
1598 end
1599 return s.to_s
1600 end
1601 end
1602
1603 redef class Map[K,V]
1604 # Concatenate couple of 'key value'.
1605 # key and value are separated by `couple_sep`.
1606 # each couple is separated each couple with `sep`.
1607 #
1608 # var m = new ArrayMap[Int, String]
1609 # m[1] = "one"
1610 # m[10] = "ten"
1611 # assert m.join("; ", "=") == "1=one; 10=ten"
1612 fun join(sep: String, couple_sep: String): String
1613 do
1614 if is_empty then return ""
1615
1616 var s = new FlatBuffer # Result
1617
1618 # Concat first item
1619 var i = iterator
1620 var k = i.key
1621 var e = i.item
1622 s.append("{k}{couple_sep}{e or else "<null>"}")
1623
1624 # Concat other items
1625 i.next
1626 while i.is_ok do
1627 s.append(sep)
1628 k = i.key
1629 e = i.item
1630 s.append("{k}{couple_sep}{e or else "<null>"}")
1631 i.next
1632 end
1633 return s.to_s
1634 end
1635 end
1636
1637 ###############################################################################
1638 # Native classes #
1639 ###############################################################################
1640
1641 # Native strings are simple C char *
1642 class NativeString
1643 super StringCapable
1644
1645 fun [](index: Int): Char is intern
1646 fun []=(index: Int, item: Char) is intern
1647 fun copy_to(dest: NativeString, length: Int, from: Int, to: Int) is intern
1648
1649 # Position of the first nul character.
1650 fun cstring_length: Int
1651 do
1652 var l = 0
1653 while self[l] != '\0' do l += 1
1654 return l
1655 end
1656 fun atoi: Int is intern
1657 fun atof: Float is extern "atof"
1658
1659 redef fun to_s
1660 do
1661 return to_s_with_length(cstring_length)
1662 end
1663
1664 fun to_s_with_length(length: Int): FlatString
1665 do
1666 assert length >= 0
1667 return new FlatString.with_infos(self, length, 0, length - 1)
1668 end
1669
1670 fun to_s_with_copy: FlatString
1671 do
1672 var length = cstring_length
1673 var new_self = calloc_string(length + 1)
1674 copy_to(new_self, length, 0, 0)
1675 return new FlatString.with_infos(new_self, length, 0, length - 1)
1676 end
1677
1678 end
1679
1680 # StringCapable objects can create native strings
1681 interface StringCapable
1682 protected fun calloc_string(size: Int): NativeString is intern
1683 end
1684
1685 redef class Sys
1686 var _args_cache: nullable Sequence[String]
1687
1688 redef fun args: Sequence[String]
1689 do
1690 if _args_cache == null then init_args
1691 return _args_cache.as(not null)
1692 end
1693
1694 # The name of the program as given by the OS
1695 fun program_name: String
1696 do
1697 return native_argv(0).to_s
1698 end
1699
1700 # Initialize `args` with the contents of `native_argc` and `native_argv`.
1701 private fun init_args
1702 do
1703 var argc = native_argc
1704 var args = new Array[String].with_capacity(0)
1705 var i = 1
1706 while i < argc do
1707 args[i-1] = native_argv(i).to_s
1708 i += 1
1709 end
1710 _args_cache = args
1711 end
1712
1713 # First argument of the main C function.
1714 private fun native_argc: Int is intern
1715
1716 # Second argument of the main C function.
1717 private fun native_argv(i: Int): NativeString is intern
1718 end
1719
1720 # Comparator that efficienlty use `to_s` to compare things
1721 #
1722 # The comparaison call `to_s` on object and use the result to order things.
1723 #
1724 # var a = [1, 2, 3, 10, 20]
1725 # (new CachedAlphaComparator).sort(a)
1726 # assert a == [1, 10, 2, 20, 3]
1727 #
1728 # Internally the result of `to_s` is cached in a HashMap to counter
1729 # uneficient implementation of `to_s`.
1730 #
1731 # Note: it caching is not usefull, see `alpha_comparator`
1732 class CachedAlphaComparator
1733 super Comparator[Object]
1734
1735 private var cache = new HashMap[Object, String]
1736
1737 private fun do_to_s(a: Object): String do
1738 if cache.has_key(a) then return cache[a]
1739 var res = a.to_s
1740 cache[a] = res
1741 return res
1742 end
1743
1744 redef fun compare(a, b) do
1745 return do_to_s(a) <=> do_to_s(b)
1746 end
1747 end
1748
1749 # see `alpha_comparator`
1750 private class AlphaComparator
1751 super Comparator[Object]
1752 redef fun compare(a, b) do return a.to_s <=> b.to_s
1753 end
1754
1755 # Stateless comparator that naively use `to_s` to compare things.
1756 #
1757 # Note: the result of `to_s` is not cached, thus can be invoked a lot
1758 # on a single instace. See `CachedAlphaComparator` as an alternative.
1759 #
1760 # var a = [1, 2, 3, 10, 20]
1761 # alpha_comparator.sort(a)
1762 # assert a == [1, 10, 2, 20, 3]
1763 fun alpha_comparator: Comparator[Object] do return once new AlphaComparator