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
447 var itsits
= other
._items
450 var obt
= other
.bytelen
452 var minln
= if mbt
< obt
then mbt
else obt
453 var mst
= _first_byte
454 var ost
= other
.first_byte
456 for i
in [0 .. minln
[ do
457 var my_curr_char
= myits
[mst
]
458 var its_curr_char
= itsits
[ost
]
460 if my_curr_char
> its_curr_char
then return false
461 if my_curr_char
< its_curr_char
then return true
474 var nlen
= mlen
+ slen
476 var mifrom
= _first_byte
477 if s
isa FlatText then
479 var sifrom
= s
.first_byte
480 var ns
= new NativeString(nlen
+ 1)
481 mits
.copy_to
(ns
, mlen
, mifrom
, 0)
482 sits
.copy_to
(ns
, slen
, sifrom
, mlen
)
483 return new FlatString.full
(ns
, nlen
, 0, nlen
- 1, length
+ o
.length
)
490 var mybtlen
= _bytelen
491 var new_bytelen
= mybtlen
* i
493 var newlen
= mylen
* i
496 var ns
= new NativeString(new_bytelen
+ 1)
497 ns
[new_bytelen
] = 0u8
500 its
.copy_to
(ns
, mybtlen
, fb
, offset
)
504 return new FlatString.full
(ns
, new_bytelen
, 0, new_bytelen
- 1, newlen
)
510 if hash_cache
== null then
511 # djb2 hash algorithm
515 var my_items
= _items
519 h
= (h
<< 5) + h
+ my_items
[i
].to_i
526 return hash_cache
.as(not null)
529 redef fun substrings
do return new FlatSubstringsIter(self)
532 private class FlatStringCharReverseIterator
533 super IndexedIterator[Char]
535 var target
: FlatString
539 redef fun is_ok
do return curr_pos
>= 0
541 redef fun item
do return target
[curr_pos
]
543 redef fun next
do curr_pos
-= 1
545 redef fun index
do return curr_pos
549 private class FlatStringCharIterator
550 super IndexedIterator[Char]
552 var target
: FlatString
554 var max
: Int is noautoinit
558 init do max
= target
.length
- 1
560 redef fun is_ok
do return curr_pos
<= max
562 redef fun item
do return target
[curr_pos
]
564 redef fun next
do curr_pos
+= 1
566 redef fun index
do return curr_pos
570 private class FlatStringCharView
573 redef type SELFTYPE: FlatString
575 redef fun [](index
) do return target
[index
]
577 redef fun iterator_from
(start
) do return new FlatStringCharIterator(target
, start
)
579 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator(target
, start
)
583 private class FlatStringByteReverseIterator
584 super IndexedIterator[Byte]
586 var target
: FlatString
588 var target_items
: NativeString is noautoinit
595 target_items
= tgt
._items
596 curr_pos
+= tgt
._first_byte
599 redef fun is_ok
do return curr_pos
>= target
._first_byte
601 redef fun item
do return target_items
[curr_pos
]
603 redef fun next
do curr_pos
-= 1
605 redef fun index
do return curr_pos
- target
._first_byte
609 private class FlatStringByteIterator
610 super IndexedIterator[Byte]
612 var target
: FlatString
614 var target_items
: NativeString is noautoinit
621 target_items
= tgt
._items
622 curr_pos
+= tgt
._first_byte
625 redef fun is_ok
do return curr_pos
<= target
._last_byte
627 redef fun item
do return target_items
[curr_pos
]
629 redef fun next
do curr_pos
+= 1
631 redef fun index
do return curr_pos
- target
._first_byte
635 private class FlatStringByteView
638 redef type SELFTYPE: FlatString
642 # Check that the index (+ _first_byte) is not larger than _last_byte
643 # In other terms, if the index is valid
645 var target
= self.target
646 var ind
= index
+ target
._first_byte
647 assert ind
<= target
._last_byte
648 return target
._items
[ind
]
651 redef fun iterator_from
(start
) do return new FlatStringByteIterator(target
, start
)
653 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator(target
, start
)
658 redef new do return new FlatBuffer
660 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
663 # Mutable strings of characters.
668 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
670 redef var bytes
= new FlatBufferByteView(self) is lazy
674 private var char_cache
: Int = -1
676 private var byte_cache
: Int = -1
678 private var capacity
= 0
680 # Real items, used as cache for when to_cstring is called
681 private var real_items
: NativeString is noinit
683 redef fun fast_cstring
do return _items
.fast_cstring
(0)
685 redef fun substrings
do return new FlatSubstringsIter(self)
687 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
689 # This happens when an operation modifies the current `Buffer` and
690 # the Copy-On-Write flag `written` is set at true.
692 var nns
= new NativeString(capacity
)
693 if _bytelen
!= 0 then _items
.copy_to
(nns
, _bytelen
, 0, 0)
698 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
700 # Internal only, does not modify _bytelen or length, this is the caller's responsability
701 private fun rshift_bytes
(from
: Int, len
: Int) do
705 if bt
+ len
> capacity
then
706 capacity
= capacity
* 2 + 2
707 nit
= new NativeString(capacity
)
708 oit
.copy_to
(nit
, 0, 0, from
)
710 oit
.copy_to
(nit
, bt
- from
, from
, from
+ len
)
713 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
715 # Internal only, does not modify _bytelen or length, this is the caller's responsability
716 private fun lshift_bytes
(from
: Int, len
: Int) do
718 it
.copy_to
(it
, _bytelen
- from
, from
, from
- len
)
721 redef fun []=(index
, item
)
723 assert index
>= 0 and index
<= length
724 if written
then reset
726 if index
== length
then
731 var ip
= it
.char_to_byte_index
(index
)
732 var c
= it
.char_at
(ip
)
733 var clen
= c
.u8char_len
734 var itemlen
= item
.u8char_len
735 var size_diff
= itemlen
- clen
736 if size_diff
> 0 then
737 rshift_bytes
(ip
+ clen
, size_diff
)
738 else if size_diff
< 0 then
739 lshift_bytes
(ip
+ clen
, -size_diff
)
741 _bytelen
+= size_diff
743 it
.set_char_at
(ip
, item
)
748 if written
then reset
750 var clen
= c
.u8char_len
753 _items
.set_char_at
(bt
, c
)
760 if written
then reset
765 redef fun empty
do return new Buffer
767 redef fun enlarge
(cap
)
770 if cap
<= c
then return
771 while c
<= cap
do c
= c
* 2 + 2
772 # The COW flag can be set at false here, since
773 # it does a copy of the current `Buffer`
776 var a
= new NativeString(c
+1)
779 if bln
> 0 then it
.copy_to
(a
, bln
, 0, 0)
789 if bln
== 0 then _items
= new NativeString(1)
790 return new FlatString.full
(_items
, bln
, 0, bln
- 1, length
)
797 var new_native
= new NativeString(bln
+ 1)
798 new_native
[bln
] = 0u8
799 if length
> 0 then _items
.copy_to
(new_native
, bln
, 0, 0)
800 real_items
= new_native
806 # Create a new empty string.
809 # Low-level creation a new buffer with given data.
811 # `_items` will be used as is, without copy, to store the characters of the buffer.
812 # Aliasing issues is the responsibility of the caller.
814 # If `_items` is shared, `written` should be set to true after the creation
815 # so that a modification will do a copy-on-write.
816 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
819 self.capacity
= capacity
820 self._bytelen
= bytelen
824 # Create a new string copied from `s`.
827 _items
= new NativeString(s
.bytelen
)
828 if s
isa FlatText then
831 for i
in substrings
do i
.as(FlatString)._items
.copy_to
(_items
, i
._bytelen
, 0, 0)
839 # Create a new empty string with a given capacity.
840 init with_capacity
(cap
: Int)
843 _items
= new NativeString(cap
+ 1)
850 if s
.is_empty
then return
853 var nln
= _bytelen
+ sl
855 if s
isa FlatText then
856 s
._items
.copy_to
(_items
, sl
, s
.first_byte
, _bytelen
)
858 for i
in s
.substrings
do append i
865 # Copies the content of self in `dest`
866 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
868 var self_chars
= self.chars
869 var dest_chars
= dest
.chars
870 for i
in [0..len-1
] do
871 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
875 redef fun substring
(from
, count
)
878 if from
< 0 then from
= 0
879 if (from
+ count
) > length
then count
= length
- from
880 if count
<= 0 then return new Buffer
882 var bytefrom
= its
.char_to_byte_index
(from
)
883 var byteto
= its
.char_to_byte_index
(count
+ from
- 1)
884 byteto
+= its
.char_at
(byteto
).u8char_len
- 1
885 var byte_length
= byteto
- bytefrom
+ 1
886 var r_items
= new NativeString(byte_length
)
887 its
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
888 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
894 var ns
= new FlatBuffer.with_capacity
(capacity
)
895 for i
in chars
.reverse_iterator
do ns
.add i
899 redef fun times
(repeats
)
902 var x
= new FlatString.full
(_items
, bln
, 0, bln
- 1, length
)
903 for i
in [1 .. repeats
[ do
910 if written
then reset
911 for i
in [0 .. length
[ do self[i
] = self[i
].to_upper
916 if written
then reset
917 for i
in [0 .. length
[ do self[i
] = self[i
].to_lower
921 private class FlatBufferByteReverseIterator
922 super IndexedIterator[Byte]
924 var target
: FlatBuffer
926 var target_items
: NativeString is noautoinit
930 init do target_items
= target
._items
932 redef fun index
do return curr_pos
934 redef fun is_ok
do return curr_pos
>= 0
936 redef fun item
do return target_items
[curr_pos
]
938 redef fun next
do curr_pos
-= 1
942 private class FlatBufferByteView
945 redef type SELFTYPE: FlatBuffer
947 redef fun [](index
) do return target
._items
[index
]
949 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator(target
, pos
)
951 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator(target
, pos
)
955 private class FlatBufferByteIterator
956 super IndexedIterator[Byte]
958 var target
: FlatBuffer
960 var target_items
: NativeString is noautoinit
964 init do target_items
= target
._items
966 redef fun index
do return curr_pos
968 redef fun is_ok
do return curr_pos
< target
._bytelen
970 redef fun item
do return target_items
[curr_pos
]
972 redef fun next
do curr_pos
+= 1
976 private class FlatBufferCharReverseIterator
977 super IndexedIterator[Char]
979 var target
: FlatBuffer
983 redef fun index
do return curr_pos
985 redef fun is_ok
do return curr_pos
>= 0
987 redef fun item
do return target
[curr_pos
]
989 redef fun next
do curr_pos
-= 1
993 private class FlatBufferCharView
996 redef type SELFTYPE: FlatBuffer
998 redef fun [](index
) do return target
[index
]
1000 redef fun []=(index
, item
)
1002 assert index
>= 0 and index
<= length
1003 if index
== length
then
1007 target
[index
] = item
1020 fun enlarge
(cap
: Int)
1027 var s_length
= s
.length
1028 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
1029 for i
in s
do target
.add i
1032 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator(target
, pos
)
1034 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator(target
, pos
)
1038 private class FlatBufferCharIterator
1039 super IndexedIterator[Char]
1041 var target
: FlatBuffer
1043 var max
: Int is noautoinit
1047 init do max
= target
.length
- 1
1049 redef fun index
do return curr_pos
1051 redef fun is_ok
do return curr_pos
<= max
1053 redef fun item
do return target
[curr_pos
]
1055 redef fun next
do curr_pos
+= 1
1059 redef class NativeString
1062 return to_s_with_length
(cstring_length
)
1065 # Returns `self` as a String of `length`.
1066 redef fun to_s_with_length
(length
): FlatString
1069 return clean_utf8
(length
)
1072 redef fun to_s_full
(bytelen
, unilen
) do
1073 return new FlatString.full
(self, bytelen
, 0, bytelen
- 1, unilen
)
1076 # Returns `self` as a new String.
1077 redef fun to_s_with_copy
: FlatString
1079 var length
= cstring_length
1080 var r
= clean_utf8
(length
)
1081 if r
.items
!= self then return r
1082 var new_self
= new NativeString(length
+ 1)
1083 copy_to
(new_self
, length
, 0, 0)
1084 var str
= new FlatString.with_infos
(new_self
, length
, 0, length
- 1)
1085 new_self
[length
] = 0u8
1086 str
.to_cstring
= new_self
1090 # Cleans a NativeString if necessary
1091 fun clean_utf8
(len
: Int): FlatString do
1092 var replacements
: nullable Array[Int] = null
1093 var end_length
= len
1098 var nxst
= length_of_char_at
(pos
)
1101 ok_st
= b
& 0x80u
8 == 0u8
1102 else if nxst
== 2 then
1103 ok_st
= b
& 0xE0u
8 == 0xC0u
8
1104 else if nxst
== 3 then
1105 ok_st
= b
& 0xF0u
8 == 0xE0u
8
1107 ok_st
= b
& 0xF8u
8 == 0xF0u
8
1110 if replacements
== null then replacements
= new Array[Int]
1111 replacements
.add pos
1118 var c
= char_at
(pos
)
1119 var cp
= c
.code_point
1121 ok_c
= cp
>= 0 and cp
<= 0x7F
1122 else if nxst
== 2 then
1123 ok_c
= cp
>= 0x80 and cp
<= 0x7FF
1124 else if nxst
== 3 then
1125 ok_c
= cp
>= 0x800 and cp
<= 0xFFFF
1126 ok_c
= ok_c
and not (cp
>= 0xD800 and cp
<= 0xDFFF) and cp
!= 0xFFFE and cp
!= 0xFFFF
1128 ok_c
= cp
>= 0x10000 and cp
<= 0x10FFFF
1131 if replacements
== null then replacements
= new Array[Int]
1132 replacements
.add pos
1142 if end_length
!= len
then
1143 ret
= new NativeString(end_length
)
1146 var repls
= replacements
.as(not null)
1147 var r
= repls
.items
.as(not null)
1148 var imax
= repls
.length
1149 for i
in [0 .. imax
[ do
1151 var chkln
= repl_pos
- old_repl
1152 copy_to
(ret
, chkln
, old_repl
, off
)
1155 ret
[off
+ 1] = 0xBFu
8
1156 ret
[off
+ 2] = 0xBDu
8
1157 old_repl
= repl_pos
+ 1
1160 copy_to
(ret
, len
- old_repl
, old_repl
, off
)
1162 return new FlatString.full
(ret
, end_length
, 0, end_length
- 1, chr_ln
)
1165 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
1167 # Very unsafe, make sure to have room for this char prior to calling this function.
1168 private fun set_char_at
(pos
: Int, c
: Char) do
1169 var ln
= c
.u8char_len
1170 native_set_char
(pos
, c
, ln
)
1173 private fun native_set_char
(pos
: Int, c
: Char, ln
: Int) `{
1174 char* dst = self + pos;
1180 dst[0] = 0xC0 | ((c & 0x7C0) >> 6);
1181 dst[1] = 0x80 | (c & 0x3F);
1184 dst[0] = 0xE0 | ((c & 0xF000) >> 12);
1185 dst[1] = 0x80 | ((c & 0xFC0) >> 6);
1186 dst[2] = 0x80 | (c & 0x3F);
1189 dst[0] = 0xF0 | ((c & 0x1C0000) >> 18);
1190 dst[1] = 0x80 | ((c & 0x3F000) >> 12);
1191 dst[2] = 0x80 | ((c & 0xFC0) >> 6);
1192 dst[3] = 0x80 | (c & 0x3F);
1199 redef fun to_base
(base
, signed
)
1201 var l
= digit_count
(base
)
1202 var s
= new FlatBuffer.from
(" " * l
)
1203 fill_buffer
(s
, base
, signed
)
1207 # return displayable int in base 10 and signed
1209 # assert 1.to_s == "1"
1210 # assert (-123).to_s == "-123"
1212 # Fast case for common numbers
1213 if self == 0 then return "0"
1214 if self == 1 then return "1"
1216 var nslen
= int_to_s_len
1217 var ns
= new NativeString(nslen
+ 1)
1219 native_int_to_s
(ns
, nslen
+ 1)
1220 return new FlatString.full
(ns
, nslen
, 0, nslen
- 1, nslen
)
1224 redef class Array[E
]
1226 # Fast implementation
1227 redef fun plain_to_s
1230 if l
== 0 then return ""
1231 var its
= _items
.as(not null)
1233 if l
== 1 then if first
== null then return "" else return first
.to_s
1234 var na
= new NativeArray[String](l
)
1240 if itsi
== null then
1250 var ns
= new NativeString(sl
+ 1)
1256 if tmp
isa FlatString then
1257 var tpl
= tmp
._bytelen
1258 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1261 for j
in tmp
.substrings
do
1262 var s
= j
.as(FlatString)
1263 var slen
= s
._bytelen
1264 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1270 return new FlatString.with_infos
(ns
, sl
, 0, sl
- 1)
1274 redef class NativeArray[E
]
1275 redef fun native_to_s
do
1276 assert self isa NativeArray[String]
1287 var ns
= new NativeString(sl
+ 1)
1293 if tmp
isa FlatString then
1294 var tpl
= tmp
._bytelen
1295 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1298 for j
in tmp
.substrings
do
1299 var s
= j
.as(FlatString)
1300 var slen
= s
._bytelen
1301 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1307 return new FlatString.with_infos
(ns
, sl
, 0, sl
- 1)
1311 redef class Map[K
,V
]
1312 redef fun join
(sep
, couple_sep
)
1314 if is_empty
then return ""
1316 var s
= new Buffer # Result
1322 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1324 # Concat other _items
1330 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")