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
39 # First byte of the NativeString
40 protected fun first_byte
: Int do return 0
42 # Last byte of the NativeString
43 protected fun last_byte
: Int do return first_byte
+ _bytelen
- 1
45 # Cache of the latest position (char) explored in the string
48 # Cached position (bytes) in the NativeString underlying the String
51 # Index of the character `index` in `_items`
52 fun char_to_byte_index
(index
: Int): Int do
53 var dpos
= index
- _position
58 if its
[b
] & 0x80u
8 == 0x00u
8 then
61 b
+= its
.length_of_char_at
(b
)
68 b
= its
.find_beginning_of_char_at
(b
- 1)
73 if dpos
== 0 then return b
77 # Find best insertion point
78 var delta_begin
= index
79 var delta_end
= (ln
- 1) - index
80 var delta_cache
= (pos
- index
).abs
83 if delta_cache
< min
then min
= delta_cache
84 if delta_end
< min
then min
= delta_end
89 if min
== delta_cache
then
92 else if min
== delta_begin
then
96 ns_i
= its
.find_beginning_of_char_at
(last_byte
)
100 ns_i
= its
.char_to_byte_index_cached
(index
, my_i
, ns_i
)
108 # By escaping `self` to HTML, how many more bytes will be needed ?
109 fun chars_to_html_escape
: Int do
118 else if c
== 0x3Eu
8 then
120 else if c
== 0x26u
8 then
122 else if c
== 0x22u
8 then
124 else if c
== 0x27u
8 then
126 else if c
== 0x2Fu
8 then
134 redef fun html_escape
136 var extra
= chars_to_html_escape
137 if extra
== 0 then return to_s
141 var nlen
= extra
+ _bytelen
142 var nits
= new NativeString(nlen
)
147 # Some HTML characters are used as meta-data, they need
148 # to be replaced by an HTML-Escaped equivalent
152 # * 0x26 (&) => &
153 # * 0x22 (") => "
154 # * 0x27 (') => '
155 # * 0x2F (/) => /
157 nits
[outpos
] = 0x26u
8
158 nits
[outpos
+ 1] = 0x6Cu
8
159 nits
[outpos
+ 2] = 0x74u
8
160 nits
[outpos
+ 3] = 0x3Bu
8
162 else if c
== 0x3Eu
8 then
163 nits
[outpos
] = 0x26u
8
164 nits
[outpos
+ 1] = 0x67u
8
165 nits
[outpos
+ 2] = 0x74u
8
166 nits
[outpos
+ 3] = 0x3Bu
8
168 else if c
== 0x26u
8 then
169 nits
[outpos
] = 0x26u
8
170 nits
[outpos
+ 1] = 0x61u
8
171 nits
[outpos
+ 2] = 0x6Du
8
172 nits
[outpos
+ 3] = 0x70u
8
173 nits
[outpos
+ 4] = 0x3Bu
8
175 else if c
== 0x22u
8 then
176 nits
[outpos
] = 0x26u
8
177 nits
[outpos
+ 1] = 0x23u
8
178 nits
[outpos
+ 2] = 0x33u
8
179 nits
[outpos
+ 3] = 0x34u
8
180 nits
[outpos
+ 4] = 0x3Bu
8
182 else if c
== 0x27u
8 then
183 nits
[outpos
] = 0x26u
8
184 nits
[outpos
+ 1] = 0x23u
8
185 nits
[outpos
+ 2] = 0x33u
8
186 nits
[outpos
+ 3] = 0x39u
8
187 nits
[outpos
+ 4] = 0x3Bu
8
189 else if c
== 0x2Fu
8 then
190 nits
[outpos
] = 0x26u
8
191 nits
[outpos
+ 1] = 0x23u
8
192 nits
[outpos
+ 2] = 0x34u
8
193 nits
[outpos
+ 3] = 0x37u
8
194 nits
[outpos
+ 4] = 0x3Bu
8
202 var s
= new FlatString.with_infos
(nits
, nlen
, 0)
206 # By escaping `self` to C, how many more bytes will be needed ?
208 # This enables a double-optimization in `escape_to_c` since if this
209 # method returns 0, then `self` does not need escaping and can be
211 fun chars_to_escape_to_c
: Int do
220 else if c
== 0x09u
8 then
222 else if c
== 0x22u
8 then
224 else if c
== 0x27u
8 then
226 else if c
== 0x5Cu
8 then
228 else if c
< 32u8
then
236 redef fun escape_to_c
do
237 var ln_extra
= chars_to_escape_to_c
238 if ln_extra
== 0 then return self.to_s
241 var nlen
= _bytelen
+ ln_extra
242 var nns
= new NativeString(nlen
)
249 # Any byte with value < 32 is a control character
250 # All their uses will be replaced by their octal
253 # There are two exceptions however:
258 # Aside from the code points above, the following are:
265 nns
[opos
+ 1] = 0x74u
8
267 else if c
== 0x0Au
8 then
269 nns
[opos
+ 1] = 0x6Eu
8
271 else if c
== 0x22u
8 then
273 nns
[opos
+ 1] = 0x22u
8
275 else if c
== 0x27u
8 then
277 nns
[opos
+ 1] = 0x27u
8
279 else if c
== 0x5Cu
8 then
281 nns
[opos
+ 1] = 0x5Cu
8
283 else if c
< 32u8
then
285 nns
[opos
+ 1] = 0x30u
8
286 nns
[opos
+ 2] = ((c
& 0x38u
8) >> 3) + 0x30u
8
287 nns
[opos
+ 3] = (c
& 0x07u
8) + 0x30u
8
295 return nns
.to_s_unsafe
(nlen
)
298 redef fun [](index
) do
302 # * ~70% want the next char
303 # * ~23% want the previous
304 # * ~7% want the same char
306 # So it makes sense to shortcut early. And early is here.
307 var dpos
= index
- _position
309 if dpos
== 1 and index
< len
- 1 then
312 if c
& 0x80u
8 == 0x00u
8 then
313 # We want the next, and current is easy.
314 # So next is easy to find!
318 # The rest will be done by `dpos==0` bellow.
321 else if dpos
== -1 and index
> 1 then
324 if c
& 0x80u
8 == 0x00u
8 then
325 # We want the previous, and it is easy.
334 # We know what we want (+0 or +1) just get it now!
337 if c
& 0x80u
8 == 0x00u
8 then return c
.ascii
338 return items
.char_at
(b
)
341 assert index
>= 0 and index
< len
342 return fetch_char_at
(index
)
345 # Gets a `Char` at `index` in `self`
347 # WARNING: Use at your own risks as no bound-checking is done
348 fun fetch_char_at
(index
: Int): Char do
349 var i
= char_to_byte_index
(index
)
352 if b
& 0x80u
8 == 0x00u
8 then return b
.ascii
353 return items
.char_at
(i
)
356 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
358 # assert "ff".to_hex == 255
359 redef fun to_hex
(pos
, ln
) do
361 if pos
== null then pos
= 0
362 if ln
== null then ln
= length
- pos
363 pos
= char_to_byte_index
(pos
)
366 for i
in [pos
.. max
[ do
368 res
+= its
[i
].ascii
.from_hex
374 # Immutable strings of characters.
375 abstract class FlatString
379 # Index at which `self` begins in `_items`, inclusively
380 redef var first_byte
is noinit
382 redef var chars
= new FlatStringCharView(self) is lazy
384 redef var bytes
= new FlatStringByteView(self) is lazy
386 redef var to_cstring
is lazy
do
388 var new_items
= new NativeString(blen
+ 1)
389 _items
.copy_to
(new_items
, blen
, _first_byte
, 0)
390 new_items
[blen
] = 0u8
394 redef fun reversed
do
395 var b
= new FlatBuffer.with_capacity
(_bytelen
+ 1)
398 b
.add
self.fetch_char_at
(i
)
401 var s
= b
.to_s
.as(FlatString)
402 s
._length
= self._length
406 redef fun fast_cstring
do return _items
.fast_cstring
(_first_byte
)
408 redef fun substring
(from
, count
)
410 if count
<= 0 then return ""
414 if count
<= 0 then return ""
419 if (count
+ from
) > ln
then count
= ln
- from
420 if count
<= 0 then return ""
421 var end_index
= from
+ count
- 1
422 return substring_impl
(from
, count
, end_index
)
425 private fun substring_impl
(from
, count
, end_index
: Int): String do
426 var cache
= _position
427 var dfrom
= (cache
- from
).abs
428 var dend
= (end_index
- from
).abs
433 bytefrom
= char_to_byte_index
(from
)
434 byteto
= char_to_byte_index
(end_index
)
436 byteto
= char_to_byte_index
(end_index
)
437 bytefrom
= char_to_byte_index
(from
)
441 byteto
+= its
.length_of_char_at
(byteto
) - 1
443 var s
= new FlatString.full
(its
, byteto
- bytefrom
+ 1, bytefrom
, count
)
447 redef fun empty
do return "".as(FlatString)
451 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
457 outstr
.add
(chars
[pos
].to_upper
)
466 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
472 outstr
.add
(chars
[pos
].to_lower
)
481 for i
in chars
do i
.output
484 ##################################################
485 # String Specific Methods #
486 ##################################################
488 # Low-level creation of a new string with minimal data.
490 # `_items` will be used as is, without copy, to retrieve the characters of the string.
491 # Aliasing issues is the responsibility of the caller.
492 private new with_infos
(items
: NativeString, bytelen
, from
: Int)
494 var len
= items
.utf8_length
(from
, bytelen
)
495 if bytelen
== len
then return new ASCIIFlatString.full_data
(items
, bytelen
, from
, len
)
496 return new UnicodeFlatString.full_data
(items
, bytelen
, from
, len
)
499 # Low-level creation of a new string with all the data.
501 # `_items` will be used as is, without copy, to retrieve the characters of the string.
502 # Aliasing issues is the responsibility of the caller.
503 private new full
(items
: NativeString, bytelen
, from
, length
: Int)
505 if bytelen
== length
then return new ASCIIFlatString.full_data
(items
, bytelen
, from
, length
)
506 return new UnicodeFlatString.full_data
(items
, bytelen
, from
, length
)
511 if not other
isa FlatText then return super
513 if self.object_id
== other
.object_id
then return true
515 var my_length
= _bytelen
517 if other
._bytelen
!= my_length
then return false
519 var my_index
= _first_byte
520 var its_index
= other
.first_byte
522 var last_iteration
= my_index
+ my_length
524 var its_items
= other
._items
525 var my_items
= self._items
527 while my_index
< last_iteration
do
528 if my_items
[my_index
] != its_items
[its_index
] then return false
538 if not other
isa FlatText then return super
540 if self.object_id
== other
.object_id
then return false
543 var itsits
= other
._items
546 var obt
= other
.bytelen
548 var minln
= if mbt
< obt
then mbt
else obt
549 var mst
= _first_byte
550 var ost
= other
.first_byte
552 for i
in [0 .. minln
[ do
553 var my_curr_char
= myits
[mst
]
554 var its_curr_char
= itsits
[ost
]
556 if my_curr_char
> its_curr_char
then return false
557 if my_curr_char
< its_curr_char
then return true
570 var nlen
= mlen
+ slen
572 var mifrom
= _first_byte
573 if s
isa FlatText then
575 var sifrom
= s
.first_byte
576 var ns
= new NativeString(nlen
+ 1)
577 mits
.copy_to
(ns
, mlen
, mifrom
, 0)
578 sits
.copy_to
(ns
, slen
, sifrom
, mlen
)
579 return new FlatString.full
(ns
, nlen
, 0, _length
+ o
.length
)
586 var mybtlen
= _bytelen
587 var new_bytelen
= mybtlen
* i
589 var newlen
= mylen
* i
592 var ns
= new NativeString(new_bytelen
+ 1)
593 ns
[new_bytelen
] = 0u8
596 its
.copy_to
(ns
, mybtlen
, fb
, offset
)
600 return new FlatString.full
(ns
, new_bytelen
, 0, newlen
)
605 if hash_cache
== null then
606 # djb2 hash algorithm
610 var my_items
= _items
614 h
= (h
<< 5) + h
+ my_items
[i
].to_i
621 return hash_cache
.as(not null)
624 redef fun substrings
do return new FlatSubstringsIter(self)
627 # Regular Nit UTF-8 strings
628 private class UnicodeFlatString
631 init full_data
(items
: NativeString, bytelen
, from
, length
: Int) do
633 self._length
= length
634 self._bytelen
= bytelen
639 redef fun substring_from
(from
) do
640 if from
>= self._length
then return empty
641 if from
<= 0 then return self
642 var c
= char_to_byte_index
(from
)
643 var st
= c
- _first_byte
644 var fln
= bytelen
- st
645 return new FlatString.full
(items
, fln
, c
, _length
- from
)
649 # Special cases of String where all the characters are ASCII-based
651 # Optimizes access operations to O(1) complexity.
652 private class ASCIIFlatString
655 init full_data
(items
: NativeString, bytelen
, from
, length
: Int) do
657 self._length
= length
658 self._bytelen
= bytelen
664 assert idx
< _bytelen
and idx
>= 0
665 return _items
[idx
+ _first_byte
].ascii
668 redef fun substring
(from
, count
) do
669 if count
<= 0 then return ""
673 if count
< 0 then return ""
677 if (count
+ from
) > ln
then count
= ln
- from
678 return new ASCIIFlatString.full_data
(_items
, count
, from
+ _first_byte
, count
)
681 redef fun reversed
do
682 var b
= new FlatBuffer.with_capacity
(_bytelen
+ 1)
688 var s
= b
.to_s
.as(FlatString)
692 redef fun char_to_byte_index
(index
) do return index
+ _first_byte
694 redef fun substring_impl
(from
, count
, end_index
) do
695 return new ASCIIFlatString.full_data
(_items
, count
, from
+ _first_byte
, count
)
698 redef fun fetch_char_at
(i
) do return _items
[i
+ _first_byte
].ascii
701 private class FlatStringCharReverseIterator
702 super IndexedIterator[Char]
704 var target
: FlatString
708 redef fun is_ok
do return curr_pos
>= 0
710 redef fun item
do return target
[curr_pos
]
712 redef fun next
do curr_pos
-= 1
714 redef fun index
do return curr_pos
718 private class FlatStringCharIterator
719 super IndexedIterator[Char]
721 var target
: FlatString
723 var max
: Int is noautoinit
727 init do max
= target
._length
- 1
729 redef fun is_ok
do return curr_pos
<= max
731 redef fun item
do return target
[curr_pos
]
733 redef fun next
do curr_pos
+= 1
735 redef fun index
do return curr_pos
739 private class FlatStringCharView
742 redef type SELFTYPE: FlatString
744 redef fun [](index
) do return target
[index
]
746 redef fun iterator_from
(start
) do return new FlatStringCharIterator(target
, start
)
748 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator(target
, start
)
752 private class FlatStringByteReverseIterator
753 super IndexedIterator[Byte]
755 var target
: FlatString
757 var target_items
: NativeString is noautoinit
764 target_items
= tgt
._items
765 curr_pos
+= tgt
._first_byte
768 redef fun is_ok
do return curr_pos
>= target
._first_byte
770 redef fun item
do return target_items
[curr_pos
]
772 redef fun next
do curr_pos
-= 1
774 redef fun index
do return curr_pos
- target
._first_byte
778 private class FlatStringByteIterator
779 super IndexedIterator[Byte]
781 var target
: FlatString
783 var target_items
: NativeString is noautoinit
790 target_items
= tgt
._items
791 curr_pos
+= tgt
._first_byte
794 redef fun is_ok
do return curr_pos
<= target
.last_byte
796 redef fun item
do return target_items
[curr_pos
]
798 redef fun next
do curr_pos
+= 1
800 redef fun index
do return curr_pos
- target
._first_byte
804 private class FlatStringByteView
807 redef type SELFTYPE: FlatString
811 # Check that the index (+ _first_byte) is not larger than last_byte
812 # In other terms, if the index is valid
814 assert index
>= 0 and index
< target
._bytelen
815 var ind
= index
+ target
._first_byte
816 return target
._items
[ind
]
819 redef fun iterator_from
(start
) do return new FlatStringByteIterator(target
, start
)
821 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator(target
, start
)
826 redef new do return new FlatBuffer
828 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
831 # Mutable strings of characters.
836 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
838 redef var bytes
= new FlatBufferByteView(self) is lazy
840 private var char_cache
: Int = -1
842 private var byte_cache
: Int = -1
844 private var capacity
= 0
846 # Real items, used as cache for when to_cstring is called
847 private var real_items
: NativeString is noinit
849 redef fun fast_cstring
do return _items
.fast_cstring
(0)
851 redef fun substrings
do return new FlatSubstringsIter(self)
853 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
855 # This happens when an operation modifies the current `Buffer` and
856 # the Copy-On-Write flag `written` is set at true.
858 var nns
= new NativeString(capacity
)
859 if _bytelen
!= 0 then _items
.copy_to
(nns
, _bytelen
, 0, 0)
864 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
866 # Internal only, does not modify _bytelen or length, this is the caller's responsability
867 private fun rshift_bytes
(from
: Int, len
: Int) do
871 if bt
+ len
> capacity
then
872 capacity
= capacity
* 2 + 2
873 nit
= new NativeString(capacity
)
874 oit
.copy_to
(nit
, 0, 0, from
)
876 oit
.copy_to
(nit
, bt
- from
, from
, from
+ len
)
879 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
881 # Internal only, does not modify _bytelen or length, this is the caller's responsability
882 private fun lshift_bytes
(from
: Int, len
: Int) do
884 it
.copy_to
(it
, _bytelen
- from
, from
, from
- len
)
887 redef fun []=(index
, item
)
889 assert index
>= 0 and index
<= _length
890 if written
then reset
892 if index
== _length
then
897 var ip
= it
.char_to_byte_index
(index
)
898 var c
= it
.char_at
(ip
)
899 var clen
= c
.u8char_len
900 var itemlen
= item
.u8char_len
901 var size_diff
= itemlen
- clen
902 if size_diff
> 0 then
903 rshift_bytes
(ip
+ clen
, size_diff
)
904 else if size_diff
< 0 then
905 lshift_bytes
(ip
+ clen
, -size_diff
)
907 _bytelen
+= size_diff
908 it
.set_char_at
(ip
, item
)
913 if written
then reset
915 var clen
= c
.u8char_len
918 _items
.set_char_at
(bt
, c
)
933 redef fun empty
do return new Buffer
935 redef fun enlarge
(cap
)
938 if cap
<= c
then return
939 if c
<= 16 then c
= 16
940 while c
<= cap
do c
= c
* 2
941 # The COW flag can be set at false here, since
942 # it does a copy of the current `Buffer`
945 var a
= new NativeString(c
)
948 if bln
> 0 then it
.copy_to
(a
, bln
, 0, 0)
958 if bln
== 0 then _items
= new NativeString(1)
959 return new FlatString.full
(_items
, bln
, 0, _length
)
966 var new_native
= new NativeString(bln
+ 1)
967 new_native
[bln
] = 0u8
968 if _length
> 0 then _items
.copy_to
(new_native
, bln
, 0, 0)
969 real_items
= new_native
975 # Create a new empty string.
978 # Low-level creation a new buffer with given data.
980 # `_items` will be used as is, without copy, to store the characters of the buffer.
981 # Aliasing issues is the responsibility of the caller.
983 # If `_items` is shared, `written` should be set to true after the creation
984 # so that a modification will do a copy-on-write.
985 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
988 self.capacity
= capacity
989 self._bytelen
= bytelen
990 self._length
= length
993 # Create a new string copied from `s`.
996 _items
= new NativeString(s
.bytelen
)
997 for i
in s
.substrings
do i
._items
.copy_to
(_items
, i
._bytelen
, first_byte
, 0)
1000 _capacity
= _bytelen
1003 # Create a new empty string with a given capacity.
1004 init with_capacity
(cap
: Int)
1007 _items
= new NativeString(cap
)
1014 if s
.is_empty
then return
1017 var nln
= _bytelen
+ sl
1019 if s
isa FlatText then
1020 s
._items
.copy_to
(_items
, sl
, s
.first_byte
, _bytelen
)
1022 for i
in s
.substrings
do append i
1029 # Copies the content of self in `dest`
1030 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
1032 var self_chars
= self.chars
1033 var dest_chars
= dest
.chars
1034 for i
in [0..len-1
] do
1035 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
1039 redef fun substring
(from
, count
)
1042 if from
< 0 then from
= 0
1043 if (from
+ count
) > _length
then count
= _length
- from
1044 if count
<= 0 then return new Buffer
1046 var bytefrom
= its
.char_to_byte_index
(from
)
1047 var byteto
= its
.char_to_byte_index
(count
+ from
- 1)
1048 byteto
+= its
.char_at
(byteto
).u8char_len
- 1
1049 var byte_length
= byteto
- bytefrom
+ 1
1050 var r_items
= new NativeString(byte_length
)
1051 its
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
1052 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
1055 redef fun append_substring_impl
(s
, from
, length
) do
1056 if length
<= 0 then return
1057 if not s
isa FlatText then
1061 var bytest
= s
.char_to_byte_index
(from
)
1062 var bytend
= s
.char_to_byte_index
(from
+ length
- 1)
1063 var btln
= bytend
- bytest
+ 1
1064 enlarge
(btln
+ _bytelen
)
1065 s
._items
.copy_to
(_items
, btln
, bytest
, _bytelen
)
1073 var ns
= new FlatBuffer.with_capacity
(capacity
)
1074 for i
in chars
.reverse_iterator
do ns
.add i
1078 redef fun times
(repeats
)
1081 var x
= new FlatString.full
(_items
, bln
, 0, _length
)
1082 for i
in [1 .. repeats
[ do
1089 if written
then reset
1090 for i
in [0 .. _length
[ do self[i
] = self[i
].to_upper
1095 if written
then reset
1096 for i
in [0 .. _length
[ do self[i
] = self[i
].to_lower
1100 private class FlatBufferByteReverseIterator
1101 super IndexedIterator[Byte]
1103 var target
: FlatBuffer
1105 var target_items
: NativeString is noautoinit
1109 init do target_items
= target
._items
1111 redef fun index
do return curr_pos
1113 redef fun is_ok
do return curr_pos
>= 0
1115 redef fun item
do return target_items
[curr_pos
]
1117 redef fun next
do curr_pos
-= 1
1121 private class FlatBufferByteView
1122 super BufferByteView
1124 redef type SELFTYPE: FlatBuffer
1126 redef fun [](index
) do return target
._items
[index
]
1128 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator(target
, pos
)
1130 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator(target
, pos
)
1134 private class FlatBufferByteIterator
1135 super IndexedIterator[Byte]
1137 var target
: FlatBuffer
1139 var target_items
: NativeString is noautoinit
1143 init do target_items
= target
._items
1145 redef fun index
do return curr_pos
1147 redef fun is_ok
do return curr_pos
< target
._bytelen
1149 redef fun item
do return target_items
[curr_pos
]
1151 redef fun next
do curr_pos
+= 1
1155 private class FlatBufferCharReverseIterator
1156 super IndexedIterator[Char]
1158 var target
: FlatBuffer
1162 redef fun index
do return curr_pos
1164 redef fun is_ok
do return curr_pos
>= 0
1166 redef fun item
do return target
[curr_pos
]
1168 redef fun next
do curr_pos
-= 1
1172 private class FlatBufferCharView
1173 super BufferCharView
1175 redef type SELFTYPE: FlatBuffer
1177 redef fun [](index
) do return target
[index
]
1179 redef fun []=(index
, item
)
1181 assert index
>= 0 and index
<= length
1182 if index
== length
then
1186 target
[index
] = item
1199 fun enlarge
(cap
: Int)
1206 var s_length
= s
.length
1207 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
._length
)
1208 for i
in s
do target
.add i
1211 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator(target
, pos
)
1213 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator(target
, pos
)
1217 private class FlatBufferCharIterator
1218 super IndexedIterator[Char]
1220 var target
: FlatBuffer
1222 var max
: Int is noautoinit
1226 init do max
= target
._length
- 1
1228 redef fun index
do return curr_pos
1230 redef fun is_ok
do return curr_pos
<= max
1232 redef fun item
do return target
[curr_pos
]
1234 redef fun next
do curr_pos
+= 1
1238 redef class NativeString
1241 return to_s_with_length
(cstring_length
)
1244 redef fun to_s_with_length
(length
)
1247 return clean_utf8
(length
)
1250 redef fun to_s_full
(bytelen
, unilen
) do
1251 return new FlatString.full
(self, bytelen
, 0, unilen
)
1254 redef fun to_s_unsafe
(len
) do
1255 if len
== null then len
= cstring_length
1256 return new FlatString.with_infos
(self, len
, 0)
1259 redef fun to_s_with_copy
do return to_s_with_copy_and_length
(cstring_length
)
1261 # Get a `String` from `length` bytes at `self` copied into Nit memory
1262 fun to_s_with_copy_and_length
(length
: Int): String
1264 var r
= clean_utf8
(length
)
1265 if r
.items
!= self then return r
1266 var new_self
= new NativeString(length
+ 1)
1267 copy_to
(new_self
, length
, 0, 0)
1268 var str
= new FlatString.with_infos
(new_self
, length
, 0)
1269 new_self
[length
] = 0u8
1270 str
.to_cstring
= new_self
1274 # Cleans a NativeString if necessary
1275 fun clean_utf8
(len
: Int): FlatString do
1276 var replacements
: nullable Array[Int] = null
1277 var end_length
= len
1283 var i
= fetch_4_chars
(pos
)
1284 if i
& 0x80808080 != 0 then break
1289 if rem
== 0 then break
1291 if b
& 0x80u
8 == 0x00u
8 then
1297 var nxst
= length_of_char_at
(pos
)
1300 ok_st
= b
& 0x80u
8 == 0u8
1301 else if nxst
== 2 then
1302 ok_st
= b
& 0xE0u
8 == 0xC0u
8
1303 else if nxst
== 3 then
1304 ok_st
= b
& 0xF0u
8 == 0xE0u
8
1306 ok_st
= b
& 0xF8u
8 == 0xF0u
8
1309 if replacements
== null then replacements
= new Array[Int]
1310 replacements
.add pos
1318 var c
= char_at
(pos
)
1319 var cp
= c
.code_point
1321 ok_c
= cp
>= 0 and cp
<= 0x7F
1322 else if nxst
== 2 then
1323 ok_c
= cp
>= 0x80 and cp
<= 0x7FF
1324 else if nxst
== 3 then
1325 ok_c
= cp
>= 0x800 and cp
<= 0xFFFF
1326 ok_c
= ok_c
and not (cp
>= 0xD800 and cp
<= 0xDFFF) and cp
!= 0xFFFE and cp
!= 0xFFFF
1328 ok_c
= cp
>= 0x10000 and cp
<= 0x10FFFF
1331 if replacements
== null then replacements
= new Array[Int]
1332 replacements
.add pos
1339 var clen
= c
.u8char_len
1345 if end_length
!= len
then
1346 ret
= new NativeString(end_length
)
1349 var repls
= replacements
.as(not null)
1350 var r
= repls
.items
.as(not null)
1351 var imax
= repls
.length
1352 for i
in [0 .. imax
[ do
1354 var chkln
= repl_pos
- old_repl
1355 copy_to
(ret
, chkln
, old_repl
, off
)
1358 ret
[off
+ 1] = 0xBFu
8
1359 ret
[off
+ 2] = 0xBDu
8
1360 old_repl
= repl_pos
+ 1
1363 copy_to
(ret
, len
- old_repl
, old_repl
, off
)
1365 return new FlatString.full
(ret
, end_length
, 0, chr_ln
)
1368 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
1370 # Very unsafe, make sure to have room for this char prior to calling this function.
1371 private fun set_char_at
(pos
: Int, c
: Char) do
1372 var cp
= c
.code_point
1377 var ln
= c
.u8char_len
1379 self[pos
] = (0xC0 | ((cp
& 0x7C0) >> 6)).to_b
1380 self[pos
+ 1] = (0x80 | (cp
& 0x3F)).to_b
1381 else if ln
== 3 then
1382 self[pos
] = (0xE0 | ((cp
& 0xF000) >> 12)).to_b
1383 self[pos
+ 1] = (0x80 | ((cp
& 0xFC0) >> 6)).to_b
1384 self[pos
+ 2] = (0x80 | (cp
& 0x3F)).to_b
1385 else if ln
== 4 then
1386 self[pos
] = (0xF0 | ((cp
& 0x1C0000) >> 18)).to_b
1387 self[pos
+ 1] = (0x80 | ((cp
& 0x3F000) >> 12)).to_b
1388 self[pos
+ 2] = (0x80 | ((cp
& 0xFC0) >> 6)).to_b
1389 self[pos
+ 3] = (0x80 | (cp
& 0x3F)).to_b
1395 # return displayable int in base 10 and signed
1397 # assert 1.to_s == "1"
1398 # assert (-123).to_s == "-123"
1400 # Fast case for common numbers
1401 if self == 0 then return "0"
1402 if self == 1 then return "1"
1404 var nslen
= int_to_s_len
1405 var ns
= new NativeString(nslen
+ 1)
1407 native_int_to_s
(ns
, nslen
+ 1)
1408 return new FlatString.full
(ns
, nslen
, 0, nslen
)
1412 redef class Array[E
]
1414 # Fast implementation
1415 redef fun plain_to_s
1418 if l
== 0 then return ""
1419 var its
= _items
.as(not null)
1421 if l
== 1 then if first
== null then return "" else return first
.to_s
1422 var na
= new NativeArray[String](l
)
1428 if itsi
== null then
1438 var ns
= new NativeString(sl
+ 1)
1444 if tmp
isa FlatString then
1445 var tpl
= tmp
._bytelen
1446 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1449 for j
in tmp
.substrings
do
1450 var s
= j
.as(FlatString)
1451 var slen
= s
._bytelen
1452 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1458 return new FlatString.with_infos
(ns
, sl
, 0)
1462 redef class NativeArray[E
]
1463 redef fun native_to_s
do
1464 assert self isa NativeArray[String]
1475 var ns
= new NativeString(sl
+ 1)
1481 if tmp
isa FlatString then
1482 var tpl
= tmp
._bytelen
1483 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1486 for j
in tmp
.substrings
do
1487 var s
= j
.as(FlatString)
1488 var slen
= s
._bytelen
1489 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1495 return new FlatString.with_infos
(ns
, sl
, 0)
1499 redef class Map[K
,V
]
1500 redef fun join
(sep
, couple_sep
)
1502 if is_empty
then return ""
1504 var s
= new Buffer # Result
1510 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1512 # Concat other _items
1518 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")