1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
11 # All the array-based text representations
14 intrude import abstract_text
22 private class FlatSubstringsIter
23 super Iterator[FlatText]
25 var tgt
: nullable FlatText
29 return tgt
.as(not null)
32 redef fun is_ok
do return tgt
!= null
34 redef fun next
do tgt
= null
37 # Immutable strings of characters.
42 # Index at which `self` begins in `items`, inclusively
43 private var first_byte
: Int is noinit
45 # Index at which `self` ends in `items`, inclusively
46 private var last_byte
: Int is noinit
48 redef var chars
= new FlatStringCharView(self) is lazy
50 redef var bytes
= new FlatStringByteView(self) is lazy
52 # Cache of the latest position (char) explored in the string
54 # Cached position (bytes) in the NativeString underlying the String
55 var bytepos
: Int = first_byte
is lateinit
57 redef var length
is lazy
do
58 if bytelen
== 0 then return 0
64 st
+= its
.length_of_char_at
(st
)
70 redef fun [](index
) do return items
.char_at
(char_to_byte_index
(index
))
72 # Index of the character `index` in `items`
73 private fun char_to_byte_index
(index
: Int): Int do
78 # Find best insertion point
79 var delta_begin
= index
80 var delta_end
= (ln
- 1) - index
81 var delta_cache
= (position
- index
).abs
85 if delta_cache
< min
then min
= delta_cache
86 if delta_end
< min
then min
= delta_end
91 if min
== delta_begin
then
94 else if min
== delta_cache
then
98 ns_i
= its
.find_beginning_of_char_at
(last_byte
)
102 ns_i
= its
.char_to_byte_index_cached
(index
, my_i
, ns_i
)
112 var b
= new FlatBuffer.with_capacity
(bytelen
+ 1)
113 for i
in [length
- 1 .. 0].step
(-1) do
116 var s
= b
.to_s
.as(FlatString)
117 s
.length
= self.length
121 redef fun fast_cstring
do return items
.fast_cstring
(first_byte
)
123 redef fun substring
(from
, count
)
129 if count
< 0 then count
= 0
133 if (count
+ from
) > length
then count
= length
- from
134 if count
<= 0 then return ""
135 var end_index
= from
+ count
- 1
137 var bytefrom
= char_to_byte_index
(from
)
138 var byteto
= char_to_byte_index
(end_index
)
139 byteto
+= items
.length_of_char_at
(byteto
) - 1
141 var s
= new FlatString.full
(items
, byteto
- bytefrom
+ 1, bytefrom
, byteto
, count
)
145 redef fun empty
do return "".as(FlatString)
149 var outstr
= new FlatBuffer.with_capacity
(self.bytelen
+ 1)
155 outstr
.add
(chars
[pos
].to_upper
)
164 var outstr
= new FlatBuffer.with_capacity
(self.bytelen
+ 1)
170 outstr
.add
(chars
[pos
].to_lower
)
179 for i
in chars
do i
.output
182 ##################################################
183 # String Specific Methods #
184 ##################################################
186 # Low-level creation of a new string with minimal data.
188 # `items` will be used as is, without copy, to retrieve the characters of the string.
189 # Aliasing issues is the responsibility of the caller.
190 private init with_infos
(items
: NativeString, bytelen
, from
, to
: Int)
193 self.bytelen
= bytelen
198 # Low-level creation of a new string with all the data.
200 # `items` will be used as is, without copy, to retrieve the characters of the string.
201 # Aliasing issues is the responsibility of the caller.
202 private init full
(items
: NativeString, bytelen
, from
, to
, length
: Int)
206 self.bytelen
= bytelen
211 redef fun to_cstring
do
212 if real_items
!= null then return real_items
.as(not null)
213 var new_items
= new NativeString(bytelen
+ 1)
214 self.items
.copy_to
(new_items
, bytelen
, first_byte
, 0)
215 new_items
[bytelen
] = 0u8
216 real_items
= new_items
222 if not other
isa FlatString then return super
224 if self.object_id
== other
.object_id
then return true
226 var my_length
= bytelen
228 if other
.bytelen
!= my_length
then return false
230 var my_index
= first_byte
231 var its_index
= other
.first_byte
233 var last_iteration
= my_index
+ my_length
235 var itsitems
= other
.items
236 var myitems
= self.items
238 while my_index
< last_iteration
do
239 if myitems
[my_index
] != itsitems
[its_index
] then return false
249 if not other
isa FlatString then return super
251 if self.object_id
== other
.object_id
then return false
253 var my_length
= self.bytelen
254 var its_length
= other
.bytelen
256 var max
= if my_length
< its_length
then my_length
else its_length
258 var myits
= self.bytes
259 var itsits
= other
.bytes
261 for i
in [0 .. max
[ do
262 var my_curr_char
= myits
[i
]
263 var its_curr_char
= itsits
[i
]
265 if my_curr_char
!= its_curr_char
then
266 if my_curr_char
< its_curr_char
then return true
271 return my_length
< its_length
278 var nlen
= mlen
+ slen
280 var mifrom
= first_byte
281 if s
isa FlatText then
283 var sifrom
= s
.as(FlatString).first_byte
284 var ns
= new NativeString(nlen
+ 1)
285 mits
.copy_to
(ns
, mlen
, mifrom
, 0)
286 sits
.copy_to
(ns
, slen
, sifrom
, mlen
)
287 return new FlatString.full
(ns
, nlen
, 0, nlen
- 1, length
+ o
.length
)
294 var mybtlen
= bytelen
295 var new_bytelen
= mybtlen
* i
297 var newlen
= mylen
* i
298 var ns
= new NativeString(new_bytelen
+ 1)
299 ns
[new_bytelen
] = 0u8
302 items
.copy_to
(ns
, bytelen
, first_byte
, offset
)
306 return new FlatString.full
(ns
, new_bytelen
, 0, new_bytelen
- 1, newlen
)
312 if hash_cache
== null then
313 # djb2 hash algorithm
319 while i
<= last_byte
do
320 h
= (h
<< 5) + h
+ myitems
[i
].to_i
327 return hash_cache
.as(not null)
330 redef fun substrings
do return new FlatSubstringsIter(self)
333 private class FlatStringCharReverseIterator
334 super IndexedIterator[Char]
336 var target
: FlatString
340 init with_pos
(tgt
: FlatString, pos
: Int)
345 redef fun is_ok
do return curr_pos
>= 0
347 redef fun item
do return target
[curr_pos
]
349 redef fun next
do curr_pos
-= 1
351 redef fun index
do return curr_pos
355 private class FlatStringCharIterator
356 super IndexedIterator[Char]
358 var target
: FlatString
364 init with_pos
(tgt
: FlatString, pos
: Int)
366 init(tgt
, tgt
.length
- 1, pos
)
369 redef fun is_ok
do return curr_pos
<= max
371 redef fun item
do return target
[curr_pos
]
373 redef fun next
do curr_pos
+= 1
375 redef fun index
do return curr_pos
379 private class FlatStringCharView
382 redef type SELFTYPE: FlatString
384 redef fun [](index
) do return target
[index
]
386 redef fun iterator_from
(start
) do return new FlatStringCharIterator.with_pos
(target
, start
)
388 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator.with_pos
(target
, start
)
392 private class FlatStringByteReverseIterator
393 super IndexedIterator[Byte]
395 var target
: FlatString
397 var target_items
: NativeString
401 init with_pos
(tgt
: FlatString, pos
: Int)
403 init(tgt
, tgt
.items
, pos
+ tgt
.first_byte
)
406 redef fun is_ok
do return curr_pos
>= target
.first_byte
408 redef fun item
do return target_items
[curr_pos
]
410 redef fun next
do curr_pos
-= 1
412 redef fun index
do return curr_pos
- target
.first_byte
416 private class FlatStringByteIterator
417 super IndexedIterator[Byte]
419 var target
: FlatString
421 var target_items
: NativeString
425 init with_pos
(tgt
: FlatString, pos
: Int)
427 init(tgt
, tgt
.items
, pos
+ tgt
.first_byte
)
430 redef fun is_ok
do return curr_pos
<= target
.last_byte
432 redef fun item
do return target_items
[curr_pos
]
434 redef fun next
do curr_pos
+= 1
436 redef fun index
do return curr_pos
- target
.first_byte
440 private class FlatStringByteView
443 redef type SELFTYPE: FlatString
447 # Check that the index (+ first_byte) is not larger than last_byte
448 # In other terms, if the index is valid
450 var target
= self.target
451 assert (index
+ target
.first_byte
) <= target
.last_byte
452 return target
.items
[index
+ target
.first_byte
]
455 redef fun iterator_from
(start
) do return new FlatStringByteIterator.with_pos
(target
, start
)
457 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator.with_pos
(target
, start
)
462 redef new do return new FlatBuffer
464 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
467 # Mutable strings of characters.
472 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
474 redef var bytes
= new FlatBufferByteView(self) is lazy
476 redef var bytelen
= 0
480 private var capacity
= 0
482 redef fun fast_cstring
do return items
.fast_cstring
(0)
484 redef fun substrings
do return new FlatSubstringsIter(self)
486 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
488 # This happens when an operation modifies the current `Buffer` and
489 # the Copy-On-Write flag `written` is set at true.
491 var nns
= new NativeString(capacity
)
492 items
.copy_to
(nns
, bytelen
, 0, 0)
497 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
499 # Internal only, does not modify bytelen or length, this is the caller's responsability
500 private fun rshift_bytes
(from
: Int, len
: Int) do
503 if bytelen
+ len
> capacity
then
504 capacity
= capacity
* 2 + 2
505 nit
= new NativeString(capacity
)
506 oit
.copy_to
(nit
, 0, 0, from
)
508 oit
.copy_to
(nit
, bytelen
- from
, from
, from
+ len
)
511 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
513 # Internal only, does not modify bytelen or length, this is the caller's responsability
514 private fun lshift_bytes
(from
: Int, len
: Int) do
515 items
.copy_to
(items
, bytelen
- from
, from
, from
- len
)
520 assert i
< length
and i
>= 0
521 return items
.char_at
(items
.char_to_byte_index
(i
))
524 redef fun []=(index
, item
)
526 assert index
>= 0 and index
<= length
527 if written
then reset
529 if index
== length
then
533 var ip
= items
.char_to_byte_index
(index
)
534 var c
= items
.char_at
(ip
)
535 var clen
= c
.u8char_len
536 var itemlen
= item
.u8char_len
537 var size_diff
= itemlen
- clen
538 if size_diff
> 0 then
539 rshift_bytes
(ip
+ clen
, size_diff
)
540 else if size_diff
< 0 then
541 lshift_bytes
(ip
+ clen
, -size_diff
)
544 items
.set_char_at
(ip
, item
)
549 if written
then reset
551 var clen
= c
.u8char_len
552 enlarge
(bytelen
+ clen
)
553 items
.set_char_at
(bytelen
, c
)
560 if written
then reset
565 redef fun empty
do return new Buffer
567 redef fun enlarge
(cap
)
570 if cap
<= c
then return
571 while c
<= cap
do c
= c
* 2 + 2
572 # The COW flag can be set at false here, since
573 # it does a copy of the current `Buffer`
575 var a
= new NativeString(c
+1)
576 if bytelen
> 0 then items
.copy_to
(a
, bytelen
, 0, 0)
584 if bytelen
== 0 then items
= new NativeString(1)
585 return new FlatString.with_infos
(items
, bytelen
, 0, bytelen
- 1)
591 var new_native
= new NativeString(bytelen
+ 1)
592 new_native
[bytelen
] = 0u8
593 if length
> 0 then items
.copy_to
(new_native
, bytelen
, 0, 0)
594 real_items
= new_native
597 return real_items
.as(not null)
600 # Create a new empty string.
603 # Low-level creation a new buffer with given data.
605 # `items` will be used as is, without copy, to store the characters of the buffer.
606 # Aliasing issues is the responsibility of the caller.
608 # If `items` is shared, `written` should be set to true after the creation
609 # so that a modification will do a copy-on-write.
610 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
613 self.capacity
= capacity
614 self.bytelen
= bytelen
618 # Create a new string copied from `s`.
621 items
= new NativeString(s
.bytelen
)
622 if s
isa FlatText then
625 for i
in substrings
do i
.as(FlatString).items
.copy_to
(items
, i
.bytelen
, 0, 0)
633 # Create a new empty string with a given capacity.
634 init with_capacity
(cap
: Int)
637 items
= new NativeString(cap
+ 1)
644 if s
.is_empty
then return
647 enlarge
(bytelen
+ sl
)
648 if s
isa FlatString then
649 s
.items
.copy_to
(items
, sl
, s
.first_byte
, bytelen
)
650 else if s
isa FlatBuffer then
651 s
.items
.copy_to
(items
, sl
, 0, bytelen
)
653 for i
in s
.substrings
do append i
660 # Copies the content of self in `dest`
661 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
663 var self_chars
= self.chars
664 var dest_chars
= dest
.chars
665 for i
in [0..len-1
] do
666 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
670 redef fun substring
(from
, count
)
673 if from
< 0 then from
= 0
674 if (from
+ count
) > length
then count
= length
- from
676 var bytefrom
= items
.char_to_byte_index
(from
)
677 var byteto
= items
.char_to_byte_index
(count
+ from
- 1)
678 byteto
+= items
.char_at
(byteto
).u8char_len
- 1
679 var byte_length
= byteto
- bytefrom
+ 1
680 var r_items
= new NativeString(byte_length
)
681 items
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
682 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
691 var ns
= new FlatBuffer.with_capacity
(capacity
)
692 for i
in chars
.reverse_iterator
do ns
.add i
696 redef fun times
(repeats
)
698 var x
= new FlatString.with_infos
(items
, bytelen
, 0, bytelen
- 1)
699 for i
in [1 .. repeats
[ do
706 if written
then reset
707 for i
in [0 .. length
[ do self[i
] = self[i
].to_upper
712 if written
then reset
713 for i
in [0 .. length
[ do self[i
] = self[i
].to_lower
717 private class FlatBufferByteReverseIterator
718 super IndexedIterator[Byte]
720 var target
: FlatBuffer
722 var target_items
: NativeString
726 init with_pos
(tgt
: FlatBuffer, pos
: Int)
728 init(tgt
, tgt
.items
, pos
)
731 redef fun index
do return curr_pos
733 redef fun is_ok
do return curr_pos
>= 0
735 redef fun item
do return target_items
[curr_pos
]
737 redef fun next
do curr_pos
-= 1
741 private class FlatBufferByteView
744 redef type SELFTYPE: FlatBuffer
746 redef fun [](index
) do return target
.items
[index
]
748 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator.with_pos
(target
, pos
)
750 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator.with_pos
(target
, pos
)
754 private class FlatBufferByteIterator
755 super IndexedIterator[Byte]
757 var target
: FlatBuffer
759 var target_items
: NativeString
763 init with_pos
(tgt
: FlatBuffer, pos
: Int)
765 init(tgt
, tgt
.items
, pos
)
768 redef fun index
do return curr_pos
770 redef fun is_ok
do return curr_pos
< target
.bytelen
772 redef fun item
do return target_items
[curr_pos
]
774 redef fun next
do curr_pos
+= 1
778 private class FlatBufferCharReverseIterator
779 super IndexedIterator[Char]
781 var target
: FlatBuffer
785 init with_pos
(tgt
: FlatBuffer, pos
: Int)
790 redef fun index
do return curr_pos
792 redef fun is_ok
do return curr_pos
>= 0
794 redef fun item
do return target
[curr_pos
]
796 redef fun next
do curr_pos
-= 1
800 private class FlatBufferCharView
803 redef type SELFTYPE: FlatBuffer
805 redef fun [](index
) do return target
[index
]
807 redef fun []=(index
, item
)
809 assert index
>= 0 and index
<= length
810 if index
== length
then
827 fun enlarge
(cap
: Int)
834 var s_length
= s
.length
835 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
836 for i
in s
do target
.add i
839 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator.with_pos
(target
, pos
)
841 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator.with_pos
(target
, pos
)
845 private class FlatBufferCharIterator
846 super IndexedIterator[Char]
848 var target
: FlatBuffer
854 init with_pos
(tgt
: FlatBuffer, pos
: Int)
856 init(tgt
, tgt
.length
- 1, pos
)
859 redef fun index
do return curr_pos
861 redef fun is_ok
do return curr_pos
<= max
863 redef fun item
do return target
[curr_pos
]
865 redef fun next
do curr_pos
+= 1
869 redef class NativeString
872 return to_s_with_length
(cstring_length
)
875 # Returns `self` as a String of `length`.
876 redef fun to_s_with_length
(length
): FlatString
879 var str
= new FlatString.with_infos
(self, length
, 0, length
- 1)
883 # Returns `self` as a new String.
884 redef fun to_s_with_copy
: FlatString
886 var length
= cstring_length
887 var new_self
= new NativeString(length
+ 1)
888 copy_to
(new_self
, length
, 0, 0)
889 var str
= new FlatString.with_infos
(new_self
, length
, 0, length
- 1)
890 new_self
[length
] = 0u8
891 str
.real_items
= new_self
895 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
897 # Very unsafe, make sure to have room for this char prior to calling this function.
898 private fun set_char_at
(pos
: Int, c
: Char) do
899 var ln
= c
.u8char_len
900 native_set_char
(pos
, c
, ln
)
903 private fun native_set_char
(pos
: Int, c
: Char, ln
: Int) `{
904 char* dst = self + pos;
910 dst[0] = 0xC0 | ((c & 0x7C0) >> 6);
911 dst[1] = 0x80 | (c & 0x3F);
914 dst[0] = 0xE0 | ((c & 0xF000) >> 12);
915 dst[1] = 0x80 | ((c & 0xFC0) >> 6);
916 dst[2] = 0x80 | (c & 0x3F);
919 dst[0] = 0xF0 | ((c & 0x1C0000) >> 18);
920 dst[1] = 0x80 | ((c & 0x3F000) >> 12);
921 dst[2] = 0x80 | ((c & 0xFC0) >> 6);
922 dst[3] = 0x80 | (c & 0x3F);
929 redef fun to_base
(base
, signed
)
931 var l
= digit_count
(base
)
932 var s
= new FlatBuffer.from
(" " * l
)
933 fill_buffer
(s
, base
, signed
)
937 # return displayable int in base 10 and signed
939 # assert 1.to_s == "1"
940 # assert (-123).to_s == "-123"
942 # Fast case for common numbers
943 if self == 0 then return "0"
944 if self == 1 then return "1"
946 var nslen
= int_to_s_len
947 var ns
= new NativeString(nslen
+ 1)
949 native_int_to_s
(ns
, nslen
+ 1)
950 return new FlatString.full
(ns
, nslen
, 0, nslen
- 1, nslen
)
956 # Fast implementation
960 if l
== 0 then return ""
961 if l
== 1 then if self[0] == null then return "" else return self[0].to_s
963 var na
= new NativeArray[String](l
)
979 var ns
= new NativeString(sl
+ 1)
985 if tmp
isa FlatString then
986 var tpl
= tmp
.bytelen
987 tmp
.items
.copy_to
(ns
, tpl
, tmp
.first_byte
, off
)
990 for j
in tmp
.substrings
do
991 var s
= j
.as(FlatString)
993 s
.items
.copy_to
(ns
, slen
, s
.first_byte
, off
)
999 return ns
.to_s_with_length
(sl
)
1003 redef class NativeArray[E
]
1004 redef fun native_to_s
do
1005 assert self isa NativeArray[String]
1016 var ns
= new NativeString(sl
+ 1)
1022 if tmp
isa FlatString then
1023 var tpl
= tmp
.bytelen
1024 tmp
.items
.copy_to
(ns
, tpl
, tmp
.first_byte
, off
)
1027 for j
in tmp
.substrings
do
1028 var s
= j
.as(FlatString)
1029 var slen
= s
.bytelen
1030 s
.items
.copy_to
(ns
, slen
, s
.first_byte
, off
)
1036 return ns
.to_s_with_length
(sl
)
1040 redef class Map[K
,V
]
1041 redef fun join
(sep
, couple_sep
)
1043 if is_empty
then return ""
1045 var s
= new Buffer # Result
1051 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1053 # Concat other items
1059 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")