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
881 if count
<= 0 then return new Buffer
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
)
895 var ns
= new FlatBuffer.with_capacity
(capacity
)
896 for i
in chars
.reverse_iterator
do ns
.add i
900 redef fun times
(repeats
)
903 var x
= new FlatString.full
(_items
, bln
, 0, bln
- 1, length
)
904 for i
in [1 .. repeats
[ do
911 if written
then reset
912 for i
in [0 .. length
[ do self[i
] = self[i
].to_upper
917 if written
then reset
918 for i
in [0 .. length
[ do self[i
] = self[i
].to_lower
922 private class FlatBufferByteReverseIterator
923 super IndexedIterator[Byte]
925 var target
: FlatBuffer
927 var target_items
: NativeString
931 init with_pos
(tgt
: FlatBuffer, pos
: Int)
933 init(tgt
, tgt
._items
, pos
)
936 redef fun index
do return curr_pos
938 redef fun is_ok
do return curr_pos
>= 0
940 redef fun item
do return target_items
[curr_pos
]
942 redef fun next
do curr_pos
-= 1
946 private class FlatBufferByteView
949 redef type SELFTYPE: FlatBuffer
951 redef fun [](index
) do return target
._items
[index
]
953 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator.with_pos
(target
, pos
)
955 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator.with_pos
(target
, pos
)
959 private class FlatBufferByteIterator
960 super IndexedIterator[Byte]
962 var target
: FlatBuffer
964 var target_items
: NativeString
968 init with_pos
(tgt
: FlatBuffer, pos
: Int)
970 init(tgt
, tgt
._items
, pos
)
973 redef fun index
do return curr_pos
975 redef fun is_ok
do return curr_pos
< target
._bytelen
977 redef fun item
do return target_items
[curr_pos
]
979 redef fun next
do curr_pos
+= 1
983 private class FlatBufferCharReverseIterator
984 super IndexedIterator[Char]
986 var target
: FlatBuffer
990 init with_pos
(tgt
: FlatBuffer, pos
: Int)
995 redef fun index
do return curr_pos
997 redef fun is_ok
do return curr_pos
>= 0
999 redef fun item
do return target
[curr_pos
]
1001 redef fun next
do curr_pos
-= 1
1005 private class FlatBufferCharView
1006 super BufferCharView
1008 redef type SELFTYPE: FlatBuffer
1010 redef fun [](index
) do return target
[index
]
1012 redef fun []=(index
, item
)
1014 assert index
>= 0 and index
<= length
1015 if index
== length
then
1019 target
[index
] = item
1032 fun enlarge
(cap
: Int)
1039 var s_length
= s
.length
1040 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
1041 for i
in s
do target
.add i
1044 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator.with_pos
(target
, pos
)
1046 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator.with_pos
(target
, pos
)
1050 private class FlatBufferCharIterator
1051 super IndexedIterator[Char]
1053 var target
: FlatBuffer
1059 init with_pos
(tgt
: FlatBuffer, pos
: Int)
1061 init(tgt
, tgt
.length
- 1, pos
)
1064 redef fun index
do return curr_pos
1066 redef fun is_ok
do return curr_pos
<= max
1068 redef fun item
do return target
[curr_pos
]
1070 redef fun next
do curr_pos
+= 1
1074 redef class NativeString
1077 return to_s_with_length
(cstring_length
)
1080 # Returns `self` as a String of `length`.
1081 redef fun to_s_with_length
(length
): FlatString
1084 return clean_utf8
(length
)
1087 redef fun to_s_full
(bytelen
, unilen
) do
1088 return new FlatString.full
(self, bytelen
, 0, bytelen
- 1, unilen
)
1091 # Returns `self` as a new String.
1092 redef fun to_s_with_copy
: FlatString
1094 var length
= cstring_length
1095 var r
= clean_utf8
(length
)
1096 if r
.items
!= self then return r
1097 var new_self
= new NativeString(length
+ 1)
1098 copy_to
(new_self
, length
, 0, 0)
1099 var str
= new FlatString.with_infos
(new_self
, length
, 0, length
- 1)
1100 new_self
[length
] = 0u8
1101 str
.to_cstring
= new_self
1105 # Cleans a NativeString if necessary
1106 fun clean_utf8
(len
: Int): FlatString do
1107 var replacements
: nullable Array[Int] = null
1108 var end_length
= len
1113 var nxst
= length_of_char_at
(pos
)
1116 ok_st
= b
& 0x80u
8 == 0u8
1117 else if nxst
== 2 then
1118 ok_st
= b
& 0xE0u
8 == 0xC0u
8
1119 else if nxst
== 3 then
1120 ok_st
= b
& 0xF0u
8 == 0xE0u
8
1122 ok_st
= b
& 0xF8u
8 == 0xF0u
8
1125 if replacements
== null then replacements
= new Array[Int]
1126 replacements
.add pos
1133 var c
= char_at
(pos
)
1134 var cp
= c
.code_point
1136 ok_c
= cp
>= 0 and cp
<= 0x7F
1137 else if nxst
== 2 then
1138 ok_c
= cp
>= 0x80 and cp
<= 0x7FF
1139 else if nxst
== 3 then
1140 ok_c
= cp
>= 0x800 and cp
<= 0xFFFF
1141 ok_c
= ok_c
and not (cp
>= 0xD800 and cp
<= 0xDFFF) and cp
!= 0xFFFE and cp
!= 0xFFFF
1143 ok_c
= cp
>= 0x10000 and cp
<= 0x10FFFF
1146 if replacements
== null then replacements
= new Array[Int]
1147 replacements
.add pos
1157 if end_length
!= len
then
1158 ret
= new NativeString(end_length
)
1161 var repls
= replacements
.as(not null)
1162 var r
= repls
.items
.as(not null)
1163 var imax
= repls
.length
1164 for i
in [0 .. imax
[ do
1166 var chkln
= repl_pos
- old_repl
1167 copy_to
(ret
, chkln
, old_repl
, off
)
1170 ret
[off
+ 1] = 0xBFu
8
1171 ret
[off
+ 2] = 0xBDu
8
1172 old_repl
= repl_pos
+ 1
1175 copy_to
(ret
, len
- old_repl
, old_repl
, off
)
1177 return new FlatString.full
(ret
, end_length
, 0, end_length
- 1, chr_ln
)
1180 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
1182 # Very unsafe, make sure to have room for this char prior to calling this function.
1183 private fun set_char_at
(pos
: Int, c
: Char) do
1184 var ln
= c
.u8char_len
1185 native_set_char
(pos
, c
, ln
)
1188 private fun native_set_char
(pos
: Int, c
: Char, ln
: Int) `{
1189 char* dst = self + pos;
1195 dst[0] = 0xC0 | ((c & 0x7C0) >> 6);
1196 dst[1] = 0x80 | (c & 0x3F);
1199 dst[0] = 0xE0 | ((c & 0xF000) >> 12);
1200 dst[1] = 0x80 | ((c & 0xFC0) >> 6);
1201 dst[2] = 0x80 | (c & 0x3F);
1204 dst[0] = 0xF0 | ((c & 0x1C0000) >> 18);
1205 dst[1] = 0x80 | ((c & 0x3F000) >> 12);
1206 dst[2] = 0x80 | ((c & 0xFC0) >> 6);
1207 dst[3] = 0x80 | (c & 0x3F);
1214 redef fun to_base
(base
, signed
)
1216 var l
= digit_count
(base
)
1217 var s
= new FlatBuffer.from
(" " * l
)
1218 fill_buffer
(s
, base
, signed
)
1222 # return displayable int in base 10 and signed
1224 # assert 1.to_s == "1"
1225 # assert (-123).to_s == "-123"
1227 # Fast case for common numbers
1228 if self == 0 then return "0"
1229 if self == 1 then return "1"
1231 var nslen
= int_to_s_len
1232 var ns
= new NativeString(nslen
+ 1)
1234 native_int_to_s
(ns
, nslen
+ 1)
1235 return new FlatString.full
(ns
, nslen
, 0, nslen
- 1, nslen
)
1239 redef class Array[E
]
1241 # Fast implementation
1242 redef fun plain_to_s
1245 if l
== 0 then return ""
1246 var its
= _items
.as(not null)
1248 if l
== 1 then if first
== null then return "" else return first
.to_s
1249 var na
= new NativeArray[String](l
)
1255 if itsi
== null then
1265 var ns
= new NativeString(sl
+ 1)
1271 if tmp
isa FlatString then
1272 var tpl
= tmp
._bytelen
1273 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1276 for j
in tmp
.substrings
do
1277 var s
= j
.as(FlatString)
1278 var slen
= s
._bytelen
1279 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1285 return new FlatString.with_infos
(ns
, sl
, 0, sl
- 1)
1289 redef class NativeArray[E
]
1290 redef fun native_to_s
do
1291 assert self isa NativeArray[String]
1302 var ns
= new NativeString(sl
+ 1)
1308 if tmp
isa FlatString then
1309 var tpl
= tmp
._bytelen
1310 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1313 for j
in tmp
.substrings
do
1314 var s
= j
.as(FlatString)
1315 var slen
= s
._bytelen
1316 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1322 return new FlatString.with_infos
(ns
, sl
, 0, sl
- 1)
1326 redef class Map[K
,V
]
1327 redef fun join
(sep
, couple_sep
)
1329 if is_empty
then return ""
1331 var s
= new Buffer # Result
1337 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1339 # Concat other _items
1345 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")