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 # Access a character at `index` in the string.
35 # assert "abcd"[2] == 'c'
36 redef fun [](index
) do return _items
[index
]
40 # assert "abcd".substring(1, 2) == "bc"
41 # assert "abcd".substring(-1, 2) == "a"
42 # assert "abcd".substring(1, 0) == ""
43 # assert "abcd".substring(2, 5) == "cd"
45 # A `from` index < 0 will be replaced by 0.
46 # Unless a `count` value is > 0 at the same time.
47 # In this case, `from += count` and `count -= from`.
48 fun substring
(from
: Int, count
: Int): String
52 if from
< 0 then from
= 0
53 if count
> length
then count
= length
55 var r
= new Buffer.with_capacity
(count
- from
)
66 # Create a substring from `self` beginning at the `from` position
68 # assert "abcd".substring_from(1) == "bcd"
69 # assert "abcd".substring_from(-1) == "abcd"
70 # assert "abcd".substring_from(2) == "cd"
72 # As with substring, a `from` index < 0 will be replaced by 0
73 fun substring_from
(from
: Int): String
76 return substring
(from
, length
- from
)
79 # Does self have a substring `str` starting from position `pos`?
81 # assert "abcd".has_substring("bc",1) == true
82 # assert "abcd".has_substring("bc",2) == false
83 fun has_substring
(str
: String, pos
: Int): Bool
85 var itsindex
= str
.length
- 1
86 var myindex
= pos
+ itsindex
88 var itsitems
= str
._items
89 if myindex
> length
or itsindex
> myindex
then return false
90 var its_index_from
= str
._index_from
91 itsindex
+= its_index_from
92 while itsindex
>= its_index_from
do
93 if myitems
[myindex
] != itsitems
[itsindex
] then return false
100 # Is this string prefixed by `prefix`?
102 # assert "abcd".has_prefix("ab") == true
103 # assert "abcbc".has_prefix("bc") == false
104 # assert "ab".has_prefix("abcd") == false
105 fun has_prefix
(prefix
: String): Bool do return has_substring
(prefix
,0)
107 # Is this string suffixed by `suffix`?
109 # assert "abcd".has_suffix("abc") == false
110 # assert "abcd".has_suffix("bcd") == true
111 fun has_suffix
(suffix
: String): Bool do return has_substring
(suffix
, length
- suffix
.length
)
113 # If `self` contains only digits, return the corresponding integer
115 # assert "123".to_i == 123
116 # assert "-1".to_i == -1
120 return to_s
.to_cstring
.atoi
123 # If `self` contains a float, return the corresponding float
125 # assert "123".to_f == 123.0
126 # assert "-1".to_f == -1.0
127 # assert "-1.2e-3".to_f == -0.0012
131 return to_s
.to_cstring
.atof
134 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
135 fun to_hex
: Int do return a_to
(16)
137 # If `self` contains only digits and letters, return the corresponding integer in a given base
139 # assert "120".a_to(3) == 15
140 fun a_to
(base
: Int) : Int
167 # Returns `true` if the string contains only Numeric values (and one "," or one "." character)
169 # assert "123".is_numeric == true
170 # assert "1.2".is_numeric == true
171 # assert "1,2".is_numeric == true
172 # assert "1..2".is_numeric == false
175 var has_point_or_comma
= false
180 if (i
== '.' or i
== ',') and not has_point_or_comma
182 has_point_or_comma
= true
191 # A upper case version of `self`
193 # assert "Hello World!".to_upper == "HELLO WORLD!"
196 var s
= new Buffer.with_capacity
(length
)
197 for i
in self do s
.add
(i
.to_upper
)
201 # A lower case version of `self`
203 # assert "Hello World!".to_lower == "hello world!"
204 fun to_lower
: String
206 var s
= new Buffer.with_capacity
(length
)
207 for i
in self do s
.add
(i
.to_lower
)
211 # Trims trailing and preceding white spaces
212 # A whitespace is defined as any character which ascii value is less than or equal to 32
214 # assert " Hello World ! ".trim == "Hello World !"
215 # assert "\na\nb\tc\t".trim == "a\nb\tc"
218 if self._length
== 0 then return self.to_s
219 # find position of the first non white space char (ascii < 32) from the start of the string
221 while self[start_pos
].ascii
<= 32 do
223 if start_pos
== _length
then return ""
225 # find position of the first non white space char from the end of the string
226 var end_pos
= length
- 1
227 while self[end_pos
].ascii
<= 32 do
229 if end_pos
== start_pos
then return self[start_pos
].to_s
231 return self.substring
(start_pos
, end_pos
- start_pos
+ 1)
243 # Mangle a string to be a unique string only made of alphanumeric characters
244 fun to_cmangle
: String
247 var underscore
= false
249 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
255 res
.append
('_'.ascii
.to_s
)
258 if c
>= '0' and c
<= '9' then
261 else if c
== '_' then
266 res
.append
(c
.ascii
.to_s
)
274 # Escape " \ ' and non printable characters using the rules of literal C strings and characters
276 # assert "abAB12<>&".escape_to_c == "abAB12<>&"
277 # assert "\n\"'\\".escape_to_c == "\\n\\\"\\'\\\\"
278 fun escape_to_c
: String
284 else if c
== '\0' then
286 else if c
== '"' then
288 else if c == '\'' then
290 else if c == '\\
' then
292 else if c.ascii < 32 then
293 b.append("\\{c.ascii.to_base(8, false)}")
301 # Escape additionnal characters
302 # The result might no be legal in C but be used in other languages
304 # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
305 fun escape_more_to_c(chars: String): String
308 for c in escape_to_c do
317 # Escape to c plus braces
319 # assert "\n\"'\\\
{\}".escape_to_nit == "\\n\\\
"\\'\\\\\\\{\\\}"
320 fun escape_to_nit
: String do return escape_more_to_c
("\{\}")
322 # Return a string where Nit escape sequences are transformed.
326 # assert s.length == 2
327 # var u = s.unescape_nit
328 # assert u.length == 1
329 # assert u[0].ascii == 10 # (the ASCII value of the "new line" character)
330 fun unescape_nit
: String
332 var res
= new Buffer.with_capacity
(self.length
)
333 var was_slash
= false
335 if not was_slash
then
346 else if c
== 'r' then
348 else if c
== 't' then
350 else if c
== '0' then
360 # Immutable strings of characters.
366 redef type OTHER: String
368 # Index in _items of the start of the string
369 readable var _index_from
: Int
371 # Indes in _items of the last item of the string
372 readable var _index_to
: Int
374 ################################################
375 # AbstractString specific methods #
376 ################################################
378 redef fun [](index
) do
380 # Check that the index (+ index_from) is not larger than indexTo
381 # In other terms, if the index is valid
382 assert (index
+ _index_from
) <= _index_to
383 return _items
[index
+ _index_from
]
386 redef fun substring
(from
: Int, count
: Int): String
392 if count
< 0 then count
= 0
396 var realFrom
= _index_from
+ from
398 if (realFrom
+ count
) > _index_to
then return new String.from_substring
(realFrom
, _index_to
, _items
)
400 if count
== 0 then return ""
402 return new String.from_substring
(realFrom
, realFrom
+ count
- 1, _items
)
405 redef fun substring_from
(from
: Int): String
407 if from
> _length
then return ""
408 if from
< 0 then from
= 0
409 return substring
(from
, _length
)
412 redef fun has_substring
(str
: String, pos
: Int): Bool
414 var itsindex
= str
._length
- 1
416 var myindex
= pos
+ itsindex
419 var itsitems
= str
._items
421 if myindex
> _length
or itsindex
> myindex
then return false
423 var itsindexfrom
= str
.index_from
424 itsindex
+= itsindexfrom
425 myindex
+= index_from
427 while itsindex
>= itsindexfrom
do
428 if myitems
[myindex
] != itsitems
[itsindex
] then return false
436 redef fun to_upper
: String
438 var outstr
= calloc_string
(self._length
+ 1)
441 var myitems
= self._items
442 var index_from
= self._index_from
443 var max
= self._index_to
445 while index_from
<= max
do
446 outstr
[out_index
] = myitems
[index_from
].to_upper
451 outstr
[self.length
] = '\0'
453 return outstr
.to_s_with_length
(self._length
)
456 redef fun to_lower
: String
458 var outstr
= calloc_string
(self._length
+ 1)
461 var myitems
= self._items
462 var index_from
= self._index_from
463 var max
= self._index_to
465 while index_from
<= max
do
466 outstr
[out_index
] = myitems
[index_from
].to_lower
471 outstr
[self.length
] = '\0'
473 return outstr
.to_s_with_length
(self._length
)
476 redef fun trim
: String
478 if self._length
== 0 then return self
479 # find position of the first non white space char (ascii < 32) from the start of the string
480 var start_pos
= self._index_from
481 while _items
[start_pos
].ascii
<= 32 do
483 if start_pos
== _index_to
+ 1 then return ""
485 # find position of the first non white space char from the end of the string
486 var end_pos
= _index_to
487 while _items
[end_pos
].ascii
<= 32 do
489 if end_pos
== start_pos
then return _items
[start_pos
].to_s
491 start_pos
-= index_from
492 end_pos
-= index_from
493 return self.substring
(start_pos
, end_pos
- start_pos
+ 1)
498 var i
= self._index_from
499 var imax
= self._index_to
506 ##################################################
507 # String Specific Methods #
508 ##################################################
510 # Creates a String object as a substring of another String
512 # From : index to start at
514 # To : Index to stop at (from + count -1)
516 private init from_substring
(from
: Int, to
: Int, internalString
: NativeString)
518 _items
= internalString
521 _length
= to
- from
+ 1
524 private init with_infos
(items
: NativeString, len
: Int, from
: Int, to
: Int)
532 # Return a null terminated char *
533 fun to_cstring
: NativeString
535 if _index_from
> 0 or _index_to
!= items
.cstring_length
- 1 then
536 var newItems
= calloc_string
(_length
+ 1)
537 self.items
.copy_to
(newItems
, _length
, _index_from
, 0)
538 newItems
[length
] = '\0'
546 if not other
isa String then return false
548 if self.object_id
== other
.object_id
then return true
550 var my_length
= _length
552 if other
._length
!= my_length
then return false
554 var my_index
= _index_from
555 var its_index
= other
._index_from
557 var last_iteration
= my_index
+ my_length
559 var itsitems
= other
._items
560 var myitems
= self._items
562 while my_index
< last_iteration
do
563 if myitems
[my_index
] != itsitems
[its_index
] then return false
571 # The comparison between two strings is done on a lexicographical basis
573 # assert ("aa" < "b") == true
576 if self.object_id
== other
.object_id
then return false
578 var my_curr_char
: Char
579 var its_curr_char
: Char
581 var curr_id_self
= self._index_from
582 var curr_id_other
= other
._index_from
584 var my_items
= self._items
585 var its_items
= other
._items
587 var my_length
= self._length
588 var its_length
= other
._length
590 var max_iterations
= curr_id_self
+ my_length
592 while curr_id_self
< max_iterations
do
593 my_curr_char
= my_items
[curr_id_self
]
594 its_curr_char
= its_items
[curr_id_other
]
596 if my_curr_char
!= its_curr_char
then
597 if my_curr_char
< its_curr_char
then return true
605 return my_length
< its_length
608 # The concatenation of `self` with `s`
610 # assert "hello " + "world!" == "hello world!"
611 fun +(s
: String): String
613 var my_length
= self._length
614 var its_length
= s
._length
616 var total_length
= my_length
+ its_length
618 var target_string
= calloc_string
(my_length
+ its_length
+ 1)
620 self._items
.copy_to
(target_string
, my_length
, _index_from
, 0)
621 s
._items
.copy_to
(target_string
, its_length
, s
._index_from
, my_length
)
623 target_string
[total_length
] = '\0'
625 return target_string
.to_s_with_length
(total_length
)
628 # `i` repetitions of `self`
630 # assert "abc"*3 == "abcabcabc"
631 # assert "abc"*1 == "abc"
632 # assert "abc"*0 == ""
633 fun *(i
: Int): String
637 var my_length
= self._length
639 var final_length
= my_length
* i
641 var my_items
= self._items
643 var target_string
= calloc_string
((final_length
) + 1)
645 target_string
[final_length
] = '\0'
649 for iteration
in [1 .. i
] do
650 my_items
.copy_to
(target_string
, my_length
, 0, current_last
)
651 current_last
+= my_length
654 return target_string
.to_s_with_length
(final_length
)
657 redef fun to_s
do return self
661 # djb2 hash algorythm
666 var strStart
= _index_from
670 while i
>= strStart
do
671 h
= (h
* 32) + h
+ self._items
[i
].ascii
679 # Mutable strings of characters.
684 super AbstractArray[Char]
686 redef type OTHER: String
688 redef fun []=(index
, item
)
690 if index
== length
then
694 assert index
>= 0 and index
< length
700 if _capacity
<= length
then enlarge
(length
+ 5)
705 redef fun enlarge
(cap
)
708 if cap
<= c
then return
709 while c
<= cap
do c
= c
* 2 + 2
710 var a
= calloc_string
(c
+1)
711 _items
.copy_to
(a
, length
, 0, 0)
720 if _capacity
< _length
+ sl
then enlarge
(_length
+ sl
)
721 s
.items
.copy_to
(_items
, sl
, s
._index_from
, _length
)
728 redef fun to_s
: String
731 var a
= calloc_string
(l
+1)
732 _items
.copy_to
(a
, l
, 0, 0)
734 # Ensure the afterlast byte is '\0' to nul-terminated char *
737 return a
.to_s_with_length
(length
)
745 while i
< l1
and i
< l2
do
746 var c1
= self[i
].ascii
762 # Create a new empty string.
770 _capacity
= s
.length
+ 1
772 _items
= calloc_string
(_capacity
)
773 s
.items
.copy_to
(_items
, _length
, s
._index_from
, 0)
776 # Create a new empty string with a given capacity.
777 init with_capacity
(cap
: Int)
780 # _items = new NativeString.calloc(cap)
781 _items
= calloc_string
(cap
+1)
788 if not o
isa Buffer then return false
790 if o
.length
!= l
then return false
795 if it
[i
] != oit
[i
] then return false
801 readable private var _capacity
: Int
804 ###############################################################################
806 ###############################################################################
809 # User readable representation of `self`.
810 fun to_s
: String do return inspect
812 # The class name of the object in NativeString format.
813 private fun native_class_name
: NativeString is intern
815 # The class name of the object.
817 # assert 5.class_name == "Int"
818 fun class_name
: String do return native_class_name
.to_s
820 # Developer readable representation of `self`.
821 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
824 return "<{inspect_head}>"
827 # Return "CLASSNAME:#OBJECTID".
828 # This function is mainly used with the redefinition of the inspect method
829 protected fun inspect_head
: String
831 return "{class_name}:#{object_id.to_hex}"
834 protected fun args
: Sequence[String]
841 # assert true.to_s == "true"
842 # assert false.to_s == "false"
854 # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
855 # assume < to_c max const of char
856 fun fill_buffer
(s
: Buffer, base
: Int, signed
: Bool)
863 else if self == 0 then
870 var pos
= digit_count
(base
) - 1
871 while pos
>= 0 and n
> 0 do
872 s
[pos
] = (n
% base
).to_c
878 # C function to convert an nit Int to a NativeString (char*)
879 private fun native_int_to_s
(len
: Int): NativeString is extern "native_int_to_s"
881 # return displayable int in base 10 and signed
883 # assert 1.to_s == "1"
884 # assert (-123).to_s == "-123"
886 var len
= digit_count
(10)
887 return native_int_to_s
(len
).to_s_with_length
(len
)
890 # return displayable int in hexadecimal (unsigned (not now))
891 fun to_hex
: String do return to_base
(16,false)
893 # return displayable int in base base and signed
894 fun to_base
(base
: Int, signed
: Bool): String
896 var l
= digit_count
(base
)
897 var s
= new Buffer.from
(" " * l
)
898 fill_buffer
(s
, base
, signed
)
904 # Pretty print self, print needoed decimals up to a max of 3.
906 var str
= to_precision
( 3 )
908 for i
in [0..len-1
] do
913 else if c
== '.' then
914 return str
.substring
( 0, j
+2 )
916 return str
.substring
( 0, j
+1 )
922 # `self` representation with `nb` digits after the '.'.
923 fun to_precision
(nb
: Int): String
925 if nb
== 0 then return self.to_i
.to_s
927 for i
in [0..nb
[ do f
= f
* 10.0
934 if i
== 0 then return "0.0"
938 var p1
= s
.substring
(0, s
.length-nb
)
939 var p2
= s
.substring
(s
.length-nb
, nb
)
942 return "0." + ("0"*(nb-sl
)) + s
946 fun to_precision_native
(nb
: Int): String import NativeString::to_s
`{
950 size = snprintf(NULL, 0, "%.*f", (int)nb, recv);
951 str = malloc(size + 1);
952 sprintf(str, "%.*f", (int)nb, recv );
954 return NativeString_to_s( str );
959 # assert 'x'.to_s == "x"
962 var s
= new Buffer.with_capacity
(1)
967 # Returns true if the char is a numerical digit
970 if self >= '0' and self <= '9'
977 # Returns true if the char is an alpha digit
980 if (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z') then return true
984 # Returns true if the char is an alpha or a numeric digit
985 fun is_alphanumeric
: Bool
987 if self.is_numeric
or self.is_alpha
then return true
992 redef class Collection[E
]
993 # Concatenate elements.
997 for e
in self do if e
!= null then s
.append
(e
.to_s
)
1001 # Concatenate and separate each elements with `sep`.
1003 # assert [1, 2, 3].join(":") == "1:2:3"
1004 # assert [1..3].join(":") == "1:2:3"
1005 fun join
(sep
: String): String
1007 if is_empty
then return ""
1009 var s
= new Buffer # Result
1014 if e
!= null then s
.append
(e
.to_s
)
1016 # Concat other items
1021 if e
!= null then s
.append
(e
.to_s
)
1028 redef class Array[E
]
1029 # Fast implementation
1037 if e
!= null then s
.append
(e
.to_s
)
1044 redef class Map[K
,V
]
1045 # Concatenate couple of 'key value'.
1046 # key and value are separated by `couple_sep`.
1047 # each couple is separated each couple with `sep`.
1049 # var m = new ArrayMap[Int, String]
1052 # assert m.join("; ", "=") == "1=one; 10=ten"
1053 fun join
(sep
: String, couple_sep
: String): String
1055 if is_empty
then return ""
1057 var s
= new Buffer # Result
1063 if e
!= null then s
.append
("{k}{couple_sep}{e}")
1065 # Concat other items
1071 if e
!= null then s
.append
("{k}{couple_sep}{e}")
1078 ###############################################################################
1080 ###############################################################################
1082 # Native strings are simple C char *
1086 fun [](index
: Int): Char is intern
1087 fun []=(index
: Int, item
: Char) is intern
1088 fun copy_to
(dest
: NativeString, length
: Int, from
: Int, to
: Int) is intern
1090 # Position of the first nul character.
1091 fun cstring_length
: Int
1094 while self[l
] != '\0' do l
+= 1
1097 fun atoi
: Int is intern
1098 fun atof
: Float is extern "atof"
1102 return to_s_with_length
(cstring_length
)
1105 fun to_s_with_length
(length
: Int): String
1108 return new String.with_infos
(self, length
, 0, length
- 1)
1111 fun to_s_with_copy
: String
1113 var length
= cstring_length
1114 var new_self
= calloc_string
(length
+ 1)
1115 copy_to
(new_self
, length
, 0, 0)
1116 return new String.with_infos
(new_self
, length
, 0, length
- 1)
1121 # StringCapable objects can create native strings
1122 interface StringCapable
1123 protected fun calloc_string
(size
: Int): NativeString is intern
1127 var _args_cache
: nullable Sequence[String]
1129 redef fun args
: Sequence[String]
1131 if _args_cache
== null then init_args
1132 return _args_cache
.as(not null)
1135 # The name of the program as given by the OS
1136 fun program_name
: String
1138 return native_argv
(0).to_s
1141 # Initialize `args` with the contents of `native_argc` and `native_argv`.
1142 private fun init_args
1144 var argc
= native_argc
1145 var args
= new Array[String].with_capacity
(0)
1148 args
[i-1
] = native_argv
(i
).to_s
1154 # First argument of the main C function.
1155 private fun native_argc
: Int is intern
1157 # Second argument of the main C function.
1158 private fun native_argv
(i
: Int): NativeString is intern