0d968c2abbf435f0dfa4de069490fdb06ea85aec
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.
826 super AbstractArray[Char]
828 redef type OTHER: String
830 redef var chars
: BufferCharView = new FlatBufferCharView(self)
832 redef fun []=(index
, item
)
834 if index
== length
then
838 assert index
>= 0 and index
< length
844 if _capacity
<= length
then enlarge
(length
+ 5)
849 redef fun enlarge
(cap
)
852 if cap
<= c
then return
853 while c
<= cap
do c
= c
* 2 + 2
854 var a
= calloc_string
(c
+1)
855 _items
.copy_to
(a
, length
, 0, 0)
864 if _capacity
< _length
+ sl
then enlarge
(_length
+ sl
)
865 s
.items
.copy_to
(_items
, sl
, s
._index_from
, _length
)
872 redef fun to_s
: String
875 var a
= calloc_string
(l
+1)
876 _items
.copy_to
(a
, l
, 0, 0)
878 # Ensure the afterlast byte is '\0' to nul-terminated char *
881 return a
.to_s_with_length
(length
)
889 while i
< l1
and i
< l2
do
890 var c1
= self.chars
[i
].ascii
891 var c2
= s
.chars
[i
].ascii
906 # Create a new empty string.
914 _capacity
= s
.length
+ 1
916 _items
= calloc_string
(_capacity
)
917 s
.items
.copy_to
(_items
, _length
, s
._index_from
, 0)
920 # Create a new empty string with a given capacity.
921 init with_capacity
(cap
: Int)
924 # _items = new NativeString.calloc(cap)
925 _items
= calloc_string
(cap
+1)
932 if not o
isa Buffer then return false
934 if o
.length
!= l
then return false
939 if it
[i
] != oit
[i
] then return false
945 readable private var _capacity
: Int
947 redef fun substring
(from
, count
)
951 if from
< 0 then from
= 0
952 if count
> length
then count
= length
954 var r
= new Buffer.with_capacity
(count
- from
)
955 while from
< count
do
956 r
.chars
.push
(_items
[from
])
966 private class FlatBufferReverseIterator
967 super IndexedIterator[Char]
971 var target_items
: NativeString
975 init with_pos
(tgt
: Buffer, pos
: Int)
978 target_items
= tgt
.items
982 redef fun index
do return curr_pos
984 redef fun is_ok
do return curr_pos
>= 0
986 redef fun item
do return target_items
[curr_pos
]
988 redef fun next
do curr_pos
-= 1
992 private class FlatBufferCharView
996 redef type SELFTYPE: Buffer
998 redef fun [](index
) do return target
._items
[index
]
1000 redef fun []=(index
, item
)
1002 assert index
>= 0 and index
<= length
1003 if index
== length
then
1007 target
._items
[index
] = item
1020 fun enlarge
(cap
: Int)
1027 var my_items
= target
.items
1028 var s_length
= s
.length
1029 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
1032 redef fun iterator_from
(pos
) do return new FlatBufferIterator.with_pos
(target
, pos
)
1034 redef fun reverse_iterator_from
(pos
) do return new FlatBufferReverseIterator.with_pos
(target
, pos
)
1038 private class FlatBufferIterator
1039 super IndexedIterator[Char]
1043 var target_items
: NativeString
1047 init with_pos
(tgt
: Buffer, pos
: Int)
1050 target_items
= tgt
.items
1054 redef fun index
do return curr_pos
1056 redef fun is_ok
do return curr_pos
< target
.length
1058 redef fun item
do return target_items
[curr_pos
]
1060 redef fun next
do curr_pos
+= 1
1064 ###############################################################################
1066 ###############################################################################
1069 # User readable representation of `self`.
1070 fun to_s
: String do return inspect
1072 # The class name of the object in NativeString format.
1073 private fun native_class_name
: NativeString is intern
1075 # The class name of the object.
1077 # assert 5.class_name == "Int"
1078 fun class_name
: String do return native_class_name
.to_s
1080 # Developer readable representation of `self`.
1081 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
1084 return "<{inspect_head}>"
1087 # Return "CLASSNAME:#OBJECTID".
1088 # This function is mainly used with the redefinition of the inspect method
1089 protected fun inspect_head
: String
1091 return "{class_name}:#{object_id.to_hex}"
1094 protected fun args
: Sequence[String]
1101 # assert true.to_s == "true"
1102 # assert false.to_s == "false"
1114 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
1115 # assume < to_c max const of char
1116 fun fill_buffer
(s
: Buffer, base
: Int, signed
: Bool)
1123 else if self == 0 then
1130 var pos
= digit_count
(base
) - 1
1131 while pos
>= 0 and n
> 0 do
1132 s
.chars
[pos
] = (n
% base
).to_c
1138 # C function to convert an nit Int to a NativeString (char*)
1139 private fun native_int_to_s
(len
: Int): NativeString is extern "native_int_to_s"
1141 # return displayable int in base 10 and signed
1143 # assert 1.to_s == "1"
1144 # assert (-123).to_s == "-123"
1146 var len
= digit_count
(10)
1147 return native_int_to_s
(len
).to_s_with_length
(len
)
1150 # return displayable int in hexadecimal (unsigned (not now))
1151 fun to_hex
: String do return to_base
(16,false)
1153 # return displayable int in base base and signed
1154 fun to_base
(base
: Int, signed
: Bool): String
1156 var l
= digit_count
(base
)
1157 var s
= new Buffer.from
(" " * l
)
1158 fill_buffer
(s
, base
, signed
)
1164 # Pretty print self, print needoed decimals up to a max of 3.
1166 var str
= to_precision
( 3 )
1167 if is_inf
!= 0 or is_nan
then return str
1168 var len
= str
.length
1169 for i
in [0..len-1
] do
1171 var c
= str
.chars
[j
]
1174 else if c
== '.' then
1175 return str
.substring
( 0, j
+2 )
1177 return str
.substring
( 0, j
+1 )
1183 # `self` representation with `nb` digits after the '.'.
1184 fun to_precision
(nb
: Int): String
1186 if is_nan
then return "nan"
1188 var isinf
= self.is_inf
1191 else if isinf
== -1 then
1195 if nb
== 0 then return self.to_i
.to_s
1197 for i
in [0..nb
[ do f
= f
* 10.0
1204 if i
== 0 then return "0.0"
1208 var p1
= s
.substring
(0, s
.length-nb
)
1209 var p2
= s
.substring
(s
.length-nb
, nb
)
1210 return p1
+ "." + p2
1212 return "0." + ("0"*(nb-sl
)) + s
1216 fun to_precision_native
(nb
: Int): String import NativeString.to_s
`{
1220 size = snprintf(NULL, 0, "%.*f", (int)nb, recv);
1221 str = malloc(size + 1);
1222 sprintf(str, "%.*f", (int)nb, recv );
1224 return NativeString_to_s( str );
1229 # assert 'x'.to_s == "x"
1232 var s
= new Buffer.with_capacity
(1)
1237 # Returns true if the char is a numerical digit
1238 fun is_numeric
: Bool
1240 if self >= '0' and self <= '9'
1247 # Returns true if the char is an alpha digit
1250 if (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z') then return true
1254 # Returns true if the char is an alpha or a numeric digit
1255 fun is_alphanumeric
: Bool
1257 if self.is_numeric
or self.is_alpha
then return true
1262 redef class Collection[E
]
1263 # Concatenate elements.
1267 for e
in self do if e
!= null then s
.append
(e
.to_s
)
1271 # Concatenate and separate each elements with `sep`.
1273 # assert [1, 2, 3].join(":") == "1:2:3"
1274 # assert [1..3].join(":") == "1:2:3"
1275 fun join
(sep
: String): String
1277 if is_empty
then return ""
1279 var s
= new Buffer # Result
1284 if e
!= null then s
.append
(e
.to_s
)
1286 # Concat other items
1291 if e
!= null then s
.append
(e
.to_s
)
1298 redef class Array[E
]
1299 # Fast implementation
1307 if e
!= null then s
.append
(e
.to_s
)
1314 redef class Map[K
,V
]
1315 # Concatenate couple of 'key value'.
1316 # key and value are separated by `couple_sep`.
1317 # each couple is separated each couple with `sep`.
1319 # var m = new ArrayMap[Int, String]
1322 # assert m.join("; ", "=") == "1=one; 10=ten"
1323 fun join
(sep
: String, couple_sep
: String): String
1325 if is_empty
then return ""
1327 var s
= new Buffer # Result
1333 s
.append
("{k}{couple_sep}{e or else "<null>"}")
1335 # Concat other items
1341 s
.append
("{k}{couple_sep}{e or else "<null>"}")
1348 ###############################################################################
1350 ###############################################################################
1352 # Native strings are simple C char *
1356 fun [](index
: Int): Char is intern
1357 fun []=(index
: Int, item
: Char) is intern
1358 fun copy_to
(dest
: NativeString, length
: Int, from
: Int, to
: Int) is intern
1360 # Position of the first nul character.
1361 fun cstring_length
: Int
1364 while self[l
] != '\0' do l
+= 1
1367 fun atoi
: Int is intern
1368 fun atof
: Float is extern "atof"
1372 return to_s_with_length
(cstring_length
)
1375 fun to_s_with_length
(length
: Int): String
1378 return new String.with_infos
(self, length
, 0, length
- 1)
1381 fun to_s_with_copy
: String
1383 var length
= cstring_length
1384 var new_self
= calloc_string
(length
+ 1)
1385 copy_to
(new_self
, length
, 0, 0)
1386 return new String.with_infos
(new_self
, length
, 0, length
- 1)
1391 # StringCapable objects can create native strings
1392 interface StringCapable
1393 protected fun calloc_string
(size
: Int): NativeString is intern
1397 var _args_cache
: nullable Sequence[String]
1399 redef fun args
: Sequence[String]
1401 if _args_cache
== null then init_args
1402 return _args_cache
.as(not null)
1405 # The name of the program as given by the OS
1406 fun program_name
: String
1408 return native_argv
(0).to_s
1411 # Initialize `args` with the contents of `native_argc` and `native_argv`.
1412 private fun init_args
1414 var argc
= native_argc
1415 var args
= new Array[String].with_capacity
(0)
1418 args
[i-1
] = native_argv
(i
).to_s
1424 # First argument of the main C function.
1425 private fun native_argc
: Int is intern
1427 # Second argument of the main C function.
1428 private fun native_argv
(i
: Int): NativeString is intern