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
)
59 r
.chars
.push
(_items
[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.chars
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.chars
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.chars
[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.chars
[end_pos
].ascii
<= 32 do
231 if end_pos
== start_pos
then return self.chars
[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
250 for c
in self.chars
do
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
283 for c
in self.chars
do
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.with_infos
(_items
, _index_to
- realFrom
+ 1, realFrom
, _index_to
)
442 if count
== 0 then return ""
444 var to
= realFrom
+ count
- 1
446 return new String.with_infos
(_items
, to
- realFrom
+ 1, realFrom
, to
)
449 redef fun substring_from
(from
: Int): String
451 if from
> _length
then return ""
452 if from
< 0 then from
= 0
453 return substring
(from
, _length
)
456 redef fun has_substring
(str
: String, pos
: Int): Bool
458 var itsindex
= str
._length
- 1
460 var myindex
= pos
+ itsindex
463 var itsitems
= str
._items
465 if myindex
> _length
or itsindex
> myindex
then return false
467 var itsindexfrom
= str
.index_from
468 itsindex
+= itsindexfrom
469 myindex
+= index_from
471 while itsindex
>= itsindexfrom
do
472 if myitems
[myindex
] != itsitems
[itsindex
] then return false
480 redef fun to_upper
: String
482 var outstr
= calloc_string
(self._length
+ 1)
485 var myitems
= self._items
486 var index_from
= self._index_from
487 var max
= self._index_to
489 while index_from
<= max
do
490 outstr
[out_index
] = myitems
[index_from
].to_upper
495 outstr
[self.length
] = '\0'
497 return outstr
.to_s_with_length
(self._length
)
500 redef fun to_lower
: String
502 var outstr
= calloc_string
(self._length
+ 1)
505 var myitems
= self._items
506 var index_from
= self._index_from
507 var max
= self._index_to
509 while index_from
<= max
do
510 outstr
[out_index
] = myitems
[index_from
].to_lower
515 outstr
[self.length
] = '\0'
517 return outstr
.to_s_with_length
(self._length
)
520 redef fun trim
: String
522 if self._length
== 0 then return self
523 # find position of the first non white space char (ascii < 32) from the start of the string
524 var start_pos
= self._index_from
525 while _items
[start_pos
].ascii
<= 32 do
527 if start_pos
== _index_to
+ 1 then return ""
529 # find position of the first non white space char from the end of the string
530 var end_pos
= _index_to
531 while _items
[end_pos
].ascii
<= 32 do
533 if end_pos
== start_pos
then return _items
[start_pos
].to_s
535 start_pos
-= index_from
536 end_pos
-= index_from
537 return self.substring
(start_pos
, end_pos
- start_pos
+ 1)
542 var i
= self._index_from
543 var imax
= self._index_to
550 ##################################################
551 # String Specific Methods #
552 ##################################################
554 private init with_infos
(items
: NativeString, len
: Int, from
: Int, to
: Int)
562 # Return a null terminated char *
563 fun to_cstring
: NativeString
565 if _index_from
> 0 or _index_to
!= items
.cstring_length
- 1 then
566 var newItems
= calloc_string
(_length
+ 1)
567 self.items
.copy_to
(newItems
, _length
, _index_from
, 0)
568 newItems
[length
] = '\0'
576 if not other
isa String then return false
578 if self.object_id
== other
.object_id
then return true
580 var my_length
= _length
582 if other
._length
!= my_length
then return false
584 var my_index
= _index_from
585 var its_index
= other
._index_from
587 var last_iteration
= my_index
+ my_length
589 var itsitems
= other
._items
590 var myitems
= self._items
592 while my_index
< last_iteration
do
593 if myitems
[my_index
] != itsitems
[its_index
] then return false
601 # The comparison between two strings is done on a lexicographical basis
603 # assert ("aa" < "b") == true
606 if self.object_id
== other
.object_id
then return false
608 var my_curr_char
: Char
609 var its_curr_char
: Char
611 var curr_id_self
= self._index_from
612 var curr_id_other
= other
._index_from
614 var my_items
= self._items
615 var its_items
= other
._items
617 var my_length
= self._length
618 var its_length
= other
._length
620 var max_iterations
= curr_id_self
+ my_length
622 while curr_id_self
< max_iterations
do
623 my_curr_char
= my_items
[curr_id_self
]
624 its_curr_char
= its_items
[curr_id_other
]
626 if my_curr_char
!= its_curr_char
then
627 if my_curr_char
< its_curr_char
then return true
635 return my_length
< its_length
638 # The concatenation of `self` with `s`
640 # assert "hello " + "world!" == "hello world!"
641 fun +(s
: String): String
643 var my_length
= self._length
644 var its_length
= s
._length
646 var total_length
= my_length
+ its_length
648 var target_string
= calloc_string
(my_length
+ its_length
+ 1)
650 self._items
.copy_to
(target_string
, my_length
, _index_from
, 0)
651 s
._items
.copy_to
(target_string
, its_length
, s
._index_from
, my_length
)
653 target_string
[total_length
] = '\0'
655 return target_string
.to_s_with_length
(total_length
)
658 # `i` repetitions of `self`
660 # assert "abc"*3 == "abcabcabc"
661 # assert "abc"*1 == "abc"
662 # assert "abc"*0 == ""
663 fun *(i
: Int): String
667 var my_length
= self._length
669 var final_length
= my_length
* i
671 var my_items
= self._items
673 var target_string
= calloc_string
((final_length
) + 1)
675 target_string
[final_length
] = '\0'
679 for iteration
in [1 .. i
] do
680 my_items
.copy_to
(target_string
, my_length
, 0, current_last
)
681 current_last
+= my_length
684 return target_string
.to_s_with_length
(final_length
)
687 redef fun to_s
do return self
691 # djb2 hash algorythm
696 var strStart
= _index_from
700 while i
>= strStart
do
701 h
= (h
* 32) + h
+ self._items
[i
].ascii
709 private class FlatStringIterator
710 super IndexedIterator[Char]
714 var target_items
: NativeString
718 init with_pos
(tgt
: String, pos
: Int)
721 target_items
= tgt
.items
722 curr_pos
= pos
+ target
.index_from
725 redef fun is_ok
do return curr_pos
<= target
.index_to
727 redef fun item
do return target_items
[curr_pos
]
729 redef fun next
do curr_pos
+= 1
731 redef fun index
do return curr_pos
- target
.index_from
735 private class FlatStringCharView
738 redef type SELFTYPE: String
742 # Check that the index (+ index_from) is not larger than indexTo
743 # In other terms, if the index is valid
745 assert (index
+ target
._index_from
) <= target
._index_to
746 return target
._items
[index
+ target
._index_from
]
749 redef fun iterator
: IndexedIterator[Char] do return new FlatStringIterator.with_pos
(target
, 0)
753 # Mutable strings of characters.
758 super AbstractArray[Char]
760 redef type OTHER: String
762 redef var chars
: BufferCharView = new FlatBufferCharView(self)
764 redef fun []=(index
, item
)
766 if index
== length
then
770 assert index
>= 0 and index
< length
776 if _capacity
<= length
then enlarge
(length
+ 5)
781 redef fun enlarge
(cap
)
784 if cap
<= c
then return
785 while c
<= cap
do c
= c
* 2 + 2
786 var a
= calloc_string
(c
+1)
787 _items
.copy_to
(a
, length
, 0, 0)
796 if _capacity
< _length
+ sl
then enlarge
(_length
+ sl
)
797 s
.items
.copy_to
(_items
, sl
, s
._index_from
, _length
)
804 redef fun to_s
: String
807 var a
= calloc_string
(l
+1)
808 _items
.copy_to
(a
, l
, 0, 0)
810 # Ensure the afterlast byte is '\0' to nul-terminated char *
813 return a
.to_s_with_length
(length
)
821 while i
< l1
and i
< l2
do
822 var c1
= self.chars
[i
].ascii
823 var c2
= s
.chars
[i
].ascii
838 # Create a new empty string.
846 _capacity
= s
.length
+ 1
848 _items
= calloc_string
(_capacity
)
849 s
.items
.copy_to
(_items
, _length
, s
._index_from
, 0)
852 # Create a new empty string with a given capacity.
853 init with_capacity
(cap
: Int)
856 # _items = new NativeString.calloc(cap)
857 _items
= calloc_string
(cap
+1)
864 if not o
isa Buffer then return false
866 if o
.length
!= l
then return false
871 if it
[i
] != oit
[i
] then return false
877 readable private var _capacity
: Int
880 private class FlatBufferCharView
884 redef type SELFTYPE: Buffer
891 redef fun [](index
) do return target
._items
[index
]
893 redef fun []=(index
, item
)
895 assert index
>= 0 and index
<= length
896 if index
== length
then
900 target
._items
[index
] = item
913 fun enlarge
(cap
: Int)
920 var my_items
= target
.items
921 var s_length
= s
.length
922 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
925 redef fun iterator
: IndexedIterator[Char] do return new FlatBufferIterator.with_pos
(target
, 0)
929 private class FlatBufferIterator
930 super IndexedIterator[Char]
934 var target_items
: NativeString
938 init with_pos
(tgt
: Buffer, pos
: Int)
941 target_items
= tgt
.items
945 redef fun index
do return curr_pos
947 redef fun is_ok
do return curr_pos
< target
.length
949 redef fun item
do return target_items
[curr_pos
]
951 redef fun next
do curr_pos
+= 1
955 ###############################################################################
957 ###############################################################################
960 # User readable representation of `self`.
961 fun to_s
: String do return inspect
963 # The class name of the object in NativeString format.
964 private fun native_class_name
: NativeString is intern
966 # The class name of the object.
968 # assert 5.class_name == "Int"
969 fun class_name
: String do return native_class_name
.to_s
971 # Developer readable representation of `self`.
972 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
975 return "<{inspect_head}>"
978 # Return "CLASSNAME:#OBJECTID".
979 # This function is mainly used with the redefinition of the inspect method
980 protected fun inspect_head
: String
982 return "{class_name}:#{object_id.to_hex}"
985 protected fun args
: Sequence[String]
992 # assert true.to_s == "true"
993 # assert false.to_s == "false"
1005 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
1006 # assume < to_c max const of char
1007 fun fill_buffer
(s
: Buffer, base
: Int, signed
: Bool)
1014 else if self == 0 then
1021 var pos
= digit_count
(base
) - 1
1022 while pos
>= 0 and n
> 0 do
1023 s
.chars
[pos
] = (n
% base
).to_c
1029 # C function to convert an nit Int to a NativeString (char*)
1030 private fun native_int_to_s
(len
: Int): NativeString is extern "native_int_to_s"
1032 # return displayable int in base 10 and signed
1034 # assert 1.to_s == "1"
1035 # assert (-123).to_s == "-123"
1037 var len
= digit_count
(10)
1038 return native_int_to_s
(len
).to_s_with_length
(len
)
1041 # return displayable int in hexadecimal (unsigned (not now))
1042 fun to_hex
: String do return to_base
(16,false)
1044 # return displayable int in base base and signed
1045 fun to_base
(base
: Int, signed
: Bool): String
1047 var l
= digit_count
(base
)
1048 var s
= new Buffer.from
(" " * l
)
1049 fill_buffer
(s
, base
, signed
)
1055 # Pretty print self, print needoed decimals up to a max of 3.
1057 var str
= to_precision
( 3 )
1058 var len
= str
.length
1059 for i
in [0..len-1
] do
1061 var c
= str
.chars
[j
]
1064 else if c
== '.' then
1065 return str
.substring
( 0, j
+2 )
1067 return str
.substring
( 0, j
+1 )
1073 # `self` representation with `nb` digits after the '.'.
1074 fun to_precision
(nb
: Int): String
1076 if nb
== 0 then return self.to_i
.to_s
1078 for i
in [0..nb
[ do f
= f
* 10.0
1085 if i
== 0 then return "0.0"
1089 var p1
= s
.substring
(0, s
.length-nb
)
1090 var p2
= s
.substring
(s
.length-nb
, nb
)
1091 return p1
+ "." + p2
1093 return "0." + ("0"*(nb-sl
)) + s
1097 fun to_precision_native
(nb
: Int): String import NativeString.to_s
`{
1101 size = snprintf(NULL, 0, "%.*f", (int)nb, recv);
1102 str = malloc(size + 1);
1103 sprintf(str, "%.*f", (int)nb, recv );
1105 return NativeString_to_s( str );
1110 # assert 'x'.to_s == "x"
1113 var s
= new Buffer.with_capacity
(1)
1118 # Returns true if the char is a numerical digit
1119 fun is_numeric
: Bool
1121 if self >= '0' and self <= '9'
1128 # Returns true if the char is an alpha digit
1131 if (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z') then return true
1135 # Returns true if the char is an alpha or a numeric digit
1136 fun is_alphanumeric
: Bool
1138 if self.is_numeric
or self.is_alpha
then return true
1143 redef class Collection[E
]
1144 # Concatenate elements.
1148 for e
in self do if e
!= null then s
.append
(e
.to_s
)
1152 # Concatenate and separate each elements with `sep`.
1154 # assert [1, 2, 3].join(":") == "1:2:3"
1155 # assert [1..3].join(":") == "1:2:3"
1156 fun join
(sep
: String): String
1158 if is_empty
then return ""
1160 var s
= new Buffer # Result
1165 if e
!= null then s
.append
(e
.to_s
)
1167 # Concat other items
1172 if e
!= null then s
.append
(e
.to_s
)
1179 redef class Array[E
]
1180 # Fast implementation
1188 if e
!= null then s
.append
(e
.to_s
)
1195 redef class Map[K
,V
]
1196 # Concatenate couple of 'key value'.
1197 # key and value are separated by `couple_sep`.
1198 # each couple is separated each couple with `sep`.
1200 # var m = new ArrayMap[Int, String]
1203 # assert m.join("; ", "=") == "1=one; 10=ten"
1204 fun join
(sep
: String, couple_sep
: String): String
1206 if is_empty
then return ""
1208 var s
= new Buffer # Result
1214 s
.append
("{k}{couple_sep}{e or else "<null>"}")
1216 # Concat other items
1222 s
.append
("{k}{couple_sep}{e or else "<null>"}")
1229 ###############################################################################
1231 ###############################################################################
1233 # Native strings are simple C char *
1237 fun [](index
: Int): Char is intern
1238 fun []=(index
: Int, item
: Char) is intern
1239 fun copy_to
(dest
: NativeString, length
: Int, from
: Int, to
: Int) is intern
1241 # Position of the first nul character.
1242 fun cstring_length
: Int
1245 while self[l
] != '\0' do l
+= 1
1248 fun atoi
: Int is intern
1249 fun atof
: Float is extern "atof"
1253 return to_s_with_length
(cstring_length
)
1256 fun to_s_with_length
(length
: Int): String
1259 return new String.with_infos
(self, length
, 0, length
- 1)
1262 fun to_s_with_copy
: String
1264 var length
= cstring_length
1265 var new_self
= calloc_string
(length
+ 1)
1266 copy_to
(new_self
, length
, 0, 0)
1267 return new String.with_infos
(new_self
, length
, 0, length
- 1)
1272 # StringCapable objects can create native strings
1273 interface StringCapable
1274 protected fun calloc_string
(size
: Int): NativeString is intern
1278 var _args_cache
: nullable Sequence[String]
1280 redef fun args
: Sequence[String]
1282 if _args_cache
== null then init_args
1283 return _args_cache
.as(not null)
1286 # The name of the program as given by the OS
1287 fun program_name
: String
1289 return native_argv
(0).to_s
1292 # Initialize `args` with the contents of `native_argc` and `native_argv`.
1293 private fun init_args
1295 var argc
= native_argc
1296 var args
= new Array[String].with_capacity
(0)
1299 args
[i-1
] = native_argv
(i
).to_s
1305 # First argument of the main C function.
1306 private fun native_argc
: Int is intern
1308 # Second argument of the main C function.
1309 private fun native_argv
(i
: Int): NativeString is intern