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
56 if dpos
== 0 then return b
58 b
+= _items
.length_of_char_at
(b
)
64 b
= _items
.find_beginning_of_char_at
(b
- 1)
72 # Find best insertion point
73 var delta_begin
= index
74 var delta_end
= (ln
- 1) - index
75 var delta_cache
= (pos
- index
).abs
79 if delta_cache
< min
then min
= delta_cache
80 if delta_end
< min
then min
= delta_end
85 if min
== delta_cache
then
88 else if min
== delta_begin
then
92 ns_i
= its
.find_beginning_of_char_at
(last_byte
)
96 ns_i
= its
.char_to_byte_index_cached
(index
, my_i
, ns_i
)
104 # By escaping `self` to HTML, how many more bytes will be needed ?
105 fun chars_to_html_escape
: Int do
114 else if c
== 0x3Eu
8 then
116 else if c
== 0x26u
8 then
118 else if c
== 0x22u
8 then
120 else if c
== 0x27u
8 then
122 else if c
== 0x2Fu
8 then
130 redef fun html_escape
132 var extra
= chars_to_html_escape
133 if extra
== 0 then return to_s
137 var nlen
= extra
+ _bytelen
138 var nits
= new NativeString(nlen
)
143 # Some HTML characters are used as meta-data, they need
144 # to be replaced by an HTML-Escaped equivalent
148 # * 0x26 (&) => &
149 # * 0x22 (") => "
150 # * 0x27 (') => '
151 # * 0x2F (/) => /
153 nits
[outpos
] = 0x26u
8
154 nits
[outpos
+ 1] = 0x6Cu
8
155 nits
[outpos
+ 2] = 0x74u
8
156 nits
[outpos
+ 3] = 0x3Bu
8
158 else if c
== 0x3Eu
8 then
159 nits
[outpos
] = 0x26u
8
160 nits
[outpos
+ 1] = 0x67u
8
161 nits
[outpos
+ 2] = 0x74u
8
162 nits
[outpos
+ 3] = 0x3Bu
8
164 else if c
== 0x26u
8 then
165 nits
[outpos
] = 0x26u
8
166 nits
[outpos
+ 1] = 0x61u
8
167 nits
[outpos
+ 2] = 0x6Du
8
168 nits
[outpos
+ 3] = 0x70u
8
169 nits
[outpos
+ 4] = 0x3Bu
8
171 else if c
== 0x22u
8 then
172 nits
[outpos
] = 0x26u
8
173 nits
[outpos
+ 1] = 0x23u
8
174 nits
[outpos
+ 2] = 0x33u
8
175 nits
[outpos
+ 3] = 0x34u
8
176 nits
[outpos
+ 4] = 0x3Bu
8
178 else if c
== 0x27u
8 then
179 nits
[outpos
] = 0x26u
8
180 nits
[outpos
+ 1] = 0x23u
8
181 nits
[outpos
+ 2] = 0x33u
8
182 nits
[outpos
+ 3] = 0x39u
8
183 nits
[outpos
+ 4] = 0x3Bu
8
185 else if c
== 0x2Fu
8 then
186 nits
[outpos
] = 0x26u
8
187 nits
[outpos
+ 1] = 0x23u
8
188 nits
[outpos
+ 2] = 0x34u
8
189 nits
[outpos
+ 3] = 0x37u
8
190 nits
[outpos
+ 4] = 0x3Bu
8
198 var s
= new FlatString.with_infos
(nits
, nlen
, 0)
202 # By escaping `self` to C, how many more bytes will be needed ?
204 # This enables a double-optimization in `escape_to_c` since if this
205 # method returns 0, then `self` does not need escaping and can be
207 fun chars_to_escape_to_c
: Int do
216 else if c
== 0x09u
8 then
218 else if c
== 0x22u
8 then
220 else if c
== 0x27u
8 then
222 else if c
== 0x5Cu
8 then
224 else if c
< 32u8
then
232 redef fun escape_to_c
do
233 var ln_extra
= chars_to_escape_to_c
234 if ln_extra
== 0 then return self.to_s
237 var nlen
= _bytelen
+ ln_extra
238 var nns
= new NativeString(nlen
)
245 # Any byte with value < 32 is a control character
246 # All their uses will be replaced by their octal
249 # There are two exceptions however:
254 # Aside from the code points above, the following are:
261 nns
[opos
+ 1] = 0x74u
8
263 else if c
== 0x0Au
8 then
265 nns
[opos
+ 1] = 0x6Eu
8
267 else if c
== 0x22u
8 then
269 nns
[opos
+ 1] = 0x22u
8
271 else if c
== 0x27u
8 then
273 nns
[opos
+ 1] = 0x27u
8
275 else if c
== 0x5Cu
8 then
277 nns
[opos
+ 1] = 0x5Cu
8
279 else if c
< 32u8
then
281 nns
[opos
+ 1] = 0x30u
8
282 nns
[opos
+ 2] = ((c
& 0x38u
8) >> 3) + 0x30u
8
283 nns
[opos
+ 3] = (c
& 0x07u
8) + 0x30u
8
291 return nns
.to_s_unsafe
(nlen
)
294 redef fun [](index
) do
295 assert index
>= 0 and index
< _length
296 return fetch_char_at
(index
)
299 # Gets a `Char` at `index` in `self`
301 # WARNING: Use at your own risks as no bound-checking is done
302 fun fetch_char_at
(index
: Int): Char do
303 var i
= char_to_byte_index
(index
)
306 if b
& 0x80u
8 == 0x00u
8 then return b
.ascii
307 return items
.char_at
(i
)
310 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
312 # assert "ff".to_hex == 255
313 redef fun to_hex
(pos
, ln
) do
315 if pos
== null then pos
= 0
316 if ln
== null then ln
= length
- pos
317 pos
= char_to_byte_index
(pos
)
320 for i
in [pos
.. max
[ do
322 res
+= its
[i
].ascii
.from_hex
328 # Immutable strings of characters.
333 # Index at which `self` begins in `_items`, inclusively
334 redef var first_byte
is noinit
336 redef var chars
= new FlatStringCharView(self) is lazy
338 redef var bytes
= new FlatStringByteView(self) is lazy
340 redef var to_cstring
is lazy
do
342 var new_items
= new NativeString(blen
+ 1)
343 _items
.copy_to
(new_items
, blen
, _first_byte
, 0)
344 new_items
[blen
] = 0u8
348 redef fun reversed
do
349 var b
= new FlatBuffer.with_capacity
(_bytelen
+ 1)
352 b
.add
self.fetch_char_at
(i
)
355 var s
= b
.to_s
.as(FlatString)
356 s
._length
= self._length
360 redef fun fast_cstring
do return _items
.fast_cstring
(_first_byte
)
362 redef fun substring
(from
, count
)
368 if count
< 0 then count
= 0
372 if (count
+ from
) > length
then count
= length
- from
373 if count
<= 0 then return ""
374 var end_index
= from
+ count
- 1
376 var bytefrom
= char_to_byte_index
(from
)
377 var byteto
= char_to_byte_index
(end_index
)
379 byteto
+= its
.length_of_char_at
(byteto
) - 1
381 var s
= new FlatString.full
(its
, byteto
- bytefrom
+ 1, bytefrom
, byteto
, count
)
385 redef fun empty
do return "".as(FlatString)
389 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
395 outstr
.add
(chars
[pos
].to_upper
)
404 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
410 outstr
.add
(chars
[pos
].to_lower
)
419 for i
in chars
do i
.output
422 ##################################################
423 # String Specific Methods #
424 ##################################################
426 # Low-level creation of a new string with minimal data.
428 # `_items` will be used as is, without copy, to retrieve the characters of the string.
429 # Aliasing issues is the responsibility of the caller.
430 private init with_infos
(items
: NativeString, bytelen
, from
: Int)
433 self._bytelen
= bytelen
436 _length
= _items
.utf8_length
(_first_byte
, last_byte
)
439 # Low-level creation of a new string with all the data.
441 # `_items` will be used as is, without copy, to retrieve the characters of the string.
442 # Aliasing issues is the responsibility of the caller.
443 private init full
(items
: NativeString, bytelen
, from
, length
: Int)
446 self._length
= length
447 self._bytelen
= bytelen
454 if not other
isa FlatText then return super
456 if self.object_id
== other
.object_id
then return true
458 var my_length
= _bytelen
460 if other
._bytelen
!= my_length
then return false
462 var my_index
= _first_byte
463 var its_index
= other
.first_byte
465 var last_iteration
= my_index
+ my_length
467 var its_items
= other
._items
468 var my_items
= self._items
470 while my_index
< last_iteration
do
471 if my_items
[my_index
] != its_items
[its_index
] then return false
481 if not other
isa FlatText then return super
483 if self.object_id
== other
.object_id
then return false
486 var itsits
= other
._items
489 var obt
= other
.bytelen
491 var minln
= if mbt
< obt
then mbt
else obt
492 var mst
= _first_byte
493 var ost
= other
.first_byte
495 for i
in [0 .. minln
[ do
496 var my_curr_char
= myits
[mst
]
497 var its_curr_char
= itsits
[ost
]
499 if my_curr_char
> its_curr_char
then return false
500 if my_curr_char
< its_curr_char
then return true
513 var nlen
= mlen
+ slen
515 var mifrom
= _first_byte
516 if s
isa FlatText then
518 var sifrom
= s
.first_byte
519 var ns
= new NativeString(nlen
+ 1)
520 mits
.copy_to
(ns
, mlen
, mifrom
, 0)
521 sits
.copy_to
(ns
, slen
, sifrom
, mlen
)
522 return new FlatString.full
(ns
, nlen
, 0, _length
+ o
.length
)
529 var mybtlen
= _bytelen
530 var new_bytelen
= mybtlen
* i
532 var newlen
= mylen
* i
535 var ns
= new NativeString(new_bytelen
+ 1)
536 ns
[new_bytelen
] = 0u8
539 its
.copy_to
(ns
, mybtlen
, fb
, offset
)
543 return new FlatString.full
(ns
, new_bytelen
, 0, newlen
)
549 if hash_cache
== null then
550 # djb2 hash algorithm
554 var my_items
= _items
558 h
= (h
<< 5) + h
+ my_items
[i
].to_i
565 return hash_cache
.as(not null)
568 redef fun substrings
do return new FlatSubstringsIter(self)
571 private class FlatStringCharReverseIterator
572 super IndexedIterator[Char]
574 var target
: FlatString
578 redef fun is_ok
do return curr_pos
>= 0
580 redef fun item
do return target
[curr_pos
]
582 redef fun next
do curr_pos
-= 1
584 redef fun index
do return curr_pos
588 private class FlatStringCharIterator
589 super IndexedIterator[Char]
591 var target
: FlatString
593 var max
: Int is noautoinit
597 init do max
= target
._length
- 1
599 redef fun is_ok
do return curr_pos
<= max
601 redef fun item
do return target
[curr_pos
]
603 redef fun next
do curr_pos
+= 1
605 redef fun index
do return curr_pos
609 private class FlatStringCharView
612 redef type SELFTYPE: FlatString
614 redef fun [](index
) do return target
[index
]
616 redef fun iterator_from
(start
) do return new FlatStringCharIterator(target
, start
)
618 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator(target
, start
)
622 private class FlatStringByteReverseIterator
623 super IndexedIterator[Byte]
625 var target
: FlatString
627 var target_items
: NativeString is noautoinit
634 target_items
= tgt
._items
635 curr_pos
+= tgt
._first_byte
638 redef fun is_ok
do return curr_pos
>= target
._first_byte
640 redef fun item
do return target_items
[curr_pos
]
642 redef fun next
do curr_pos
-= 1
644 redef fun index
do return curr_pos
- target
._first_byte
648 private class FlatStringByteIterator
649 super IndexedIterator[Byte]
651 var target
: FlatString
653 var target_items
: NativeString is noautoinit
660 target_items
= tgt
._items
661 curr_pos
+= tgt
._first_byte
664 redef fun is_ok
do return curr_pos
<= target
.last_byte
666 redef fun item
do return target_items
[curr_pos
]
668 redef fun next
do curr_pos
+= 1
670 redef fun index
do return curr_pos
- target
._first_byte
674 private class FlatStringByteView
677 redef type SELFTYPE: FlatString
681 # Check that the index (+ _first_byte) is not larger than last_byte
682 # In other terms, if the index is valid
684 assert index
>= 0 and index
< target
._bytelen
685 var ind
= index
+ target
._first_byte
686 return target
._items
[ind
]
689 redef fun iterator_from
(start
) do return new FlatStringByteIterator(target
, start
)
691 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator(target
, start
)
696 redef new do return new FlatBuffer
698 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
701 # Mutable strings of characters.
706 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
708 redef var bytes
= new FlatBufferByteView(self) is lazy
710 private var char_cache
: Int = -1
712 private var byte_cache
: Int = -1
714 private var capacity
= 0
716 # Real items, used as cache for when to_cstring is called
717 private var real_items
: NativeString is noinit
719 redef fun fast_cstring
do return _items
.fast_cstring
(0)
721 redef fun substrings
do return new FlatSubstringsIter(self)
723 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
725 # This happens when an operation modifies the current `Buffer` and
726 # the Copy-On-Write flag `written` is set at true.
728 var nns
= new NativeString(capacity
)
729 if _bytelen
!= 0 then _items
.copy_to
(nns
, _bytelen
, 0, 0)
734 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
736 # Internal only, does not modify _bytelen or length, this is the caller's responsability
737 private fun rshift_bytes
(from
: Int, len
: Int) do
741 if bt
+ len
> capacity
then
742 capacity
= capacity
* 2 + 2
743 nit
= new NativeString(capacity
)
744 oit
.copy_to
(nit
, 0, 0, from
)
746 oit
.copy_to
(nit
, bt
- from
, from
, from
+ len
)
749 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
751 # Internal only, does not modify _bytelen or length, this is the caller's responsability
752 private fun lshift_bytes
(from
: Int, len
: Int) do
754 it
.copy_to
(it
, _bytelen
- from
, from
, from
- len
)
757 redef fun []=(index
, item
)
759 assert index
>= 0 and index
<= _length
760 if written
then reset
762 if index
== _length
then
767 var ip
= it
.char_to_byte_index
(index
)
768 var c
= it
.char_at
(ip
)
769 var clen
= c
.u8char_len
770 var itemlen
= item
.u8char_len
771 var size_diff
= itemlen
- clen
772 if size_diff
> 0 then
773 rshift_bytes
(ip
+ clen
, size_diff
)
774 else if size_diff
< 0 then
775 lshift_bytes
(ip
+ clen
, -size_diff
)
777 _bytelen
+= size_diff
778 it
.set_char_at
(ip
, item
)
783 if written
then reset
785 var clen
= c
.u8char_len
788 _items
.set_char_at
(bt
, c
)
795 if written
then reset
800 redef fun empty
do return new Buffer
802 redef fun enlarge
(cap
)
805 if cap
<= c
then return
806 while c
<= cap
do c
= c
* 2 + 2
807 # The COW flag can be set at false here, since
808 # it does a copy of the current `Buffer`
811 var a
= new NativeString(c
+1)
814 if bln
> 0 then it
.copy_to
(a
, bln
, 0, 0)
824 if bln
== 0 then _items
= new NativeString(1)
825 return new FlatString.full
(_items
, bln
, 0, _length
)
832 var new_native
= new NativeString(bln
+ 1)
833 new_native
[bln
] = 0u8
834 if _length
> 0 then _items
.copy_to
(new_native
, bln
, 0, 0)
835 real_items
= new_native
841 # Create a new empty string.
844 # Low-level creation a new buffer with given data.
846 # `_items` will be used as is, without copy, to store the characters of the buffer.
847 # Aliasing issues is the responsibility of the caller.
849 # If `_items` is shared, `written` should be set to true after the creation
850 # so that a modification will do a copy-on-write.
851 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
854 self.capacity
= capacity
855 self._bytelen
= bytelen
856 self._length
= length
859 # Create a new string copied from `s`.
862 _items
= new NativeString(s
.bytelen
)
863 if s
isa FlatText then
866 for i
in substrings
do i
.as(FlatString)._items
.copy_to
(_items
, i
._bytelen
, 0, 0)
874 # Create a new empty string with a given capacity.
875 init with_capacity
(cap
: Int)
878 _items
= new NativeString(cap
+ 1)
885 if s
.is_empty
then return
888 var nln
= _bytelen
+ sl
890 if s
isa FlatText then
891 s
._items
.copy_to
(_items
, sl
, s
.first_byte
, _bytelen
)
893 for i
in s
.substrings
do append i
900 # Copies the content of self in `dest`
901 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
903 var self_chars
= self.chars
904 var dest_chars
= dest
.chars
905 for i
in [0..len-1
] do
906 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
910 redef fun substring
(from
, count
)
913 if from
< 0 then from
= 0
914 if (from
+ count
) > _length
then count
= _length
- from
915 if count
<= 0 then return new Buffer
917 var bytefrom
= its
.char_to_byte_index
(from
)
918 var byteto
= its
.char_to_byte_index
(count
+ from
- 1)
919 byteto
+= its
.char_at
(byteto
).u8char_len
- 1
920 var byte_length
= byteto
- bytefrom
+ 1
921 var r_items
= new NativeString(byte_length
)
922 its
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
923 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
929 var ns
= new FlatBuffer.with_capacity
(capacity
)
930 for i
in chars
.reverse_iterator
do ns
.add i
934 redef fun times
(repeats
)
937 var x
= new FlatString.full
(_items
, bln
, 0, _length
)
938 for i
in [1 .. repeats
[ do
945 if written
then reset
946 for i
in [0 .. _length
[ do self[i
] = self[i
].to_upper
951 if written
then reset
952 for i
in [0 .. _length
[ do self[i
] = self[i
].to_lower
956 private class FlatBufferByteReverseIterator
957 super IndexedIterator[Byte]
959 var target
: FlatBuffer
961 var target_items
: NativeString is noautoinit
965 init do target_items
= target
._items
967 redef fun index
do return curr_pos
969 redef fun is_ok
do return curr_pos
>= 0
971 redef fun item
do return target_items
[curr_pos
]
973 redef fun next
do curr_pos
-= 1
977 private class FlatBufferByteView
980 redef type SELFTYPE: FlatBuffer
982 redef fun [](index
) do return target
._items
[index
]
984 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator(target
, pos
)
986 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator(target
, pos
)
990 private class FlatBufferByteIterator
991 super IndexedIterator[Byte]
993 var target
: FlatBuffer
995 var target_items
: NativeString is noautoinit
999 init do target_items
= target
._items
1001 redef fun index
do return curr_pos
1003 redef fun is_ok
do return curr_pos
< target
._bytelen
1005 redef fun item
do return target_items
[curr_pos
]
1007 redef fun next
do curr_pos
+= 1
1011 private class FlatBufferCharReverseIterator
1012 super IndexedIterator[Char]
1014 var target
: FlatBuffer
1018 redef fun index
do return curr_pos
1020 redef fun is_ok
do return curr_pos
>= 0
1022 redef fun item
do return target
[curr_pos
]
1024 redef fun next
do curr_pos
-= 1
1028 private class FlatBufferCharView
1029 super BufferCharView
1031 redef type SELFTYPE: FlatBuffer
1033 redef fun [](index
) do return target
[index
]
1035 redef fun []=(index
, item
)
1037 assert index
>= 0 and index
<= length
1038 if index
== length
then
1042 target
[index
] = item
1055 fun enlarge
(cap
: Int)
1062 var s_length
= s
.length
1063 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
._length
)
1064 for i
in s
do target
.add i
1067 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator(target
, pos
)
1069 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator(target
, pos
)
1073 private class FlatBufferCharIterator
1074 super IndexedIterator[Char]
1076 var target
: FlatBuffer
1078 var max
: Int is noautoinit
1082 init do max
= target
._length
- 1
1084 redef fun index
do return curr_pos
1086 redef fun is_ok
do return curr_pos
<= max
1088 redef fun item
do return target
[curr_pos
]
1090 redef fun next
do curr_pos
+= 1
1094 redef class NativeString
1097 return to_s_with_length
(cstring_length
)
1100 # Returns `self` as a String of `length`.
1101 redef fun to_s_with_length
(length
): FlatString
1104 return clean_utf8
(length
)
1107 redef fun to_s_full
(bytelen
, unilen
) do
1108 return new FlatString.full
(self, bytelen
, 0, unilen
)
1111 redef fun to_s_unsafe
(len
) do
1112 if len
== null then len
= cstring_length
1113 return new FlatString.with_infos
(self, len
, 0)
1116 # Returns `self` as a new String.
1117 redef fun to_s_with_copy
: FlatString
1119 var length
= cstring_length
1120 var r
= clean_utf8
(length
)
1121 if r
.items
!= self then return r
1122 var new_self
= new NativeString(length
+ 1)
1123 copy_to
(new_self
, length
, 0, 0)
1124 var str
= new FlatString.with_infos
(new_self
, length
, 0)
1125 new_self
[length
] = 0u8
1126 str
.to_cstring
= new_self
1130 # Cleans a NativeString if necessary
1131 fun clean_utf8
(len
: Int): FlatString do
1132 var replacements
: nullable Array[Int] = null
1133 var end_length
= len
1139 var i
= fetch_4_chars
(pos
)
1140 if i
& 0x80808080 != 0 then break
1145 if rem
== 0 then break
1147 if b
& 0x80u
8 == 0x00u
8 then
1153 var nxst
= length_of_char_at
(pos
)
1156 ok_st
= b
& 0x80u
8 == 0u8
1157 else if nxst
== 2 then
1158 ok_st
= b
& 0xE0u
8 == 0xC0u
8
1159 else if nxst
== 3 then
1160 ok_st
= b
& 0xF0u
8 == 0xE0u
8
1162 ok_st
= b
& 0xF8u
8 == 0xF0u
8
1165 if replacements
== null then replacements
= new Array[Int]
1166 replacements
.add pos
1174 var c
= char_at
(pos
)
1175 var cp
= c
.code_point
1177 ok_c
= cp
>= 0 and cp
<= 0x7F
1178 else if nxst
== 2 then
1179 ok_c
= cp
>= 0x80 and cp
<= 0x7FF
1180 else if nxst
== 3 then
1181 ok_c
= cp
>= 0x800 and cp
<= 0xFFFF
1182 ok_c
= ok_c
and not (cp
>= 0xD800 and cp
<= 0xDFFF) and cp
!= 0xFFFE and cp
!= 0xFFFF
1184 ok_c
= cp
>= 0x10000 and cp
<= 0x10FFFF
1187 if replacements
== null then replacements
= new Array[Int]
1188 replacements
.add pos
1195 var clen
= c
.u8char_len
1201 if end_length
!= len
then
1202 ret
= new NativeString(end_length
)
1205 var repls
= replacements
.as(not null)
1206 var r
= repls
.items
.as(not null)
1207 var imax
= repls
.length
1208 for i
in [0 .. imax
[ do
1210 var chkln
= repl_pos
- old_repl
1211 copy_to
(ret
, chkln
, old_repl
, off
)
1214 ret
[off
+ 1] = 0xBFu
8
1215 ret
[off
+ 2] = 0xBDu
8
1216 old_repl
= repl_pos
+ 1
1219 copy_to
(ret
, len
- old_repl
, old_repl
, off
)
1221 return new FlatString.full
(ret
, end_length
, 0, chr_ln
)
1224 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
1226 # Very unsafe, make sure to have room for this char prior to calling this function.
1227 private fun set_char_at
(pos
: Int, c
: Char) do
1228 var ln
= c
.u8char_len
1229 native_set_char
(pos
, c
, ln
)
1232 private fun native_set_char
(pos
: Int, c
: Char, ln
: Int) `{
1233 char* dst = self + pos;
1239 dst[0] = 0xC0 | ((c & 0x7C0) >> 6);
1240 dst[1] = 0x80 | (c & 0x3F);
1243 dst[0] = 0xE0 | ((c & 0xF000) >> 12);
1244 dst[1] = 0x80 | ((c & 0xFC0) >> 6);
1245 dst[2] = 0x80 | (c & 0x3F);
1248 dst[0] = 0xF0 | ((c & 0x1C0000) >> 18);
1249 dst[1] = 0x80 | ((c & 0x3F000) >> 12);
1250 dst[2] = 0x80 | ((c & 0xFC0) >> 6);
1251 dst[3] = 0x80 | (c & 0x3F);
1258 redef fun to_base
(base
, signed
)
1260 var l
= digit_count
(base
)
1261 var s
= new FlatBuffer.from
(" " * l
)
1262 fill_buffer
(s
, base
, signed
)
1266 # return displayable int in base 10 and signed
1268 # assert 1.to_s == "1"
1269 # assert (-123).to_s == "-123"
1271 # Fast case for common numbers
1272 if self == 0 then return "0"
1273 if self == 1 then return "1"
1275 var nslen
= int_to_s_len
1276 var ns
= new NativeString(nslen
+ 1)
1278 native_int_to_s
(ns
, nslen
+ 1)
1279 return new FlatString.full
(ns
, nslen
, 0, nslen
)
1283 redef class Array[E
]
1285 # Fast implementation
1286 redef fun plain_to_s
1289 if l
== 0 then return ""
1290 var its
= _items
.as(not null)
1292 if l
== 1 then if first
== null then return "" else return first
.to_s
1293 var na
= new NativeArray[String](l
)
1299 if itsi
== null then
1309 var ns
= new NativeString(sl
+ 1)
1315 if tmp
isa FlatString then
1316 var tpl
= tmp
._bytelen
1317 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1320 for j
in tmp
.substrings
do
1321 var s
= j
.as(FlatString)
1322 var slen
= s
._bytelen
1323 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1329 return new FlatString.with_infos
(ns
, sl
, 0)
1333 redef class NativeArray[E
]
1334 redef fun native_to_s
do
1335 assert self isa NativeArray[String]
1346 var ns
= new NativeString(sl
+ 1)
1352 if tmp
isa FlatString then
1353 var tpl
= tmp
._bytelen
1354 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1357 for j
in tmp
.substrings
do
1358 var s
= j
.as(FlatString)
1359 var slen
= s
._bytelen
1360 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1366 return new FlatString.with_infos
(ns
, sl
, 0)
1370 redef class Map[K
,V
]
1371 redef fun join
(sep
, couple_sep
)
1373 if is_empty
then return ""
1375 var s
= new Buffer # Result
1381 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1383 # Concat other _items
1389 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")