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 only digits and alpha <= 'f', return the corresponding integer.
106 fun to_hex
: Int do return a_to
(16)
108 # If `self' contains only digits and letters, return the corresponding integer in a given base
109 fun a_to
(base
: Int) : Int
136 # A upper case version of `self'
139 var s
= new Buffer.with_capacity
(length
)
140 for i
in self do s
.add
(i
.to_upper
)
144 # A lower case version of `self'
145 fun to_lower
: String
147 var s
= new Buffer.with_capacity
(length
)
148 for i
in self do s
.add
(i
.to_lower
)
163 # Immutable strings of characters.
168 redef type OTHER: String
170 # Create a new string from a given char *.
171 init with_native
(nat
: NativeString, size
: Int)
178 # Create a new string from a null terminated char *.
179 init from_cstring
(str
: NativeString)
181 var size
= str
.cstring_length
186 # Return a null terminated char *
187 fun to_cstring
: NativeString
194 if not o
isa String or o
is null then return false
196 if o
.length
!= l
then return false
201 if it
[i
] != oit
[i
] then return false
214 while i
< l1
and i
< l2
do
231 # The concatenation of `self' with `r'
232 fun +(s
: String): String
234 var r
= new Buffer.with_capacity
(length
+ s
.length
)
240 # i repetitions of self
241 fun *(i
: Int): String
244 var r
= new Buffer.with_capacity
(length
* i
)
252 redef fun to_s
do return self
256 # djb2 hash algorythm
261 h
= (h
* 32) + h
+ it
[i
].ascii
268 fun to_f
: Float is extern import String::to_cstring
271 # Mutable strings of characters.
276 super AbstractArray[Char]
278 redef type OTHER: String
280 redef fun []=(index
, item
)
282 if index
== length
then
286 assert index
>= 0 and index
< length
292 if _capacity
<= length
then enlarge
(length
+ 5)
297 redef fun enlarge
(cap
)
300 if cap
<= c
then return
301 while c
<= cap
do c
= c
* 2 + 2
302 var a
= calloc_string
(c
+1)
303 _items
.copy_to
(a
, length
, 0, 0)
312 if _capacity
< length
+ sl
then enlarge
(length
+ sl
)
313 s
.items
.copy_to
(_items
, sl
, 0, length
)
320 redef fun to_s
: String
323 var a
= calloc_string
(l
+1)
324 _items
.copy_to
(a
, l
, 0, 0)
326 # Ensure the afterlast byte is '\0' to nul-terminated char *
329 return new String.with_native
(a
, length
)
337 while i
< l1
and i
< l2
do
338 var c1
= self[i
].ascii
354 # Create a new empty string.
362 _capacity
= s
.length
+ 1
364 _items
= calloc_string
(_capacity
)
365 s
.items
.copy_to
(_items
, _length
, 0, 0)
368 # Create a new empty string with a given capacity.
369 init with_capacity
(cap
: Int)
372 # _items = new NativeString.calloc(cap)
373 _items
= calloc_string
(cap
+1)
380 if not o
isa Buffer or o
is null then return false
382 if o
.length
!= l
then return false
387 if it
[i
] != oit
[i
] then return false
393 readable private var _capacity
: Int
396 ###############################################################################
398 ###############################################################################
401 # User readable representation of `self'.
402 fun to_s
: String do return inspect
404 # The class name of the object in NativeString format.
405 private fun native_class_name
: NativeString is intern
407 # The class name of the object.
408 # FIXME: real type information is not available at runtime.
409 # Therefore, for instance, an instance of List[Bool] has just
410 # "List" for class_name
411 fun class_name
: String do return new String.from_cstring
(native_class_name
)
413 # Developer readable representation of `self'.
414 # Usually, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
417 return "<{inspect_head}>"
420 # Return "CLASSNAME:#OBJECTID".
421 # This function is mainly used with the redefinition of the inspect method
422 protected fun inspect_head
: String
424 return "{class_name}:#{object_id.to_hex}"
427 protected fun args
: Sequence[String]
445 fun fill_buffer
(s
: Buffer, base
: Int, signed
: Bool)
446 # Fill `s' with the digits in base 'base' of `self' (and with the '-' sign if 'signed' and negative).
447 # assume < to_c max const of char
454 else if self == 0 then
461 var pos
= digit_count
(base
) - 1
462 while pos
>= 0 and n
> 0 do
463 s
[pos
] = (n
% base
).to_c
469 # return displayable int in base 10 and signed
470 redef fun to_s
do return to_base
(10,true)
472 # return displayable int in hexadecimal (unsigned (not now))
473 fun to_hex
: String do return to_base
(16,false)
475 # return displayable int in base base and signed
476 fun to_base
(base
: Int, signed
: Bool): String
478 var l
= digit_count
(base
)
479 var s
= new Buffer.from
(" " * l
)
480 fill_buffer
(s
, base
, signed
)
486 redef fun to_s
do return to_precision
(6)
488 # `self' representation with `nb' digits after the '.'.
489 fun to_precision
(nb
: Int): String
491 if nb
== 0 then return to_i
.to_s
499 var d
= ((self-i
.to_f
)*dec
).to_i
507 var s
= new Buffer.with_capacity
(1)
513 redef class Collection[E
]
514 # Concatenate elements.
518 for e
in self do if e
!= null then s
.append
(e
.to_s
)
522 # Concatenate and separate each elements with `sep'.
523 fun join
(sep
: String): String
525 if is_empty
then return ""
527 var s
= new Buffer # Result
532 if e
!= null then s
.append
(e
.to_s
)
539 if e
!= null then s
.append
(e
.to_s
)
547 # Fast implementation
555 if e
!= null then s
.append
(e
.to_s
)
563 # Concatenate couple of 'key value'.
564 # key and value are separated by 'couple_sep'.
565 # each couple is separated each couple with `sep'.
566 fun join
(sep
: String, couple_sep
: String): String
568 if is_empty
then return ""
570 var s
= new Buffer # Result
576 if e
!= null then s
.append
("{k}{couple_sep}{e}")
584 if e
!= null then s
.append
("{k}{couple_sep}{e}")
591 ###############################################################################
593 ###############################################################################
595 # Native strings are simple C char *
597 fun [](index
: Int): Char is intern
598 fun []=(index
: Int, item
: Char) is intern
599 fun copy_to
(dest
: NativeString, length
: Int, from
: Int, to
: Int) is intern
601 # Position of the first nul character.
602 fun cstring_length
: Int
605 while self[l
] != '\0' do l
+= 1
608 fun atoi
: Int is intern
611 # StringCapable objects can create native strings
612 interface StringCapable
613 protected fun calloc_string
(size
: Int): NativeString is intern
617 var _args_cache
: nullable Sequence[String]
619 redef fun args
: Sequence[String]
621 if _args_cache
== null then init_args
622 return _args_cache
.as(not null)
625 # The name of the program as given by the OS
626 fun program_name
: String
628 return new String.from_cstring
(native_argv
(0))
631 # Initialize `args' with the contents of `native_argc' and `native_argv'.
632 private fun init_args
634 var argc
= native_argc
635 var args
= new Array[String].with_capacity
(0)
638 args
[i-1
] = new String.from_cstring
(native_argv
(i
))
644 private fun native_argc
: Int is extern "kernel_Sys_Sys_native_argc_0" # First argument of the main C function.
646 private fun native_argv
(i
: Int): NativeString is extern "kernel_Sys_Sys_native_argv_1" # Second argument of the main C function.