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
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)
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 redef var chars
= new FlatStringCharView(self) is lazy
310 redef var bytes
= new FlatStringByteView(self) is lazy
312 redef var to_cstring
is lazy
do
314 var new_items
= new NativeString(blen
+ 1)
315 _items
.copy_to
(new_items
, blen
, _first_byte
, 0)
316 new_items
[blen
] = 0u8
322 var b
= new FlatBuffer.with_capacity
(_bytelen
+ 1)
323 for i
in [0 .. _length
[.step
(-1) do
326 var s
= b
.to_s
.as(FlatString)
327 s
._length
= self._length
331 redef fun fast_cstring
do return _items
.fast_cstring
(_first_byte
)
333 redef fun substring
(from
, count
)
339 if count
< 0 then count
= 0
343 if (count
+ from
) > length
then count
= length
- from
344 if count
<= 0 then return ""
345 var end_index
= from
+ count
- 1
347 var bytefrom
= char_to_byte_index
(from
)
348 var byteto
= char_to_byte_index
(end_index
)
350 byteto
+= its
.length_of_char_at
(byteto
) - 1
352 var s
= new FlatString.full
(its
, byteto
- bytefrom
+ 1, bytefrom
, byteto
, count
)
356 redef fun empty
do return "".as(FlatString)
360 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
366 outstr
.add
(chars
[pos
].to_upper
)
375 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
381 outstr
.add
(chars
[pos
].to_lower
)
390 for i
in chars
do i
.output
393 ##################################################
394 # String Specific Methods #
395 ##################################################
397 # Low-level creation of a new string with minimal data.
399 # `_items` will be used as is, without copy, to retrieve the characters of the string.
400 # Aliasing issues is the responsibility of the caller.
401 private init with_infos
(items
: NativeString, bytelen
, from
: Int)
404 self._bytelen
= bytelen
407 _length
= _items
.utf8_length
(_first_byte
, last_byte
)
410 # Low-level creation of a new string with all the data.
412 # `_items` will be used as is, without copy, to retrieve the characters of the string.
413 # Aliasing issues is the responsibility of the caller.
414 private init full
(items
: NativeString, bytelen
, from
, length
: Int)
417 self._length
= length
418 self._bytelen
= bytelen
425 if not other
isa FlatText then return super
427 if self.object_id
== other
.object_id
then return true
429 var my_length
= _bytelen
431 if other
._bytelen
!= my_length
then return false
433 var my_index
= _first_byte
434 var its_index
= other
.first_byte
436 var last_iteration
= my_index
+ my_length
438 var its_items
= other
._items
439 var my_items
= self._items
441 while my_index
< last_iteration
do
442 if my_items
[my_index
] != its_items
[its_index
] then return false
452 if not other
isa FlatText then return super
454 if self.object_id
== other
.object_id
then return false
457 var itsits
= other
._items
460 var obt
= other
.bytelen
462 var minln
= if mbt
< obt
then mbt
else obt
463 var mst
= _first_byte
464 var ost
= other
.first_byte
466 for i
in [0 .. minln
[ do
467 var my_curr_char
= myits
[mst
]
468 var its_curr_char
= itsits
[ost
]
470 if my_curr_char
> its_curr_char
then return false
471 if my_curr_char
< its_curr_char
then return true
484 var nlen
= mlen
+ slen
486 var mifrom
= _first_byte
487 if s
isa FlatText then
489 var sifrom
= s
.first_byte
490 var ns
= new NativeString(nlen
+ 1)
491 mits
.copy_to
(ns
, mlen
, mifrom
, 0)
492 sits
.copy_to
(ns
, slen
, sifrom
, mlen
)
493 return new FlatString.full
(ns
, nlen
, 0, _length
+ o
.length
)
500 var mybtlen
= _bytelen
501 var new_bytelen
= mybtlen
* i
503 var newlen
= mylen
* i
506 var ns
= new NativeString(new_bytelen
+ 1)
507 ns
[new_bytelen
] = 0u8
510 its
.copy_to
(ns
, mybtlen
, fb
, offset
)
514 return new FlatString.full
(ns
, new_bytelen
, 0, newlen
)
520 if hash_cache
== null then
521 # djb2 hash algorithm
525 var my_items
= _items
529 h
= (h
<< 5) + h
+ my_items
[i
].to_i
536 return hash_cache
.as(not null)
539 redef fun substrings
do return new FlatSubstringsIter(self)
542 private class FlatStringCharReverseIterator
543 super IndexedIterator[Char]
545 var target
: FlatString
549 redef fun is_ok
do return curr_pos
>= 0
551 redef fun item
do return target
[curr_pos
]
553 redef fun next
do curr_pos
-= 1
555 redef fun index
do return curr_pos
559 private class FlatStringCharIterator
560 super IndexedIterator[Char]
562 var target
: FlatString
564 var max
: Int is noautoinit
568 init do max
= target
._length
- 1
570 redef fun is_ok
do return curr_pos
<= max
572 redef fun item
do return target
[curr_pos
]
574 redef fun next
do curr_pos
+= 1
576 redef fun index
do return curr_pos
580 private class FlatStringCharView
583 redef type SELFTYPE: FlatString
585 redef fun [](index
) do return target
[index
]
587 redef fun iterator_from
(start
) do return new FlatStringCharIterator(target
, start
)
589 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator(target
, start
)
593 private class FlatStringByteReverseIterator
594 super IndexedIterator[Byte]
596 var target
: FlatString
598 var target_items
: NativeString is noautoinit
605 target_items
= tgt
._items
606 curr_pos
+= tgt
._first_byte
609 redef fun is_ok
do return curr_pos
>= target
._first_byte
611 redef fun item
do return target_items
[curr_pos
]
613 redef fun next
do curr_pos
-= 1
615 redef fun index
do return curr_pos
- target
._first_byte
619 private class FlatStringByteIterator
620 super IndexedIterator[Byte]
622 var target
: FlatString
624 var target_items
: NativeString is noautoinit
631 target_items
= tgt
._items
632 curr_pos
+= tgt
._first_byte
635 redef fun is_ok
do return curr_pos
<= target
.last_byte
637 redef fun item
do return target_items
[curr_pos
]
639 redef fun next
do curr_pos
+= 1
641 redef fun index
do return curr_pos
- target
._first_byte
645 private class FlatStringByteView
648 redef type SELFTYPE: FlatString
652 # Check that the index (+ _first_byte) is not larger than last_byte
653 # In other terms, if the index is valid
655 var target
= self.target
656 var ind
= index
+ target
._first_byte
657 assert ind
<= target
.last_byte
658 return target
._items
[ind
]
661 redef fun iterator_from
(start
) do return new FlatStringByteIterator(target
, start
)
663 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator(target
, start
)
668 redef new do return new FlatBuffer
670 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
673 # Mutable strings of characters.
678 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
680 redef var bytes
= new FlatBufferByteView(self) is lazy
682 private var char_cache
: Int = -1
684 private var byte_cache
: Int = -1
686 private var capacity
= 0
688 # Real items, used as cache for when to_cstring is called
689 private var real_items
: NativeString is noinit
691 redef fun fast_cstring
do return _items
.fast_cstring
(0)
693 redef fun substrings
do return new FlatSubstringsIter(self)
695 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
697 # This happens when an operation modifies the current `Buffer` and
698 # the Copy-On-Write flag `written` is set at true.
700 var nns
= new NativeString(capacity
)
701 if _bytelen
!= 0 then _items
.copy_to
(nns
, _bytelen
, 0, 0)
706 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
708 # Internal only, does not modify _bytelen or length, this is the caller's responsability
709 private fun rshift_bytes
(from
: Int, len
: Int) do
713 if bt
+ len
> capacity
then
714 capacity
= capacity
* 2 + 2
715 nit
= new NativeString(capacity
)
716 oit
.copy_to
(nit
, 0, 0, from
)
718 oit
.copy_to
(nit
, bt
- from
, from
, from
+ len
)
721 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
723 # Internal only, does not modify _bytelen or length, this is the caller's responsability
724 private fun lshift_bytes
(from
: Int, len
: Int) do
726 it
.copy_to
(it
, _bytelen
- from
, from
, from
- len
)
729 redef fun []=(index
, item
)
731 assert index
>= 0 and index
<= _length
732 if written
then reset
734 if index
== _length
then
739 var ip
= it
.char_to_byte_index
(index
)
740 var c
= it
.char_at
(ip
)
741 var clen
= c
.u8char_len
742 var itemlen
= item
.u8char_len
743 var size_diff
= itemlen
- clen
744 if size_diff
> 0 then
745 rshift_bytes
(ip
+ clen
, size_diff
)
746 else if size_diff
< 0 then
747 lshift_bytes
(ip
+ clen
, -size_diff
)
749 _bytelen
+= size_diff
751 it
.set_char_at
(ip
, item
)
756 if written
then reset
758 var clen
= c
.u8char_len
761 _items
.set_char_at
(bt
, c
)
768 if written
then reset
773 redef fun empty
do return new Buffer
775 redef fun enlarge
(cap
)
778 if cap
<= c
then return
779 while c
<= cap
do c
= c
* 2 + 2
780 # The COW flag can be set at false here, since
781 # it does a copy of the current `Buffer`
784 var a
= new NativeString(c
+1)
787 if bln
> 0 then it
.copy_to
(a
, bln
, 0, 0)
797 if bln
== 0 then _items
= new NativeString(1)
798 return new FlatString.full
(_items
, bln
, 0, _length
)
805 var new_native
= new NativeString(bln
+ 1)
806 new_native
[bln
] = 0u8
807 if _length
> 0 then _items
.copy_to
(new_native
, bln
, 0, 0)
808 real_items
= new_native
814 # Create a new empty string.
817 # Low-level creation a new buffer with given data.
819 # `_items` will be used as is, without copy, to store the characters of the buffer.
820 # Aliasing issues is the responsibility of the caller.
822 # If `_items` is shared, `written` should be set to true after the creation
823 # so that a modification will do a copy-on-write.
824 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
827 self.capacity
= capacity
828 self._bytelen
= bytelen
829 self._length
= length
832 # Create a new string copied from `s`.
835 _items
= new NativeString(s
.bytelen
)
836 if s
isa FlatText then
839 for i
in substrings
do i
.as(FlatString)._items
.copy_to
(_items
, i
._bytelen
, 0, 0)
847 # Create a new empty string with a given capacity.
848 init with_capacity
(cap
: Int)
851 _items
= new NativeString(cap
+ 1)
858 if s
.is_empty
then return
861 var nln
= _bytelen
+ sl
863 if s
isa FlatText then
864 s
._items
.copy_to
(_items
, sl
, s
.first_byte
, _bytelen
)
866 for i
in s
.substrings
do append i
873 # Copies the content of self in `dest`
874 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
876 var self_chars
= self.chars
877 var dest_chars
= dest
.chars
878 for i
in [0..len-1
] do
879 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
883 redef fun substring
(from
, count
)
886 if from
< 0 then from
= 0
887 if (from
+ count
) > _length
then count
= _length
- from
888 if count
<= 0 then return new Buffer
890 var bytefrom
= its
.char_to_byte_index
(from
)
891 var byteto
= its
.char_to_byte_index
(count
+ from
- 1)
892 byteto
+= its
.char_at
(byteto
).u8char_len
- 1
893 var byte_length
= byteto
- bytefrom
+ 1
894 var r_items
= new NativeString(byte_length
)
895 its
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
896 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
902 var ns
= new FlatBuffer.with_capacity
(capacity
)
903 for i
in chars
.reverse_iterator
do ns
.add i
907 redef fun times
(repeats
)
910 var x
= new FlatString.full
(_items
, bln
, 0, _length
)
911 for i
in [1 .. repeats
[ do
918 if written
then reset
919 for i
in [0 .. _length
[ do self[i
] = self[i
].to_upper
924 if written
then reset
925 for i
in [0 .. _length
[ do self[i
] = self[i
].to_lower
929 private class FlatBufferByteReverseIterator
930 super IndexedIterator[Byte]
932 var target
: FlatBuffer
934 var target_items
: NativeString is noautoinit
938 init do target_items
= target
._items
940 redef fun index
do return curr_pos
942 redef fun is_ok
do return curr_pos
>= 0
944 redef fun item
do return target_items
[curr_pos
]
946 redef fun next
do curr_pos
-= 1
950 private class FlatBufferByteView
953 redef type SELFTYPE: FlatBuffer
955 redef fun [](index
) do return target
._items
[index
]
957 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator(target
, pos
)
959 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator(target
, pos
)
963 private class FlatBufferByteIterator
964 super IndexedIterator[Byte]
966 var target
: FlatBuffer
968 var target_items
: NativeString is noautoinit
972 init do target_items
= target
._items
974 redef fun index
do return curr_pos
976 redef fun is_ok
do return curr_pos
< target
._bytelen
978 redef fun item
do return target_items
[curr_pos
]
980 redef fun next
do curr_pos
+= 1
984 private class FlatBufferCharReverseIterator
985 super IndexedIterator[Char]
987 var target
: FlatBuffer
991 redef fun index
do return curr_pos
993 redef fun is_ok
do return curr_pos
>= 0
995 redef fun item
do return target
[curr_pos
]
997 redef fun next
do curr_pos
-= 1
1001 private class FlatBufferCharView
1002 super BufferCharView
1004 redef type SELFTYPE: FlatBuffer
1006 redef fun [](index
) do return target
[index
]
1008 redef fun []=(index
, item
)
1010 assert index
>= 0 and index
<= length
1011 if index
== length
then
1015 target
[index
] = item
1028 fun enlarge
(cap
: Int)
1035 var s_length
= s
.length
1036 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
._length
)
1037 for i
in s
do target
.add i
1040 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator(target
, pos
)
1042 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator(target
, pos
)
1046 private class FlatBufferCharIterator
1047 super IndexedIterator[Char]
1049 var target
: FlatBuffer
1051 var max
: Int is noautoinit
1055 init do max
= target
._length
- 1
1057 redef fun index
do return curr_pos
1059 redef fun is_ok
do return curr_pos
<= max
1061 redef fun item
do return target
[curr_pos
]
1063 redef fun next
do curr_pos
+= 1
1067 redef class NativeString
1070 return to_s_with_length
(cstring_length
)
1073 # Returns `self` as a String of `length`.
1074 redef fun to_s_with_length
(length
): FlatString
1077 return clean_utf8
(length
)
1080 redef fun to_s_full
(bytelen
, unilen
) do
1081 return new FlatString.full
(self, bytelen
, 0, unilen
)
1084 # Returns `self` as a new String.
1085 redef fun to_s_with_copy
: FlatString
1087 var length
= cstring_length
1088 var r
= clean_utf8
(length
)
1089 if r
.items
!= self then return r
1090 var new_self
= new NativeString(length
+ 1)
1091 copy_to
(new_self
, length
, 0, 0)
1092 var str
= new FlatString.with_infos
(new_self
, length
, 0)
1093 new_self
[length
] = 0u8
1094 str
.to_cstring
= new_self
1098 # Cleans a NativeString if necessary
1099 fun clean_utf8
(len
: Int): FlatString do
1100 var replacements
: nullable Array[Int] = null
1101 var end_length
= len
1106 var nxst
= length_of_char_at
(pos
)
1109 ok_st
= b
& 0x80u
8 == 0u8
1110 else if nxst
== 2 then
1111 ok_st
= b
& 0xE0u
8 == 0xC0u
8
1112 else if nxst
== 3 then
1113 ok_st
= b
& 0xF0u
8 == 0xE0u
8
1115 ok_st
= b
& 0xF8u
8 == 0xF0u
8
1118 if replacements
== null then replacements
= new Array[Int]
1119 replacements
.add pos
1126 var c
= char_at
(pos
)
1127 var cp
= c
.code_point
1129 ok_c
= cp
>= 0 and cp
<= 0x7F
1130 else if nxst
== 2 then
1131 ok_c
= cp
>= 0x80 and cp
<= 0x7FF
1132 else if nxst
== 3 then
1133 ok_c
= cp
>= 0x800 and cp
<= 0xFFFF
1134 ok_c
= ok_c
and not (cp
>= 0xD800 and cp
<= 0xDFFF) and cp
!= 0xFFFE and cp
!= 0xFFFF
1136 ok_c
= cp
>= 0x10000 and cp
<= 0x10FFFF
1139 if replacements
== null then replacements
= new Array[Int]
1140 replacements
.add pos
1150 if end_length
!= len
then
1151 ret
= new NativeString(end_length
)
1154 var repls
= replacements
.as(not null)
1155 var r
= repls
.items
.as(not null)
1156 var imax
= repls
.length
1157 for i
in [0 .. imax
[ do
1159 var chkln
= repl_pos
- old_repl
1160 copy_to
(ret
, chkln
, old_repl
, off
)
1163 ret
[off
+ 1] = 0xBFu
8
1164 ret
[off
+ 2] = 0xBDu
8
1165 old_repl
= repl_pos
+ 1
1168 copy_to
(ret
, len
- old_repl
, old_repl
, off
)
1170 return new FlatString.full
(ret
, end_length
, 0, chr_ln
)
1173 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
1175 # Very unsafe, make sure to have room for this char prior to calling this function.
1176 private fun set_char_at
(pos
: Int, c
: Char) do
1177 var ln
= c
.u8char_len
1178 native_set_char
(pos
, c
, ln
)
1181 private fun native_set_char
(pos
: Int, c
: Char, ln
: Int) `{
1182 char* dst = self + pos;
1188 dst[0] = 0xC0 | ((c & 0x7C0) >> 6);
1189 dst[1] = 0x80 | (c & 0x3F);
1192 dst[0] = 0xE0 | ((c & 0xF000) >> 12);
1193 dst[1] = 0x80 | ((c & 0xFC0) >> 6);
1194 dst[2] = 0x80 | (c & 0x3F);
1197 dst[0] = 0xF0 | ((c & 0x1C0000) >> 18);
1198 dst[1] = 0x80 | ((c & 0x3F000) >> 12);
1199 dst[2] = 0x80 | ((c & 0xFC0) >> 6);
1200 dst[3] = 0x80 | (c & 0x3F);
1207 redef fun to_base
(base
, signed
)
1209 var l
= digit_count
(base
)
1210 var s
= new FlatBuffer.from
(" " * l
)
1211 fill_buffer
(s
, base
, signed
)
1215 # return displayable int in base 10 and signed
1217 # assert 1.to_s == "1"
1218 # assert (-123).to_s == "-123"
1220 # Fast case for common numbers
1221 if self == 0 then return "0"
1222 if self == 1 then return "1"
1224 var nslen
= int_to_s_len
1225 var ns
= new NativeString(nslen
+ 1)
1227 native_int_to_s
(ns
, nslen
+ 1)
1228 return new FlatString.full
(ns
, nslen
, 0, nslen
)
1232 redef class Array[E
]
1234 # Fast implementation
1235 redef fun plain_to_s
1238 if l
== 0 then return ""
1239 var its
= _items
.as(not null)
1241 if l
== 1 then if first
== null then return "" else return first
.to_s
1242 var na
= new NativeArray[String](l
)
1248 if itsi
== null then
1258 var ns
= new NativeString(sl
+ 1)
1264 if tmp
isa FlatString then
1265 var tpl
= tmp
._bytelen
1266 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1269 for j
in tmp
.substrings
do
1270 var s
= j
.as(FlatString)
1271 var slen
= s
._bytelen
1272 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1278 return new FlatString.with_infos
(ns
, sl
, 0)
1282 redef class NativeArray[E
]
1283 redef fun native_to_s
do
1284 assert self isa NativeArray[String]
1295 var ns
= new NativeString(sl
+ 1)
1301 if tmp
isa FlatString then
1302 var tpl
= tmp
._bytelen
1303 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1306 for j
in tmp
.substrings
do
1307 var s
= j
.as(FlatString)
1308 var slen
= s
._bytelen
1309 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1315 return new FlatString.with_infos
(ns
, sl
, 0)
1319 redef class Map[K
,V
]
1320 redef fun join
(sep
, couple_sep
)
1322 if is_empty
then return ""
1324 var s
= new Buffer # Result
1330 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1332 # Concat other _items
1338 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")