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
299 assert index
>= 0 and index
< _length
300 return fetch_char_at
(index
)
303 # Gets a `Char` at `index` in `self`
305 # WARNING: Use at your own risks as no bound-checking is done
306 fun fetch_char_at
(index
: Int): Char do
307 var i
= char_to_byte_index
(index
)
310 if b
& 0x80u
8 == 0x00u
8 then return b
.ascii
311 return items
.char_at
(i
)
314 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
316 # assert "ff".to_hex == 255
317 redef fun to_hex
(pos
, ln
) do
319 if pos
== null then pos
= 0
320 if ln
== null then ln
= length
- pos
321 pos
= char_to_byte_index
(pos
)
324 for i
in [pos
.. max
[ do
326 res
+= its
[i
].ascii
.from_hex
332 # Immutable strings of characters.
337 # Index at which `self` begins in `_items`, inclusively
338 redef var first_byte
is noinit
340 redef var chars
= new FlatStringCharView(self) is lazy
342 redef var bytes
= new FlatStringByteView(self) is lazy
344 redef var to_cstring
is lazy
do
346 var new_items
= new NativeString(blen
+ 1)
347 _items
.copy_to
(new_items
, blen
, _first_byte
, 0)
348 new_items
[blen
] = 0u8
352 redef fun reversed
do
353 var b
= new FlatBuffer.with_capacity
(_bytelen
+ 1)
356 b
.add
self.fetch_char_at
(i
)
359 var s
= b
.to_s
.as(FlatString)
360 s
._length
= self._length
364 redef fun fast_cstring
do return _items
.fast_cstring
(_first_byte
)
366 redef fun substring_from
(from
) do
367 if from
>= self._length
then return empty
368 if from
<= 0 then return self
369 var c
= char_to_byte_index
(from
)
370 var st
= c
- _first_byte
371 var fln
= bytelen
- st
372 return new FlatString.full
(items
, fln
, c
, _length
- from
)
375 redef fun substring
(from
, count
)
377 if count
<= 0 then return ""
381 if count
< 0 then return ""
386 if (count
+ from
) > ln
then count
= ln
- from
387 if count
<= 0 then return ""
388 var end_index
= from
+ count
- 1
389 return substring_impl
(from
, count
, end_index
)
392 private fun substring_impl
(from
, count
, end_index
: Int): String do
393 var cache
= _position
394 var dfrom
= (cache
- from
).abs
395 var dend
= (end_index
- from
).abs
400 bytefrom
= char_to_byte_index
(from
)
401 byteto
= char_to_byte_index
(end_index
)
403 byteto
= char_to_byte_index
(end_index
)
404 bytefrom
= char_to_byte_index
(from
)
408 byteto
+= its
.length_of_char_at
(byteto
) - 1
410 var s
= new FlatString.full
(its
, byteto
- bytefrom
+ 1, bytefrom
, count
)
414 redef fun empty
do return "".as(FlatString)
418 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
424 outstr
.add
(chars
[pos
].to_upper
)
433 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
439 outstr
.add
(chars
[pos
].to_lower
)
448 for i
in chars
do i
.output
451 ##################################################
452 # String Specific Methods #
453 ##################################################
455 # Low-level creation of a new string with minimal data.
457 # `_items` will be used as is, without copy, to retrieve the characters of the string.
458 # Aliasing issues is the responsibility of the caller.
459 private init with_infos
(items
: NativeString, bytelen
, from
: Int)
462 self._bytelen
= bytelen
465 _length
= _items
.utf8_length
(_first_byte
, bytelen
)
468 # Low-level creation of a new string with all the data.
470 # `_items` will be used as is, without copy, to retrieve the characters of the string.
471 # Aliasing issues is the responsibility of the caller.
472 private init full
(items
: NativeString, bytelen
, from
, length
: Int)
475 self._length
= length
476 self._bytelen
= bytelen
483 if not other
isa FlatText then return super
485 if self.object_id
== other
.object_id
then return true
487 var my_length
= _bytelen
489 if other
._bytelen
!= my_length
then return false
491 var my_index
= _first_byte
492 var its_index
= other
.first_byte
494 var last_iteration
= my_index
+ my_length
496 var its_items
= other
._items
497 var my_items
= self._items
499 while my_index
< last_iteration
do
500 if my_items
[my_index
] != its_items
[its_index
] then return false
510 if not other
isa FlatText then return super
512 if self.object_id
== other
.object_id
then return false
515 var itsits
= other
._items
518 var obt
= other
.bytelen
520 var minln
= if mbt
< obt
then mbt
else obt
521 var mst
= _first_byte
522 var ost
= other
.first_byte
524 for i
in [0 .. minln
[ do
525 var my_curr_char
= myits
[mst
]
526 var its_curr_char
= itsits
[ost
]
528 if my_curr_char
> its_curr_char
then return false
529 if my_curr_char
< its_curr_char
then return true
542 var nlen
= mlen
+ slen
544 var mifrom
= _first_byte
545 if s
isa FlatText then
547 var sifrom
= s
.first_byte
548 var ns
= new NativeString(nlen
+ 1)
549 mits
.copy_to
(ns
, mlen
, mifrom
, 0)
550 sits
.copy_to
(ns
, slen
, sifrom
, mlen
)
551 return new FlatString.full
(ns
, nlen
, 0, _length
+ o
.length
)
558 var mybtlen
= _bytelen
559 var new_bytelen
= mybtlen
* i
561 var newlen
= mylen
* i
564 var ns
= new NativeString(new_bytelen
+ 1)
565 ns
[new_bytelen
] = 0u8
568 its
.copy_to
(ns
, mybtlen
, fb
, offset
)
572 return new FlatString.full
(ns
, new_bytelen
, 0, newlen
)
578 if hash_cache
== null then
579 # djb2 hash algorithm
583 var my_items
= _items
587 h
= (h
<< 5) + h
+ my_items
[i
].to_i
594 return hash_cache
.as(not null)
597 redef fun substrings
do return new FlatSubstringsIter(self)
600 private class FlatStringCharReverseIterator
601 super IndexedIterator[Char]
603 var target
: FlatString
607 redef fun is_ok
do return curr_pos
>= 0
609 redef fun item
do return target
[curr_pos
]
611 redef fun next
do curr_pos
-= 1
613 redef fun index
do return curr_pos
617 private class FlatStringCharIterator
618 super IndexedIterator[Char]
620 var target
: FlatString
622 var max
: Int is noautoinit
626 init do max
= target
._length
- 1
628 redef fun is_ok
do return curr_pos
<= max
630 redef fun item
do return target
[curr_pos
]
632 redef fun next
do curr_pos
+= 1
634 redef fun index
do return curr_pos
638 private class FlatStringCharView
641 redef type SELFTYPE: FlatString
643 redef fun [](index
) do return target
[index
]
645 redef fun iterator_from
(start
) do return new FlatStringCharIterator(target
, start
)
647 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator(target
, start
)
651 private class FlatStringByteReverseIterator
652 super IndexedIterator[Byte]
654 var target
: FlatString
656 var target_items
: NativeString is noautoinit
663 target_items
= tgt
._items
664 curr_pos
+= tgt
._first_byte
667 redef fun is_ok
do return curr_pos
>= target
._first_byte
669 redef fun item
do return target_items
[curr_pos
]
671 redef fun next
do curr_pos
-= 1
673 redef fun index
do return curr_pos
- target
._first_byte
677 private class FlatStringByteIterator
678 super IndexedIterator[Byte]
680 var target
: FlatString
682 var target_items
: NativeString is noautoinit
689 target_items
= tgt
._items
690 curr_pos
+= tgt
._first_byte
693 redef fun is_ok
do return curr_pos
<= target
.last_byte
695 redef fun item
do return target_items
[curr_pos
]
697 redef fun next
do curr_pos
+= 1
699 redef fun index
do return curr_pos
- target
._first_byte
703 private class FlatStringByteView
706 redef type SELFTYPE: FlatString
710 # Check that the index (+ _first_byte) is not larger than last_byte
711 # In other terms, if the index is valid
713 assert index
>= 0 and index
< target
._bytelen
714 var ind
= index
+ target
._first_byte
715 return target
._items
[ind
]
718 redef fun iterator_from
(start
) do return new FlatStringByteIterator(target
, start
)
720 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator(target
, start
)
725 redef new do return new FlatBuffer
727 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
730 # Mutable strings of characters.
735 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
737 redef var bytes
= new FlatBufferByteView(self) is lazy
739 private var char_cache
: Int = -1
741 private var byte_cache
: Int = -1
743 private var capacity
= 0
745 # Real items, used as cache for when to_cstring is called
746 private var real_items
: NativeString is noinit
748 redef fun fast_cstring
do return _items
.fast_cstring
(0)
750 redef fun substrings
do return new FlatSubstringsIter(self)
752 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
754 # This happens when an operation modifies the current `Buffer` and
755 # the Copy-On-Write flag `written` is set at true.
757 var nns
= new NativeString(capacity
)
758 if _bytelen
!= 0 then _items
.copy_to
(nns
, _bytelen
, 0, 0)
763 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
765 # Internal only, does not modify _bytelen or length, this is the caller's responsability
766 private fun rshift_bytes
(from
: Int, len
: Int) do
770 if bt
+ len
> capacity
then
771 capacity
= capacity
* 2 + 2
772 nit
= new NativeString(capacity
)
773 oit
.copy_to
(nit
, 0, 0, from
)
775 oit
.copy_to
(nit
, bt
- from
, from
, from
+ len
)
778 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
780 # Internal only, does not modify _bytelen or length, this is the caller's responsability
781 private fun lshift_bytes
(from
: Int, len
: Int) do
783 it
.copy_to
(it
, _bytelen
- from
, from
, from
- len
)
786 redef fun []=(index
, item
)
788 assert index
>= 0 and index
<= _length
789 if written
then reset
791 if index
== _length
then
796 var ip
= it
.char_to_byte_index
(index
)
797 var c
= it
.char_at
(ip
)
798 var clen
= c
.u8char_len
799 var itemlen
= item
.u8char_len
800 var size_diff
= itemlen
- clen
801 if size_diff
> 0 then
802 rshift_bytes
(ip
+ clen
, size_diff
)
803 else if size_diff
< 0 then
804 lshift_bytes
(ip
+ clen
, -size_diff
)
806 _bytelen
+= size_diff
807 it
.set_char_at
(ip
, item
)
812 if written
then reset
814 var clen
= c
.u8char_len
817 _items
.set_char_at
(bt
, c
)
824 if written
then reset
829 redef fun empty
do return new Buffer
831 redef fun enlarge
(cap
)
834 if cap
<= c
then return
835 while c
<= cap
do c
= c
* 2 + 2
836 # The COW flag can be set at false here, since
837 # it does a copy of the current `Buffer`
840 var a
= new NativeString(c
+1)
843 if bln
> 0 then it
.copy_to
(a
, bln
, 0, 0)
853 if bln
== 0 then _items
= new NativeString(1)
854 return new FlatString.full
(_items
, bln
, 0, _length
)
861 var new_native
= new NativeString(bln
+ 1)
862 new_native
[bln
] = 0u8
863 if _length
> 0 then _items
.copy_to
(new_native
, bln
, 0, 0)
864 real_items
= new_native
870 # Create a new empty string.
873 # Low-level creation a new buffer with given data.
875 # `_items` will be used as is, without copy, to store the characters of the buffer.
876 # Aliasing issues is the responsibility of the caller.
878 # If `_items` is shared, `written` should be set to true after the creation
879 # so that a modification will do a copy-on-write.
880 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
883 self.capacity
= capacity
884 self._bytelen
= bytelen
885 self._length
= length
888 # Create a new string copied from `s`.
891 _items
= new NativeString(s
.bytelen
)
892 if s
isa FlatText then
895 for i
in substrings
do i
.as(FlatString)._items
.copy_to
(_items
, i
._bytelen
, 0, 0)
903 # Create a new empty string with a given capacity.
904 init with_capacity
(cap
: Int)
907 _items
= new NativeString(cap
+ 1)
914 if s
.is_empty
then return
917 var nln
= _bytelen
+ sl
919 if s
isa FlatText then
920 s
._items
.copy_to
(_items
, sl
, s
.first_byte
, _bytelen
)
922 for i
in s
.substrings
do append i
929 # Copies the content of self in `dest`
930 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
932 var self_chars
= self.chars
933 var dest_chars
= dest
.chars
934 for i
in [0..len-1
] do
935 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
939 redef fun substring
(from
, count
)
942 if from
< 0 then from
= 0
943 if (from
+ count
) > _length
then count
= _length
- from
944 if count
<= 0 then return new Buffer
946 var bytefrom
= its
.char_to_byte_index
(from
)
947 var byteto
= its
.char_to_byte_index
(count
+ from
- 1)
948 byteto
+= its
.char_at
(byteto
).u8char_len
- 1
949 var byte_length
= byteto
- bytefrom
+ 1
950 var r_items
= new NativeString(byte_length
)
951 its
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
952 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
958 var ns
= new FlatBuffer.with_capacity
(capacity
)
959 for i
in chars
.reverse_iterator
do ns
.add i
963 redef fun times
(repeats
)
966 var x
= new FlatString.full
(_items
, bln
, 0, _length
)
967 for i
in [1 .. repeats
[ do
974 if written
then reset
975 for i
in [0 .. _length
[ do self[i
] = self[i
].to_upper
980 if written
then reset
981 for i
in [0 .. _length
[ do self[i
] = self[i
].to_lower
985 private class FlatBufferByteReverseIterator
986 super IndexedIterator[Byte]
988 var target
: FlatBuffer
990 var target_items
: NativeString is noautoinit
994 init do target_items
= target
._items
996 redef fun index
do return curr_pos
998 redef fun is_ok
do return curr_pos
>= 0
1000 redef fun item
do return target_items
[curr_pos
]
1002 redef fun next
do curr_pos
-= 1
1006 private class FlatBufferByteView
1007 super BufferByteView
1009 redef type SELFTYPE: FlatBuffer
1011 redef fun [](index
) do return target
._items
[index
]
1013 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator(target
, pos
)
1015 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator(target
, pos
)
1019 private class FlatBufferByteIterator
1020 super IndexedIterator[Byte]
1022 var target
: FlatBuffer
1024 var target_items
: NativeString is noautoinit
1028 init do target_items
= target
._items
1030 redef fun index
do return curr_pos
1032 redef fun is_ok
do return curr_pos
< target
._bytelen
1034 redef fun item
do return target_items
[curr_pos
]
1036 redef fun next
do curr_pos
+= 1
1040 private class FlatBufferCharReverseIterator
1041 super IndexedIterator[Char]
1043 var target
: FlatBuffer
1047 redef fun index
do return curr_pos
1049 redef fun is_ok
do return curr_pos
>= 0
1051 redef fun item
do return target
[curr_pos
]
1053 redef fun next
do curr_pos
-= 1
1057 private class FlatBufferCharView
1058 super BufferCharView
1060 redef type SELFTYPE: FlatBuffer
1062 redef fun [](index
) do return target
[index
]
1064 redef fun []=(index
, item
)
1066 assert index
>= 0 and index
<= length
1067 if index
== length
then
1071 target
[index
] = item
1084 fun enlarge
(cap
: Int)
1091 var s_length
= s
.length
1092 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
._length
)
1093 for i
in s
do target
.add i
1096 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator(target
, pos
)
1098 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator(target
, pos
)
1102 private class FlatBufferCharIterator
1103 super IndexedIterator[Char]
1105 var target
: FlatBuffer
1107 var max
: Int is noautoinit
1111 init do max
= target
._length
- 1
1113 redef fun index
do return curr_pos
1115 redef fun is_ok
do return curr_pos
<= max
1117 redef fun item
do return target
[curr_pos
]
1119 redef fun next
do curr_pos
+= 1
1123 redef class NativeString
1126 return to_s_with_length
(cstring_length
)
1129 # Returns `self` as a String of `length`.
1130 redef fun to_s_with_length
(length
): FlatString
1133 return clean_utf8
(length
)
1136 redef fun to_s_full
(bytelen
, unilen
) do
1137 return new FlatString.full
(self, bytelen
, 0, unilen
)
1140 redef fun to_s_unsafe
(len
) do
1141 if len
== null then len
= cstring_length
1142 return new FlatString.with_infos
(self, len
, 0)
1145 # Returns `self` as a new String.
1146 redef fun to_s_with_copy
: FlatString
1148 var length
= cstring_length
1149 var r
= clean_utf8
(length
)
1150 if r
.items
!= self then return r
1151 var new_self
= new NativeString(length
+ 1)
1152 copy_to
(new_self
, length
, 0, 0)
1153 var str
= new FlatString.with_infos
(new_self
, length
, 0)
1154 new_self
[length
] = 0u8
1155 str
.to_cstring
= new_self
1159 # Cleans a NativeString if necessary
1160 fun clean_utf8
(len
: Int): FlatString do
1161 var replacements
: nullable Array[Int] = null
1162 var end_length
= len
1168 var i
= fetch_4_chars
(pos
)
1169 if i
& 0x80808080 != 0 then break
1174 if rem
== 0 then break
1176 if b
& 0x80u
8 == 0x00u
8 then
1182 var nxst
= length_of_char_at
(pos
)
1185 ok_st
= b
& 0x80u
8 == 0u8
1186 else if nxst
== 2 then
1187 ok_st
= b
& 0xE0u
8 == 0xC0u
8
1188 else if nxst
== 3 then
1189 ok_st
= b
& 0xF0u
8 == 0xE0u
8
1191 ok_st
= b
& 0xF8u
8 == 0xF0u
8
1194 if replacements
== null then replacements
= new Array[Int]
1195 replacements
.add pos
1203 var c
= char_at
(pos
)
1204 var cp
= c
.code_point
1206 ok_c
= cp
>= 0 and cp
<= 0x7F
1207 else if nxst
== 2 then
1208 ok_c
= cp
>= 0x80 and cp
<= 0x7FF
1209 else if nxst
== 3 then
1210 ok_c
= cp
>= 0x800 and cp
<= 0xFFFF
1211 ok_c
= ok_c
and not (cp
>= 0xD800 and cp
<= 0xDFFF) and cp
!= 0xFFFE and cp
!= 0xFFFF
1213 ok_c
= cp
>= 0x10000 and cp
<= 0x10FFFF
1216 if replacements
== null then replacements
= new Array[Int]
1217 replacements
.add pos
1224 var clen
= c
.u8char_len
1230 if end_length
!= len
then
1231 ret
= new NativeString(end_length
)
1234 var repls
= replacements
.as(not null)
1235 var r
= repls
.items
.as(not null)
1236 var imax
= repls
.length
1237 for i
in [0 .. imax
[ do
1239 var chkln
= repl_pos
- old_repl
1240 copy_to
(ret
, chkln
, old_repl
, off
)
1243 ret
[off
+ 1] = 0xBFu
8
1244 ret
[off
+ 2] = 0xBDu
8
1245 old_repl
= repl_pos
+ 1
1248 copy_to
(ret
, len
- old_repl
, old_repl
, off
)
1250 return new FlatString.full
(ret
, end_length
, 0, chr_ln
)
1253 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
1255 # Very unsafe, make sure to have room for this char prior to calling this function.
1256 private fun set_char_at
(pos
: Int, c
: Char) do
1257 if c
.code_point
< 128 then
1258 self[pos
] = c
.code_point
.to_b
1261 var ln
= c
.u8char_len
1262 native_set_char
(pos
, c
, ln
)
1265 private fun native_set_char
(pos
: Int, c
: Char, ln
: Int) `{
1266 char* dst = self + pos;
1272 dst[0] = 0xC0 | ((c & 0x7C0) >> 6);
1273 dst[1] = 0x80 | (c & 0x3F);
1276 dst[0] = 0xE0 | ((c & 0xF000) >> 12);
1277 dst[1] = 0x80 | ((c & 0xFC0) >> 6);
1278 dst[2] = 0x80 | (c & 0x3F);
1281 dst[0] = 0xF0 | ((c & 0x1C0000) >> 18);
1282 dst[1] = 0x80 | ((c & 0x3F000) >> 12);
1283 dst[2] = 0x80 | ((c & 0xFC0) >> 6);
1284 dst[3] = 0x80 | (c & 0x3F);
1291 redef fun to_base
(base
, signed
)
1293 var l
= digit_count
(base
)
1294 var s
= new FlatBuffer.from
(" " * l
)
1295 fill_buffer
(s
, base
, signed
)
1299 # return displayable int in base 10 and signed
1301 # assert 1.to_s == "1"
1302 # assert (-123).to_s == "-123"
1304 # Fast case for common numbers
1305 if self == 0 then return "0"
1306 if self == 1 then return "1"
1308 var nslen
= int_to_s_len
1309 var ns
= new NativeString(nslen
+ 1)
1311 native_int_to_s
(ns
, nslen
+ 1)
1312 return new FlatString.full
(ns
, nslen
, 0, nslen
)
1316 redef class Array[E
]
1318 # Fast implementation
1319 redef fun plain_to_s
1322 if l
== 0 then return ""
1323 var its
= _items
.as(not null)
1325 if l
== 1 then if first
== null then return "" else return first
.to_s
1326 var na
= new NativeArray[String](l
)
1332 if itsi
== null then
1342 var ns
= new NativeString(sl
+ 1)
1348 if tmp
isa FlatString then
1349 var tpl
= tmp
._bytelen
1350 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1353 for j
in tmp
.substrings
do
1354 var s
= j
.as(FlatString)
1355 var slen
= s
._bytelen
1356 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1362 return new FlatString.with_infos
(ns
, sl
, 0)
1366 redef class NativeArray[E
]
1367 redef fun native_to_s
do
1368 assert self isa NativeArray[String]
1379 var ns
= new NativeString(sl
+ 1)
1385 if tmp
isa FlatString then
1386 var tpl
= tmp
._bytelen
1387 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1390 for j
in tmp
.substrings
do
1391 var s
= j
.as(FlatString)
1392 var slen
= s
._bytelen
1393 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1399 return new FlatString.with_infos
(ns
, sl
, 0)
1403 redef class Map[K
,V
]
1404 redef fun join
(sep
, couple_sep
)
1406 if is_empty
then return ""
1408 var s
= new Buffer # Result
1414 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1416 # Concat other _items
1422 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")