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 fun first_byte
: Int do return 0
41 fun last_byte
: Int do return _bytelen
- 1
43 # Cache of the latest position (char) explored in the string
46 # Cached position (bytes) in the NativeString underlying the String
49 # Index of the character `index` in `_items`
50 fun char_to_byte_index
(index
: Int): Int do
56 # Find best insertion point
57 var delta_begin
= index
58 var delta_end
= (ln
- 1) - index
59 var delta_cache
= (pos
- index
).abs
63 if delta_cache
< min
then min
= delta_cache
64 if delta_end
< min
then min
= delta_end
69 if min
== delta_begin
then
72 else if min
== delta_cache
then
76 ns_i
= its
.find_beginning_of_char_at
(last_byte
)
80 ns_i
= its
.char_to_byte_index_cached
(index
, my_i
, ns_i
)
88 # By escaping `self` to HTML, how many more bytes will be needed ?
89 fun chars_to_html_escape
: Int do
98 else if c
== 0x3Eu
8 then
100 else if c
== 0x26u
8 then
102 else if c
== 0x22u
8 then
104 else if c
== 0x27u
8 then
106 else if c
== 0x2Fu
8 then
114 redef fun html_escape
116 var extra
= chars_to_html_escape
117 if extra
== 0 then return to_s
121 var nlen
= extra
+ _bytelen
122 var nits
= new NativeString(nlen
)
127 # Some HTML characters are used as meta-data, they need
128 # to be replaced by an HTML-Escaped equivalent
132 # * 0x26 (&) => &
133 # * 0x22 (") => "
134 # * 0x27 (') => '
135 # * 0x2F (/) => /
137 nits
[outpos
] = 0x26u
8
138 nits
[outpos
+ 1] = 0x6Cu
8
139 nits
[outpos
+ 2] = 0x74u
8
140 nits
[outpos
+ 3] = 0x3Bu
8
142 else if c
== 0x3Eu
8 then
143 nits
[outpos
] = 0x26u
8
144 nits
[outpos
+ 1] = 0x67u
8
145 nits
[outpos
+ 2] = 0x74u
8
146 nits
[outpos
+ 3] = 0x3Bu
8
148 else if c
== 0x26u
8 then
149 nits
[outpos
] = 0x26u
8
150 nits
[outpos
+ 1] = 0x61u
8
151 nits
[outpos
+ 2] = 0x6Du
8
152 nits
[outpos
+ 3] = 0x70u
8
153 nits
[outpos
+ 4] = 0x3Bu
8
155 else if c
== 0x22u
8 then
156 nits
[outpos
] = 0x26u
8
157 nits
[outpos
+ 1] = 0x23u
8
158 nits
[outpos
+ 2] = 0x33u
8
159 nits
[outpos
+ 3] = 0x34u
8
160 nits
[outpos
+ 4] = 0x3Bu
8
162 else if c
== 0x27u
8 then
163 nits
[outpos
] = 0x26u
8
164 nits
[outpos
+ 1] = 0x23u
8
165 nits
[outpos
+ 2] = 0x33u
8
166 nits
[outpos
+ 3] = 0x39u
8
167 nits
[outpos
+ 4] = 0x3Bu
8
169 else if c
== 0x2Fu
8 then
170 nits
[outpos
] = 0x26u
8
171 nits
[outpos
+ 1] = 0x23u
8
172 nits
[outpos
+ 2] = 0x34u
8
173 nits
[outpos
+ 3] = 0x37u
8
174 nits
[outpos
+ 4] = 0x3Bu
8
182 var s
= new FlatString.with_infos
(nits
, nlen
, 0, nlen
- 1)
186 # By escaping `self` to C, how many more bytes will be needed ?
188 # This enables a double-optimization in `escape_to_c` since if this
189 # method returns 0, then `self` does not need escaping and can be
191 fun chars_to_escape_to_c
: Int do
200 else if c
== 0x09u
8 then
202 else if c
== 0x22u
8 then
204 else if c
== 0x27u
8 then
206 else if c
== 0x5Cu
8 then
208 else if c
< 32u8
then
216 redef fun escape_to_c
do
217 var ln_extra
= chars_to_escape_to_c
218 if ln_extra
== 0 then return self.to_s
221 var nlen
= _bytelen
+ ln_extra
222 var nns
= new NativeString(nlen
)
229 # Any byte with value < 32 is a control character
230 # All their uses will be replaced by their octal
233 # There are two exceptions however:
238 # Aside from the code points above, the following are:
245 nns
[opos
+ 1] = 0x74u
8
247 else if c
== 0x0Au
8 then
249 nns
[opos
+ 1] = 0x6Eu
8
251 else if c
== 0x22u
8 then
253 nns
[opos
+ 1] = 0x22u
8
255 else if c
== 0x27u
8 then
257 nns
[opos
+ 1] = 0x27u
8
259 else if c
== 0x5Cu
8 then
261 nns
[opos
+ 1] = 0x5Cu
8
263 else if c
< 32u8
then
265 nns
[opos
+ 1] = 0x30u
8
266 nns
[opos
+ 2] = ((c
& 0x38u
8) >> 3) + 0x30u
8
267 nns
[opos
+ 3] = (c
& 0x07u
8) + 0x30u
8
275 return nns
.to_s_with_length
(nlen
)
278 redef fun [](index
) do return _items
.char_at
(char_to_byte_index
(index
))
281 # Immutable strings of characters.
286 # Index at which `self` begins in `_items`, inclusively
287 redef var first_byte
is noinit
289 # Index at which `self` ends in `_items`, inclusively
290 redef var last_byte
is noinit
292 redef var chars
= new FlatStringCharView(self) is lazy
294 redef var bytes
= new FlatStringByteView(self) is lazy
296 redef var length
is lazy
do
297 if _bytelen
== 0 then return 0
298 return _items
.utf8_length
(_first_byte
, _last_byte
)
301 redef var to_cstring
is lazy
do
303 var new_items
= new NativeString(blen
+ 1)
304 _items
.copy_to
(new_items
, blen
, _first_byte
, 0)
305 new_items
[blen
] = 0u8
311 var b
= new FlatBuffer.with_capacity
(_bytelen
+ 1)
312 for i
in [length
- 1 .. 0].step
(-1) do
315 var s
= b
.to_s
.as(FlatString)
316 s
.length
= self.length
320 redef fun fast_cstring
do return _items
.fast_cstring
(_first_byte
)
322 redef fun substring
(from
, count
)
328 if count
< 0 then count
= 0
332 if (count
+ from
) > length
then count
= length
- from
333 if count
<= 0 then return ""
334 var end_index
= from
+ count
- 1
336 var bytefrom
= char_to_byte_index
(from
)
337 var byteto
= char_to_byte_index
(end_index
)
339 byteto
+= its
.length_of_char_at
(byteto
) - 1
341 var s
= new FlatString.full
(its
, byteto
- bytefrom
+ 1, bytefrom
, byteto
, count
)
345 redef fun empty
do return "".as(FlatString)
349 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
355 outstr
.add
(chars
[pos
].to_upper
)
364 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
370 outstr
.add
(chars
[pos
].to_lower
)
379 for i
in chars
do i
.output
382 ##################################################
383 # String Specific Methods #
384 ##################################################
386 # Low-level creation of a new string with minimal data.
388 # `_items` will be used as is, without copy, to retrieve the characters of the string.
389 # Aliasing issues is the responsibility of the caller.
390 private init with_infos
(items
: NativeString, bytelen
, from
, to
: Int)
393 self._bytelen
= bytelen
399 # Low-level creation of a new string with all the data.
401 # `_items` will be used as is, without copy, to retrieve the characters of the string.
402 # Aliasing issues is the responsibility of the caller.
403 private init full
(items
: NativeString, bytelen
, from
, to
, length
: Int)
407 self._bytelen
= bytelen
415 if not other
isa FlatString then return super
417 if self.object_id
== other
.object_id
then return true
419 var my_length
= _bytelen
421 if other
._bytelen
!= my_length
then return false
423 var my_index
= _first_byte
424 var its_index
= other
._first_byte
426 var last_iteration
= my_index
+ my_length
428 var its_items
= other
._items
429 var my_items
= self._items
431 while my_index
< last_iteration
do
432 if my_items
[my_index
] != its_items
[its_index
] then return false
442 if not other
isa FlatString then return super
444 if self.object_id
== other
.object_id
then return false
446 var my_length
= self._bytelen
447 var its_length
= other
._bytelen
449 var max
= if my_length
< its_length
then my_length
else its_length
451 var myits
= self.bytes
452 var itsits
= other
.bytes
454 for i
in [0 .. max
[ do
455 var my_curr_char
= myits
[i
]
456 var its_curr_char
= itsits
[i
]
458 if my_curr_char
!= its_curr_char
then
459 if my_curr_char
< its_curr_char
then return true
464 return my_length
< its_length
471 var nlen
= mlen
+ slen
473 var mifrom
= _first_byte
474 if s
isa FlatText then
476 var sifrom
= s
.first_byte
477 var ns
= new NativeString(nlen
+ 1)
478 mits
.copy_to
(ns
, mlen
, mifrom
, 0)
479 sits
.copy_to
(ns
, slen
, sifrom
, mlen
)
480 return new FlatString.full
(ns
, nlen
, 0, nlen
- 1, length
+ o
.length
)
487 var mybtlen
= _bytelen
488 var new_bytelen
= mybtlen
* i
490 var newlen
= mylen
* i
493 var ns
= new NativeString(new_bytelen
+ 1)
494 ns
[new_bytelen
] = 0u8
497 its
.copy_to
(ns
, mybtlen
, fb
, offset
)
501 return new FlatString.full
(ns
, new_bytelen
, 0, new_bytelen
- 1, newlen
)
507 if hash_cache
== null then
508 # djb2 hash algorithm
512 var my_items
= _items
516 h
= (h
<< 5) + h
+ my_items
[i
].to_i
523 return hash_cache
.as(not null)
526 redef fun substrings
do return new FlatSubstringsIter(self)
529 private class FlatStringCharReverseIterator
530 super IndexedIterator[Char]
532 var target
: FlatString
536 init with_pos
(tgt
: FlatString, pos
: Int)
541 redef fun is_ok
do return curr_pos
>= 0
543 redef fun item
do return target
[curr_pos
]
545 redef fun next
do curr_pos
-= 1
547 redef fun index
do return curr_pos
551 private class FlatStringCharIterator
552 super IndexedIterator[Char]
554 var target
: FlatString
560 init with_pos
(tgt
: FlatString, pos
: Int)
562 init(tgt
, tgt
.length
- 1, pos
)
565 redef fun is_ok
do return curr_pos
<= max
567 redef fun item
do return target
[curr_pos
]
569 redef fun next
do curr_pos
+= 1
571 redef fun index
do return curr_pos
575 private class FlatStringCharView
578 redef type SELFTYPE: FlatString
580 redef fun [](index
) do return target
[index
]
582 redef fun iterator_from
(start
) do return new FlatStringCharIterator.with_pos
(target
, start
)
584 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator.with_pos
(target
, start
)
588 private class FlatStringByteReverseIterator
589 super IndexedIterator[Byte]
591 var target
: FlatString
593 var target_items
: NativeString
597 init with_pos
(tgt
: FlatString, pos
: Int)
599 init(tgt
, tgt
._items
, pos
+ tgt
._first_byte
)
602 redef fun is_ok
do return curr_pos
>= target
._first_byte
604 redef fun item
do return target_items
[curr_pos
]
606 redef fun next
do curr_pos
-= 1
608 redef fun index
do return curr_pos
- target
._first_byte
612 private class FlatStringByteIterator
613 super IndexedIterator[Byte]
615 var target
: FlatString
617 var target_items
: NativeString
621 init with_pos
(tgt
: FlatString, pos
: Int)
623 init(tgt
, tgt
._items
, pos
+ tgt
._first_byte
)
626 redef fun is_ok
do return curr_pos
<= target
._last_byte
628 redef fun item
do return target_items
[curr_pos
]
630 redef fun next
do curr_pos
+= 1
632 redef fun index
do return curr_pos
- target
._first_byte
636 private class FlatStringByteView
639 redef type SELFTYPE: FlatString
643 # Check that the index (+ _first_byte) is not larger than _last_byte
644 # In other terms, if the index is valid
646 var target
= self.target
647 var ind
= index
+ target
._first_byte
648 assert ind
<= target
._last_byte
649 return target
._items
[ind
]
652 redef fun iterator_from
(start
) do return new FlatStringByteIterator.with_pos
(target
, start
)
654 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator.with_pos
(target
, start
)
659 redef new do return new FlatBuffer
661 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
664 # Mutable strings of characters.
669 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
671 redef var bytes
= new FlatBufferByteView(self) is lazy
675 private var char_cache
: Int = -1
677 private var byte_cache
: Int = -1
679 private var capacity
= 0
681 # Real items, used as cache for when to_cstring is called
682 private var real_items
: NativeString is noinit
684 redef fun fast_cstring
do return _items
.fast_cstring
(0)
686 redef fun substrings
do return new FlatSubstringsIter(self)
688 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
690 # This happens when an operation modifies the current `Buffer` and
691 # the Copy-On-Write flag `written` is set at true.
693 var nns
= new NativeString(capacity
)
694 if _bytelen
!= 0 then _items
.copy_to
(nns
, _bytelen
, 0, 0)
699 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
701 # Internal only, does not modify _bytelen or length, this is the caller's responsability
702 private fun rshift_bytes
(from
: Int, len
: Int) do
706 if bt
+ len
> capacity
then
707 capacity
= capacity
* 2 + 2
708 nit
= new NativeString(capacity
)
709 oit
.copy_to
(nit
, 0, 0, from
)
711 oit
.copy_to
(nit
, bt
- from
, from
, from
+ len
)
714 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
716 # Internal only, does not modify _bytelen or length, this is the caller's responsability
717 private fun lshift_bytes
(from
: Int, len
: Int) do
719 it
.copy_to
(it
, _bytelen
- from
, from
, from
- len
)
722 redef fun []=(index
, item
)
724 assert index
>= 0 and index
<= length
725 if written
then reset
727 if index
== length
then
732 var ip
= it
.char_to_byte_index
(index
)
733 var c
= it
.char_at
(ip
)
734 var clen
= c
.u8char_len
735 var itemlen
= item
.u8char_len
736 var size_diff
= itemlen
- clen
737 if size_diff
> 0 then
738 rshift_bytes
(ip
+ clen
, size_diff
)
739 else if size_diff
< 0 then
740 lshift_bytes
(ip
+ clen
, -size_diff
)
742 _bytelen
+= size_diff
744 it
.set_char_at
(ip
, item
)
749 if written
then reset
751 var clen
= c
.u8char_len
754 _items
.set_char_at
(bt
, c
)
761 if written
then reset
766 redef fun empty
do return new Buffer
768 redef fun enlarge
(cap
)
771 if cap
<= c
then return
772 while c
<= cap
do c
= c
* 2 + 2
773 # The COW flag can be set at false here, since
774 # it does a copy of the current `Buffer`
777 var a
= new NativeString(c
+1)
780 if bln
> 0 then it
.copy_to
(a
, bln
, 0, 0)
790 if bln
== 0 then _items
= new NativeString(1)
791 return new FlatString.full
(_items
, bln
, 0, bln
- 1, length
)
798 var new_native
= new NativeString(bln
+ 1)
799 new_native
[bln
] = 0u8
800 if length
> 0 then _items
.copy_to
(new_native
, bln
, 0, 0)
801 real_items
= new_native
807 # Create a new empty string.
810 # Low-level creation a new buffer with given data.
812 # `_items` will be used as is, without copy, to store the characters of the buffer.
813 # Aliasing issues is the responsibility of the caller.
815 # If `_items` is shared, `written` should be set to true after the creation
816 # so that a modification will do a copy-on-write.
817 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
820 self.capacity
= capacity
821 self._bytelen
= bytelen
825 # Create a new string copied from `s`.
828 _items
= new NativeString(s
.bytelen
)
829 if s
isa FlatText then
832 for i
in substrings
do i
.as(FlatString)._items
.copy_to
(_items
, i
._bytelen
, 0, 0)
840 # Create a new empty string with a given capacity.
841 init with_capacity
(cap
: Int)
844 _items
= new NativeString(cap
+ 1)
851 if s
.is_empty
then return
854 var nln
= _bytelen
+ sl
856 if s
isa FlatText then
857 s
._items
.copy_to
(_items
, sl
, s
.first_byte
, _bytelen
)
859 for i
in s
.substrings
do append i
866 # Copies the content of self in `dest`
867 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
869 var self_chars
= self.chars
870 var dest_chars
= dest
.chars
871 for i
in [0..len-1
] do
872 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
876 redef fun substring
(from
, count
)
879 if from
< 0 then from
= 0
880 if (from
+ count
) > length
then count
= length
- from
883 var bytefrom
= its
.char_to_byte_index
(from
)
884 var byteto
= its
.char_to_byte_index
(count
+ from
- 1)
885 byteto
+= its
.char_at
(byteto
).u8char_len
- 1
886 var byte_length
= byteto
- bytefrom
+ 1
887 var r_items
= new NativeString(byte_length
)
888 its
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
889 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
898 var ns
= new FlatBuffer.with_capacity
(capacity
)
899 for i
in chars
.reverse_iterator
do ns
.add i
903 redef fun times
(repeats
)
906 var x
= new FlatString.full
(_items
, bln
, 0, bln
- 1, length
)
907 for i
in [1 .. repeats
[ do
914 if written
then reset
915 for i
in [0 .. length
[ do self[i
] = self[i
].to_upper
920 if written
then reset
921 for i
in [0 .. length
[ do self[i
] = self[i
].to_lower
925 private class FlatBufferByteReverseIterator
926 super IndexedIterator[Byte]
928 var target
: FlatBuffer
930 var target_items
: NativeString
934 init with_pos
(tgt
: FlatBuffer, pos
: Int)
936 init(tgt
, tgt
._items
, pos
)
939 redef fun index
do return curr_pos
941 redef fun is_ok
do return curr_pos
>= 0
943 redef fun item
do return target_items
[curr_pos
]
945 redef fun next
do curr_pos
-= 1
949 private class FlatBufferByteView
952 redef type SELFTYPE: FlatBuffer
954 redef fun [](index
) do return target
._items
[index
]
956 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator.with_pos
(target
, pos
)
958 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator.with_pos
(target
, pos
)
962 private class FlatBufferByteIterator
963 super IndexedIterator[Byte]
965 var target
: FlatBuffer
967 var target_items
: NativeString
971 init with_pos
(tgt
: FlatBuffer, pos
: Int)
973 init(tgt
, tgt
._items
, pos
)
976 redef fun index
do return curr_pos
978 redef fun is_ok
do return curr_pos
< target
._bytelen
980 redef fun item
do return target_items
[curr_pos
]
982 redef fun next
do curr_pos
+= 1
986 private class FlatBufferCharReverseIterator
987 super IndexedIterator[Char]
989 var target
: FlatBuffer
993 init with_pos
(tgt
: FlatBuffer, pos
: Int)
998 redef fun index
do return curr_pos
1000 redef fun is_ok
do return curr_pos
>= 0
1002 redef fun item
do return target
[curr_pos
]
1004 redef fun next
do curr_pos
-= 1
1008 private class FlatBufferCharView
1009 super BufferCharView
1011 redef type SELFTYPE: FlatBuffer
1013 redef fun [](index
) do return target
[index
]
1015 redef fun []=(index
, item
)
1017 assert index
>= 0 and index
<= length
1018 if index
== length
then
1022 target
[index
] = item
1035 fun enlarge
(cap
: Int)
1042 var s_length
= s
.length
1043 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
1044 for i
in s
do target
.add i
1047 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator.with_pos
(target
, pos
)
1049 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator.with_pos
(target
, pos
)
1053 private class FlatBufferCharIterator
1054 super IndexedIterator[Char]
1056 var target
: FlatBuffer
1062 init with_pos
(tgt
: FlatBuffer, pos
: Int)
1064 init(tgt
, tgt
.length
- 1, pos
)
1067 redef fun index
do return curr_pos
1069 redef fun is_ok
do return curr_pos
<= max
1071 redef fun item
do return target
[curr_pos
]
1073 redef fun next
do curr_pos
+= 1
1077 redef class NativeString
1080 return to_s_with_length
(cstring_length
)
1083 # Returns `self` as a String of `length`.
1084 redef fun to_s_with_length
(length
): FlatString
1087 return clean_utf8
(length
)
1090 redef fun to_s_full
(bytelen
, unilen
) do
1091 return new FlatString.full
(self, bytelen
, 0, bytelen
- 1, unilen
)
1094 # Returns `self` as a new String.
1095 redef fun to_s_with_copy
: FlatString
1097 var length
= cstring_length
1098 var r
= clean_utf8
(length
)
1099 if r
.items
!= self then return r
1100 var new_self
= new NativeString(length
+ 1)
1101 copy_to
(new_self
, length
, 0, 0)
1102 var str
= new FlatString.with_infos
(new_self
, length
, 0, length
- 1)
1103 new_self
[length
] = 0u8
1104 str
.to_cstring
= new_self
1108 # Cleans a NativeString if necessary
1109 fun clean_utf8
(len
: Int): FlatString do
1110 var replacements
: nullable Array[Int] = null
1111 var end_length
= len
1116 var nxst
= length_of_char_at
(pos
)
1119 ok_st
= b
& 0x80u
8 == 0u8
1120 else if nxst
== 2 then
1121 ok_st
= b
& 0xE0u
8 == 0xC0u
8
1122 else if nxst
== 3 then
1123 ok_st
= b
& 0xF0u
8 == 0xE0u
8
1125 ok_st
= b
& 0xF8u
8 == 0xF0u
8
1128 if replacements
== null then replacements
= new Array[Int]
1129 replacements
.add pos
1136 var c
= char_at
(pos
)
1137 var cp
= c
.code_point
1139 ok_c
= cp
>= 0 and cp
<= 0x7F
1140 else if nxst
== 2 then
1141 ok_c
= cp
>= 0x80 and cp
<= 0x7FF
1142 else if nxst
== 3 then
1143 ok_c
= cp
>= 0x800 and cp
<= 0xFFFF
1144 ok_c
= ok_c
and not (cp
>= 0xD800 and cp
<= 0xDFFF) and cp
!= 0xFFFE and cp
!= 0xFFFF
1146 ok_c
= cp
>= 0x10000 and cp
<= 0x10FFFF
1149 if replacements
== null then replacements
= new Array[Int]
1150 replacements
.add pos
1160 if end_length
!= len
then
1161 ret
= new NativeString(end_length
)
1164 var repls
= replacements
.as(not null)
1165 var r
= repls
.items
.as(not null)
1166 var imax
= repls
.length
1167 for i
in [0 .. imax
[ do
1169 var chkln
= repl_pos
- old_repl
1170 copy_to
(ret
, chkln
, old_repl
, off
)
1173 ret
[off
+ 1] = 0xBFu
8
1174 ret
[off
+ 2] = 0xBDu
8
1175 old_repl
= repl_pos
+ 1
1178 copy_to
(ret
, len
- old_repl
, old_repl
, off
)
1180 return new FlatString.full
(ret
, end_length
, 0, end_length
- 1, chr_ln
)
1183 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
1185 # Very unsafe, make sure to have room for this char prior to calling this function.
1186 private fun set_char_at
(pos
: Int, c
: Char) do
1187 var ln
= c
.u8char_len
1188 native_set_char
(pos
, c
, ln
)
1191 private fun native_set_char
(pos
: Int, c
: Char, ln
: Int) `{
1192 char* dst = self + pos;
1198 dst[0] = 0xC0 | ((c & 0x7C0) >> 6);
1199 dst[1] = 0x80 | (c & 0x3F);
1202 dst[0] = 0xE0 | ((c & 0xF000) >> 12);
1203 dst[1] = 0x80 | ((c & 0xFC0) >> 6);
1204 dst[2] = 0x80 | (c & 0x3F);
1207 dst[0] = 0xF0 | ((c & 0x1C0000) >> 18);
1208 dst[1] = 0x80 | ((c & 0x3F000) >> 12);
1209 dst[2] = 0x80 | ((c & 0xFC0) >> 6);
1210 dst[3] = 0x80 | (c & 0x3F);
1217 redef fun to_base
(base
, signed
)
1219 var l
= digit_count
(base
)
1220 var s
= new FlatBuffer.from
(" " * l
)
1221 fill_buffer
(s
, base
, signed
)
1225 # return displayable int in base 10 and signed
1227 # assert 1.to_s == "1"
1228 # assert (-123).to_s == "-123"
1230 # Fast case for common numbers
1231 if self == 0 then return "0"
1232 if self == 1 then return "1"
1234 var nslen
= int_to_s_len
1235 var ns
= new NativeString(nslen
+ 1)
1237 native_int_to_s
(ns
, nslen
+ 1)
1238 return new FlatString.full
(ns
, nslen
, 0, nslen
- 1, nslen
)
1242 redef class Array[E
]
1244 # Fast implementation
1245 redef fun plain_to_s
1248 if l
== 0 then return ""
1249 var its
= _items
.as(not null)
1251 if l
== 1 then if first
== null then return "" else return first
.to_s
1252 var na
= new NativeArray[String](l
)
1258 if itsi
== null then
1268 var ns
= new NativeString(sl
+ 1)
1274 if tmp
isa FlatString then
1275 var tpl
= tmp
._bytelen
1276 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1279 for j
in tmp
.substrings
do
1280 var s
= j
.as(FlatString)
1281 var slen
= s
._bytelen
1282 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1288 return new FlatString.with_infos
(ns
, sl
, 0, sl
- 1)
1292 redef class NativeArray[E
]
1293 redef fun native_to_s
do
1294 assert self isa NativeArray[String]
1305 var ns
= new NativeString(sl
+ 1)
1311 if tmp
isa FlatString then
1312 var tpl
= tmp
._bytelen
1313 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1316 for j
in tmp
.substrings
do
1317 var s
= j
.as(FlatString)
1318 var slen
= s
._bytelen
1319 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1325 return new FlatString.with_infos
(ns
, sl
, 0, sl
- 1)
1329 redef class Map[K
,V
]
1330 redef fun join
(sep
, couple_sep
)
1332 if is_empty
then return ""
1334 var s
= new Buffer # Result
1340 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1342 # Concat other _items
1348 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")