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 redef fun is_ok
do return curr_pos
>= 0
538 redef fun item
do return target
[curr_pos
]
540 redef fun next
do curr_pos
-= 1
542 redef fun index
do return curr_pos
546 private class FlatStringCharIterator
547 super IndexedIterator[Char]
549 var target
: FlatString
551 var max
: Int is noautoinit
555 init do max
= target
.length
- 1
557 redef fun is_ok
do return curr_pos
<= max
559 redef fun item
do return target
[curr_pos
]
561 redef fun next
do curr_pos
+= 1
563 redef fun index
do return curr_pos
567 private class FlatStringCharView
570 redef type SELFTYPE: FlatString
572 redef fun [](index
) do return target
[index
]
574 redef fun iterator_from
(start
) do return new FlatStringCharIterator(target
, start
)
576 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator(target
, start
)
580 private class FlatStringByteReverseIterator
581 super IndexedIterator[Byte]
583 var target
: FlatString
585 var target_items
: NativeString is noautoinit
592 target_items
= tgt
._items
593 curr_pos
+= tgt
._first_byte
596 redef fun is_ok
do return curr_pos
>= target
._first_byte
598 redef fun item
do return target_items
[curr_pos
]
600 redef fun next
do curr_pos
-= 1
602 redef fun index
do return curr_pos
- target
._first_byte
606 private class FlatStringByteIterator
607 super IndexedIterator[Byte]
609 var target
: FlatString
611 var target_items
: NativeString is noautoinit
618 target_items
= tgt
._items
619 curr_pos
+= tgt
._first_byte
622 redef fun is_ok
do return curr_pos
<= target
._last_byte
624 redef fun item
do return target_items
[curr_pos
]
626 redef fun next
do curr_pos
+= 1
628 redef fun index
do return curr_pos
- target
._first_byte
632 private class FlatStringByteView
635 redef type SELFTYPE: FlatString
639 # Check that the index (+ _first_byte) is not larger than _last_byte
640 # In other terms, if the index is valid
642 var target
= self.target
643 var ind
= index
+ target
._first_byte
644 assert ind
<= target
._last_byte
645 return target
._items
[ind
]
648 redef fun iterator_from
(start
) do return new FlatStringByteIterator(target
, start
)
650 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator(target
, start
)
655 redef new do return new FlatBuffer
657 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
660 # Mutable strings of characters.
665 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
667 redef var bytes
= new FlatBufferByteView(self) is lazy
671 private var char_cache
: Int = -1
673 private var byte_cache
: Int = -1
675 private var capacity
= 0
677 # Real items, used as cache for when to_cstring is called
678 private var real_items
: NativeString is noinit
680 redef fun fast_cstring
do return _items
.fast_cstring
(0)
682 redef fun substrings
do return new FlatSubstringsIter(self)
684 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
686 # This happens when an operation modifies the current `Buffer` and
687 # the Copy-On-Write flag `written` is set at true.
689 var nns
= new NativeString(capacity
)
690 if _bytelen
!= 0 then _items
.copy_to
(nns
, _bytelen
, 0, 0)
695 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
697 # Internal only, does not modify _bytelen or length, this is the caller's responsability
698 private fun rshift_bytes
(from
: Int, len
: Int) do
702 if bt
+ len
> capacity
then
703 capacity
= capacity
* 2 + 2
704 nit
= new NativeString(capacity
)
705 oit
.copy_to
(nit
, 0, 0, from
)
707 oit
.copy_to
(nit
, bt
- from
, from
, from
+ len
)
710 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
712 # Internal only, does not modify _bytelen or length, this is the caller's responsability
713 private fun lshift_bytes
(from
: Int, len
: Int) do
715 it
.copy_to
(it
, _bytelen
- from
, from
, from
- len
)
718 redef fun []=(index
, item
)
720 assert index
>= 0 and index
<= length
721 if written
then reset
723 if index
== length
then
728 var ip
= it
.char_to_byte_index
(index
)
729 var c
= it
.char_at
(ip
)
730 var clen
= c
.u8char_len
731 var itemlen
= item
.u8char_len
732 var size_diff
= itemlen
- clen
733 if size_diff
> 0 then
734 rshift_bytes
(ip
+ clen
, size_diff
)
735 else if size_diff
< 0 then
736 lshift_bytes
(ip
+ clen
, -size_diff
)
738 _bytelen
+= size_diff
740 it
.set_char_at
(ip
, item
)
745 if written
then reset
747 var clen
= c
.u8char_len
750 _items
.set_char_at
(bt
, c
)
757 if written
then reset
762 redef fun empty
do return new Buffer
764 redef fun enlarge
(cap
)
767 if cap
<= c
then return
768 while c
<= cap
do c
= c
* 2 + 2
769 # The COW flag can be set at false here, since
770 # it does a copy of the current `Buffer`
773 var a
= new NativeString(c
+1)
776 if bln
> 0 then it
.copy_to
(a
, bln
, 0, 0)
786 if bln
== 0 then _items
= new NativeString(1)
787 return new FlatString.full
(_items
, bln
, 0, bln
- 1, length
)
794 var new_native
= new NativeString(bln
+ 1)
795 new_native
[bln
] = 0u8
796 if length
> 0 then _items
.copy_to
(new_native
, bln
, 0, 0)
797 real_items
= new_native
803 # Create a new empty string.
806 # Low-level creation a new buffer with given data.
808 # `_items` will be used as is, without copy, to store the characters of the buffer.
809 # Aliasing issues is the responsibility of the caller.
811 # If `_items` is shared, `written` should be set to true after the creation
812 # so that a modification will do a copy-on-write.
813 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
816 self.capacity
= capacity
817 self._bytelen
= bytelen
821 # Create a new string copied from `s`.
824 _items
= new NativeString(s
.bytelen
)
825 if s
isa FlatText then
828 for i
in substrings
do i
.as(FlatString)._items
.copy_to
(_items
, i
._bytelen
, 0, 0)
836 # Create a new empty string with a given capacity.
837 init with_capacity
(cap
: Int)
840 _items
= new NativeString(cap
+ 1)
847 if s
.is_empty
then return
850 var nln
= _bytelen
+ sl
852 if s
isa FlatText then
853 s
._items
.copy_to
(_items
, sl
, s
.first_byte
, _bytelen
)
855 for i
in s
.substrings
do append i
862 # Copies the content of self in `dest`
863 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
865 var self_chars
= self.chars
866 var dest_chars
= dest
.chars
867 for i
in [0..len-1
] do
868 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
872 redef fun substring
(from
, count
)
875 if from
< 0 then from
= 0
876 if (from
+ count
) > length
then count
= length
- from
877 if count
<= 0 then return new Buffer
879 var bytefrom
= its
.char_to_byte_index
(from
)
880 var byteto
= its
.char_to_byte_index
(count
+ from
- 1)
881 byteto
+= its
.char_at
(byteto
).u8char_len
- 1
882 var byte_length
= byteto
- bytefrom
+ 1
883 var r_items
= new NativeString(byte_length
)
884 its
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
885 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
891 var ns
= new FlatBuffer.with_capacity
(capacity
)
892 for i
in chars
.reverse_iterator
do ns
.add i
896 redef fun times
(repeats
)
899 var x
= new FlatString.full
(_items
, bln
, 0, bln
- 1, length
)
900 for i
in [1 .. repeats
[ do
907 if written
then reset
908 for i
in [0 .. length
[ do self[i
] = self[i
].to_upper
913 if written
then reset
914 for i
in [0 .. length
[ do self[i
] = self[i
].to_lower
918 private class FlatBufferByteReverseIterator
919 super IndexedIterator[Byte]
921 var target
: FlatBuffer
923 var target_items
: NativeString is noautoinit
927 init do target_items
= target
._items
929 redef fun index
do return curr_pos
931 redef fun is_ok
do return curr_pos
>= 0
933 redef fun item
do return target_items
[curr_pos
]
935 redef fun next
do curr_pos
-= 1
939 private class FlatBufferByteView
942 redef type SELFTYPE: FlatBuffer
944 redef fun [](index
) do return target
._items
[index
]
946 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator(target
, pos
)
948 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator(target
, pos
)
952 private class FlatBufferByteIterator
953 super IndexedIterator[Byte]
955 var target
: FlatBuffer
957 var target_items
: NativeString is noautoinit
961 init do target_items
= target
._items
963 redef fun index
do return curr_pos
965 redef fun is_ok
do return curr_pos
< target
._bytelen
967 redef fun item
do return target_items
[curr_pos
]
969 redef fun next
do curr_pos
+= 1
973 private class FlatBufferCharReverseIterator
974 super IndexedIterator[Char]
976 var target
: FlatBuffer
980 redef fun index
do return curr_pos
982 redef fun is_ok
do return curr_pos
>= 0
984 redef fun item
do return target
[curr_pos
]
986 redef fun next
do curr_pos
-= 1
990 private class FlatBufferCharView
993 redef type SELFTYPE: FlatBuffer
995 redef fun [](index
) do return target
[index
]
997 redef fun []=(index
, item
)
999 assert index
>= 0 and index
<= length
1000 if index
== length
then
1004 target
[index
] = item
1017 fun enlarge
(cap
: Int)
1024 var s_length
= s
.length
1025 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
1026 for i
in s
do target
.add i
1029 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator(target
, pos
)
1031 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator(target
, pos
)
1035 private class FlatBufferCharIterator
1036 super IndexedIterator[Char]
1038 var target
: FlatBuffer
1040 var max
: Int is noautoinit
1044 init do max
= target
.length
- 1
1046 redef fun index
do return curr_pos
1048 redef fun is_ok
do return curr_pos
<= max
1050 redef fun item
do return target
[curr_pos
]
1052 redef fun next
do curr_pos
+= 1
1056 redef class NativeString
1059 return to_s_with_length
(cstring_length
)
1062 # Returns `self` as a String of `length`.
1063 redef fun to_s_with_length
(length
): FlatString
1066 return clean_utf8
(length
)
1069 redef fun to_s_full
(bytelen
, unilen
) do
1070 return new FlatString.full
(self, bytelen
, 0, bytelen
- 1, unilen
)
1073 # Returns `self` as a new String.
1074 redef fun to_s_with_copy
: FlatString
1076 var length
= cstring_length
1077 var r
= clean_utf8
(length
)
1078 if r
.items
!= self then return r
1079 var new_self
= new NativeString(length
+ 1)
1080 copy_to
(new_self
, length
, 0, 0)
1081 var str
= new FlatString.with_infos
(new_self
, length
, 0, length
- 1)
1082 new_self
[length
] = 0u8
1083 str
.to_cstring
= new_self
1087 # Cleans a NativeString if necessary
1088 fun clean_utf8
(len
: Int): FlatString do
1089 var replacements
: nullable Array[Int] = null
1090 var end_length
= len
1095 var nxst
= length_of_char_at
(pos
)
1098 ok_st
= b
& 0x80u
8 == 0u8
1099 else if nxst
== 2 then
1100 ok_st
= b
& 0xE0u
8 == 0xC0u
8
1101 else if nxst
== 3 then
1102 ok_st
= b
& 0xF0u
8 == 0xE0u
8
1104 ok_st
= b
& 0xF8u
8 == 0xF0u
8
1107 if replacements
== null then replacements
= new Array[Int]
1108 replacements
.add pos
1115 var c
= char_at
(pos
)
1116 var cp
= c
.code_point
1118 ok_c
= cp
>= 0 and cp
<= 0x7F
1119 else if nxst
== 2 then
1120 ok_c
= cp
>= 0x80 and cp
<= 0x7FF
1121 else if nxst
== 3 then
1122 ok_c
= cp
>= 0x800 and cp
<= 0xFFFF
1123 ok_c
= ok_c
and not (cp
>= 0xD800 and cp
<= 0xDFFF) and cp
!= 0xFFFE and cp
!= 0xFFFF
1125 ok_c
= cp
>= 0x10000 and cp
<= 0x10FFFF
1128 if replacements
== null then replacements
= new Array[Int]
1129 replacements
.add pos
1139 if end_length
!= len
then
1140 ret
= new NativeString(end_length
)
1143 var repls
= replacements
.as(not null)
1144 var r
= repls
.items
.as(not null)
1145 var imax
= repls
.length
1146 for i
in [0 .. imax
[ do
1148 var chkln
= repl_pos
- old_repl
1149 copy_to
(ret
, chkln
, old_repl
, off
)
1152 ret
[off
+ 1] = 0xBFu
8
1153 ret
[off
+ 2] = 0xBDu
8
1154 old_repl
= repl_pos
+ 1
1157 copy_to
(ret
, len
- old_repl
, old_repl
, off
)
1159 return new FlatString.full
(ret
, end_length
, 0, end_length
- 1, chr_ln
)
1162 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
1164 # Very unsafe, make sure to have room for this char prior to calling this function.
1165 private fun set_char_at
(pos
: Int, c
: Char) do
1166 var ln
= c
.u8char_len
1167 native_set_char
(pos
, c
, ln
)
1170 private fun native_set_char
(pos
: Int, c
: Char, ln
: Int) `{
1171 char* dst = self + pos;
1177 dst[0] = 0xC0 | ((c & 0x7C0) >> 6);
1178 dst[1] = 0x80 | (c & 0x3F);
1181 dst[0] = 0xE0 | ((c & 0xF000) >> 12);
1182 dst[1] = 0x80 | ((c & 0xFC0) >> 6);
1183 dst[2] = 0x80 | (c & 0x3F);
1186 dst[0] = 0xF0 | ((c & 0x1C0000) >> 18);
1187 dst[1] = 0x80 | ((c & 0x3F000) >> 12);
1188 dst[2] = 0x80 | ((c & 0xFC0) >> 6);
1189 dst[3] = 0x80 | (c & 0x3F);
1196 redef fun to_base
(base
, signed
)
1198 var l
= digit_count
(base
)
1199 var s
= new FlatBuffer.from
(" " * l
)
1200 fill_buffer
(s
, base
, signed
)
1204 # return displayable int in base 10 and signed
1206 # assert 1.to_s == "1"
1207 # assert (-123).to_s == "-123"
1209 # Fast case for common numbers
1210 if self == 0 then return "0"
1211 if self == 1 then return "1"
1213 var nslen
= int_to_s_len
1214 var ns
= new NativeString(nslen
+ 1)
1216 native_int_to_s
(ns
, nslen
+ 1)
1217 return new FlatString.full
(ns
, nslen
, 0, nslen
- 1, nslen
)
1221 redef class Array[E
]
1223 # Fast implementation
1224 redef fun plain_to_s
1227 if l
== 0 then return ""
1228 var its
= _items
.as(not null)
1230 if l
== 1 then if first
== null then return "" else return first
.to_s
1231 var na
= new NativeArray[String](l
)
1237 if itsi
== null then
1247 var ns
= new NativeString(sl
+ 1)
1253 if tmp
isa FlatString then
1254 var tpl
= tmp
._bytelen
1255 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1258 for j
in tmp
.substrings
do
1259 var s
= j
.as(FlatString)
1260 var slen
= s
._bytelen
1261 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1267 return new FlatString.with_infos
(ns
, sl
, 0, sl
- 1)
1271 redef class NativeArray[E
]
1272 redef fun native_to_s
do
1273 assert self isa NativeArray[String]
1284 var ns
= new NativeString(sl
+ 1)
1290 if tmp
isa FlatString then
1291 var tpl
= tmp
._bytelen
1292 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1295 for j
in tmp
.substrings
do
1296 var s
= j
.as(FlatString)
1297 var slen
= s
._bytelen
1298 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1304 return new FlatString.with_infos
(ns
, sl
, 0, sl
- 1)
1308 redef class Map[K
,V
]
1309 redef fun join
(sep
, couple_sep
)
1311 if is_empty
then return ""
1313 var s
= new Buffer # Result
1319 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1321 # Concat other _items
1327 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")