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 _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
58 # Find best insertion point
59 var delta_begin
= index
60 var delta_end
= (ln
- 1) - index
61 var delta_cache
= (pos
- index
).abs
65 if delta_cache
< min
then min
= delta_cache
66 if delta_end
< min
then min
= delta_end
71 if min
== delta_begin
then
74 else if min
== delta_cache
then
78 ns_i
= its
.find_beginning_of_char_at
(last_byte
)
82 ns_i
= its
.char_to_byte_index_cached
(index
, my_i
, ns_i
)
90 # By escaping `self` to HTML, how many more bytes will be needed ?
91 fun chars_to_html_escape
: Int do
100 else if c
== 0x3Eu
8 then
102 else if c
== 0x26u
8 then
104 else if c
== 0x22u
8 then
106 else if c
== 0x27u
8 then
108 else if c
== 0x2Fu
8 then
116 redef fun html_escape
118 var extra
= chars_to_html_escape
119 if extra
== 0 then return to_s
123 var nlen
= extra
+ _bytelen
124 var nits
= new NativeString(nlen
)
129 # Some HTML characters are used as meta-data, they need
130 # to be replaced by an HTML-Escaped equivalent
134 # * 0x26 (&) => &
135 # * 0x22 (") => "
136 # * 0x27 (') => '
137 # * 0x2F (/) => /
139 nits
[outpos
] = 0x26u
8
140 nits
[outpos
+ 1] = 0x6Cu
8
141 nits
[outpos
+ 2] = 0x74u
8
142 nits
[outpos
+ 3] = 0x3Bu
8
144 else if c
== 0x3Eu
8 then
145 nits
[outpos
] = 0x26u
8
146 nits
[outpos
+ 1] = 0x67u
8
147 nits
[outpos
+ 2] = 0x74u
8
148 nits
[outpos
+ 3] = 0x3Bu
8
150 else if c
== 0x26u
8 then
151 nits
[outpos
] = 0x26u
8
152 nits
[outpos
+ 1] = 0x61u
8
153 nits
[outpos
+ 2] = 0x6Du
8
154 nits
[outpos
+ 3] = 0x70u
8
155 nits
[outpos
+ 4] = 0x3Bu
8
157 else if c
== 0x22u
8 then
158 nits
[outpos
] = 0x26u
8
159 nits
[outpos
+ 1] = 0x23u
8
160 nits
[outpos
+ 2] = 0x33u
8
161 nits
[outpos
+ 3] = 0x34u
8
162 nits
[outpos
+ 4] = 0x3Bu
8
164 else if c
== 0x27u
8 then
165 nits
[outpos
] = 0x26u
8
166 nits
[outpos
+ 1] = 0x23u
8
167 nits
[outpos
+ 2] = 0x33u
8
168 nits
[outpos
+ 3] = 0x39u
8
169 nits
[outpos
+ 4] = 0x3Bu
8
171 else if c
== 0x2Fu
8 then
172 nits
[outpos
] = 0x26u
8
173 nits
[outpos
+ 1] = 0x23u
8
174 nits
[outpos
+ 2] = 0x34u
8
175 nits
[outpos
+ 3] = 0x37u
8
176 nits
[outpos
+ 4] = 0x3Bu
8
184 var s
= new FlatString.with_infos
(nits
, nlen
, 0, nlen
- 1)
188 # By escaping `self` to C, how many more bytes will be needed ?
190 # This enables a double-optimization in `escape_to_c` since if this
191 # method returns 0, then `self` does not need escaping and can be
193 fun chars_to_escape_to_c
: Int do
202 else if c
== 0x09u
8 then
204 else if c
== 0x22u
8 then
206 else if c
== 0x27u
8 then
208 else if c
== 0x5Cu
8 then
210 else if c
< 32u8
then
218 redef fun escape_to_c
do
219 var ln_extra
= chars_to_escape_to_c
220 if ln_extra
== 0 then return self.to_s
223 var nlen
= _bytelen
+ ln_extra
224 var nns
= new NativeString(nlen
)
231 # Any byte with value < 32 is a control character
232 # All their uses will be replaced by their octal
235 # There are two exceptions however:
240 # Aside from the code points above, the following are:
247 nns
[opos
+ 1] = 0x74u
8
249 else if c
== 0x0Au
8 then
251 nns
[opos
+ 1] = 0x6Eu
8
253 else if c
== 0x22u
8 then
255 nns
[opos
+ 1] = 0x22u
8
257 else if c
== 0x27u
8 then
259 nns
[opos
+ 1] = 0x27u
8
261 else if c
== 0x5Cu
8 then
263 nns
[opos
+ 1] = 0x5Cu
8
265 else if c
< 32u8
then
267 nns
[opos
+ 1] = 0x30u
8
268 nns
[opos
+ 2] = ((c
& 0x38u
8) >> 3) + 0x30u
8
269 nns
[opos
+ 3] = (c
& 0x07u
8) + 0x30u
8
277 return nns
.to_s_with_length
(nlen
)
280 redef fun [](index
) do return _items
.char_at
(char_to_byte_index
(index
))
282 # If `self` contains only digits and alpha <= 'f', return the corresponding integer.
284 # assert "ff".to_hex == 255
285 redef fun to_hex
(pos
, ln
) do
287 if pos
== null then pos
= 0
288 if ln
== null then ln
= length
- pos
289 pos
= char_to_byte_index
(pos
)
292 for i
in [pos
.. max
[ do
294 res
+= its
[i
].ascii
.from_hex
300 # Immutable strings of characters.
305 # Index at which `self` begins in `_items`, inclusively
306 redef var first_byte
is noinit
308 # Index at which `self` ends in `_items`, inclusively
309 redef var last_byte
is noinit
311 redef var chars
= new FlatStringCharView(self) is lazy
313 redef var bytes
= new FlatStringByteView(self) is lazy
315 redef var length
is lazy
do
316 if _bytelen
== 0 then return 0
317 return _items
.utf8_length
(_first_byte
, _last_byte
)
320 redef var to_cstring
is lazy
do
322 var new_items
= new NativeString(blen
+ 1)
323 _items
.copy_to
(new_items
, blen
, _first_byte
, 0)
324 new_items
[blen
] = 0u8
330 var b
= new FlatBuffer.with_capacity
(_bytelen
+ 1)
331 for i
in [length
- 1 .. 0].step
(-1) do
334 var s
= b
.to_s
.as(FlatString)
335 s
.length
= self.length
339 redef fun fast_cstring
do return _items
.fast_cstring
(_first_byte
)
341 redef fun substring
(from
, count
)
347 if count
< 0 then count
= 0
351 if (count
+ from
) > length
then count
= length
- from
352 if count
<= 0 then return ""
353 var end_index
= from
+ count
- 1
355 var bytefrom
= char_to_byte_index
(from
)
356 var byteto
= char_to_byte_index
(end_index
)
358 byteto
+= its
.length_of_char_at
(byteto
) - 1
360 var s
= new FlatString.full
(its
, byteto
- bytefrom
+ 1, bytefrom
, byteto
, count
)
364 redef fun empty
do return "".as(FlatString)
368 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
374 outstr
.add
(chars
[pos
].to_upper
)
383 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
389 outstr
.add
(chars
[pos
].to_lower
)
398 for i
in chars
do i
.output
401 ##################################################
402 # String Specific Methods #
403 ##################################################
405 # Low-level creation of a new string with minimal data.
407 # `_items` will be used as is, without copy, to retrieve the characters of the string.
408 # Aliasing issues is the responsibility of the caller.
409 private init with_infos
(items
: NativeString, bytelen
, from
, to
: Int)
412 self._bytelen
= bytelen
418 # Low-level creation of a new string with all the data.
420 # `_items` will be used as is, without copy, to retrieve the characters of the string.
421 # Aliasing issues is the responsibility of the caller.
422 private init full
(items
: NativeString, bytelen
, from
, to
, length
: Int)
426 self._bytelen
= bytelen
434 if not other
isa FlatText then return super
436 if self.object_id
== other
.object_id
then return true
438 var my_length
= _bytelen
440 if other
._bytelen
!= my_length
then return false
442 var my_index
= _first_byte
443 var its_index
= other
.first_byte
445 var last_iteration
= my_index
+ my_length
447 var its_items
= other
._items
448 var my_items
= self._items
450 while my_index
< last_iteration
do
451 if my_items
[my_index
] != its_items
[its_index
] then return false
461 if not other
isa FlatText then return super
463 if self.object_id
== other
.object_id
then return false
466 var itsits
= other
._items
469 var obt
= other
.bytelen
471 var minln
= if mbt
< obt
then mbt
else obt
472 var mst
= _first_byte
473 var ost
= other
.first_byte
475 for i
in [0 .. minln
[ do
476 var my_curr_char
= myits
[mst
]
477 var its_curr_char
= itsits
[ost
]
479 if my_curr_char
> its_curr_char
then return false
480 if my_curr_char
< its_curr_char
then return true
493 var nlen
= mlen
+ slen
495 var mifrom
= _first_byte
496 if s
isa FlatText then
498 var sifrom
= s
.first_byte
499 var ns
= new NativeString(nlen
+ 1)
500 mits
.copy_to
(ns
, mlen
, mifrom
, 0)
501 sits
.copy_to
(ns
, slen
, sifrom
, mlen
)
502 return new FlatString.full
(ns
, nlen
, 0, nlen
- 1, length
+ o
.length
)
509 var mybtlen
= _bytelen
510 var new_bytelen
= mybtlen
* i
512 var newlen
= mylen
* i
515 var ns
= new NativeString(new_bytelen
+ 1)
516 ns
[new_bytelen
] = 0u8
519 its
.copy_to
(ns
, mybtlen
, fb
, offset
)
523 return new FlatString.full
(ns
, new_bytelen
, 0, new_bytelen
- 1, newlen
)
529 if hash_cache
== null then
530 # djb2 hash algorithm
534 var my_items
= _items
538 h
= (h
<< 5) + h
+ my_items
[i
].to_i
545 return hash_cache
.as(not null)
548 redef fun substrings
do return new FlatSubstringsIter(self)
551 private class FlatStringCharReverseIterator
552 super IndexedIterator[Char]
554 var target
: FlatString
558 redef fun is_ok
do return curr_pos
>= 0
560 redef fun item
do return target
[curr_pos
]
562 redef fun next
do curr_pos
-= 1
564 redef fun index
do return curr_pos
568 private class FlatStringCharIterator
569 super IndexedIterator[Char]
571 var target
: FlatString
573 var max
: Int is noautoinit
577 init do max
= target
.length
- 1
579 redef fun is_ok
do return curr_pos
<= max
581 redef fun item
do return target
[curr_pos
]
583 redef fun next
do curr_pos
+= 1
585 redef fun index
do return curr_pos
589 private class FlatStringCharView
592 redef type SELFTYPE: FlatString
594 redef fun [](index
) do return target
[index
]
596 redef fun iterator_from
(start
) do return new FlatStringCharIterator(target
, start
)
598 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator(target
, start
)
602 private class FlatStringByteReverseIterator
603 super IndexedIterator[Byte]
605 var target
: FlatString
607 var target_items
: NativeString is noautoinit
614 target_items
= tgt
._items
615 curr_pos
+= tgt
._first_byte
618 redef fun is_ok
do return curr_pos
>= target
._first_byte
620 redef fun item
do return target_items
[curr_pos
]
622 redef fun next
do curr_pos
-= 1
624 redef fun index
do return curr_pos
- target
._first_byte
628 private class FlatStringByteIterator
629 super IndexedIterator[Byte]
631 var target
: FlatString
633 var target_items
: NativeString is noautoinit
640 target_items
= tgt
._items
641 curr_pos
+= tgt
._first_byte
644 redef fun is_ok
do return curr_pos
<= target
._last_byte
646 redef fun item
do return target_items
[curr_pos
]
648 redef fun next
do curr_pos
+= 1
650 redef fun index
do return curr_pos
- target
._first_byte
654 private class FlatStringByteView
657 redef type SELFTYPE: FlatString
661 # Check that the index (+ _first_byte) is not larger than _last_byte
662 # In other terms, if the index is valid
664 var target
= self.target
665 var ind
= index
+ target
._first_byte
666 assert ind
<= target
._last_byte
667 return target
._items
[ind
]
670 redef fun iterator_from
(start
) do return new FlatStringByteIterator(target
, start
)
672 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator(target
, start
)
677 redef new do return new FlatBuffer
679 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
682 # Mutable strings of characters.
687 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
689 redef var bytes
= new FlatBufferByteView(self) is lazy
693 private var char_cache
: Int = -1
695 private var byte_cache
: Int = -1
697 private var capacity
= 0
699 # Real items, used as cache for when to_cstring is called
700 private var real_items
: NativeString is noinit
702 redef fun fast_cstring
do return _items
.fast_cstring
(0)
704 redef fun substrings
do return new FlatSubstringsIter(self)
706 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
708 # This happens when an operation modifies the current `Buffer` and
709 # the Copy-On-Write flag `written` is set at true.
711 var nns
= new NativeString(capacity
)
712 if _bytelen
!= 0 then _items
.copy_to
(nns
, _bytelen
, 0, 0)
717 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
719 # Internal only, does not modify _bytelen or length, this is the caller's responsability
720 private fun rshift_bytes
(from
: Int, len
: Int) do
724 if bt
+ len
> capacity
then
725 capacity
= capacity
* 2 + 2
726 nit
= new NativeString(capacity
)
727 oit
.copy_to
(nit
, 0, 0, from
)
729 oit
.copy_to
(nit
, bt
- from
, from
, from
+ len
)
732 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
734 # Internal only, does not modify _bytelen or length, this is the caller's responsability
735 private fun lshift_bytes
(from
: Int, len
: Int) do
737 it
.copy_to
(it
, _bytelen
- from
, from
, from
- len
)
740 redef fun []=(index
, item
)
742 assert index
>= 0 and index
<= length
743 if written
then reset
745 if index
== length
then
750 var ip
= it
.char_to_byte_index
(index
)
751 var c
= it
.char_at
(ip
)
752 var clen
= c
.u8char_len
753 var itemlen
= item
.u8char_len
754 var size_diff
= itemlen
- clen
755 if size_diff
> 0 then
756 rshift_bytes
(ip
+ clen
, size_diff
)
757 else if size_diff
< 0 then
758 lshift_bytes
(ip
+ clen
, -size_diff
)
760 _bytelen
+= size_diff
762 it
.set_char_at
(ip
, item
)
767 if written
then reset
769 var clen
= c
.u8char_len
772 _items
.set_char_at
(bt
, c
)
779 if written
then reset
784 redef fun empty
do return new Buffer
786 redef fun enlarge
(cap
)
789 if cap
<= c
then return
790 while c
<= cap
do c
= c
* 2 + 2
791 # The COW flag can be set at false here, since
792 # it does a copy of the current `Buffer`
795 var a
= new NativeString(c
+1)
798 if bln
> 0 then it
.copy_to
(a
, bln
, 0, 0)
808 if bln
== 0 then _items
= new NativeString(1)
809 return new FlatString.full
(_items
, bln
, 0, bln
- 1, length
)
816 var new_native
= new NativeString(bln
+ 1)
817 new_native
[bln
] = 0u8
818 if length
> 0 then _items
.copy_to
(new_native
, bln
, 0, 0)
819 real_items
= new_native
825 # Create a new empty string.
828 # Low-level creation a new buffer with given data.
830 # `_items` will be used as is, without copy, to store the characters of the buffer.
831 # Aliasing issues is the responsibility of the caller.
833 # If `_items` is shared, `written` should be set to true after the creation
834 # so that a modification will do a copy-on-write.
835 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
838 self.capacity
= capacity
839 self._bytelen
= bytelen
843 # Create a new string copied from `s`.
846 _items
= new NativeString(s
.bytelen
)
847 if s
isa FlatText then
850 for i
in substrings
do i
.as(FlatString)._items
.copy_to
(_items
, i
._bytelen
, 0, 0)
858 # Create a new empty string with a given capacity.
859 init with_capacity
(cap
: Int)
862 _items
= new NativeString(cap
+ 1)
869 if s
.is_empty
then return
872 var nln
= _bytelen
+ sl
874 if s
isa FlatText then
875 s
._items
.copy_to
(_items
, sl
, s
.first_byte
, _bytelen
)
877 for i
in s
.substrings
do append i
884 # Copies the content of self in `dest`
885 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
887 var self_chars
= self.chars
888 var dest_chars
= dest
.chars
889 for i
in [0..len-1
] do
890 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
894 redef fun substring
(from
, count
)
897 if from
< 0 then from
= 0
898 if (from
+ count
) > length
then count
= length
- from
899 if count
<= 0 then return new Buffer
901 var bytefrom
= its
.char_to_byte_index
(from
)
902 var byteto
= its
.char_to_byte_index
(count
+ from
- 1)
903 byteto
+= its
.char_at
(byteto
).u8char_len
- 1
904 var byte_length
= byteto
- bytefrom
+ 1
905 var r_items
= new NativeString(byte_length
)
906 its
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
907 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
913 var ns
= new FlatBuffer.with_capacity
(capacity
)
914 for i
in chars
.reverse_iterator
do ns
.add i
918 redef fun times
(repeats
)
921 var x
= new FlatString.full
(_items
, bln
, 0, bln
- 1, length
)
922 for i
in [1 .. repeats
[ do
929 if written
then reset
930 for i
in [0 .. length
[ do self[i
] = self[i
].to_upper
935 if written
then reset
936 for i
in [0 .. length
[ do self[i
] = self[i
].to_lower
940 private class FlatBufferByteReverseIterator
941 super IndexedIterator[Byte]
943 var target
: FlatBuffer
945 var target_items
: NativeString is noautoinit
949 init do target_items
= target
._items
951 redef fun index
do return curr_pos
953 redef fun is_ok
do return curr_pos
>= 0
955 redef fun item
do return target_items
[curr_pos
]
957 redef fun next
do curr_pos
-= 1
961 private class FlatBufferByteView
964 redef type SELFTYPE: FlatBuffer
966 redef fun [](index
) do return target
._items
[index
]
968 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator(target
, pos
)
970 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator(target
, pos
)
974 private class FlatBufferByteIterator
975 super IndexedIterator[Byte]
977 var target
: FlatBuffer
979 var target_items
: NativeString is noautoinit
983 init do target_items
= target
._items
985 redef fun index
do return curr_pos
987 redef fun is_ok
do return curr_pos
< target
._bytelen
989 redef fun item
do return target_items
[curr_pos
]
991 redef fun next
do curr_pos
+= 1
995 private class FlatBufferCharReverseIterator
996 super IndexedIterator[Char]
998 var target
: FlatBuffer
1002 redef fun index
do return curr_pos
1004 redef fun is_ok
do return curr_pos
>= 0
1006 redef fun item
do return target
[curr_pos
]
1008 redef fun next
do curr_pos
-= 1
1012 private class FlatBufferCharView
1013 super BufferCharView
1015 redef type SELFTYPE: FlatBuffer
1017 redef fun [](index
) do return target
[index
]
1019 redef fun []=(index
, item
)
1021 assert index
>= 0 and index
<= length
1022 if index
== length
then
1026 target
[index
] = item
1039 fun enlarge
(cap
: Int)
1046 var s_length
= s
.length
1047 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
1048 for i
in s
do target
.add i
1051 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator(target
, pos
)
1053 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator(target
, pos
)
1057 private class FlatBufferCharIterator
1058 super IndexedIterator[Char]
1060 var target
: FlatBuffer
1062 var max
: Int is noautoinit
1066 init do max
= target
.length
- 1
1068 redef fun index
do return curr_pos
1070 redef fun is_ok
do return curr_pos
<= max
1072 redef fun item
do return target
[curr_pos
]
1074 redef fun next
do curr_pos
+= 1
1078 redef class NativeString
1081 return to_s_with_length
(cstring_length
)
1084 # Returns `self` as a String of `length`.
1085 redef fun to_s_with_length
(length
): FlatString
1088 return clean_utf8
(length
)
1091 redef fun to_s_full
(bytelen
, unilen
) do
1092 return new FlatString.full
(self, bytelen
, 0, bytelen
- 1, unilen
)
1095 # Returns `self` as a new String.
1096 redef fun to_s_with_copy
: FlatString
1098 var length
= cstring_length
1099 var r
= clean_utf8
(length
)
1100 if r
.items
!= self then return r
1101 var new_self
= new NativeString(length
+ 1)
1102 copy_to
(new_self
, length
, 0, 0)
1103 var str
= new FlatString.with_infos
(new_self
, length
, 0, length
- 1)
1104 new_self
[length
] = 0u8
1105 str
.to_cstring
= new_self
1109 # Cleans a NativeString if necessary
1110 fun clean_utf8
(len
: Int): FlatString do
1111 var replacements
: nullable Array[Int] = null
1112 var end_length
= len
1117 var nxst
= length_of_char_at
(pos
)
1120 ok_st
= b
& 0x80u
8 == 0u8
1121 else if nxst
== 2 then
1122 ok_st
= b
& 0xE0u
8 == 0xC0u
8
1123 else if nxst
== 3 then
1124 ok_st
= b
& 0xF0u
8 == 0xE0u
8
1126 ok_st
= b
& 0xF8u
8 == 0xF0u
8
1129 if replacements
== null then replacements
= new Array[Int]
1130 replacements
.add pos
1137 var c
= char_at
(pos
)
1138 var cp
= c
.code_point
1140 ok_c
= cp
>= 0 and cp
<= 0x7F
1141 else if nxst
== 2 then
1142 ok_c
= cp
>= 0x80 and cp
<= 0x7FF
1143 else if nxst
== 3 then
1144 ok_c
= cp
>= 0x800 and cp
<= 0xFFFF
1145 ok_c
= ok_c
and not (cp
>= 0xD800 and cp
<= 0xDFFF) and cp
!= 0xFFFE and cp
!= 0xFFFF
1147 ok_c
= cp
>= 0x10000 and cp
<= 0x10FFFF
1150 if replacements
== null then replacements
= new Array[Int]
1151 replacements
.add pos
1161 if end_length
!= len
then
1162 ret
= new NativeString(end_length
)
1165 var repls
= replacements
.as(not null)
1166 var r
= repls
.items
.as(not null)
1167 var imax
= repls
.length
1168 for i
in [0 .. imax
[ do
1170 var chkln
= repl_pos
- old_repl
1171 copy_to
(ret
, chkln
, old_repl
, off
)
1174 ret
[off
+ 1] = 0xBFu
8
1175 ret
[off
+ 2] = 0xBDu
8
1176 old_repl
= repl_pos
+ 1
1179 copy_to
(ret
, len
- old_repl
, old_repl
, off
)
1181 return new FlatString.full
(ret
, end_length
, 0, end_length
- 1, chr_ln
)
1184 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
1186 # Very unsafe, make sure to have room for this char prior to calling this function.
1187 private fun set_char_at
(pos
: Int, c
: Char) do
1188 var ln
= c
.u8char_len
1189 native_set_char
(pos
, c
, ln
)
1192 private fun native_set_char
(pos
: Int, c
: Char, ln
: Int) `{
1193 char* dst = self + pos;
1199 dst[0] = 0xC0 | ((c & 0x7C0) >> 6);
1200 dst[1] = 0x80 | (c & 0x3F);
1203 dst[0] = 0xE0 | ((c & 0xF000) >> 12);
1204 dst[1] = 0x80 | ((c & 0xFC0) >> 6);
1205 dst[2] = 0x80 | (c & 0x3F);
1208 dst[0] = 0xF0 | ((c & 0x1C0000) >> 18);
1209 dst[1] = 0x80 | ((c & 0x3F000) >> 12);
1210 dst[2] = 0x80 | ((c & 0xFC0) >> 6);
1211 dst[3] = 0x80 | (c & 0x3F);
1218 redef fun to_base
(base
, signed
)
1220 var l
= digit_count
(base
)
1221 var s
= new FlatBuffer.from
(" " * l
)
1222 fill_buffer
(s
, base
, signed
)
1226 # return displayable int in base 10 and signed
1228 # assert 1.to_s == "1"
1229 # assert (-123).to_s == "-123"
1231 # Fast case for common numbers
1232 if self == 0 then return "0"
1233 if self == 1 then return "1"
1235 var nslen
= int_to_s_len
1236 var ns
= new NativeString(nslen
+ 1)
1238 native_int_to_s
(ns
, nslen
+ 1)
1239 return new FlatString.full
(ns
, nslen
, 0, nslen
- 1, nslen
)
1243 redef class Array[E
]
1245 # Fast implementation
1246 redef fun plain_to_s
1249 if l
== 0 then return ""
1250 var its
= _items
.as(not null)
1252 if l
== 1 then if first
== null then return "" else return first
.to_s
1253 var na
= new NativeArray[String](l
)
1259 if itsi
== null then
1269 var ns
= new NativeString(sl
+ 1)
1275 if tmp
isa FlatString then
1276 var tpl
= tmp
._bytelen
1277 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1280 for j
in tmp
.substrings
do
1281 var s
= j
.as(FlatString)
1282 var slen
= s
._bytelen
1283 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1289 return new FlatString.with_infos
(ns
, sl
, 0, sl
- 1)
1293 redef class NativeArray[E
]
1294 redef fun native_to_s
do
1295 assert self isa NativeArray[String]
1306 var ns
= new NativeString(sl
+ 1)
1312 if tmp
isa FlatString then
1313 var tpl
= tmp
._bytelen
1314 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1317 for j
in tmp
.substrings
do
1318 var s
= j
.as(FlatString)
1319 var slen
= s
._bytelen
1320 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1326 return new FlatString.with_infos
(ns
, sl
, 0, sl
- 1)
1330 redef class Map[K
,V
]
1331 redef fun join
(sep
, couple_sep
)
1333 if is_empty
then return ""
1335 var s
= new Buffer # Result
1341 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1343 # Concat other _items
1349 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")