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 length
is lazy
do
313 if _bytelen
== 0 then return 0
314 return _items
.utf8_length
(_first_byte
, _last_byte
)
317 redef var to_cstring
is lazy
do
319 var new_items
= new NativeString(blen
+ 1)
320 _items
.copy_to
(new_items
, blen
, _first_byte
, 0)
321 new_items
[blen
] = 0u8
327 var b
= new FlatBuffer.with_capacity
(_bytelen
+ 1)
328 for i
in [length
- 1 .. 0].step
(-1) do
331 var s
= b
.to_s
.as(FlatString)
332 s
.length
= self.length
336 redef fun fast_cstring
do return _items
.fast_cstring
(_first_byte
)
338 redef fun substring
(from
, count
)
344 if count
< 0 then count
= 0
348 if (count
+ from
) > length
then count
= length
- from
349 if count
<= 0 then return ""
350 var end_index
= from
+ count
- 1
352 var bytefrom
= char_to_byte_index
(from
)
353 var byteto
= char_to_byte_index
(end_index
)
355 byteto
+= its
.length_of_char_at
(byteto
) - 1
357 var s
= new FlatString.full
(its
, byteto
- bytefrom
+ 1, bytefrom
, byteto
, count
)
361 redef fun empty
do return "".as(FlatString)
365 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
371 outstr
.add
(chars
[pos
].to_upper
)
380 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
386 outstr
.add
(chars
[pos
].to_lower
)
395 for i
in chars
do i
.output
398 ##################################################
399 # String Specific Methods #
400 ##################################################
402 # Low-level creation of a new string with minimal data.
404 # `_items` will be used as is, without copy, to retrieve the characters of the string.
405 # Aliasing issues is the responsibility of the caller.
406 private init with_infos
(items
: NativeString, bytelen
, from
: Int)
409 self._bytelen
= bytelen
414 # Low-level creation of a new string with all the data.
416 # `_items` will be used as is, without copy, to retrieve the characters of the string.
417 # Aliasing issues is the responsibility of the caller.
418 private init full
(items
: NativeString, bytelen
, from
, length
: Int)
422 self._bytelen
= bytelen
429 if not other
isa FlatText then return super
431 if self.object_id
== other
.object_id
then return true
433 var my_length
= _bytelen
435 if other
._bytelen
!= my_length
then return false
437 var my_index
= _first_byte
438 var its_index
= other
.first_byte
440 var last_iteration
= my_index
+ my_length
442 var its_items
= other
._items
443 var my_items
= self._items
445 while my_index
< last_iteration
do
446 if my_items
[my_index
] != its_items
[its_index
] then return false
456 if not other
isa FlatText then return super
458 if self.object_id
== other
.object_id
then return false
461 var itsits
= other
._items
464 var obt
= other
.bytelen
466 var minln
= if mbt
< obt
then mbt
else obt
467 var mst
= _first_byte
468 var ost
= other
.first_byte
470 for i
in [0 .. minln
[ do
471 var my_curr_char
= myits
[mst
]
472 var its_curr_char
= itsits
[ost
]
474 if my_curr_char
> its_curr_char
then return false
475 if my_curr_char
< its_curr_char
then return true
488 var nlen
= mlen
+ slen
490 var mifrom
= _first_byte
491 if s
isa FlatText then
493 var sifrom
= s
.first_byte
494 var ns
= new NativeString(nlen
+ 1)
495 mits
.copy_to
(ns
, mlen
, mifrom
, 0)
496 sits
.copy_to
(ns
, slen
, sifrom
, mlen
)
497 return new FlatString.full
(ns
, nlen
, 0, _length
+ o
.length
)
504 var mybtlen
= _bytelen
505 var new_bytelen
= mybtlen
* i
507 var newlen
= mylen
* i
510 var ns
= new NativeString(new_bytelen
+ 1)
511 ns
[new_bytelen
] = 0u8
514 its
.copy_to
(ns
, mybtlen
, fb
, offset
)
518 return new FlatString.full
(ns
, new_bytelen
, 0, newlen
)
524 if hash_cache
== null then
525 # djb2 hash algorithm
529 var my_items
= _items
533 h
= (h
<< 5) + h
+ my_items
[i
].to_i
540 return hash_cache
.as(not null)
543 redef fun substrings
do return new FlatSubstringsIter(self)
546 private class FlatStringCharReverseIterator
547 super IndexedIterator[Char]
549 var target
: FlatString
553 redef fun is_ok
do return curr_pos
>= 0
555 redef fun item
do return target
[curr_pos
]
557 redef fun next
do curr_pos
-= 1
559 redef fun index
do return curr_pos
563 private class FlatStringCharIterator
564 super IndexedIterator[Char]
566 var target
: FlatString
568 var max
: Int is noautoinit
572 init do max
= target
.length
- 1
574 redef fun is_ok
do return curr_pos
<= max
576 redef fun item
do return target
[curr_pos
]
578 redef fun next
do curr_pos
+= 1
580 redef fun index
do return curr_pos
584 private class FlatStringCharView
587 redef type SELFTYPE: FlatString
589 redef fun [](index
) do return target
[index
]
591 redef fun iterator_from
(start
) do return new FlatStringCharIterator(target
, start
)
593 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator(target
, start
)
597 private class FlatStringByteReverseIterator
598 super IndexedIterator[Byte]
600 var target
: FlatString
602 var target_items
: NativeString is noautoinit
609 target_items
= tgt
._items
610 curr_pos
+= tgt
._first_byte
613 redef fun is_ok
do return curr_pos
>= target
._first_byte
615 redef fun item
do return target_items
[curr_pos
]
617 redef fun next
do curr_pos
-= 1
619 redef fun index
do return curr_pos
- target
._first_byte
623 private class FlatStringByteIterator
624 super IndexedIterator[Byte]
626 var target
: FlatString
628 var target_items
: NativeString is noautoinit
635 target_items
= tgt
._items
636 curr_pos
+= tgt
._first_byte
639 redef fun is_ok
do return curr_pos
<= target
.last_byte
641 redef fun item
do return target_items
[curr_pos
]
643 redef fun next
do curr_pos
+= 1
645 redef fun index
do return curr_pos
- target
._first_byte
649 private class FlatStringByteView
652 redef type SELFTYPE: FlatString
656 # Check that the index (+ _first_byte) is not larger than last_byte
657 # In other terms, if the index is valid
659 var target
= self.target
660 var ind
= index
+ target
._first_byte
661 assert ind
<= target
.last_byte
662 return target
._items
[ind
]
665 redef fun iterator_from
(start
) do return new FlatStringByteIterator(target
, start
)
667 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator(target
, start
)
672 redef new do return new FlatBuffer
674 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
677 # Mutable strings of characters.
682 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
684 redef var bytes
= new FlatBufferByteView(self) is lazy
688 private var char_cache
: Int = -1
690 private var byte_cache
: Int = -1
692 private var capacity
= 0
694 # Real items, used as cache for when to_cstring is called
695 private var real_items
: NativeString is noinit
697 redef fun fast_cstring
do return _items
.fast_cstring
(0)
699 redef fun substrings
do return new FlatSubstringsIter(self)
701 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
703 # This happens when an operation modifies the current `Buffer` and
704 # the Copy-On-Write flag `written` is set at true.
706 var nns
= new NativeString(capacity
)
707 if _bytelen
!= 0 then _items
.copy_to
(nns
, _bytelen
, 0, 0)
712 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
714 # Internal only, does not modify _bytelen or length, this is the caller's responsability
715 private fun rshift_bytes
(from
: Int, len
: Int) do
719 if bt
+ len
> capacity
then
720 capacity
= capacity
* 2 + 2
721 nit
= new NativeString(capacity
)
722 oit
.copy_to
(nit
, 0, 0, from
)
724 oit
.copy_to
(nit
, bt
- from
, from
, from
+ len
)
727 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
729 # Internal only, does not modify _bytelen or length, this is the caller's responsability
730 private fun lshift_bytes
(from
: Int, len
: Int) do
732 it
.copy_to
(it
, _bytelen
- from
, from
, from
- len
)
735 redef fun []=(index
, item
)
737 assert index
>= 0 and index
<= length
738 if written
then reset
740 if index
== length
then
745 var ip
= it
.char_to_byte_index
(index
)
746 var c
= it
.char_at
(ip
)
747 var clen
= c
.u8char_len
748 var itemlen
= item
.u8char_len
749 var size_diff
= itemlen
- clen
750 if size_diff
> 0 then
751 rshift_bytes
(ip
+ clen
, size_diff
)
752 else if size_diff
< 0 then
753 lshift_bytes
(ip
+ clen
, -size_diff
)
755 _bytelen
+= size_diff
757 it
.set_char_at
(ip
, item
)
762 if written
then reset
764 var clen
= c
.u8char_len
767 _items
.set_char_at
(bt
, c
)
774 if written
then reset
779 redef fun empty
do return new Buffer
781 redef fun enlarge
(cap
)
784 if cap
<= c
then return
785 while c
<= cap
do c
= c
* 2 + 2
786 # The COW flag can be set at false here, since
787 # it does a copy of the current `Buffer`
790 var a
= new NativeString(c
+1)
793 if bln
> 0 then it
.copy_to
(a
, bln
, 0, 0)
803 if bln
== 0 then _items
= new NativeString(1)
804 return new FlatString.full
(_items
, bln
, 0, _length
)
811 var new_native
= new NativeString(bln
+ 1)
812 new_native
[bln
] = 0u8
813 if length
> 0 then _items
.copy_to
(new_native
, bln
, 0, 0)
814 real_items
= new_native
820 # Create a new empty string.
823 # Low-level creation a new buffer with given data.
825 # `_items` will be used as is, without copy, to store the characters of the buffer.
826 # Aliasing issues is the responsibility of the caller.
828 # If `_items` is shared, `written` should be set to true after the creation
829 # so that a modification will do a copy-on-write.
830 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
833 self.capacity
= capacity
834 self._bytelen
= bytelen
838 # Create a new string copied from `s`.
841 _items
= new NativeString(s
.bytelen
)
842 if s
isa FlatText then
845 for i
in substrings
do i
.as(FlatString)._items
.copy_to
(_items
, i
._bytelen
, 0, 0)
853 # Create a new empty string with a given capacity.
854 init with_capacity
(cap
: Int)
857 _items
= new NativeString(cap
+ 1)
864 if s
.is_empty
then return
867 var nln
= _bytelen
+ sl
869 if s
isa FlatText then
870 s
._items
.copy_to
(_items
, sl
, s
.first_byte
, _bytelen
)
872 for i
in s
.substrings
do append i
879 # Copies the content of self in `dest`
880 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
882 var self_chars
= self.chars
883 var dest_chars
= dest
.chars
884 for i
in [0..len-1
] do
885 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
889 redef fun substring
(from
, count
)
892 if from
< 0 then from
= 0
893 if (from
+ count
) > length
then count
= length
- from
894 if count
<= 0 then return new Buffer
896 var bytefrom
= its
.char_to_byte_index
(from
)
897 var byteto
= its
.char_to_byte_index
(count
+ from
- 1)
898 byteto
+= its
.char_at
(byteto
).u8char_len
- 1
899 var byte_length
= byteto
- bytefrom
+ 1
900 var r_items
= new NativeString(byte_length
)
901 its
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
902 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
908 var ns
= new FlatBuffer.with_capacity
(capacity
)
909 for i
in chars
.reverse_iterator
do ns
.add i
913 redef fun times
(repeats
)
916 var x
= new FlatString.full
(_items
, bln
, 0, _length
)
917 for i
in [1 .. repeats
[ do
924 if written
then reset
925 for i
in [0 .. length
[ do self[i
] = self[i
].to_upper
930 if written
then reset
931 for i
in [0 .. length
[ do self[i
] = self[i
].to_lower
935 private class FlatBufferByteReverseIterator
936 super IndexedIterator[Byte]
938 var target
: FlatBuffer
940 var target_items
: NativeString is noautoinit
944 init do target_items
= target
._items
946 redef fun index
do return curr_pos
948 redef fun is_ok
do return curr_pos
>= 0
950 redef fun item
do return target_items
[curr_pos
]
952 redef fun next
do curr_pos
-= 1
956 private class FlatBufferByteView
959 redef type SELFTYPE: FlatBuffer
961 redef fun [](index
) do return target
._items
[index
]
963 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator(target
, pos
)
965 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator(target
, pos
)
969 private class FlatBufferByteIterator
970 super IndexedIterator[Byte]
972 var target
: FlatBuffer
974 var target_items
: NativeString is noautoinit
978 init do target_items
= target
._items
980 redef fun index
do return curr_pos
982 redef fun is_ok
do return curr_pos
< target
._bytelen
984 redef fun item
do return target_items
[curr_pos
]
986 redef fun next
do curr_pos
+= 1
990 private class FlatBufferCharReverseIterator
991 super IndexedIterator[Char]
993 var target
: FlatBuffer
997 redef fun index
do return curr_pos
999 redef fun is_ok
do return curr_pos
>= 0
1001 redef fun item
do return target
[curr_pos
]
1003 redef fun next
do curr_pos
-= 1
1007 private class FlatBufferCharView
1008 super BufferCharView
1010 redef type SELFTYPE: FlatBuffer
1012 redef fun [](index
) do return target
[index
]
1014 redef fun []=(index
, item
)
1016 assert index
>= 0 and index
<= length
1017 if index
== length
then
1021 target
[index
] = item
1034 fun enlarge
(cap
: Int)
1041 var s_length
= s
.length
1042 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
1043 for i
in s
do target
.add i
1046 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator(target
, pos
)
1048 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator(target
, pos
)
1052 private class FlatBufferCharIterator
1053 super IndexedIterator[Char]
1055 var target
: FlatBuffer
1057 var max
: Int is noautoinit
1061 init do max
= target
.length
- 1
1063 redef fun index
do return curr_pos
1065 redef fun is_ok
do return curr_pos
<= max
1067 redef fun item
do return target
[curr_pos
]
1069 redef fun next
do curr_pos
+= 1
1073 redef class NativeString
1076 return to_s_with_length
(cstring_length
)
1079 # Returns `self` as a String of `length`.
1080 redef fun to_s_with_length
(length
): FlatString
1083 return clean_utf8
(length
)
1086 redef fun to_s_full
(bytelen
, unilen
) do
1087 return new FlatString.full
(self, bytelen
, 0, unilen
)
1090 # Returns `self` as a new String.
1091 redef fun to_s_with_copy
: FlatString
1093 var length
= cstring_length
1094 var r
= clean_utf8
(length
)
1095 if r
.items
!= self then return r
1096 var new_self
= new NativeString(length
+ 1)
1097 copy_to
(new_self
, length
, 0, 0)
1098 var str
= new FlatString.with_infos
(new_self
, length
, 0)
1099 new_self
[length
] = 0u8
1100 str
.to_cstring
= new_self
1104 # Cleans a NativeString if necessary
1105 fun clean_utf8
(len
: Int): FlatString do
1106 var replacements
: nullable Array[Int] = null
1107 var end_length
= len
1112 var nxst
= length_of_char_at
(pos
)
1115 ok_st
= b
& 0x80u
8 == 0u8
1116 else if nxst
== 2 then
1117 ok_st
= b
& 0xE0u
8 == 0xC0u
8
1118 else if nxst
== 3 then
1119 ok_st
= b
& 0xF0u
8 == 0xE0u
8
1121 ok_st
= b
& 0xF8u
8 == 0xF0u
8
1124 if replacements
== null then replacements
= new Array[Int]
1125 replacements
.add pos
1132 var c
= char_at
(pos
)
1133 var cp
= c
.code_point
1135 ok_c
= cp
>= 0 and cp
<= 0x7F
1136 else if nxst
== 2 then
1137 ok_c
= cp
>= 0x80 and cp
<= 0x7FF
1138 else if nxst
== 3 then
1139 ok_c
= cp
>= 0x800 and cp
<= 0xFFFF
1140 ok_c
= ok_c
and not (cp
>= 0xD800 and cp
<= 0xDFFF) and cp
!= 0xFFFE and cp
!= 0xFFFF
1142 ok_c
= cp
>= 0x10000 and cp
<= 0x10FFFF
1145 if replacements
== null then replacements
= new Array[Int]
1146 replacements
.add pos
1156 if end_length
!= len
then
1157 ret
= new NativeString(end_length
)
1160 var repls
= replacements
.as(not null)
1161 var r
= repls
.items
.as(not null)
1162 var imax
= repls
.length
1163 for i
in [0 .. imax
[ do
1165 var chkln
= repl_pos
- old_repl
1166 copy_to
(ret
, chkln
, old_repl
, off
)
1169 ret
[off
+ 1] = 0xBFu
8
1170 ret
[off
+ 2] = 0xBDu
8
1171 old_repl
= repl_pos
+ 1
1174 copy_to
(ret
, len
- old_repl
, old_repl
, off
)
1176 return new FlatString.full
(ret
, end_length
, 0, chr_ln
)
1179 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
1181 # Very unsafe, make sure to have room for this char prior to calling this function.
1182 private fun set_char_at
(pos
: Int, c
: Char) do
1183 var ln
= c
.u8char_len
1184 native_set_char
(pos
, c
, ln
)
1187 private fun native_set_char
(pos
: Int, c
: Char, ln
: Int) `{
1188 char* dst = self + pos;
1194 dst[0] = 0xC0 | ((c & 0x7C0) >> 6);
1195 dst[1] = 0x80 | (c & 0x3F);
1198 dst[0] = 0xE0 | ((c & 0xF000) >> 12);
1199 dst[1] = 0x80 | ((c & 0xFC0) >> 6);
1200 dst[2] = 0x80 | (c & 0x3F);
1203 dst[0] = 0xF0 | ((c & 0x1C0000) >> 18);
1204 dst[1] = 0x80 | ((c & 0x3F000) >> 12);
1205 dst[2] = 0x80 | ((c & 0xFC0) >> 6);
1206 dst[3] = 0x80 | (c & 0x3F);
1213 redef fun to_base
(base
, signed
)
1215 var l
= digit_count
(base
)
1216 var s
= new FlatBuffer.from
(" " * l
)
1217 fill_buffer
(s
, base
, signed
)
1221 # return displayable int in base 10 and signed
1223 # assert 1.to_s == "1"
1224 # assert (-123).to_s == "-123"
1226 # Fast case for common numbers
1227 if self == 0 then return "0"
1228 if self == 1 then return "1"
1230 var nslen
= int_to_s_len
1231 var ns
= new NativeString(nslen
+ 1)
1233 native_int_to_s
(ns
, nslen
+ 1)
1234 return new FlatString.full
(ns
, nslen
, 0, nslen
)
1238 redef class Array[E
]
1240 # Fast implementation
1241 redef fun plain_to_s
1244 if l
== 0 then return ""
1245 var its
= _items
.as(not null)
1247 if l
== 1 then if first
== null then return "" else return first
.to_s
1248 var na
= new NativeArray[String](l
)
1254 if itsi
== null then
1264 var ns
= new NativeString(sl
+ 1)
1270 if tmp
isa FlatString then
1271 var tpl
= tmp
._bytelen
1272 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1275 for j
in tmp
.substrings
do
1276 var s
= j
.as(FlatString)
1277 var slen
= s
._bytelen
1278 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1284 return new FlatString.with_infos
(ns
, sl
, 0)
1288 redef class NativeArray[E
]
1289 redef fun native_to_s
do
1290 assert self isa NativeArray[String]
1301 var ns
= new NativeString(sl
+ 1)
1307 if tmp
isa FlatString then
1308 var tpl
= tmp
._bytelen
1309 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1312 for j
in tmp
.substrings
do
1313 var s
= j
.as(FlatString)
1314 var slen
= s
._bytelen
1315 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1321 return new FlatString.with_infos
(ns
, sl
, 0)
1325 redef class Map[K
,V
]
1326 redef fun join
(sep
, couple_sep
)
1328 if is_empty
then return ""
1330 var s
= new Buffer # Result
1336 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1338 # Concat other _items
1344 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")