1289de2a6d3f70540fbe135f60a9b975132eeeaa
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
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
14 # Basic manipulations of strings of characters
18 intrude import collection
# FIXME should be collection::array
24 ###############################################################################
26 ###############################################################################
28 # High-level abstraction for all text representations
30 super AbstractArrayRead[Char]
32 # Gets a view on the chars of the Text object
33 fun chars
: StringCharView is abstract
37 # assert "abcd".substring(1, 2) == "bc"
38 # assert "abcd".substring(-1, 2) == "a"
39 # assert "abcd".substring(1, 0) == ""
40 # assert "abcd".substring(2, 5) == "cd"
42 # A `from` index < 0 will be replaced by 0.
43 # Unless a `count` value is > 0 at the same time.
44 # In this case, `from += count` and `count -= from`.
45 fun substring
(from
: Int, count
: Int): String is abstract
47 redef fun is_empty
: Bool do return self.length
== 0
49 redef fun index_of
(c
: Char): Int
51 return index_of_from
(c
, 0)
54 redef fun last
: Char do return self.chars
[length-1
]
56 redef fun index_of_from
(c
: Char, pos
: Int): Int
58 var iter
= self.chars
.iterator_from
(pos
)
60 if iter
.item
== c
then return iter
.index
65 redef fun last_index_of
(c
: Char): Int
67 return last_index_of_from
(c
, length
- 1)
70 redef fun last_index_of_from
(item
: Char, pos
: Int): Int
72 var iter
= self.chars
.reverse_iterator_from
(pos
)
74 if iter
.item
== item
then return iter
.index
80 redef fun iterator
: Iterator[Char]
82 return self.chars
.iterator
85 redef fun has
(c
: Char): Bool
87 return self.chars
.has
(c
)
90 redef fun to_a
: Array[Char] do return chars
.to_a
92 # Create a substring from `self` beginning at the `from` position
94 # assert "abcd".substring_from(1) == "bcd"
95 # assert "abcd".substring_from(-1) == "abcd"
96 # assert "abcd".substring_from(2) == "cd"
98 # As with substring, a `from` index < 0 will be replaced by 0
99 fun substring_from
(from
: Int): String
102 return substring
(from
, length
- from
)
105 # Does self have a substring `str` starting from position `pos`?
107 # assert "abcd".has_substring("bc",1) == true
108 # assert "abcd".has_substring("bc",2) == false
109 fun has_substring
(str
: String, pos
: Int): Bool
111 var myiter
= self.chars
.iterator_from
(pos
)
112 var itsiter
= str
.iterator
113 while myiter
.is_ok
and itsiter
.is_ok
do
114 if myiter
.item
!= itsiter
.item
then return false
118 if itsiter
.is_ok
then return false
122 # Is this string prefixed by `prefix`?
124 # assert "abcd".has_prefix("ab") == true
125 # assert "abcbc".has_prefix("bc") == false
126 # assert "ab".has_prefix("abcd") == false
127 fun has_prefix
(prefix
: String): Bool do return has_substring
(prefix
,0)
129 # Is this string suffixed by `suffix`?
131 # assert "abcd".has_suffix("abc") == false
132 # assert "abcd".has_suffix("bcd") == true
133 fun has_suffix
(suffix
: String): Bool do return has_substring
(suffix
, length
- suffix
.length
)
135 # If `self` contains only digits, return the corresponding integer
137 # assert "123".to_i == 123
138 # assert "-1".to_i == -1
142 return to_s
.to_cstring
.atoi
145 # If `self` contains a float, return the corresponding float
147 # assert "123".to_f == 123.0
148 # assert "-1".to_f == -1.0
149 # assert "-1.2e-3".to_f == -0.0012
153 return to_s
.to_cstring
.atof
156 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
157 fun to_hex
: Int do return a_to
(16)
159 # If `self` contains only digits and letters, return the corresponding integer in a given base
161 # assert "120".a_to(3) == 15
162 fun a_to
(base
: Int) : Int
189 # Returns `true` if the string contains only Numeric values (and one "," or one "." character)
191 # assert "123".is_numeric == true
192 # assert "1.2".is_numeric == true
193 # assert "1,2".is_numeric == true
194 # assert "1..2".is_numeric == false
197 var has_point_or_comma
= false
202 if (i
== '.' or i
== ',') and not has_point_or_comma
204 has_point_or_comma
= true
213 # A upper case version of `self`
215 # assert "Hello World!".to_upper == "HELLO WORLD!"
218 var s
= new Buffer.with_capacity
(length
)
219 for i
in self.chars
do s
.add
(i
.to_upper
)
223 # A lower case version of `self`
225 # assert "Hello World!".to_lower == "hello world!"
226 fun to_lower
: String
228 var s
= new Buffer.with_capacity
(length
)
229 for i
in self.chars
do s
.add
(i
.to_lower
)
233 # Trims trailing and preceding white spaces
234 # A whitespace is defined as any character which ascii value is less than or equal to 32
236 # assert " Hello World ! ".trim == "Hello World !"
237 # assert "\na\nb\tc\t".trim == "a\nb\tc"
240 if self.length
== 0 then return self.to_s
241 # find position of the first non white space char (ascii < 32) from the start of the string
243 while self.chars
[start_pos
].ascii
<= 32 do
245 if start_pos
== length
then return ""
247 # find position of the first non white space char from the end of the string
248 var end_pos
= length
- 1
249 while self.chars
[end_pos
].ascii
<= 32 do
251 if end_pos
== start_pos
then return self.chars
[start_pos
].to_s
253 return self.substring
(start_pos
, end_pos
- start_pos
+ 1)
256 # Mangle a string to be a unique string only made of alphanumeric characters
257 fun to_cmangle
: String
260 var underscore
= false
261 for c
in self.chars
do
262 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
268 res
.append
('_'.ascii
.to_s
)
271 if c
>= '0' and c
<= '9' then
274 else if c
== '_' then
279 res
.append
(c
.ascii
.to_s
)
287 # Escape " \ ' and non printable characters using the rules of literal C strings and characters
289 # assert "abAB12<>&".escape_to_c == "abAB12<>&"
290 # assert "\n\"'\\".escape_to_c == "\\n\\\"\\'\\\\"
291 fun escape_to_c
: String
294 for c
in self.chars
do
297 else if c
== '\0' then
299 else if c
== '"' then
301 else if c == '\'' then
303 else if c == '\\
' then
305 else if c.ascii < 32 then
306 b.append("\\{c.ascii.to_base(8, false)}")
314 # Escape additionnal characters
315 # The result might no be legal in C but be used in other languages
317 # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
318 fun escape_more_to_c(chars: String): String
321 for c in escape_to_c do
322 if chars.chars.has(c) then
330 # Escape to c plus braces
332 # assert "\n\"'\\\
{\}".escape_to_nit == "\\n\\\
"\\'\\\\\\\{\\\}"
333 fun escape_to_nit
: String do return escape_more_to_c
("\{\}")
335 # Return a string where Nit escape sequences are transformed.
339 # assert s.length == 2
340 # var u = s.unescape_nit
341 # assert u.length == 1
342 # assert u[0].ascii == 10 # (the ASCII value of the "new line" character)
343 fun unescape_nit
: String
345 var res
= new Buffer.with_capacity
(self.length
)
346 var was_slash
= false
348 if not was_slash
then
359 else if c
== 'r' then
361 else if c
== 't' then
363 else if c
== '0' then
374 # Common subclass for String and Buffer
375 abstract class AbstractString
378 readable private var _items
: NativeString
380 redef var _length
: Int
394 # Abstract class for the SequenceRead compatible
395 # views on String and Buffer objects
396 abstract class StringCharView
397 super SequenceRead[Char]
401 private var target
: SELFTYPE
403 private init(tgt
: SELFTYPE)
408 redef fun is_empty
do return target
.is_empty
410 redef fun length
do return target
.length
412 redef fun iterator
: IndexedIterator[Char] do return self.iterator_from
(0)
414 # Gets a new Iterator starting at position `pos`
417 # var iter = "abcd".iterator_from(2)
418 # while iter.is_ok do
424 fun iterator_from
(pos
: Int): IndexedIterator[Char] is abstract
426 # Gets an iterator starting at the end and going backwards
429 # var reviter = "now step live...".reverse_iterator
430 # while reviter.is_ok do
431 # printn reviter.item
435 # Outputs : ...evil pets won
436 fun reverse_iterator
: IndexedIterator[Char] do return self.reverse_iterator_from
(self.length
- 1)
438 # Gets an iterator on the chars of self starting from `pos`
441 # var iter = "abcd".reverse_iterator_from(1)
442 # while iter.is_ok do
448 fun reverse_iterator_from
(pos
: Int): IndexedIterator[Char] is abstract
450 redef fun has
(c
: Char): Bool
453 if i
== c
then return true
460 # View on Buffer objects, extends Sequence
461 # for mutation operations
462 abstract class BufferCharView
466 redef type SELFTYPE: Buffer
470 # Immutable strings of characters.
476 redef type OTHER: String
478 # Index in _items of the start of the string
479 readable var _index_from
: Int
481 # Indes in _items of the last item of the string
482 readable var _index_to
: Int
484 redef var chars
: StringCharView = new FlatStringCharView(self)
486 ################################################
487 # AbstractString specific methods #
488 ################################################
490 redef fun [](index
) do
492 # Check that the index (+ index_from) is not larger than indexTo
493 # In other terms, if the index is valid
494 assert (index
+ _index_from
) <= _index_to
495 return _items
[index
+ _index_from
]
498 redef fun substring
(from
: Int, count
: Int): String
504 if count
< 0 then count
= 0
508 var realFrom
= _index_from
+ from
510 if (realFrom
+ count
) > _index_to
then return new String.with_infos
(_items
, _index_to
- realFrom
+ 1, realFrom
, _index_to
)
512 if count
== 0 then return ""
514 var to
= realFrom
+ count
- 1
516 return new String.with_infos
(_items
, to
- realFrom
+ 1, realFrom
, to
)
520 redef fun to_upper
: String
522 var outstr
= calloc_string
(self._length
+ 1)
525 var myitems
= self._items
526 var index_from
= self._index_from
527 var max
= self._index_to
529 while index_from
<= max
do
530 outstr
[out_index
] = myitems
[index_from
].to_upper
535 outstr
[self.length
] = '\0'
537 return outstr
.to_s_with_length
(self._length
)
540 redef fun to_lower
: String
542 var outstr
= calloc_string
(self._length
+ 1)
545 var myitems
= self._items
546 var index_from
= self._index_from
547 var max
= self._index_to
549 while index_from
<= max
do
550 outstr
[out_index
] = myitems
[index_from
].to_lower
555 outstr
[self.length
] = '\0'
557 return outstr
.to_s_with_length
(self._length
)
560 redef fun trim
: String
562 if self._length
== 0 then return self
563 # find position of the first non white space char (ascii < 32) from the start of the string
564 var start_pos
= self._index_from
565 while _items
[start_pos
].ascii
<= 32 do
567 if start_pos
== _index_to
+ 1 then return ""
569 # find position of the first non white space char from the end of the string
570 var end_pos
= _index_to
571 while _items
[end_pos
].ascii
<= 32 do
573 if end_pos
== start_pos
then return _items
[start_pos
].to_s
575 start_pos
-= index_from
576 end_pos
-= index_from
577 return self.substring
(start_pos
, end_pos
- start_pos
+ 1)
582 var i
= self._index_from
583 var imax
= self._index_to
590 ##################################################
591 # String Specific Methods #
592 ##################################################
594 private init with_infos
(items
: NativeString, len
: Int, from
: Int, to
: Int)
602 # Return a null terminated char *
603 fun to_cstring
: NativeString
605 if _index_from
> 0 or _index_to
!= items
.cstring_length
- 1 then
606 var newItems
= calloc_string
(_length
+ 1)
607 self.items
.copy_to
(newItems
, _length
, _index_from
, 0)
608 newItems
[length
] = '\0'
616 if not other
isa String then return false
618 if self.object_id
== other
.object_id
then return true
620 var my_length
= _length
622 if other
._length
!= my_length
then return false
624 var my_index
= _index_from
625 var its_index
= other
._index_from
627 var last_iteration
= my_index
+ my_length
629 var itsitems
= other
._items
630 var myitems
= self._items
632 while my_index
< last_iteration
do
633 if myitems
[my_index
] != itsitems
[its_index
] then return false
641 # The comparison between two strings is done on a lexicographical basis
643 # assert ("aa" < "b") == true
646 if self.object_id
== other
.object_id
then return false
648 var my_curr_char
: Char
649 var its_curr_char
: Char
651 var curr_id_self
= self._index_from
652 var curr_id_other
= other
._index_from
654 var my_items
= self._items
655 var its_items
= other
._items
657 var my_length
= self._length
658 var its_length
= other
._length
660 var max_iterations
= curr_id_self
+ my_length
662 while curr_id_self
< max_iterations
do
663 my_curr_char
= my_items
[curr_id_self
]
664 its_curr_char
= its_items
[curr_id_other
]
666 if my_curr_char
!= its_curr_char
then
667 if my_curr_char
< its_curr_char
then return true
675 return my_length
< its_length
678 # The concatenation of `self` with `s`
680 # assert "hello " + "world!" == "hello world!"
681 fun +(s
: String): String
683 var my_length
= self._length
684 var its_length
= s
._length
686 var total_length
= my_length
+ its_length
688 var target_string
= calloc_string
(my_length
+ its_length
+ 1)
690 self._items
.copy_to
(target_string
, my_length
, _index_from
, 0)
691 s
._items
.copy_to
(target_string
, its_length
, s
._index_from
, my_length
)
693 target_string
[total_length
] = '\0'
695 return target_string
.to_s_with_length
(total_length
)
698 # `i` repetitions of `self`
700 # assert "abc"*3 == "abcabcabc"
701 # assert "abc"*1 == "abc"
702 # assert "abc"*0 == ""
703 fun *(i
: Int): String
707 var my_length
= self._length
709 var final_length
= my_length
* i
711 var my_items
= self._items
713 var target_string
= calloc_string
((final_length
) + 1)
715 target_string
[final_length
] = '\0'
719 for iteration
in [1 .. i
] do
720 my_items
.copy_to
(target_string
, my_length
, 0, current_last
)
721 current_last
+= my_length
724 return target_string
.to_s_with_length
(final_length
)
727 redef fun to_s
do return self
731 # djb2 hash algorythm
736 var strStart
= _index_from
740 while i
>= strStart
do
741 h
= (h
* 32) + h
+ self._items
[i
].ascii
749 private class FlatStringReverseIterator
750 super IndexedIterator[Char]
754 var target_items
: NativeString
758 init with_pos
(tgt
: String, pos
: Int)
761 target_items
= tgt
.items
762 curr_pos
= pos
+ tgt
.index_from
765 redef fun is_ok
do return curr_pos
>= 0
767 redef fun item
do return target_items
[curr_pos
]
769 redef fun next
do curr_pos
-= 1
771 redef fun index
do return curr_pos
- target
.index_from
775 private class FlatStringIterator
776 super IndexedIterator[Char]
780 var target_items
: NativeString
784 init with_pos
(tgt
: String, pos
: Int)
787 target_items
= tgt
.items
788 curr_pos
= pos
+ target
.index_from
791 redef fun is_ok
do return curr_pos
<= target
.index_to
793 redef fun item
do return target_items
[curr_pos
]
795 redef fun next
do curr_pos
+= 1
797 redef fun index
do return curr_pos
- target
.index_from
801 private class FlatStringCharView
804 redef type SELFTYPE: String
808 # Check that the index (+ index_from) is not larger than indexTo
809 # In other terms, if the index is valid
811 assert (index
+ target
._index_from
) <= target
._index_to
812 return target
._items
[index
+ target
._index_from
]
815 redef fun iterator_from
(start
) do return new FlatStringIterator.with_pos
(target
, start
)
817 redef fun reverse_iterator_from
(start
) do return new FlatStringReverseIterator.with_pos
(target
, start
)
821 # Mutable strings of characters.
827 redef type OTHER: String
829 redef var chars
: BufferCharView = new FlatBufferCharView(self)
831 # Modifies the char contained at pos `index`
833 # DEPRECATED : Use self.chars.[]= instead
834 fun []=(index
: Int, item
: Char)
836 if index
== length
then
840 assert index
>= 0 and index
< length
844 # Adds a char `c` at the end of self
846 # DEPRECATED : Use self.chars.add instead
849 if _capacity
<= length
then enlarge
(length
+ 5)
855 fun clear
do _length
= 0
857 # Enlarges the subsequent array containing the chars of self
858 fun enlarge
(cap
: Int)
861 if cap
<= c
then return
862 while c
<= cap
do c
= c
* 2 + 2
863 var a
= calloc_string
(c
+1)
864 _items
.copy_to
(a
, length
, 0, 0)
867 items
.copy_to
(a
, length
, 0, 0)
870 redef fun to_s
: String
873 var a
= calloc_string
(l
+1)
874 _items
.copy_to
(a
, l
, 0, 0)
876 # Ensure the afterlast byte is '\0' to nul-terminated char *
879 return a
.to_s_with_length
(length
)
887 while i
< l1
and i
< l2
do
888 var c1
= self.chars
[i
].ascii
889 var c2
= s
.chars
[i
].ascii
904 # Create a new empty string.
912 _capacity
= s
.length
+ 1
914 _items
= calloc_string
(_capacity
)
915 s
.items
.copy_to
(_items
, _length
, s
._index_from
, 0)
918 # Create a new empty string with a given capacity.
919 init with_capacity
(cap
: Int)
922 # _items = new NativeString.calloc(cap)
923 _items
= calloc_string
(cap
+1)
928 # Adds the content of string `s` at the end of self
929 fun append
(s
: String)
932 if capacity
< length
+ sl
then enlarge
(length
+ sl
)
933 s
.items
.copy_to
(items
, sl
, s
.index_from
, length
)
939 if not o
isa Buffer then return false
941 if o
.length
!= l
then return false
946 if it
[i
] != oit
[i
] then return false
952 readable private var _capacity
: Int
954 # Copies the content of self in `dest`
955 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
957 var self_chars
= self.chars
958 var dest_chars
= dest
.chars
959 for i
in [0..len-1
] do
960 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
964 redef fun substring
(from
, count
)
968 if from
< 0 then from
= 0
969 if count
> length
then count
= length
971 var r
= new Buffer.with_capacity
(count
- from
)
972 while from
< count
do
973 r
.chars
.push
(_items
[from
])
983 private class FlatBufferReverseIterator
984 super IndexedIterator[Char]
988 var target_items
: NativeString
992 init with_pos
(tgt
: Buffer, pos
: Int)
995 target_items
= tgt
.items
999 redef fun index
do return curr_pos
1001 redef fun is_ok
do return curr_pos
>= 0
1003 redef fun item
do return target_items
[curr_pos
]
1005 redef fun next
do curr_pos
-= 1
1009 private class FlatBufferCharView
1010 super BufferCharView
1013 redef type SELFTYPE: Buffer
1015 redef fun [](index
) do return target
._items
[index
]
1017 redef fun []=(index
, item
)
1019 assert index
>= 0 and index
<= length
1020 if index
== length
then
1024 target
._items
[index
] = item
1037 fun enlarge
(cap
: Int)
1044 var my_items
= target
.items
1045 var s_length
= s
.length
1046 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
1049 redef fun iterator_from
(pos
) do return new FlatBufferIterator.with_pos
(target
, pos
)
1051 redef fun reverse_iterator_from
(pos
) do return new FlatBufferReverseIterator.with_pos
(target
, pos
)
1055 private class FlatBufferIterator
1056 super IndexedIterator[Char]
1060 var target_items
: NativeString
1064 init with_pos
(tgt
: Buffer, pos
: Int)
1067 target_items
= tgt
.items
1071 redef fun index
do return curr_pos
1073 redef fun is_ok
do return curr_pos
< target
.length
1075 redef fun item
do return target_items
[curr_pos
]
1077 redef fun next
do curr_pos
+= 1
1081 ###############################################################################
1083 ###############################################################################
1086 # User readable representation of `self`.
1087 fun to_s
: String do return inspect
1089 # The class name of the object in NativeString format.
1090 private fun native_class_name
: NativeString is intern
1092 # The class name of the object.
1094 # assert 5.class_name == "Int"
1095 fun class_name
: String do return native_class_name
.to_s
1097 # Developer readable representation of `self`.
1098 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
1101 return "<{inspect_head}>"
1104 # Return "CLASSNAME:#OBJECTID".
1105 # This function is mainly used with the redefinition of the inspect method
1106 protected fun inspect_head
: String
1108 return "{class_name}:#{object_id.to_hex}"
1111 protected fun args
: Sequence[String]
1118 # assert true.to_s == "true"
1119 # assert false.to_s == "false"
1131 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
1132 # assume < to_c max const of char
1133 fun fill_buffer
(s
: Buffer, base
: Int, signed
: Bool)
1140 else if self == 0 then
1147 var pos
= digit_count
(base
) - 1
1148 while pos
>= 0 and n
> 0 do
1149 s
.chars
[pos
] = (n
% base
).to_c
1155 # C function to convert an nit Int to a NativeString (char*)
1156 private fun native_int_to_s
(len
: Int): NativeString is extern "native_int_to_s"
1158 # return displayable int in base 10 and signed
1160 # assert 1.to_s == "1"
1161 # assert (-123).to_s == "-123"
1163 var len
= digit_count
(10)
1164 return native_int_to_s
(len
).to_s_with_length
(len
)
1167 # return displayable int in hexadecimal (unsigned (not now))
1168 fun to_hex
: String do return to_base
(16,false)
1170 # return displayable int in base base and signed
1171 fun to_base
(base
: Int, signed
: Bool): String
1173 var l
= digit_count
(base
)
1174 var s
= new Buffer.from
(" " * l
)
1175 fill_buffer
(s
, base
, signed
)
1181 # Pretty print self, print needoed decimals up to a max of 3.
1183 var str
= to_precision
( 3 )
1184 if is_inf
!= 0 or is_nan
then return str
1185 var len
= str
.length
1186 for i
in [0..len-1
] do
1188 var c
= str
.chars
[j
]
1191 else if c
== '.' then
1192 return str
.substring
( 0, j
+2 )
1194 return str
.substring
( 0, j
+1 )
1200 # `self` representation with `nb` digits after the '.'.
1201 fun to_precision
(nb
: Int): String
1203 if is_nan
then return "nan"
1205 var isinf
= self.is_inf
1208 else if isinf
== -1 then
1212 if nb
== 0 then return self.to_i
.to_s
1214 for i
in [0..nb
[ do f
= f
* 10.0
1221 if i
== 0 then return "0.0"
1225 var p1
= s
.substring
(0, s
.length-nb
)
1226 var p2
= s
.substring
(s
.length-nb
, nb
)
1227 return p1
+ "." + p2
1229 return "0." + ("0"*(nb-sl
)) + s
1233 fun to_precision_native
(nb
: Int): String import NativeString.to_s
`{
1237 size = snprintf(NULL, 0, "%.*f", (int)nb, recv);
1238 str = malloc(size + 1);
1239 sprintf(str, "%.*f", (int)nb, recv );
1241 return NativeString_to_s( str );
1246 # assert 'x'.to_s == "x"
1249 var s
= new Buffer.with_capacity
(1)
1254 # Returns true if the char is a numerical digit
1255 fun is_numeric
: Bool
1257 if self >= '0' and self <= '9'
1264 # Returns true if the char is an alpha digit
1267 if (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z') then return true
1271 # Returns true if the char is an alpha or a numeric digit
1272 fun is_alphanumeric
: Bool
1274 if self.is_numeric
or self.is_alpha
then return true
1279 redef class Collection[E
]
1280 # Concatenate elements.
1284 for e
in self do if e
!= null then s
.append
(e
.to_s
)
1288 # Concatenate and separate each elements with `sep`.
1290 # assert [1, 2, 3].join(":") == "1:2:3"
1291 # assert [1..3].join(":") == "1:2:3"
1292 fun join
(sep
: String): String
1294 if is_empty
then return ""
1296 var s
= new Buffer # Result
1301 if e
!= null then s
.append
(e
.to_s
)
1303 # Concat other items
1308 if e
!= null then s
.append
(e
.to_s
)
1315 redef class Array[E
]
1316 # Fast implementation
1324 if e
!= null then s
.append
(e
.to_s
)
1331 redef class Map[K
,V
]
1332 # Concatenate couple of 'key value'.
1333 # key and value are separated by `couple_sep`.
1334 # each couple is separated each couple with `sep`.
1336 # var m = new ArrayMap[Int, String]
1339 # assert m.join("; ", "=") == "1=one; 10=ten"
1340 fun join
(sep
: String, couple_sep
: String): String
1342 if is_empty
then return ""
1344 var s
= new Buffer # Result
1350 s
.append
("{k}{couple_sep}{e or else "<null>"}")
1352 # Concat other items
1358 s
.append
("{k}{couple_sep}{e or else "<null>"}")
1365 ###############################################################################
1367 ###############################################################################
1369 # Native strings are simple C char *
1373 fun [](index
: Int): Char is intern
1374 fun []=(index
: Int, item
: Char) is intern
1375 fun copy_to
(dest
: NativeString, length
: Int, from
: Int, to
: Int) is intern
1377 # Position of the first nul character.
1378 fun cstring_length
: Int
1381 while self[l
] != '\0' do l
+= 1
1384 fun atoi
: Int is intern
1385 fun atof
: Float is extern "atof"
1389 return to_s_with_length
(cstring_length
)
1392 fun to_s_with_length
(length
: Int): String
1395 return new String.with_infos
(self, length
, 0, length
- 1)
1398 fun to_s_with_copy
: String
1400 var length
= cstring_length
1401 var new_self
= calloc_string
(length
+ 1)
1402 copy_to
(new_self
, length
, 0, 0)
1403 return new String.with_infos
(new_self
, length
, 0, length
- 1)
1408 # StringCapable objects can create native strings
1409 interface StringCapable
1410 protected fun calloc_string
(size
: Int): NativeString is intern
1414 var _args_cache
: nullable Sequence[String]
1416 redef fun args
: Sequence[String]
1418 if _args_cache
== null then init_args
1419 return _args_cache
.as(not null)
1422 # The name of the program as given by the OS
1423 fun program_name
: String
1425 return native_argv
(0).to_s
1428 # Initialize `args` with the contents of `native_argc` and `native_argv`.
1429 private fun init_args
1431 var argc
= native_argc
1432 var args
= new Array[String].with_capacity
(0)
1435 args
[i-1
] = native_argv
(i
).to_s
1441 # First argument of the main C function.
1442 private fun native_argc
: Int is intern
1444 # Second argument of the main C function.
1445 private fun native_argv
(i
: Int): NativeString is intern