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