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 # This module is about character strings.
17 intrude import collection
# FIXME should be collection::array
20 ###############################################################################
22 ###############################################################################
24 abstract class AbstractString
25 super AbstractArrayRead[Char]
26 readable private var _items
: NativeString
28 redef fun [](index
) do return _items
[index
]
32 # "abcd".substring(1, 2) # --> "bc"
33 # "abcd".substring(-1, 2) # --> "a"
34 # "abcd".substring(1, 0) # --> ""
35 # "abcd".substring(2, 5) # --> "cd"
36 fun substring
(from
: Int, count
: Int): String
40 if from
< 0 then from
= 0
41 if count
> length
then count
= length
43 var r
= new Buffer.with_capacity
(count
- from
)
54 # Create a substring with the string beginning at the 'from' position
56 # "abcd".substring(1) # --> "bcd"
57 # "abcd".substring(-1) # --> "abcd"
58 # "abcd".substring(2) # --> "cd"
59 fun substring_from
(from
: Int): String
62 return substring
(from
, length
- from
)
65 # is this string a substring of the 'of' string from pos 'pos'
67 # "bc".is_substring("abcd",1) # --> true
68 # "bc".is_substring("abcd",2) # --> false
69 fun has_substring
(str
: String, pos
: Int): Bool
71 var itsindex
= str
.length
- 1
72 var myindex
= pos
+ itsindex
74 var itsitems
= str
._items
75 if myindex
> length
or itsindex
> myindex
then return false
76 while itsindex
>= 0 do
77 if myitems
[myindex
] != itsitems
[itsindex
] then return false
84 # Is this string prefixed by 'prefix'
86 # "abc".is_prefix("abcd") # --> true
87 # "bc".is_prefix("abcd") # --> false
88 fun has_prefix
(prefix
: String): Bool do return has_substring
(prefix
,0)
90 # Is this string suffixed by 'suffix'
92 # "abcd".has_suffix("abc") # --> false
93 # "abcd".has_suffix("bcd") # --> true
94 fun has_suffix
(suffix
: String): Bool do return has_substring
(suffix
, length
- suffix
.length
)
96 # If `self' contains only digits, return the corresponding integer
100 return to_s
.to_cstring
.atoi
103 # If `self' contains only digits and alpha <= 'f', return the corresponding integer.
104 fun to_hex
: Int do return a_to
(16)
106 # If `self' contains only digits and letters, return the corresponding integer in a given base
107 fun a_to
(base
: Int) : Int
134 # String to upper case
137 var s
= new Buffer.with_capacity
(length
)
138 for i
in self do s
.add
(i
.to_upper
)
142 # String to lower case
143 fun to_lower
: String
145 var s
= new Buffer.with_capacity
(length
)
146 for i
in self do s
.add
(i
.to_lower
)
165 redef type OTHER: String
167 # Create a new string from a given char *.
168 init with_native
(nat
: NativeString, size
: Int)
175 # Create a new string from a null terminated char *.
176 init from_cstring
(str
: NativeString)
178 var size
= str
.cstring_length
183 # Return a null terminated char *
184 fun to_cstring
: NativeString
191 if not o
isa String or o
is null then return false
193 if o
.length
!= l
then return false
198 if it
[i
] != oit
[i
] then return false
211 while i
< l1
and i
< l2
do
228 # The concatenation of `self' with `r'
229 fun +(s
: String): String
231 var r
= new Buffer.with_capacity
(length
+ s
.length
)
237 # i repetitions of self
238 fun *(i
: Int): String
241 var r
= new Buffer.with_capacity
(length
* i
)
249 redef fun to_s
do return self
253 # djb2 hash algorythm
258 h
= (h
* 32) + h
+ it
[i
].ascii
266 # Strings are arrays of characters.
271 super AbstractArray[Char]
273 redef type OTHER: String
275 redef fun []=(index
, item
)
277 if index
== length
then
281 assert index
>= 0 and index
< length
287 if _capacity
<= length
then enlarge
(length
+ 5)
292 redef fun enlarge
(cap
)
295 if cap
<= c
then return
296 while c
<= cap
do c
= c
* 2 + 2
297 var a
= calloc_string
(c
+1)
298 _items
.copy_to
(a
, length
, 0, 0)
307 if _capacity
< length
+ sl
then enlarge
(length
+ sl
)
308 s
.items
.copy_to
(_items
, sl
, 0, length
)
315 redef fun to_s
: String
318 var a
= calloc_string
(l
+1)
319 _items
.copy_to
(a
, l
, 0, 0)
321 # Ensure the afterlast byte is '\0' to nul-terminated char *
324 return new String.with_native
(a
, length
)
332 while i
< l1
and i
< l2
do
333 var c1
= self[i
].ascii
349 # Create a new empty string.
357 _capacity
= s
.length
+ 1
359 _items
= calloc_string
(_capacity
)
360 s
.items
.copy_to
(_items
, _length
, 0, 0)
363 # Create a new empty string with a given capacity.
364 init with_capacity
(cap
: Int)
367 # _items = new NativeString.calloc(cap)
368 _items
= calloc_string
(cap
+1)
375 if not o
isa Buffer or o
is null then return false
377 if o
.length
!= l
then return false
382 if it
[i
] != oit
[i
] then return false
388 readable private var _capacity
: Int
391 ###############################################################################
393 ###############################################################################
396 # fun class_name: String is extern intern # The name of the class
398 # User redeable representation of `self'.
399 fun to_s
: String do return inspect
401 # Developper readable representation of `self'.
402 # Usualy, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
410 # Return "<CLASSNAME:#OBJECTID".
411 # This fuction is mainly used with the redefinition of the inspect(0) method
412 protected fun inspect_head
: String
414 return "<{object_id.to_hex}"
417 protected fun args
: Sequence[String]
435 fun fill_buffer
(s
: Buffer, base
: Int, signed
: Bool)
436 # Fill `s' with the digits in base 'base' of `self' (and with the '-' sign if 'signed' and negative).
437 # assume < to_c max const of char
444 else if self == 0 then
451 var pos
= digit_count
(base
) - 1
452 while pos
>= 0 and n
> 0 do
453 s
[pos
] = (n
% base
).to_c
459 # return displayable int in base 10 and signed
460 redef fun to_s
do return to_base
(10,true)
462 # return displayable int in hexadecimal (unsigned (not now))
463 fun to_hex
: String do return to_base
(16,false)
465 # return displayable int in base base and signed
466 fun to_base
(base
: Int, signed
: Bool): String
468 var l
= digit_count
(base
)
469 var s
= new Buffer.from
(" " * l
)
470 fill_buffer
(s
, base
, signed
)
476 redef fun to_s
do return to_precision
(6)
478 # `self' representation with `nb' digits after the '.'.
479 fun to_precision
(nb
: Int): String
481 if nb
== 0 then return to_i
.to_s
489 var d
= ((self-i
.to_f
)*dec
).to_i
497 var s
= new Buffer.with_capacity
(1)
503 redef class Collection[E
]
504 # Concatenate elements.
508 for e
in self do if e
!= null then s
.append
(e
.to_s
)
512 # Concatenate and separate each elements with `sep'.
513 fun join
(sep
: String): String
515 if is_empty
then return ""
517 var s
= new Buffer # Result
522 if e
!= null then s
.append
(e
.to_s
)
529 if e
!= null then s
.append
(e
.to_s
)
537 # Fast implementation
545 if e
!= null then s
.append
(e
.to_s
)
553 # Concatenate couple of 'key value' separate by 'couple_sep' and separate each couple with `sep'.
554 fun map_join
(sep
: String, couple_sep
: String): String
556 if is_empty
then return ""
558 var s
= new Buffer # Result
564 if e
!= null then s
.append
("{k}{couple_sep}{e}")
572 if e
!= null then s
.append
("{k}{couple_sep}{e}")
579 ###############################################################################
581 ###############################################################################
583 # Native strings are simple C char *
585 fun [](index
: Int): Char is intern
586 fun []=(index
: Int, item
: Char) is intern
587 fun copy_to
(dest
: NativeString, length
: Int, from
: Int, to
: Int) is intern
589 # Position of the first nul character.
590 fun cstring_length
: Int
593 while self[l
] != '\0' do l
+= 1
596 fun atoi
: Int is intern
599 # StringCapable objects can create native strings
601 protected fun calloc_string
(size
: Int): NativeString is intern
605 var _args_cache
: nullable Sequence[String]
607 redef fun args
: Sequence[String]
609 if _args_cache
== null then init_args
610 return _args_cache
.as(not null)
613 # The name of the program as given by the OS
614 fun program_name
: String
616 return new String.from_cstring
(native_argv
(0))
619 # Initialize `args' with the contents of `native_argc' and `native_argv'.
620 private fun init_args
622 var argc
= native_argc
623 var args
= new Array[String].with_capacity
(0)
626 args
[i-1
] = new String.from_cstring
(native_argv
(i
))
632 private fun native_argc
: Int is extern "kernel_Sys_Sys_native_argc_0" # First argument of the main C function.
634 private fun native_argv
(i
: Int): NativeString is extern "kernel_Sys_Sys_native_argv_1" # Second argument of the main C function.