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 # Common subclass for String and Buffer
29 abstract class AbstractString
30 super AbstractArrayRead[Char]
32 readable private var _items
: NativeString
34 fun chars
: StringCharView is abstract
36 # Access a character at `index` in the string.
38 # assert "abcd"[2] == 'c'
39 redef fun [](index
) do return _items
[index
]
43 # assert "abcd".substring(1, 2) == "bc"
44 # assert "abcd".substring(-1, 2) == "a"
45 # assert "abcd".substring(1, 0) == ""
46 # assert "abcd".substring(2, 5) == "cd"
48 # A `from` index < 0 will be replaced by 0.
49 # Unless a `count` value is > 0 at the same time.
50 # In this case, `from += count` and `count -= from`.
51 fun substring
(from
: Int, count
: Int): String is abstract
53 # Create a substring from `self` beginning at the `from` position
55 # assert "abcd".substring_from(1) == "bcd"
56 # assert "abcd".substring_from(-1) == "abcd"
57 # assert "abcd".substring_from(2) == "cd"
59 # As with substring, a `from` index < 0 will be replaced by 0
60 fun substring_from
(from
: Int): String
63 return substring
(from
, length
- from
)
66 # Does self have a substring `str` starting from position `pos`?
68 # assert "abcd".has_substring("bc",1) == true
69 # assert "abcd".has_substring("bc",2) == false
70 fun has_substring
(str
: String, pos
: Int): Bool
72 var itsindex
= str
.length
- 1
73 var myindex
= pos
+ itsindex
75 var itsitems
= str
._items
76 if myindex
> length
or itsindex
> myindex
then return false
77 var its_index_from
= str
._index_from
78 itsindex
+= its_index_from
79 while itsindex
>= its_index_from
do
80 if myitems
[myindex
] != itsitems
[itsindex
] then return false
87 # Is this string prefixed by `prefix`?
89 # assert "abcd".has_prefix("ab") == true
90 # assert "abcbc".has_prefix("bc") == false
91 # assert "ab".has_prefix("abcd") == false
92 fun has_prefix
(prefix
: String): Bool do return has_substring
(prefix
,0)
94 # Is this string suffixed by `suffix`?
96 # assert "abcd".has_suffix("abc") == false
97 # assert "abcd".has_suffix("bcd") == true
98 fun has_suffix
(suffix
: String): Bool do return has_substring
(suffix
, length
- suffix
.length
)
100 # If `self` contains only digits, return the corresponding integer
102 # assert "123".to_i == 123
103 # assert "-1".to_i == -1
107 return to_s
.to_cstring
.atoi
110 # If `self` contains a float, return the corresponding float
112 # assert "123".to_f == 123.0
113 # assert "-1".to_f == -1.0
114 # assert "-1.2e-3".to_f == -0.0012
118 return to_s
.to_cstring
.atof
121 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
122 fun to_hex
: Int do return a_to
(16)
124 # If `self` contains only digits and letters, return the corresponding integer in a given base
126 # assert "120".a_to(3) == 15
127 fun a_to
(base
: Int) : Int
154 # Returns `true` if the string contains only Numeric values (and one "," or one "." character)
156 # assert "123".is_numeric == true
157 # assert "1.2".is_numeric == true
158 # assert "1,2".is_numeric == true
159 # assert "1..2".is_numeric == false
162 var has_point_or_comma
= false
167 if (i
== '.' or i
== ',') and not has_point_or_comma
169 has_point_or_comma
= true
178 # A upper case version of `self`
180 # assert "Hello World!".to_upper == "HELLO WORLD!"
183 var s
= new Buffer.with_capacity
(length
)
184 for i
in self.chars
do s
.add
(i
.to_upper
)
188 # A lower case version of `self`
190 # assert "Hello World!".to_lower == "hello world!"
191 fun to_lower
: String
193 var s
= new Buffer.with_capacity
(length
)
194 for i
in self.chars
do s
.add
(i
.to_lower
)
198 # Trims trailing and preceding white spaces
199 # A whitespace is defined as any character which ascii value is less than or equal to 32
201 # assert " Hello World ! ".trim == "Hello World !"
202 # assert "\na\nb\tc\t".trim == "a\nb\tc"
205 if self.length
== 0 then return self.to_s
206 # find position of the first non white space char (ascii < 32) from the start of the string
208 while self.chars
[start_pos
].ascii
<= 32 do
210 if start_pos
== length
then return ""
212 # find position of the first non white space char from the end of the string
213 var end_pos
= length
- 1
214 while self.chars
[end_pos
].ascii
<= 32 do
216 if end_pos
== start_pos
then return self.chars
[start_pos
].to_s
218 return self.substring
(start_pos
, end_pos
- start_pos
+ 1)
230 # Mangle a string to be a unique string only made of alphanumeric characters
231 fun to_cmangle
: String
234 var underscore
= false
235 for c
in self.chars
do
236 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
242 res
.append
('_'.ascii
.to_s
)
245 if c
>= '0' and c
<= '9' then
248 else if c
== '_' then
253 res
.append
(c
.ascii
.to_s
)
261 # Escape " \ ' and non printable characters using the rules of literal C strings and characters
263 # assert "abAB12<>&".escape_to_c == "abAB12<>&"
264 # assert "\n\"'\\".escape_to_c == "\\n\\\"\\'\\\\"
265 fun escape_to_c
: String
268 for c
in self.chars
do
271 else if c
== '\0' then
273 else if c
== '"' then
275 else if c == '\'' then
277 else if c == '\\
' then
279 else if c.ascii < 32 then
280 b.append("\\{c.ascii.to_base(8, false)}")
288 # Escape additionnal characters
289 # The result might no be legal in C but be used in other languages
291 # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
292 fun escape_more_to_c(chars: String): String
295 for c in escape_to_c do
304 # Escape to c plus braces
306 # assert "\n\"'\\\
{\}".escape_to_nit == "\\n\\\
"\\'\\\\\\\{\\\}"
307 fun escape_to_nit
: String do return escape_more_to_c
("\{\}")
309 # Return a string where Nit escape sequences are transformed.
313 # assert s.length == 2
314 # var u = s.unescape_nit
315 # assert u.length == 1
316 # assert u[0].ascii == 10 # (the ASCII value of the "new line" character)
317 fun unescape_nit
: String
319 var res
= new Buffer.with_capacity
(self.length
)
320 var was_slash
= false
322 if not was_slash
then
333 else if c
== 'r' then
335 else if c
== 't' then
337 else if c
== '0' then
347 # Abstract class for the SequenceRead compatible
348 # views on String and Buffer objects
349 abstract class StringCharView
350 super SequenceRead[Char]
352 type SELFTYPE: AbstractString
354 private var target
: SELFTYPE
356 private init(tgt
: SELFTYPE)
361 redef fun is_empty
do return target
.is_empty
363 redef fun length
do return target
.length
365 redef fun iterator
: IndexedIterator[Char] do return self.iterator_from
(0)
367 # Gets a new Iterator starting at position `pos`
370 # var iter = "abcd".iterator_from(2)
371 # while iter.is_ok do
377 fun iterator_from
(pos
: Int): IndexedIterator[Char] is abstract
379 # Gets an iterator starting at the end and going backwards
382 # var reviter = "now step live...".reverse_iterator
383 # while reviter.is_ok do
384 # printn reviter.item
388 # Outputs : ...evil pets won
389 fun reverse_iterator
: IndexedIterator[Char] do return self.reverse_iterator_from
(self.length
- 1)
391 # Gets an iterator on the chars of self starting from `pos`
394 # var iter = "abcd".reverse_iterator_from(1)
395 # while iter.is_ok do
401 fun reverse_iterator_from
(pos
: Int): IndexedIterator[Char] is abstract
403 redef fun has
(c
: Char): Bool
406 if i
== c
then return true
413 # View on Buffer objects, extends Sequence
414 # for mutation operations
415 abstract class BufferCharView
419 redef type SELFTYPE: Buffer
423 # Immutable strings of characters.
429 redef type OTHER: String
431 # Index in _items of the start of the string
432 readable var _index_from
: Int
434 # Indes in _items of the last item of the string
435 readable var _index_to
: Int
437 redef var chars
: StringCharView = new FlatStringCharView(self)
439 ################################################
440 # AbstractString specific methods #
441 ################################################
443 redef fun [](index
) do
445 # Check that the index (+ index_from) is not larger than indexTo
446 # In other terms, if the index is valid
447 assert (index
+ _index_from
) <= _index_to
448 return _items
[index
+ _index_from
]
451 redef fun substring
(from
: Int, count
: Int): String
457 if count
< 0 then count
= 0
461 var realFrom
= _index_from
+ from
463 if (realFrom
+ count
) > _index_to
then return new String.with_infos
(_items
, _index_to
- realFrom
+ 1, realFrom
, _index_to
)
465 if count
== 0 then return ""
467 var to
= realFrom
+ count
- 1
469 return new String.with_infos
(_items
, to
- realFrom
+ 1, realFrom
, to
)
472 redef fun substring_from
(from
: Int): String
474 if from
> _length
then return ""
475 if from
< 0 then from
= 0
476 return substring
(from
, _length
)
479 redef fun has_substring
(str
: String, pos
: Int): Bool
481 var itsindex
= str
._length
- 1
483 var myindex
= pos
+ itsindex
486 var itsitems
= str
._items
488 if myindex
> _length
or itsindex
> myindex
then return false
490 var itsindexfrom
= str
.index_from
491 itsindex
+= itsindexfrom
492 myindex
+= index_from
494 while itsindex
>= itsindexfrom
do
495 if myitems
[myindex
] != itsitems
[itsindex
] then return false
503 redef fun to_upper
: String
505 var outstr
= calloc_string
(self._length
+ 1)
508 var myitems
= self._items
509 var index_from
= self._index_from
510 var max
= self._index_to
512 while index_from
<= max
do
513 outstr
[out_index
] = myitems
[index_from
].to_upper
518 outstr
[self.length
] = '\0'
520 return outstr
.to_s_with_length
(self._length
)
523 redef fun to_lower
: String
525 var outstr
= calloc_string
(self._length
+ 1)
528 var myitems
= self._items
529 var index_from
= self._index_from
530 var max
= self._index_to
532 while index_from
<= max
do
533 outstr
[out_index
] = myitems
[index_from
].to_lower
538 outstr
[self.length
] = '\0'
540 return outstr
.to_s_with_length
(self._length
)
543 redef fun trim
: String
545 if self._length
== 0 then return self
546 # find position of the first non white space char (ascii < 32) from the start of the string
547 var start_pos
= self._index_from
548 while _items
[start_pos
].ascii
<= 32 do
550 if start_pos
== _index_to
+ 1 then return ""
552 # find position of the first non white space char from the end of the string
553 var end_pos
= _index_to
554 while _items
[end_pos
].ascii
<= 32 do
556 if end_pos
== start_pos
then return _items
[start_pos
].to_s
558 start_pos
-= index_from
559 end_pos
-= index_from
560 return self.substring
(start_pos
, end_pos
- start_pos
+ 1)
565 var i
= self._index_from
566 var imax
= self._index_to
573 ##################################################
574 # String Specific Methods #
575 ##################################################
577 private init with_infos
(items
: NativeString, len
: Int, from
: Int, to
: Int)
585 # Return a null terminated char *
586 fun to_cstring
: NativeString
588 if _index_from
> 0 or _index_to
!= items
.cstring_length
- 1 then
589 var newItems
= calloc_string
(_length
+ 1)
590 self.items
.copy_to
(newItems
, _length
, _index_from
, 0)
591 newItems
[length
] = '\0'
599 if not other
isa String then return false
601 if self.object_id
== other
.object_id
then return true
603 var my_length
= _length
605 if other
._length
!= my_length
then return false
607 var my_index
= _index_from
608 var its_index
= other
._index_from
610 var last_iteration
= my_index
+ my_length
612 var itsitems
= other
._items
613 var myitems
= self._items
615 while my_index
< last_iteration
do
616 if myitems
[my_index
] != itsitems
[its_index
] then return false
624 # The comparison between two strings is done on a lexicographical basis
626 # assert ("aa" < "b") == true
629 if self.object_id
== other
.object_id
then return false
631 var my_curr_char
: Char
632 var its_curr_char
: Char
634 var curr_id_self
= self._index_from
635 var curr_id_other
= other
._index_from
637 var my_items
= self._items
638 var its_items
= other
._items
640 var my_length
= self._length
641 var its_length
= other
._length
643 var max_iterations
= curr_id_self
+ my_length
645 while curr_id_self
< max_iterations
do
646 my_curr_char
= my_items
[curr_id_self
]
647 its_curr_char
= its_items
[curr_id_other
]
649 if my_curr_char
!= its_curr_char
then
650 if my_curr_char
< its_curr_char
then return true
658 return my_length
< its_length
661 # The concatenation of `self` with `s`
663 # assert "hello " + "world!" == "hello world!"
664 fun +(s
: String): String
666 var my_length
= self._length
667 var its_length
= s
._length
669 var total_length
= my_length
+ its_length
671 var target_string
= calloc_string
(my_length
+ its_length
+ 1)
673 self._items
.copy_to
(target_string
, my_length
, _index_from
, 0)
674 s
._items
.copy_to
(target_string
, its_length
, s
._index_from
, my_length
)
676 target_string
[total_length
] = '\0'
678 return target_string
.to_s_with_length
(total_length
)
681 # `i` repetitions of `self`
683 # assert "abc"*3 == "abcabcabc"
684 # assert "abc"*1 == "abc"
685 # assert "abc"*0 == ""
686 fun *(i
: Int): String
690 var my_length
= self._length
692 var final_length
= my_length
* i
694 var my_items
= self._items
696 var target_string
= calloc_string
((final_length
) + 1)
698 target_string
[final_length
] = '\0'
702 for iteration
in [1 .. i
] do
703 my_items
.copy_to
(target_string
, my_length
, 0, current_last
)
704 current_last
+= my_length
707 return target_string
.to_s_with_length
(final_length
)
710 redef fun to_s
do return self
714 # djb2 hash algorythm
719 var strStart
= _index_from
723 while i
>= strStart
do
724 h
= (h
* 32) + h
+ self._items
[i
].ascii
732 private class FlatStringReverseIterator
733 super IndexedIterator[Char]
737 var target_items
: NativeString
741 init with_pos
(tgt
: String, pos
: Int)
744 target_items
= tgt
.items
745 curr_pos
= pos
+ tgt
.index_from
748 redef fun is_ok
do return curr_pos
>= 0
750 redef fun item
do return target_items
[curr_pos
]
752 redef fun next
do curr_pos
-= 1
754 redef fun index
do return curr_pos
- target
.index_from
758 private class FlatStringIterator
759 super IndexedIterator[Char]
763 var target_items
: NativeString
767 init with_pos
(tgt
: String, pos
: Int)
770 target_items
= tgt
.items
771 curr_pos
= pos
+ target
.index_from
774 redef fun is_ok
do return curr_pos
<= target
.index_to
776 redef fun item
do return target_items
[curr_pos
]
778 redef fun next
do curr_pos
+= 1
780 redef fun index
do return curr_pos
- target
.index_from
784 private class FlatStringCharView
787 redef type SELFTYPE: String
791 # Check that the index (+ index_from) is not larger than indexTo
792 # In other terms, if the index is valid
794 assert (index
+ target
._index_from
) <= target
._index_to
795 return target
._items
[index
+ target
._index_from
]
798 redef fun iterator_from
(start
) do return new FlatStringIterator.with_pos
(target
, start
)
800 redef fun reverse_iterator_from
(start
) do return new FlatStringReverseIterator.with_pos
(target
, start
)
804 # Mutable strings of characters.
809 super AbstractArray[Char]
811 redef type OTHER: String
813 redef var chars
: BufferCharView = new FlatBufferCharView(self)
815 redef fun []=(index
, item
)
817 if index
== length
then
821 assert index
>= 0 and index
< length
827 if _capacity
<= length
then enlarge
(length
+ 5)
832 redef fun enlarge
(cap
)
835 if cap
<= c
then return
836 while c
<= cap
do c
= c
* 2 + 2
837 var a
= calloc_string
(c
+1)
838 _items
.copy_to
(a
, length
, 0, 0)
847 if _capacity
< _length
+ sl
then enlarge
(_length
+ sl
)
848 s
.items
.copy_to
(_items
, sl
, s
._index_from
, _length
)
855 redef fun to_s
: String
858 var a
= calloc_string
(l
+1)
859 _items
.copy_to
(a
, l
, 0, 0)
861 # Ensure the afterlast byte is '\0' to nul-terminated char *
864 return a
.to_s_with_length
(length
)
872 while i
< l1
and i
< l2
do
873 var c1
= self.chars
[i
].ascii
874 var c2
= s
.chars
[i
].ascii
889 # Create a new empty string.
897 _capacity
= s
.length
+ 1
899 _items
= calloc_string
(_capacity
)
900 s
.items
.copy_to
(_items
, _length
, s
._index_from
, 0)
903 # Create a new empty string with a given capacity.
904 init with_capacity
(cap
: Int)
907 # _items = new NativeString.calloc(cap)
908 _items
= calloc_string
(cap
+1)
915 if not o
isa Buffer then return false
917 if o
.length
!= l
then return false
922 if it
[i
] != oit
[i
] then return false
928 readable private var _capacity
: Int
930 redef fun substring
(from
, count
)
934 if from
< 0 then from
= 0
935 if count
> length
then count
= length
937 var r
= new Buffer.with_capacity
(count
- from
)
938 while from
< count
do
939 r
.chars
.push
(_items
[from
])
950 private class FlatBufferReverseIterator
951 super IndexedIterator[Char]
955 var target_items
: NativeString
959 init with_pos
(tgt
: Buffer, pos
: Int)
962 target_items
= tgt
.items
966 redef fun index
do return curr_pos
968 redef fun is_ok
do return curr_pos
>= 0
970 redef fun item
do return target_items
[curr_pos
]
972 redef fun next
do curr_pos
-= 1
976 private class FlatBufferCharView
980 redef type SELFTYPE: Buffer
982 redef fun [](index
) do return target
._items
[index
]
984 redef fun []=(index
, item
)
986 assert index
>= 0 and index
<= length
987 if index
== length
then
991 target
._items
[index
] = item
1004 fun enlarge
(cap
: Int)
1011 var my_items
= target
.items
1012 var s_length
= s
.length
1013 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
1016 redef fun iterator_from
(pos
) do return new FlatBufferIterator.with_pos
(target
, pos
)
1018 redef fun reverse_iterator_from
(pos
) do return new FlatBufferReverseIterator.with_pos
(target
, pos
)
1022 private class FlatBufferIterator
1023 super IndexedIterator[Char]
1027 var target_items
: NativeString
1031 init with_pos
(tgt
: Buffer, pos
: Int)
1034 target_items
= tgt
.items
1038 redef fun index
do return curr_pos
1040 redef fun is_ok
do return curr_pos
< target
.length
1042 redef fun item
do return target_items
[curr_pos
]
1044 redef fun next
do curr_pos
+= 1
1048 ###############################################################################
1050 ###############################################################################
1053 # User readable representation of `self`.
1054 fun to_s
: String do return inspect
1056 # The class name of the object in NativeString format.
1057 private fun native_class_name
: NativeString is intern
1059 # The class name of the object.
1061 # assert 5.class_name == "Int"
1062 fun class_name
: String do return native_class_name
.to_s
1064 # Developer readable representation of `self`.
1065 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
1068 return "<{inspect_head}>"
1071 # Return "CLASSNAME:#OBJECTID".
1072 # This function is mainly used with the redefinition of the inspect method
1073 protected fun inspect_head
: String
1075 return "{class_name}:#{object_id.to_hex}"
1078 protected fun args
: Sequence[String]
1085 # assert true.to_s == "true"
1086 # assert false.to_s == "false"
1098 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
1099 # assume < to_c max const of char
1100 fun fill_buffer
(s
: Buffer, base
: Int, signed
: Bool)
1107 else if self == 0 then
1114 var pos
= digit_count
(base
) - 1
1115 while pos
>= 0 and n
> 0 do
1116 s
.chars
[pos
] = (n
% base
).to_c
1122 # C function to convert an nit Int to a NativeString (char*)
1123 private fun native_int_to_s
(len
: Int): NativeString is extern "native_int_to_s"
1125 # return displayable int in base 10 and signed
1127 # assert 1.to_s == "1"
1128 # assert (-123).to_s == "-123"
1130 var len
= digit_count
(10)
1131 return native_int_to_s
(len
).to_s_with_length
(len
)
1134 # return displayable int in hexadecimal (unsigned (not now))
1135 fun to_hex
: String do return to_base
(16,false)
1137 # return displayable int in base base and signed
1138 fun to_base
(base
: Int, signed
: Bool): String
1140 var l
= digit_count
(base
)
1141 var s
= new Buffer.from
(" " * l
)
1142 fill_buffer
(s
, base
, signed
)
1148 # Pretty print self, print needoed decimals up to a max of 3.
1150 var str
= to_precision
( 3 )
1151 if is_inf
!= 0 or is_nan
then return str
1152 var len
= str
.length
1153 for i
in [0..len-1
] do
1155 var c
= str
.chars
[j
]
1158 else if c
== '.' then
1159 return str
.substring
( 0, j
+2 )
1161 return str
.substring
( 0, j
+1 )
1167 # `self` representation with `nb` digits after the '.'.
1168 fun to_precision
(nb
: Int): String
1170 if is_nan
then return "nan"
1172 var isinf
= self.is_inf
1175 else if isinf
== -1 then
1179 if nb
== 0 then return self.to_i
.to_s
1181 for i
in [0..nb
[ do f
= f
* 10.0
1188 if i
== 0 then return "0.0"
1192 var p1
= s
.substring
(0, s
.length-nb
)
1193 var p2
= s
.substring
(s
.length-nb
, nb
)
1194 return p1
+ "." + p2
1196 return "0." + ("0"*(nb-sl
)) + s
1200 fun to_precision_native
(nb
: Int): String import NativeString.to_s
`{
1204 size = snprintf(NULL, 0, "%.*f", (int)nb, recv);
1205 str = malloc(size + 1);
1206 sprintf(str, "%.*f", (int)nb, recv );
1208 return NativeString_to_s( str );
1213 # assert 'x'.to_s == "x"
1216 var s
= new Buffer.with_capacity
(1)
1221 # Returns true if the char is a numerical digit
1222 fun is_numeric
: Bool
1224 if self >= '0' and self <= '9'
1231 # Returns true if the char is an alpha digit
1234 if (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z') then return true
1238 # Returns true if the char is an alpha or a numeric digit
1239 fun is_alphanumeric
: Bool
1241 if self.is_numeric
or self.is_alpha
then return true
1246 redef class Collection[E
]
1247 # Concatenate elements.
1251 for e
in self do if e
!= null then s
.append
(e
.to_s
)
1255 # Concatenate and separate each elements with `sep`.
1257 # assert [1, 2, 3].join(":") == "1:2:3"
1258 # assert [1..3].join(":") == "1:2:3"
1259 fun join
(sep
: String): String
1261 if is_empty
then return ""
1263 var s
= new Buffer # Result
1268 if e
!= null then s
.append
(e
.to_s
)
1270 # Concat other items
1275 if e
!= null then s
.append
(e
.to_s
)
1282 redef class Array[E
]
1283 # Fast implementation
1291 if e
!= null then s
.append
(e
.to_s
)
1298 redef class Map[K
,V
]
1299 # Concatenate couple of 'key value'.
1300 # key and value are separated by `couple_sep`.
1301 # each couple is separated each couple with `sep`.
1303 # var m = new ArrayMap[Int, String]
1306 # assert m.join("; ", "=") == "1=one; 10=ten"
1307 fun join
(sep
: String, couple_sep
: String): String
1309 if is_empty
then return ""
1311 var s
= new Buffer # Result
1317 s
.append
("{k}{couple_sep}{e or else "<null>"}")
1319 # Concat other items
1325 s
.append
("{k}{couple_sep}{e or else "<null>"}")
1332 ###############################################################################
1334 ###############################################################################
1336 # Native strings are simple C char *
1340 fun [](index
: Int): Char is intern
1341 fun []=(index
: Int, item
: Char) is intern
1342 fun copy_to
(dest
: NativeString, length
: Int, from
: Int, to
: Int) is intern
1344 # Position of the first nul character.
1345 fun cstring_length
: Int
1348 while self[l
] != '\0' do l
+= 1
1351 fun atoi
: Int is intern
1352 fun atof
: Float is extern "atof"
1356 return to_s_with_length
(cstring_length
)
1359 fun to_s_with_length
(length
: Int): String
1362 return new String.with_infos
(self, length
, 0, length
- 1)
1365 fun to_s_with_copy
: String
1367 var length
= cstring_length
1368 var new_self
= calloc_string
(length
+ 1)
1369 copy_to
(new_self
, length
, 0, 0)
1370 return new String.with_infos
(new_self
, length
, 0, length
- 1)
1375 # StringCapable objects can create native strings
1376 interface StringCapable
1377 protected fun calloc_string
(size
: Int): NativeString is intern
1381 var _args_cache
: nullable Sequence[String]
1383 redef fun args
: Sequence[String]
1385 if _args_cache
== null then init_args
1386 return _args_cache
.as(not null)
1389 # The name of the program as given by the OS
1390 fun program_name
: String
1392 return native_argv
(0).to_s
1395 # Initialize `args` with the contents of `native_argc` and `native_argv`.
1396 private fun init_args
1398 var argc
= native_argc
1399 var args
= new Array[String].with_capacity
(0)
1402 args
[i-1
] = native_argv
(i
).to_s
1408 # First argument of the main C function.
1409 private fun native_argc
: Int is intern
1411 # Second argument of the main C function.
1412 private fun native_argv
(i
: Int): NativeString is intern