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
== 0x3Fu
8 then
232 # We ignore `??'` because it will be escaped as `??\'`.
244 else if c
< 32u8
then
252 redef fun escape_to_c
do
253 var ln_extra
= chars_to_escape_to_c
254 if ln_extra
== 0 then return self.to_s
257 var nlen
= _bytelen
+ ln_extra
258 var nns
= new NativeString(nlen
)
265 # Any byte with value < 32 is a control character
266 # All their uses will be replaced by their octal
269 # There are two exceptions however:
274 # Aside from the code points above, the following are:
281 nns
[opos
+ 1] = 0x74u
8
283 else if c
== 0x0Au
8 then
285 nns
[opos
+ 1] = 0x6Eu
8
287 else if c
== 0x22u
8 then
289 nns
[opos
+ 1] = 0x22u
8
291 else if c
== 0x27u
8 then
293 nns
[opos
+ 1] = 0x27u
8
295 else if c
== 0x5Cu
8 then
297 nns
[opos
+ 1] = 0x5Cu
8
299 else if c
== 0x3Fu
8 then
303 # We ignore `??'` because it will be escaped as `??\'`.
320 else if c
< 32u8
then
322 nns
[opos
+ 1] = 0x30u
8
323 nns
[opos
+ 2] = ((c
& 0x38u
8) >> 3) + 0x30u
8
324 nns
[opos
+ 3] = (c
& 0x07u
8) + 0x30u
8
332 return nns
.to_s_unsafe
(nlen
)
335 redef fun [](index
) do
339 # * ~70% want the next char
340 # * ~23% want the previous
341 # * ~7% want the same char
343 # So it makes sense to shortcut early. And early is here.
344 var dpos
= index
- _position
346 if dpos
== 1 and index
< len
- 1 then
349 if c
& 0x80u
8 == 0x00u
8 then
350 # We want the next, and current is easy.
351 # So next is easy to find!
355 # The rest will be done by `dpos==0` bellow.
358 else if dpos
== -1 and index
> 1 then
361 if c
& 0x80u
8 == 0x00u
8 then
362 # We want the previous, and it is easy.
371 # We know what we want (+0 or +1) just get it now!
374 if c
& 0x80u
8 == 0x00u
8 then return c
.ascii
375 return items
.char_at
(b
)
378 assert index
>= 0 and index
< len
379 return fetch_char_at
(index
)
382 # Gets a `Char` at `index` in `self`
384 # WARNING: Use at your own risks as no bound-checking is done
385 fun fetch_char_at
(index
: Int): Char do
386 var i
= char_to_byte_index
(index
)
389 if b
& 0x80u
8 == 0x00u
8 then return b
.ascii
390 return items
.char_at
(i
)
393 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
395 # assert "ff".to_hex == 255
396 redef fun to_hex
(pos
, ln
) do
398 if pos
== null then pos
= 0
399 if ln
== null then ln
= length
- pos
400 pos
= char_to_byte_index
(pos
)
403 for i
in [pos
.. max
[ do
405 res
+= its
[i
].ascii
.from_hex
410 redef fun copy_to_native
(dst
, n
, src_off
, dst_off
) do
411 _items
.copy_to
(dst
, n
, first_byte
+ src_off
, dst_off
)
415 # Immutable strings of characters.
416 abstract class FlatString
420 # Index at which `self` begins in `_items`, inclusively
421 redef var first_byte
is noinit
423 redef var chars
= new FlatStringCharView(self) is lazy
425 redef var bytes
= new FlatStringByteView(self) is lazy
427 redef var to_cstring
is lazy
do
429 var new_items
= new NativeString(blen
+ 1)
430 _items
.copy_to
(new_items
, blen
, _first_byte
, 0)
431 new_items
[blen
] = 0u8
435 redef fun reversed
do
436 var b
= new FlatBuffer.with_capacity
(_bytelen
+ 1)
439 b
.add
self.fetch_char_at
(i
)
442 var s
= b
.to_s
.as(FlatString)
443 s
._length
= self._length
447 redef fun fast_cstring
do return _items
.fast_cstring
(_first_byte
)
449 redef fun substring
(from
, count
)
451 if count
<= 0 then return ""
455 if count
<= 0 then return ""
460 if (count
+ from
) > ln
then count
= ln
- from
461 if count
<= 0 then return ""
462 var end_index
= from
+ count
- 1
463 return substring_impl
(from
, count
, end_index
)
466 private fun substring_impl
(from
, count
, end_index
: Int): String do
467 var cache
= _position
468 var dfrom
= (cache
- from
).abs
469 var dend
= (end_index
- from
).abs
474 bytefrom
= char_to_byte_index
(from
)
475 byteto
= char_to_byte_index
(end_index
)
477 byteto
= char_to_byte_index
(end_index
)
478 bytefrom
= char_to_byte_index
(from
)
482 byteto
+= its
.length_of_char_at
(byteto
) - 1
484 var s
= new FlatString.full
(its
, byteto
- bytefrom
+ 1, bytefrom
, count
)
488 redef fun empty
do return "".as(FlatString)
492 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
498 outstr
.add
(chars
[pos
].to_upper
)
507 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
513 outstr
.add
(chars
[pos
].to_lower
)
522 for i
in chars
do i
.output
525 ##################################################
526 # String Specific Methods #
527 ##################################################
529 # Low-level creation of a new string with minimal data.
531 # `_items` will be used as is, without copy, to retrieve the characters of the string.
532 # Aliasing issues is the responsibility of the caller.
533 private new with_infos
(items
: NativeString, bytelen
, from
: Int)
535 var len
= items
.utf8_length
(from
, bytelen
)
536 if bytelen
== len
then return new ASCIIFlatString.full_data
(items
, bytelen
, from
, len
)
537 return new UnicodeFlatString.full_data
(items
, bytelen
, from
, len
)
540 # Low-level creation of a new string with all the data.
542 # `_items` will be used as is, without copy, to retrieve the characters of the string.
543 # Aliasing issues is the responsibility of the caller.
544 private new full
(items
: NativeString, bytelen
, from
, length
: Int)
546 if bytelen
== length
then return new ASCIIFlatString.full_data
(items
, bytelen
, from
, length
)
547 return new UnicodeFlatString.full_data
(items
, bytelen
, from
, length
)
552 if not other
isa FlatText then return super
554 if self.object_id
== other
.object_id
then return true
556 var my_length
= _bytelen
558 if other
._bytelen
!= my_length
then return false
560 var my_index
= _first_byte
561 var its_index
= other
.first_byte
563 var last_iteration
= my_index
+ my_length
565 var its_items
= other
._items
566 var my_items
= self._items
568 while my_index
< last_iteration
do
569 if my_items
[my_index
] != its_items
[its_index
] then return false
579 if not other
isa FlatText then return super
581 if self.object_id
== other
.object_id
then return false
584 var itsits
= other
._items
587 var obt
= other
.bytelen
589 var minln
= if mbt
< obt
then mbt
else obt
590 var mst
= _first_byte
591 var ost
= other
.first_byte
593 for i
in [0 .. minln
[ do
594 var my_curr_char
= myits
[mst
]
595 var its_curr_char
= itsits
[ost
]
597 if my_curr_char
> its_curr_char
then return false
598 if my_curr_char
< its_curr_char
then return true
611 var nlen
= mlen
+ slen
613 var mifrom
= _first_byte
614 if s
isa FlatText then
616 var sifrom
= s
.first_byte
617 var ns
= new NativeString(nlen
+ 1)
618 mits
.copy_to
(ns
, mlen
, mifrom
, 0)
619 sits
.copy_to
(ns
, slen
, sifrom
, mlen
)
620 return new FlatString.full
(ns
, nlen
, 0, _length
+ o
.length
)
627 var mybtlen
= _bytelen
628 var new_bytelen
= mybtlen
* i
630 var newlen
= mylen
* i
633 var ns
= new NativeString(new_bytelen
+ 1)
634 ns
[new_bytelen
] = 0u8
637 its
.copy_to
(ns
, mybtlen
, fb
, offset
)
641 return new FlatString.full
(ns
, new_bytelen
, 0, newlen
)
646 if hash_cache
== null then
647 # djb2 hash algorithm
651 var my_items
= _items
655 h
= (h
<< 5) + h
+ my_items
[i
].to_i
662 return hash_cache
.as(not null)
665 redef fun substrings
do return new FlatSubstringsIter(self)
668 # Regular Nit UTF-8 strings
669 private class UnicodeFlatString
672 init full_data
(items
: NativeString, bytelen
, from
, length
: Int) do
674 self._length
= length
675 self._bytelen
= bytelen
680 redef fun substring_from
(from
) do
681 if from
>= self._length
then return empty
682 if from
<= 0 then return self
683 var c
= char_to_byte_index
(from
)
684 var st
= c
- _first_byte
685 var fln
= bytelen
- st
686 return new FlatString.full
(items
, fln
, c
, _length
- from
)
690 # Special cases of String where all the characters are ASCII-based
692 # Optimizes access operations to O(1) complexity.
693 private class ASCIIFlatString
696 init full_data
(items
: NativeString, bytelen
, from
, length
: Int) do
698 self._length
= length
699 self._bytelen
= bytelen
705 assert idx
< _bytelen
and idx
>= 0
706 return _items
[idx
+ _first_byte
].ascii
709 redef fun substring
(from
, count
) do
711 if count
<= 0 then return ""
712 if (count
+ from
) > ln
then count
= ln
- from
713 if count
<= 0 then return ""
716 if count
<= 0 then return ""
719 return new ASCIIFlatString.full_data
(_items
, count
, from
+ _first_byte
, count
)
722 redef fun reversed
do
723 var b
= new FlatBuffer.with_capacity
(_bytelen
+ 1)
729 var s
= b
.to_s
.as(FlatString)
733 redef fun char_to_byte_index
(index
) do return index
+ _first_byte
735 redef fun substring_impl
(from
, count
, end_index
) do
736 return new ASCIIFlatString.full_data
(_items
, count
, from
+ _first_byte
, count
)
739 redef fun fetch_char_at
(i
) do return _items
[i
+ _first_byte
].ascii
742 private class FlatStringCharReverseIterator
743 super IndexedIterator[Char]
745 var target
: FlatString
749 redef fun is_ok
do return curr_pos
>= 0
751 redef fun item
do return target
[curr_pos
]
753 redef fun next
do curr_pos
-= 1
755 redef fun index
do return curr_pos
759 private class FlatStringCharIterator
760 super IndexedIterator[Char]
762 var target
: FlatString
764 var max
: Int is noautoinit
768 init do max
= target
._length
- 1
770 redef fun is_ok
do return curr_pos
<= max
772 redef fun item
do return target
[curr_pos
]
774 redef fun next
do curr_pos
+= 1
776 redef fun index
do return curr_pos
780 private class FlatStringCharView
783 redef type SELFTYPE: FlatString
785 redef fun [](index
) do return target
[index
]
787 redef fun iterator_from
(start
) do return new FlatStringCharIterator(target
, start
)
789 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator(target
, start
)
793 private class FlatStringByteReverseIterator
794 super IndexedIterator[Byte]
796 var target
: FlatString
798 var target_items
: NativeString is noautoinit
805 target_items
= tgt
._items
806 curr_pos
+= tgt
._first_byte
809 redef fun is_ok
do return curr_pos
>= target
._first_byte
811 redef fun item
do return target_items
[curr_pos
]
813 redef fun next
do curr_pos
-= 1
815 redef fun index
do return curr_pos
- target
._first_byte
819 private class FlatStringByteIterator
820 super IndexedIterator[Byte]
822 var target
: FlatString
824 var target_items
: NativeString is noautoinit
831 target_items
= tgt
._items
832 curr_pos
+= tgt
._first_byte
835 redef fun is_ok
do return curr_pos
<= target
.last_byte
837 redef fun item
do return target_items
[curr_pos
]
839 redef fun next
do curr_pos
+= 1
841 redef fun index
do return curr_pos
- target
._first_byte
845 private class FlatStringByteView
848 redef type SELFTYPE: FlatString
852 # Check that the index (+ _first_byte) is not larger than last_byte
853 # In other terms, if the index is valid
855 assert index
>= 0 and index
< target
._bytelen
856 var ind
= index
+ target
._first_byte
857 return target
._items
[ind
]
860 redef fun iterator_from
(start
) do return new FlatStringByteIterator(target
, start
)
862 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator(target
, start
)
867 redef new do return new FlatBuffer
869 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
872 # Mutable strings of characters.
877 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
879 redef var bytes
= new FlatBufferByteView(self) is lazy
881 private var char_cache
: Int = -1
883 private var byte_cache
: Int = -1
885 private var capacity
= 0
887 # Real items, used as cache for when to_cstring is called
888 private var real_items
: NativeString is noinit
890 redef fun fast_cstring
do return _items
.fast_cstring
(0)
892 redef fun substrings
do return new FlatSubstringsIter(self)
894 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
896 # This happens when an operation modifies the current `Buffer` and
897 # the Copy-On-Write flag `written` is set at true.
899 var nns
= new NativeString(capacity
)
900 if _bytelen
!= 0 then _items
.copy_to
(nns
, _bytelen
, 0, 0)
905 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
907 # Internal only, does not modify _bytelen or length, this is the caller's responsability
908 private fun rshift_bytes
(from
: Int, len
: Int) do
912 if bt
+ len
> capacity
then
913 capacity
= capacity
* 2 + 2
914 nit
= new NativeString(capacity
)
915 oit
.copy_to
(nit
, 0, 0, from
)
917 oit
.copy_to
(nit
, bt
- from
, from
, from
+ len
)
920 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
922 # Internal only, does not modify _bytelen or length, this is the caller's responsability
923 private fun lshift_bytes
(from
: Int, len
: Int) do
925 it
.copy_to
(it
, _bytelen
- from
, from
, from
- len
)
928 redef fun []=(index
, item
)
930 assert index
>= 0 and index
<= _length
931 if written
then reset
933 if index
== _length
then
938 var ip
= it
.char_to_byte_index
(index
)
939 var c
= it
.char_at
(ip
)
940 var clen
= c
.u8char_len
941 var itemlen
= item
.u8char_len
942 var size_diff
= itemlen
- clen
943 if size_diff
> 0 then
944 rshift_bytes
(ip
+ clen
, size_diff
)
945 else if size_diff
< 0 then
946 lshift_bytes
(ip
+ clen
, -size_diff
)
948 _bytelen
+= size_diff
949 it
.set_char_at
(ip
, item
)
954 if written
then reset
956 var clen
= c
.u8char_len
959 _items
.set_char_at
(bt
, c
)
974 redef fun empty
do return new Buffer
976 redef fun enlarge
(cap
)
979 if cap
<= c
then return
980 if c
<= 16 then c
= 16
981 while c
<= cap
do c
= c
* 2
982 # The COW flag can be set at false here, since
983 # it does a copy of the current `Buffer`
986 var a
= new NativeString(c
)
989 if bln
> 0 then it
.copy_to
(a
, bln
, 0, 0)
999 if bln
== 0 then _items
= new NativeString(1)
1000 return new FlatString.full
(_items
, bln
, 0, _length
)
1003 redef fun to_cstring
1007 var new_native
= new NativeString(bln
+ 1)
1008 new_native
[bln
] = 0u8
1009 if _length
> 0 then _items
.copy_to
(new_native
, bln
, 0, 0)
1010 real_items
= new_native
1016 # Create a new empty string.
1019 # Low-level creation a new buffer with given data.
1021 # `_items` will be used as is, without copy, to store the characters of the buffer.
1022 # Aliasing issues is the responsibility of the caller.
1024 # If `_items` is shared, `written` should be set to true after the creation
1025 # so that a modification will do a copy-on-write.
1026 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
1029 self.capacity
= capacity
1030 self._bytelen
= bytelen
1031 self._length
= length
1034 # Create a new string copied from `s`.
1037 _items
= new NativeString(s
.bytelen
)
1038 for i
in s
.substrings
do i
._items
.copy_to
(_items
, i
._bytelen
, first_byte
, 0)
1039 _bytelen
= s
.bytelen
1041 _capacity
= _bytelen
1044 # Create a new empty string with a given capacity.
1045 init with_capacity
(cap
: Int)
1048 _items
= new NativeString(cap
)
1055 if s
.is_empty
then return
1058 var nln
= _bytelen
+ sl
1060 if s
isa FlatText then
1061 s
._items
.copy_to
(_items
, sl
, s
.first_byte
, _bytelen
)
1063 for i
in s
.substrings
do append i
1070 # Copies the content of self in `dest`
1071 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
1073 var self_chars
= self.chars
1074 var dest_chars
= dest
.chars
1075 for i
in [0..len-1
] do
1076 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
1080 redef fun substring
(from
, count
)
1083 if from
< 0 then from
= 0
1084 if (from
+ count
) > _length
then count
= _length
- from
1085 if count
<= 0 then return new Buffer
1087 var bytefrom
= its
.char_to_byte_index
(from
)
1088 var byteto
= its
.char_to_byte_index
(count
+ from
- 1)
1089 byteto
+= its
.char_at
(byteto
).u8char_len
- 1
1090 var byte_length
= byteto
- bytefrom
+ 1
1091 var r_items
= new NativeString(byte_length
)
1092 its
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
1093 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
1096 redef fun append_substring_impl
(s
, from
, length
) do
1097 if length
<= 0 then return
1098 if not s
isa FlatText then
1102 var bytest
= s
.char_to_byte_index
(from
)
1103 var bytend
= s
.char_to_byte_index
(from
+ length
- 1)
1104 var btln
= bytend
- bytest
+ 1
1105 enlarge
(btln
+ _bytelen
)
1106 s
._items
.copy_to
(_items
, btln
, bytest
, _bytelen
)
1114 var ns
= new FlatBuffer.with_capacity
(capacity
)
1115 for i
in chars
.reverse_iterator
do ns
.add i
1119 redef fun times
(repeats
)
1122 var x
= new FlatString.full
(_items
, bln
, 0, _length
)
1123 for i
in [1 .. repeats
[ do
1130 if written
then reset
1131 for i
in [0 .. _length
[ do self[i
] = self[i
].to_upper
1136 if written
then reset
1137 for i
in [0 .. _length
[ do self[i
] = self[i
].to_lower
1141 private class FlatBufferByteReverseIterator
1142 super IndexedIterator[Byte]
1144 var target
: FlatBuffer
1146 var target_items
: NativeString is noautoinit
1150 init do target_items
= target
._items
1152 redef fun index
do return curr_pos
1154 redef fun is_ok
do return curr_pos
>= 0
1156 redef fun item
do return target_items
[curr_pos
]
1158 redef fun next
do curr_pos
-= 1
1162 private class FlatBufferByteView
1163 super BufferByteView
1165 redef type SELFTYPE: FlatBuffer
1167 redef fun [](index
) do return target
._items
[index
]
1169 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator(target
, pos
)
1171 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator(target
, pos
)
1175 private class FlatBufferByteIterator
1176 super IndexedIterator[Byte]
1178 var target
: FlatBuffer
1180 var target_items
: NativeString is noautoinit
1184 init do target_items
= target
._items
1186 redef fun index
do return curr_pos
1188 redef fun is_ok
do return curr_pos
< target
._bytelen
1190 redef fun item
do return target_items
[curr_pos
]
1192 redef fun next
do curr_pos
+= 1
1196 private class FlatBufferCharReverseIterator
1197 super IndexedIterator[Char]
1199 var target
: FlatBuffer
1203 redef fun index
do return curr_pos
1205 redef fun is_ok
do return curr_pos
>= 0
1207 redef fun item
do return target
[curr_pos
]
1209 redef fun next
do curr_pos
-= 1
1213 private class FlatBufferCharView
1214 super BufferCharView
1216 redef type SELFTYPE: FlatBuffer
1218 redef fun [](index
) do return target
[index
]
1220 redef fun []=(index
, item
)
1222 assert index
>= 0 and index
<= length
1223 if index
== length
then
1227 target
[index
] = item
1240 fun enlarge
(cap
: Int)
1247 var s_length
= s
.length
1248 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
._length
)
1249 for i
in s
do target
.add i
1252 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator(target
, pos
)
1254 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator(target
, pos
)
1258 private class FlatBufferCharIterator
1259 super IndexedIterator[Char]
1261 var target
: FlatBuffer
1263 var max
: Int is noautoinit
1267 init do max
= target
._length
- 1
1269 redef fun index
do return curr_pos
1271 redef fun is_ok
do return curr_pos
<= max
1273 redef fun item
do return target
[curr_pos
]
1275 redef fun next
do curr_pos
+= 1
1279 redef class NativeString
1282 return to_s_with_length
(cstring_length
)
1285 redef fun to_s_with_length
(length
)
1288 return clean_utf8
(length
)
1291 redef fun to_s_full
(bytelen
, unilen
) do
1292 return new FlatString.full
(self, bytelen
, 0, unilen
)
1295 redef fun to_s_unsafe
(len
) do
1296 if len
== null then len
= cstring_length
1297 return new FlatString.with_infos
(self, len
, 0)
1300 redef fun to_s_with_copy
do return to_s_with_copy_and_length
(cstring_length
)
1302 # Get a `String` from `length` bytes at `self` copied into Nit memory
1303 fun to_s_with_copy_and_length
(length
: Int): String
1305 var r
= clean_utf8
(length
)
1306 if r
.items
!= self then return r
1307 var new_self
= new NativeString(length
+ 1)
1308 copy_to
(new_self
, length
, 0, 0)
1309 var str
= new FlatString.with_infos
(new_self
, length
, 0)
1310 new_self
[length
] = 0u8
1311 str
.to_cstring
= new_self
1315 # Cleans a NativeString if necessary
1316 fun clean_utf8
(len
: Int): FlatString do
1317 var replacements
: nullable Array[Int] = null
1318 var end_length
= len
1324 var i
= fetch_4_chars
(pos
)
1325 if i
& 0x80808080 != 0 then break
1330 if rem
== 0 then break
1332 if b
& 0x80u
8 == 0x00u
8 then
1338 var nxst
= length_of_char_at
(pos
)
1341 ok_st
= b
& 0x80u
8 == 0u8
1342 else if nxst
== 2 then
1343 ok_st
= b
& 0xE0u
8 == 0xC0u
8
1344 else if nxst
== 3 then
1345 ok_st
= b
& 0xF0u
8 == 0xE0u
8
1347 ok_st
= b
& 0xF8u
8 == 0xF0u
8
1350 if replacements
== null then replacements
= new Array[Int]
1351 replacements
.add pos
1359 var c
= char_at
(pos
)
1360 var cp
= c
.code_point
1362 ok_c
= cp
>= 0 and cp
<= 0x7F
1363 else if nxst
== 2 then
1364 ok_c
= cp
>= 0x80 and cp
<= 0x7FF
1365 else if nxst
== 3 then
1366 ok_c
= cp
>= 0x800 and cp
<= 0xFFFF
1367 ok_c
= ok_c
and not (cp
>= 0xD800 and cp
<= 0xDFFF) and cp
!= 0xFFFE and cp
!= 0xFFFF
1369 ok_c
= cp
>= 0x10000 and cp
<= 0x10FFFF
1372 if replacements
== null then replacements
= new Array[Int]
1373 replacements
.add pos
1380 var clen
= c
.u8char_len
1386 if end_length
!= len
then
1387 ret
= new NativeString(end_length
)
1390 var repls
= replacements
.as(not null)
1391 var r
= repls
.items
.as(not null)
1392 var imax
= repls
.length
1393 for i
in [0 .. imax
[ do
1395 var chkln
= repl_pos
- old_repl
1396 copy_to
(ret
, chkln
, old_repl
, off
)
1399 ret
[off
+ 1] = 0xBFu
8
1400 ret
[off
+ 2] = 0xBDu
8
1401 old_repl
= repl_pos
+ 1
1404 copy_to
(ret
, len
- old_repl
, old_repl
, off
)
1406 return new FlatString.full
(ret
, end_length
, 0, chr_ln
)
1409 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
1411 # Very unsafe, make sure to have room for this char prior to calling this function.
1412 private fun set_char_at
(pos
: Int, c
: Char) do
1413 var cp
= c
.code_point
1418 var ln
= c
.u8char_len
1420 self[pos
] = (0xC0 | ((cp
& 0x7C0) >> 6)).to_b
1421 self[pos
+ 1] = (0x80 | (cp
& 0x3F)).to_b
1422 else if ln
== 3 then
1423 self[pos
] = (0xE0 | ((cp
& 0xF000) >> 12)).to_b
1424 self[pos
+ 1] = (0x80 | ((cp
& 0xFC0) >> 6)).to_b
1425 self[pos
+ 2] = (0x80 | (cp
& 0x3F)).to_b
1426 else if ln
== 4 then
1427 self[pos
] = (0xF0 | ((cp
& 0x1C0000) >> 18)).to_b
1428 self[pos
+ 1] = (0x80 | ((cp
& 0x3F000) >> 12)).to_b
1429 self[pos
+ 2] = (0x80 | ((cp
& 0xFC0) >> 6)).to_b
1430 self[pos
+ 3] = (0x80 | (cp
& 0x3F)).to_b
1436 # return displayable int in base 10 and signed
1438 # assert 1.to_s == "1"
1439 # assert (-123).to_s == "-123"
1441 # Fast case for common numbers
1442 if self == 0 then return "0"
1443 if self == 1 then return "1"
1445 var nslen
= int_to_s_len
1446 var ns
= new NativeString(nslen
+ 1)
1448 native_int_to_s
(ns
, nslen
+ 1)
1449 return new FlatString.full
(ns
, nslen
, 0, nslen
)
1453 redef class Array[E
]
1455 # Fast implementation
1456 redef fun plain_to_s
1459 if l
== 0 then return ""
1460 var its
= _items
.as(not null)
1462 if l
== 1 then if first
== null then return "" else return first
.to_s
1463 var na
= new NativeArray[String](l
)
1469 if itsi
== null then
1479 var ns
= new NativeString(sl
+ 1)
1485 if tmp
isa FlatString then
1486 var tpl
= tmp
._bytelen
1487 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1490 for j
in tmp
.substrings
do
1491 var s
= j
.as(FlatString)
1492 var slen
= s
._bytelen
1493 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1499 return new FlatString.with_infos
(ns
, sl
, 0)
1503 redef class NativeArray[E
]
1504 redef fun native_to_s
do
1505 assert self isa NativeArray[String]
1516 var ns
= new NativeString(sl
+ 1)
1522 if tmp
isa FlatString then
1523 var tpl
= tmp
._bytelen
1524 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1527 for j
in tmp
.substrings
do
1528 var s
= j
.as(FlatString)
1529 var slen
= s
._bytelen
1530 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1536 return new FlatString.with_infos
(ns
, sl
, 0)
1540 redef class Map[K
,V
]
1541 redef fun join
(sep
, couple_sep
)
1543 if is_empty
then return ""
1545 var s
= new Buffer # Result
1551 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1553 # Concat other _items
1559 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")