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