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
17 intrude import collection
# FIXME should be collection::array
23 ###############################################################################
25 ###############################################################################
27 # Common subclass for String and Buffer
28 abstract class AbstractString
29 super AbstractArrayRead[Char]
31 readable private var _items
: NativeString
33 fun chars
: StringCharView is abstract
35 # Access a character at `index` in the string.
37 # assert "abcd"[2] == 'c'
38 redef fun [](index
) do return _items
[index
]
42 # assert "abcd".substring(1, 2) == "bc"
43 # assert "abcd".substring(-1, 2) == "a"
44 # assert "abcd".substring(1, 0) == ""
45 # assert "abcd".substring(2, 5) == "cd"
47 # A `from` index < 0 will be replaced by 0.
48 # Unless a `count` value is > 0 at the same time.
49 # In this case, `from += count` and `count -= from`.
50 fun substring
(from
: Int, count
: Int): String
54 if from
< 0 then from
= 0
55 if count
> length
then count
= length
57 var r
= new Buffer.with_capacity
(count
- from
)
68 # Create a substring from `self` beginning at the `from` position
70 # assert "abcd".substring_from(1) == "bcd"
71 # assert "abcd".substring_from(-1) == "abcd"
72 # assert "abcd".substring_from(2) == "cd"
74 # As with substring, a `from` index < 0 will be replaced by 0
75 fun substring_from
(from
: Int): String
78 return substring
(from
, length
- from
)
81 # Does self have a substring `str` starting from position `pos`?
83 # assert "abcd".has_substring("bc",1) == true
84 # assert "abcd".has_substring("bc",2) == false
85 fun has_substring
(str
: String, pos
: Int): Bool
87 var itsindex
= str
.length
- 1
88 var myindex
= pos
+ itsindex
90 var itsitems
= str
._items
91 if myindex
> length
or itsindex
> myindex
then return false
92 var its_index_from
= str
._index_from
93 itsindex
+= its_index_from
94 while itsindex
>= its_index_from
do
95 if myitems
[myindex
] != itsitems
[itsindex
] then return false
102 # Is this string prefixed by `prefix`?
104 # assert "abcd".has_prefix("ab") == true
105 # assert "abcbc".has_prefix("bc") == false
106 # assert "ab".has_prefix("abcd") == false
107 fun has_prefix
(prefix
: String): Bool do return has_substring
(prefix
,0)
109 # Is this string suffixed by `suffix`?
111 # assert "abcd".has_suffix("abc") == false
112 # assert "abcd".has_suffix("bcd") == true
113 fun has_suffix
(suffix
: String): Bool do return has_substring
(suffix
, length
- suffix
.length
)
115 # If `self` contains only digits, return the corresponding integer
117 # assert "123".to_i == 123
118 # assert "-1".to_i == -1
122 return to_s
.to_cstring
.atoi
125 # If `self` contains a float, return the corresponding float
127 # assert "123".to_f == 123.0
128 # assert "-1".to_f == -1.0
129 # assert "-1.2e-3".to_f == -0.0012
133 return to_s
.to_cstring
.atof
136 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
137 fun to_hex
: Int do return a_to
(16)
139 # If `self` contains only digits and letters, return the corresponding integer in a given base
141 # assert "120".a_to(3) == 15
142 fun a_to
(base
: Int) : Int
169 # Returns `true` if the string contains only Numeric values (and one "," or one "." character)
171 # assert "123".is_numeric == true
172 # assert "1.2".is_numeric == true
173 # assert "1,2".is_numeric == true
174 # assert "1..2".is_numeric == false
177 var has_point_or_comma
= false
182 if (i
== '.' or i
== ',') and not has_point_or_comma
184 has_point_or_comma
= true
193 # A upper case version of `self`
195 # assert "Hello World!".to_upper == "HELLO WORLD!"
198 var s
= new Buffer.with_capacity
(length
)
199 for i
in self do s
.add
(i
.to_upper
)
203 # A lower case version of `self`
205 # assert "Hello World!".to_lower == "hello world!"
206 fun to_lower
: String
208 var s
= new Buffer.with_capacity
(length
)
209 for i
in self do s
.add
(i
.to_lower
)
213 # Trims trailing and preceding white spaces
214 # A whitespace is defined as any character which ascii value is less than or equal to 32
216 # assert " Hello World ! ".trim == "Hello World !"
217 # assert "\na\nb\tc\t".trim == "a\nb\tc"
220 if self._length
== 0 then return self.to_s
221 # find position of the first non white space char (ascii < 32) from the start of the string
223 while self[start_pos
].ascii
<= 32 do
225 if start_pos
== _length
then return ""
227 # find position of the first non white space char from the end of the string
228 var end_pos
= length
- 1
229 while self[end_pos
].ascii
<= 32 do
231 if end_pos
== start_pos
then return self[start_pos
].to_s
233 return self.substring
(start_pos
, end_pos
- start_pos
+ 1)
245 # Mangle a string to be a unique string only made of alphanumeric characters
246 fun to_cmangle
: String
249 var underscore
= false
251 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
257 res
.append
('_'.ascii
.to_s
)
260 if c
>= '0' and c
<= '9' then
263 else if c
== '_' then
268 res
.append
(c
.ascii
.to_s
)
276 # Escape " \ ' and non printable characters using the rules of literal C strings and characters
278 # assert "abAB12<>&".escape_to_c == "abAB12<>&"
279 # assert "\n\"'\\".escape_to_c == "\\n\\\"\\'\\\\"
280 fun escape_to_c
: String
286 else if c
== '\0' then
288 else if c
== '"' then
290 else if c == '\'' then
292 else if c == '\\
' then
294 else if c.ascii < 32 then
295 b.append("\\{c.ascii.to_base(8, false)}")
303 # Escape additionnal characters
304 # The result might no be legal in C but be used in other languages
306 # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
307 fun escape_more_to_c(chars: String): String
310 for c in escape_to_c do
319 # Escape to c plus braces
321 # assert "\n\"'\\\
{\}".escape_to_nit == "\\n\\\
"\\'\\\\\\\{\\\}"
322 fun escape_to_nit
: String do return escape_more_to_c
("\{\}")
324 # Return a string where Nit escape sequences are transformed.
328 # assert s.length == 2
329 # var u = s.unescape_nit
330 # assert u.length == 1
331 # assert u[0].ascii == 10 # (the ASCII value of the "new line" character)
332 fun unescape_nit
: String
334 var res
= new Buffer.with_capacity
(self.length
)
335 var was_slash
= false
337 if not was_slash
then
348 else if c
== 'r' then
350 else if c
== 't' then
352 else if c
== '0' then
362 # Abstract class for the SequenceRead compatible
363 # views on String and Buffer objects
364 abstract class StringCharView
365 super SequenceRead[Char]
367 type SELFTYPE: AbstractString
369 private var target
: SELFTYPE
371 private init(tgt
: SELFTYPE)
376 redef fun is_empty
do return target
.is_empty
378 redef fun length
do return target
.length
380 redef fun has
(c
: Char): Bool
383 if i
== c
then return true
390 # View on Buffer objects, extends Sequence
391 # for mutation operations
392 abstract class BufferCharView
396 redef type SELFTYPE: Buffer
400 # Immutable strings of characters.
406 redef type OTHER: String
408 # Index in _items of the start of the string
409 readable var _index_from
: Int
411 # Indes in _items of the last item of the string
412 readable var _index_to
: Int
414 redef var chars
: StringCharView = new FlatStringCharView(self)
416 ################################################
417 # AbstractString specific methods #
418 ################################################
420 redef fun [](index
) do
422 # Check that the index (+ index_from) is not larger than indexTo
423 # In other terms, if the index is valid
424 assert (index
+ _index_from
) <= _index_to
425 return _items
[index
+ _index_from
]
428 redef fun substring
(from
: Int, count
: Int): String
434 if count
< 0 then count
= 0
438 var realFrom
= _index_from
+ from
440 if (realFrom
+ count
) > _index_to
then return new String.from_substring
(realFrom
, _index_to
, _items
)
442 if count
== 0 then return ""
444 return new String.from_substring
(realFrom
, realFrom
+ count
- 1, _items
)
447 redef fun substring_from
(from
: Int): String
449 if from
> _length
then return ""
450 if from
< 0 then from
= 0
451 return substring
(from
, _length
)
454 redef fun has_substring
(str
: String, pos
: Int): Bool
456 var itsindex
= str
._length
- 1
458 var myindex
= pos
+ itsindex
461 var itsitems
= str
._items
463 if myindex
> _length
or itsindex
> myindex
then return false
465 var itsindexfrom
= str
.index_from
466 itsindex
+= itsindexfrom
467 myindex
+= index_from
469 while itsindex
>= itsindexfrom
do
470 if myitems
[myindex
] != itsitems
[itsindex
] then return false
478 redef fun to_upper
: String
480 var outstr
= calloc_string
(self._length
+ 1)
483 var myitems
= self._items
484 var index_from
= self._index_from
485 var max
= self._index_to
487 while index_from
<= max
do
488 outstr
[out_index
] = myitems
[index_from
].to_upper
493 outstr
[self.length
] = '\0'
495 return outstr
.to_s_with_length
(self._length
)
498 redef fun to_lower
: String
500 var outstr
= calloc_string
(self._length
+ 1)
503 var myitems
= self._items
504 var index_from
= self._index_from
505 var max
= self._index_to
507 while index_from
<= max
do
508 outstr
[out_index
] = myitems
[index_from
].to_lower
513 outstr
[self.length
] = '\0'
515 return outstr
.to_s_with_length
(self._length
)
518 redef fun trim
: String
520 if self._length
== 0 then return self
521 # find position of the first non white space char (ascii < 32) from the start of the string
522 var start_pos
= self._index_from
523 while _items
[start_pos
].ascii
<= 32 do
525 if start_pos
== _index_to
+ 1 then return ""
527 # find position of the first non white space char from the end of the string
528 var end_pos
= _index_to
529 while _items
[end_pos
].ascii
<= 32 do
531 if end_pos
== start_pos
then return _items
[start_pos
].to_s
533 start_pos
-= index_from
534 end_pos
-= index_from
535 return self.substring
(start_pos
, end_pos
- start_pos
+ 1)
540 var i
= self._index_from
541 var imax
= self._index_to
548 ##################################################
549 # String Specific Methods #
550 ##################################################
552 # Creates a String object as a substring of another String
554 # From : index to start at
556 # To : Index to stop at (from + count -1)
558 private init from_substring
(from
: Int, to
: Int, internalString
: NativeString)
560 _items
= internalString
563 _length
= to
- from
+ 1
566 private init with_infos
(items
: NativeString, len
: Int, from
: Int, to
: Int)
574 # Return a null terminated char *
575 fun to_cstring
: NativeString
577 if _index_from
> 0 or _index_to
!= items
.cstring_length
- 1 then
578 var newItems
= calloc_string
(_length
+ 1)
579 self.items
.copy_to
(newItems
, _length
, _index_from
, 0)
580 newItems
[length
] = '\0'
588 if not other
isa String then return false
590 if self.object_id
== other
.object_id
then return true
592 var my_length
= _length
594 if other
._length
!= my_length
then return false
596 var my_index
= _index_from
597 var its_index
= other
._index_from
599 var last_iteration
= my_index
+ my_length
601 var itsitems
= other
._items
602 var myitems
= self._items
604 while my_index
< last_iteration
do
605 if myitems
[my_index
] != itsitems
[its_index
] then return false
613 # The comparison between two strings is done on a lexicographical basis
615 # assert ("aa" < "b") == true
618 if self.object_id
== other
.object_id
then return false
620 var my_curr_char
: Char
621 var its_curr_char
: Char
623 var curr_id_self
= self._index_from
624 var curr_id_other
= other
._index_from
626 var my_items
= self._items
627 var its_items
= other
._items
629 var my_length
= self._length
630 var its_length
= other
._length
632 var max_iterations
= curr_id_self
+ my_length
634 while curr_id_self
< max_iterations
do
635 my_curr_char
= my_items
[curr_id_self
]
636 its_curr_char
= its_items
[curr_id_other
]
638 if my_curr_char
!= its_curr_char
then
639 if my_curr_char
< its_curr_char
then return true
647 return my_length
< its_length
650 # The concatenation of `self` with `s`
652 # assert "hello " + "world!" == "hello world!"
653 fun +(s
: String): String
655 var my_length
= self._length
656 var its_length
= s
._length
658 var total_length
= my_length
+ its_length
660 var target_string
= calloc_string
(my_length
+ its_length
+ 1)
662 self._items
.copy_to
(target_string
, my_length
, _index_from
, 0)
663 s
._items
.copy_to
(target_string
, its_length
, s
._index_from
, my_length
)
665 target_string
[total_length
] = '\0'
667 return target_string
.to_s_with_length
(total_length
)
670 # `i` repetitions of `self`
672 # assert "abc"*3 == "abcabcabc"
673 # assert "abc"*1 == "abc"
674 # assert "abc"*0 == ""
675 fun *(i
: Int): String
679 var my_length
= self._length
681 var final_length
= my_length
* i
683 var my_items
= self._items
685 var target_string
= calloc_string
((final_length
) + 1)
687 target_string
[final_length
] = '\0'
691 for iteration
in [1 .. i
] do
692 my_items
.copy_to
(target_string
, my_length
, 0, current_last
)
693 current_last
+= my_length
696 return target_string
.to_s_with_length
(final_length
)
699 redef fun to_s
do return self
703 # djb2 hash algorythm
708 var strStart
= _index_from
712 while i
>= strStart
do
713 h
= (h
* 32) + h
+ self._items
[i
].ascii
721 private class FlatStringIterator
722 super IndexedIterator[Char]
726 var target_items
: NativeString
730 init with_pos
(tgt
: String, pos
: Int)
733 target_items
= tgt
.items
734 curr_pos
= pos
+ target
.index_from
737 redef fun is_ok
do return curr_pos
<= target
.index_to
739 redef fun item
do return target_items
[curr_pos
]
741 redef fun next
do curr_pos
+= 1
743 redef fun index
do return curr_pos
- target
.index_from
747 private class FlatStringCharView
750 redef type SELFTYPE: String
754 # Check that the index (+ index_from) is not larger than indexTo
755 # In other terms, if the index is valid
757 assert (index
+ target
._index_from
) <= target
._index_to
758 return target
._items
[index
+ target
._index_from
]
761 redef fun iterator
: IndexedIterator[Char] do return new FlatStringIterator.with_pos
(target
, 0)
765 # Mutable strings of characters.
770 super AbstractArray[Char]
772 redef type OTHER: String
774 redef var chars
: BufferCharView = new FlatBufferCharView(self)
776 redef fun []=(index
, item
)
778 if index
== length
then
782 assert index
>= 0 and index
< length
788 if _capacity
<= length
then enlarge
(length
+ 5)
793 redef fun enlarge
(cap
)
796 if cap
<= c
then return
797 while c
<= cap
do c
= c
* 2 + 2
798 var a
= calloc_string
(c
+1)
799 _items
.copy_to
(a
, length
, 0, 0)
808 if _capacity
< _length
+ sl
then enlarge
(_length
+ sl
)
809 s
.items
.copy_to
(_items
, sl
, s
._index_from
, _length
)
816 redef fun to_s
: String
819 var a
= calloc_string
(l
+1)
820 _items
.copy_to
(a
, l
, 0, 0)
822 # Ensure the afterlast byte is '\0' to nul-terminated char *
825 return a
.to_s_with_length
(length
)
833 while i
< l1
and i
< l2
do
834 var c1
= self[i
].ascii
850 # Create a new empty string.
858 _capacity
= s
.length
+ 1
860 _items
= calloc_string
(_capacity
)
861 s
.items
.copy_to
(_items
, _length
, s
._index_from
, 0)
864 # Create a new empty string with a given capacity.
865 init with_capacity
(cap
: Int)
868 # _items = new NativeString.calloc(cap)
869 _items
= calloc_string
(cap
+1)
876 if not o
isa Buffer then return false
878 if o
.length
!= l
then return false
883 if it
[i
] != oit
[i
] then return false
889 readable private var _capacity
: Int
892 private class FlatBufferCharView
896 redef type SELFTYPE: Buffer
903 redef fun [](index
) do return target
._items
[index
]
905 redef fun []=(index
, item
)
907 assert index
>= 0 and index
<= length
908 if index
== length
then
912 target
._items
[index
] = item
925 fun enlarge
(cap
: Int)
932 var my_items
= target
.items
933 var s_length
= s
.length
934 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
937 redef fun iterator
: IndexedIterator[Char] do return new FlatBufferIterator.with_pos
(target
, 0)
941 private class FlatBufferIterator
942 super IndexedIterator[Char]
946 var target_items
: NativeString
950 init with_pos
(tgt
: Buffer, pos
: Int)
953 target_items
= tgt
.items
957 redef fun index
do return curr_pos
959 redef fun is_ok
do return curr_pos
< target
.length
961 redef fun item
do return target_items
[curr_pos
]
963 redef fun next
do curr_pos
+= 1
967 ###############################################################################
969 ###############################################################################
972 # User readable representation of `self`.
973 fun to_s
: String do return inspect
975 # The class name of the object in NativeString format.
976 private fun native_class_name
: NativeString is intern
978 # The class name of the object.
980 # assert 5.class_name == "Int"
981 fun class_name
: String do return native_class_name
.to_s
983 # Developer readable representation of `self`.
984 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
987 return "<{inspect_head}>"
990 # Return "CLASSNAME:#OBJECTID".
991 # This function is mainly used with the redefinition of the inspect method
992 protected fun inspect_head
: String
994 return "{class_name}:#{object_id.to_hex}"
997 protected fun args
: Sequence[String]
1004 # assert true.to_s == "true"
1005 # assert false.to_s == "false"
1017 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
1018 # assume < to_c max const of char
1019 fun fill_buffer
(s
: Buffer, base
: Int, signed
: Bool)
1026 else if self == 0 then
1033 var pos
= digit_count
(base
) - 1
1034 while pos
>= 0 and n
> 0 do
1035 s
[pos
] = (n
% base
).to_c
1041 # C function to convert an nit Int to a NativeString (char*)
1042 private fun native_int_to_s
(len
: Int): NativeString is extern "native_int_to_s"
1044 # return displayable int in base 10 and signed
1046 # assert 1.to_s == "1"
1047 # assert (-123).to_s == "-123"
1049 var len
= digit_count
(10)
1050 return native_int_to_s
(len
).to_s_with_length
(len
)
1053 # return displayable int in hexadecimal (unsigned (not now))
1054 fun to_hex
: String do return to_base
(16,false)
1056 # return displayable int in base base and signed
1057 fun to_base
(base
: Int, signed
: Bool): String
1059 var l
= digit_count
(base
)
1060 var s
= new Buffer.from
(" " * l
)
1061 fill_buffer
(s
, base
, signed
)
1067 # Pretty print self, print needoed decimals up to a max of 3.
1069 var str
= to_precision
( 3 )
1070 var len
= str
.length
1071 for i
in [0..len-1
] do
1076 else if c
== '.' then
1077 return str
.substring
( 0, j
+2 )
1079 return str
.substring
( 0, j
+1 )
1085 # `self` representation with `nb` digits after the '.'.
1086 fun to_precision
(nb
: Int): String
1088 if nb
== 0 then return self.to_i
.to_s
1090 for i
in [0..nb
[ do f
= f
* 10.0
1097 if i
== 0 then return "0.0"
1101 var p1
= s
.substring
(0, s
.length-nb
)
1102 var p2
= s
.substring
(s
.length-nb
, nb
)
1103 return p1
+ "." + p2
1105 return "0." + ("0"*(nb-sl
)) + s
1109 fun to_precision_native
(nb
: Int): String import NativeString::to_s
`{
1113 size = snprintf(NULL, 0, "%.*f", (int)nb, recv);
1114 str = malloc(size + 1);
1115 sprintf(str, "%.*f", (int)nb, recv );
1117 return NativeString_to_s( str );
1122 # assert 'x'.to_s == "x"
1125 var s
= new Buffer.with_capacity
(1)
1130 # Returns true if the char is a numerical digit
1131 fun is_numeric
: Bool
1133 if self >= '0' and self <= '9'
1140 # Returns true if the char is an alpha digit
1143 if (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z') then return true
1147 # Returns true if the char is an alpha or a numeric digit
1148 fun is_alphanumeric
: Bool
1150 if self.is_numeric
or self.is_alpha
then return true
1155 redef class Collection[E
]
1156 # Concatenate elements.
1160 for e
in self do if e
!= null then s
.append
(e
.to_s
)
1164 # Concatenate and separate each elements with `sep`.
1166 # assert [1, 2, 3].join(":") == "1:2:3"
1167 # assert [1..3].join(":") == "1:2:3"
1168 fun join
(sep
: String): String
1170 if is_empty
then return ""
1172 var s
= new Buffer # Result
1177 if e
!= null then s
.append
(e
.to_s
)
1179 # Concat other items
1184 if e
!= null then s
.append
(e
.to_s
)
1191 redef class Array[E
]
1192 # Fast implementation
1200 if e
!= null then s
.append
(e
.to_s
)
1207 redef class Map[K
,V
]
1208 # Concatenate couple of 'key value'.
1209 # key and value are separated by `couple_sep`.
1210 # each couple is separated each couple with `sep`.
1212 # var m = new ArrayMap[Int, String]
1215 # assert m.join("; ", "=") == "1=one; 10=ten"
1216 fun join
(sep
: String, couple_sep
: String): String
1218 if is_empty
then return ""
1220 var s
= new Buffer # Result
1226 s
.append
("{k}{couple_sep}{e or else "<null>"}")
1228 # Concat other items
1234 s
.append
("{k}{couple_sep}{e or else "<null>"}")
1241 ###############################################################################
1243 ###############################################################################
1245 # Native strings are simple C char *
1249 fun [](index
: Int): Char is intern
1250 fun []=(index
: Int, item
: Char) is intern
1251 fun copy_to
(dest
: NativeString, length
: Int, from
: Int, to
: Int) is intern
1253 # Position of the first nul character.
1254 fun cstring_length
: Int
1257 while self[l
] != '\0' do l
+= 1
1260 fun atoi
: Int is intern
1261 fun atof
: Float is extern "atof"
1265 return to_s_with_length
(cstring_length
)
1268 fun to_s_with_length
(length
: Int): String
1271 return new String.with_infos
(self, length
, 0, length
- 1)
1274 fun to_s_with_copy
: String
1276 var length
= cstring_length
1277 var new_self
= calloc_string
(length
+ 1)
1278 copy_to
(new_self
, length
, 0, 0)
1279 return new String.with_infos
(new_self
, length
, 0, length
- 1)
1284 # StringCapable objects can create native strings
1285 interface StringCapable
1286 protected fun calloc_string
(size
: Int): NativeString is intern
1290 var _args_cache
: nullable Sequence[String]
1292 redef fun args
: Sequence[String]
1294 if _args_cache
== null then init_args
1295 return _args_cache
.as(not null)
1298 # The name of the program as given by the OS
1299 fun program_name
: String
1301 return native_argv
(0).to_s
1304 # Initialize `args` with the contents of `native_argc` and `native_argv`.
1305 private fun init_args
1307 var argc
= native_argc
1308 var args
= new Array[String].with_capacity
(0)
1311 args
[i-1
] = native_argv
(i
).to_s
1317 # First argument of the main C function.
1318 private fun native_argc
: Int is intern
1320 # Second argument of the main C function.
1321 private fun native_argv
(i
: Int): NativeString is intern