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
20 ###############################################################################
22 ###############################################################################
24 # Common subclass for String and Buffer
25 abstract class AbstractString
26 super AbstractArrayRead[Char]
28 readable private var _items
: NativeString
30 redef fun [](index
) do return _items
[index
]
34 # "abcd".substring(1, 2) # --> "bc"
35 # "abcd".substring(-1, 2) # --> "a"
36 # "abcd".substring(1, 0) # --> ""
37 # "abcd".substring(2, 5) # --> "cd"
38 fun substring
(from
: Int, count
: Int): String
42 if from
< 0 then from
= 0
43 if count
> length
then count
= length
45 var r
= new Buffer.with_capacity
(count
- from
)
56 # Create a substring from `self' beginning at the 'from' position
58 # "abcd".substring(1) # --> "bcd"
59 # "abcd".substring(-1) # --> "abcd"
60 # "abcd".substring(2) # --> "cd"
61 fun substring_from
(from
: Int): String
64 return substring
(from
, length
- from
)
67 # Is `self' a substring of the `str' string from pos `pos'
69 # "bc".is_substring("abcd",1) # --> true
70 # "bc".is_substring("abcd",2) # --> false
71 fun has_substring
(str
: String, pos
: Int): Bool
73 var itsindex
= str
.length
- 1
74 var myindex
= pos
+ itsindex
76 var itsitems
= str
._items
77 if myindex
> length
or itsindex
> myindex
then return false
78 while itsindex
>= 0 do
79 if myitems
[myindex
] != itsitems
[itsindex
] then return false
86 # Is this string prefixed by 'prefix'
88 # "abc".is_prefix("abcd") # --> true
89 # "bc".is_prefix("abcd") # --> false
90 fun has_prefix
(prefix
: String): Bool do return has_substring
(prefix
,0)
92 # Is this string suffixed by 'suffix'
94 # "abcd".has_suffix("abc") # --> false
95 # "abcd".has_suffix("bcd") # --> true
96 fun has_suffix
(suffix
: String): Bool do return has_substring
(suffix
, length
- suffix
.length
)
98 # If `self' contains only digits, return the corresponding integer
102 return to_s
.to_cstring
.atoi
105 # If `self' contains a float, return the corresponding float
109 return to_s
.to_cstring
.atof
112 # If `self' contains only digits and alpha <= 'f', return the corresponding integer.
113 fun to_hex
: Int do return a_to
(16)
115 # If `self' contains only digits and letters, return the corresponding integer in a given base
116 fun a_to
(base
: Int) : Int
143 # Returns true if the string contains only Numeric values (and one "," or one "." character)
146 var has_point_or_comma
= false
151 if (i
== '.' or i
== ',') and not has_point_or_comma
153 has_point_or_comma
= true
162 # A upper case version of `self'
165 var s
= new Buffer.with_capacity
(length
)
166 for i
in self do s
.add
(i
.to_upper
)
170 # A lower case version of `self'
171 fun to_lower
: String
173 var s
= new Buffer.with_capacity
(length
)
174 for i
in self do s
.add
(i
.to_lower
)
189 # Immutable strings of characters.
194 redef type OTHER: String
196 # Create a new string from a given char *.
197 init with_native
(nat
: NativeString, size
: Int)
204 # Create a new string from a null terminated char *.
205 init from_cstring
(str
: NativeString)
207 var size
= str
.cstring_length
212 # Return a null terminated char *
213 fun to_cstring
: NativeString
220 if not o
isa String or o
is null then return false
222 if o
.length
!= l
then return false
227 if it
[i
] != oit
[i
] then return false
240 while i
< l1
and i
< l2
do
257 # The concatenation of `self' with `r'
258 fun +(s
: String): String
260 var r
= new Buffer.with_capacity
(length
+ s
.length
)
266 # i repetitions of self
267 fun *(i
: Int): String
270 var r
= new Buffer.with_capacity
(length
* i
)
278 redef fun to_s
do return self
282 # djb2 hash algorythm
287 h
= (h
* 32) + h
+ it
[i
].ascii
295 # Mutable strings of characters.
300 super AbstractArray[Char]
302 redef type OTHER: String
304 redef fun []=(index
, item
)
306 if index
== length
then
310 assert index
>= 0 and index
< length
316 if _capacity
<= length
then enlarge
(length
+ 5)
321 redef fun enlarge
(cap
)
324 if cap
<= c
then return
325 while c
<= cap
do c
= c
* 2 + 2
326 var a
= calloc_string
(c
+1)
327 _items
.copy_to
(a
, length
, 0, 0)
336 if _capacity
< length
+ sl
then enlarge
(length
+ sl
)
337 s
.items
.copy_to
(_items
, sl
, 0, length
)
344 redef fun to_s
: String
347 var a
= calloc_string
(l
+1)
348 _items
.copy_to
(a
, l
, 0, 0)
350 # Ensure the afterlast byte is '\0' to nul-terminated char *
353 return new String.with_native
(a
, length
)
361 while i
< l1
and i
< l2
do
362 var c1
= self[i
].ascii
378 # Create a new empty string.
386 _capacity
= s
.length
+ 1
388 _items
= calloc_string
(_capacity
)
389 s
.items
.copy_to
(_items
, _length
, 0, 0)
392 # Create a new empty string with a given capacity.
393 init with_capacity
(cap
: Int)
396 # _items = new NativeString.calloc(cap)
397 _items
= calloc_string
(cap
+1)
404 if not o
isa Buffer or o
is null then return false
406 if o
.length
!= l
then return false
411 if it
[i
] != oit
[i
] then return false
417 readable private var _capacity
: Int
420 ###############################################################################
422 ###############################################################################
425 # User readable representation of `self'.
426 fun to_s
: String do return inspect
428 # The class name of the object in NativeString format.
429 private fun native_class_name
: NativeString is intern
431 # The class name of the object.
432 # FIXME: real type information is not available at runtime.
433 # Therefore, for instance, an instance of List[Bool] has just
434 # "List" for class_name
435 fun class_name
: String do return new String.from_cstring
(native_class_name
)
437 # Developer readable representation of `self'.
438 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
441 return "<{inspect_head}>"
444 # Return "CLASSNAME:#OBJECTID".
445 # This function is mainly used with the redefinition of the inspect method
446 protected fun inspect_head
: String
448 return "{class_name}:#{object_id.to_hex}"
451 protected fun args
: Sequence[String]
469 fun fill_buffer
(s
: Buffer, base
: Int, signed
: Bool)
470 # Fill `s' with the digits in base 'base' of `self' (and with the '-' sign if 'signed' and negative).
471 # assume < to_c max const of char
478 else if self == 0 then
485 var pos
= digit_count
(base
) - 1
486 while pos
>= 0 and n
> 0 do
487 s
[pos
] = (n
% base
).to_c
493 # return displayable int in base 10 and signed
494 redef fun to_s
do return to_base
(10,true)
496 # return displayable int in hexadecimal (unsigned (not now))
497 fun to_hex
: String do return to_base
(16,false)
499 # return displayable int in base base and signed
500 fun to_base
(base
: Int, signed
: Bool): String
502 var l
= digit_count
(base
)
503 var s
= new Buffer.from
(" " * l
)
504 fill_buffer
(s
, base
, signed
)
510 redef fun to_s
do return to_precision
(6)
512 # `self' representation with `nb' digits after the '.'.
513 fun to_precision
(nb
: Int): String
515 if nb
== 0 then return to_i
.to_s
523 var d
= ((self-i
.to_f
)*dec
).to_i
531 var s
= new Buffer.with_capacity
(1)
536 # Returns true if the char is a numerical digit
539 if self >= '0' and self <= '9'
546 # Returns true if the char is an alpha digit
549 if (self >= 'a' and self <= 'z') or (self >= 'A' and self <= 'Z') then return true
553 # Returns true if the char is an alpha or a numeric digit
554 fun is_alphanumeric
: Bool
556 if self.is_numeric
or self.is_alpha
then return true
561 redef class Collection[E
]
562 # Concatenate elements.
566 for e
in self do if e
!= null then s
.append
(e
.to_s
)
570 # Concatenate and separate each elements with `sep'.
571 fun join
(sep
: String): String
573 if is_empty
then return ""
575 var s
= new Buffer # Result
580 if e
!= null then s
.append
(e
.to_s
)
587 if e
!= null then s
.append
(e
.to_s
)
595 # Fast implementation
603 if e
!= null then s
.append
(e
.to_s
)
611 # Concatenate couple of 'key value'.
612 # key and value are separated by 'couple_sep'.
613 # each couple is separated each couple with `sep'.
614 fun join
(sep
: String, couple_sep
: String): String
616 if is_empty
then return ""
618 var s
= new Buffer # Result
624 if e
!= null then s
.append
("{k}{couple_sep}{e}")
632 if e
!= null then s
.append
("{k}{couple_sep}{e}")
639 ###############################################################################
641 ###############################################################################
643 # Native strings are simple C char *
645 fun [](index
: Int): Char is intern
646 fun []=(index
: Int, item
: Char) is intern
647 fun copy_to
(dest
: NativeString, length
: Int, from
: Int, to
: Int) is intern
649 # Position of the first nul character.
650 fun cstring_length
: Int
653 while self[l
] != '\0' do l
+= 1
656 fun atoi
: Int is intern
657 fun atof
: Float is extern "atof"
660 # StringCapable objects can create native strings
661 interface StringCapable
662 protected fun calloc_string
(size
: Int): NativeString is intern
666 var _args_cache
: nullable Sequence[String]
668 redef fun args
: Sequence[String]
670 if _args_cache
== null then init_args
671 return _args_cache
.as(not null)
674 # The name of the program as given by the OS
675 fun program_name
: String
677 return new String.from_cstring
(native_argv
(0))
680 # Initialize `args' with the contents of `native_argc' and `native_argv'.
681 private fun init_args
683 var argc
= native_argc
684 var args
= new Array[String].with_capacity
(0)
687 args
[i-1
] = new String.from_cstring
(native_argv
(i
))
693 private fun native_argc
: Int is extern "kernel_Sys_Sys_native_argc_0" # First argument of the main C function.
695 private fun native_argv
(i
: Int): NativeString is extern "kernel_Sys_Sys_native_argv_1" # Second argument of the main C function.