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 private fun first_byte
: Int do return 0
41 private fun last_byte
: Int do return _bytelen
- 1
43 # Cache of the latest position (char) explored in the string
44 private var position
: Int = 0
46 # Cached position (bytes) in the NativeString underlying the String
47 private var bytepos
: Int = 0
49 # Index of the character `index` in `_items`
50 private 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 C, how many more bytes will be needed ?
90 # This enables a double-optimization in `escape_to_c` since if this
91 # method returns 0, then `self` does not need escaping and can be
93 protected fun chars_to_escape_to_c
: Int do
102 else if c
== 0x09u
8 then
104 else if c
== 0x22u
8 then
106 else if c
== 0x27u
8 then
108 else if c
== 0x5Cu
8 then
110 else if c
< 32u8
then
118 redef fun escape_to_c
do
119 var ln_extra
= chars_to_escape_to_c
120 if ln_extra
== 0 then return self.to_s
123 var nlen
= _bytelen
+ ln_extra
124 var nns
= new NativeString(nlen
)
131 # Any byte with value < 32 is a control character
132 # All their uses will be replaced by their octal
135 # There are two exceptions however:
140 # Aside from the code points above, the following are:
147 nns
[opos
+ 1] = 0x74u
8
149 else if c
== 0x0Au
8 then
151 nns
[opos
+ 1] = 0x6Eu
8
153 else if c
== 0x22u
8 then
155 nns
[opos
+ 1] = 0x22u
8
157 else if c
== 0x27u
8 then
159 nns
[opos
+ 1] = 0x27u
8
161 else if c
== 0x5Cu
8 then
163 nns
[opos
+ 1] = 0x5Cu
8
165 else if c
< 32u8
then
167 nns
[opos
+ 1] = 0x30u
8
168 nns
[opos
+ 2] = ((c
& 0x38u
8) >> 3) + 0x30u
8
169 nns
[opos
+ 3] = (c
& 0x07u
8) + 0x30u
8
177 return nns
.to_s_with_length
(nlen
)
180 redef fun [](index
) do return _items
.char_at
(char_to_byte_index
(index
))
183 # Immutable strings of characters.
188 # Index at which `self` begins in `_items`, inclusively
189 redef var first_byte
is noinit
191 # Index at which `self` ends in `_items`, inclusively
192 redef var last_byte
is noinit
194 redef var chars
= new FlatStringCharView(self) is lazy
196 redef var bytes
= new FlatStringByteView(self) is lazy
198 redef var length
is lazy
do
199 if _bytelen
== 0 then return 0
205 st
+= its
.length_of_char_at
(st
)
213 var b
= new FlatBuffer.with_capacity
(_bytelen
+ 1)
214 for i
in [length
- 1 .. 0].step
(-1) do
217 var s
= b
.to_s
.as(FlatString)
218 s
.length
= self.length
222 redef fun fast_cstring
do return _items
.fast_cstring
(_first_byte
)
224 redef fun substring
(from
, count
)
230 if count
< 0 then count
= 0
234 if (count
+ from
) > length
then count
= length
- from
235 if count
<= 0 then return ""
236 var end_index
= from
+ count
- 1
238 var bytefrom
= char_to_byte_index
(from
)
239 var byteto
= char_to_byte_index
(end_index
)
241 byteto
+= its
.length_of_char_at
(byteto
) - 1
243 var s
= new FlatString.full
(its
, byteto
- bytefrom
+ 1, bytefrom
, byteto
, count
)
247 redef fun empty
do return "".as(FlatString)
251 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
257 outstr
.add
(chars
[pos
].to_upper
)
266 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
272 outstr
.add
(chars
[pos
].to_lower
)
281 for i
in chars
do i
.output
284 ##################################################
285 # String Specific Methods #
286 ##################################################
288 # Low-level creation of a new string with minimal data.
290 # `_items` will be used as is, without copy, to retrieve the characters of the string.
291 # Aliasing issues is the responsibility of the caller.
292 private init with_infos
(items
: NativeString, bytelen
, from
, to
: Int)
295 self._bytelen
= bytelen
301 # Low-level creation of a new string with all the data.
303 # `_items` will be used as is, without copy, to retrieve the characters of the string.
304 # Aliasing issues is the responsibility of the caller.
305 private init full
(items
: NativeString, bytelen
, from
, to
, length
: Int)
309 self._bytelen
= bytelen
315 redef fun to_cstring
do
316 if real_items
!= null then return real_items
.as(not null)
318 var new_items
= new NativeString(blen
+ 1)
319 _items
.copy_to
(new_items
, blen
, _first_byte
, 0)
320 new_items
[blen
] = 0u8
321 real_items
= new_items
327 if not other
isa FlatString then return super
329 if self.object_id
== other
.object_id
then return true
331 var my_length
= _bytelen
333 if other
._bytelen
!= my_length
then return false
335 var my_index
= _first_byte
336 var its_index
= other
._first_byte
338 var last_iteration
= my_index
+ my_length
340 var its_items
= other
._items
341 var my_items
= self._items
343 while my_index
< last_iteration
do
344 if my_items
[my_index
] != its_items
[its_index
] then return false
354 if not other
isa FlatString then return super
356 if self.object_id
== other
.object_id
then return false
358 var my_length
= self._bytelen
359 var its_length
= other
._bytelen
361 var max
= if my_length
< its_length
then my_length
else its_length
363 var myits
= self.bytes
364 var itsits
= other
.bytes
366 for i
in [0 .. max
[ do
367 var my_curr_char
= myits
[i
]
368 var its_curr_char
= itsits
[i
]
370 if my_curr_char
!= its_curr_char
then
371 if my_curr_char
< its_curr_char
then return true
376 return my_length
< its_length
383 var nlen
= mlen
+ slen
385 var mifrom
= _first_byte
386 if s
isa FlatText then
388 var sifrom
= s
.first_byte
389 var ns
= new NativeString(nlen
+ 1)
390 mits
.copy_to
(ns
, mlen
, mifrom
, 0)
391 sits
.copy_to
(ns
, slen
, sifrom
, mlen
)
392 return new FlatString.full
(ns
, nlen
, 0, nlen
- 1, length
+ o
.length
)
399 var mybtlen
= _bytelen
400 var new_bytelen
= mybtlen
* i
402 var newlen
= mylen
* i
405 var ns
= new NativeString(new_bytelen
+ 1)
406 ns
[new_bytelen
] = 0u8
409 its
.copy_to
(ns
, mybtlen
, fb
, offset
)
413 return new FlatString.full
(ns
, new_bytelen
, 0, new_bytelen
- 1, newlen
)
419 if hash_cache
== null then
420 # djb2 hash algorithm
424 var my_items
= _items
428 h
= (h
<< 5) + h
+ my_items
[i
].to_i
435 return hash_cache
.as(not null)
438 redef fun substrings
do return new FlatSubstringsIter(self)
441 private class FlatStringCharReverseIterator
442 super IndexedIterator[Char]
444 var target
: FlatString
448 init with_pos
(tgt
: FlatString, pos
: Int)
453 redef fun is_ok
do return curr_pos
>= 0
455 redef fun item
do return target
[curr_pos
]
457 redef fun next
do curr_pos
-= 1
459 redef fun index
do return curr_pos
463 private class FlatStringCharIterator
464 super IndexedIterator[Char]
466 var target
: FlatString
472 init with_pos
(tgt
: FlatString, pos
: Int)
474 init(tgt
, tgt
.length
- 1, pos
)
477 redef fun is_ok
do return curr_pos
<= max
479 redef fun item
do return target
[curr_pos
]
481 redef fun next
do curr_pos
+= 1
483 redef fun index
do return curr_pos
487 private class FlatStringCharView
490 redef type SELFTYPE: FlatString
492 redef fun [](index
) do return target
[index
]
494 redef fun iterator_from
(start
) do return new FlatStringCharIterator.with_pos
(target
, start
)
496 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator.with_pos
(target
, start
)
500 private class FlatStringByteReverseIterator
501 super IndexedIterator[Byte]
503 var target
: FlatString
505 var target_items
: NativeString
509 init with_pos
(tgt
: FlatString, pos
: Int)
511 init(tgt
, tgt
._items
, pos
+ tgt
._first_byte
)
514 redef fun is_ok
do return curr_pos
>= target
._first_byte
516 redef fun item
do return target_items
[curr_pos
]
518 redef fun next
do curr_pos
-= 1
520 redef fun index
do return curr_pos
- target
._first_byte
524 private class FlatStringByteIterator
525 super IndexedIterator[Byte]
527 var target
: FlatString
529 var target_items
: NativeString
533 init with_pos
(tgt
: FlatString, pos
: Int)
535 init(tgt
, tgt
._items
, pos
+ tgt
._first_byte
)
538 redef fun is_ok
do return curr_pos
<= target
._last_byte
540 redef fun item
do return target_items
[curr_pos
]
542 redef fun next
do curr_pos
+= 1
544 redef fun index
do return curr_pos
- target
._first_byte
548 private class FlatStringByteView
551 redef type SELFTYPE: FlatString
555 # Check that the index (+ _first_byte) is not larger than _last_byte
556 # In other terms, if the index is valid
558 var target
= self.target
559 var ind
= index
+ target
._first_byte
560 assert ind
<= target
._last_byte
561 return target
._items
[ind
]
564 redef fun iterator_from
(start
) do return new FlatStringByteIterator.with_pos
(target
, start
)
566 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator.with_pos
(target
, start
)
571 redef new do return new FlatBuffer
573 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
576 # Mutable strings of characters.
581 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
583 redef var bytes
= new FlatBufferByteView(self) is lazy
587 private var char_cache
: Int = -1
589 private var byte_cache
: Int = -1
591 private var capacity
= 0
593 redef fun fast_cstring
do return _items
.fast_cstring
(0)
595 redef fun substrings
do return new FlatSubstringsIter(self)
597 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
599 # This happens when an operation modifies the current `Buffer` and
600 # the Copy-On-Write flag `written` is set at true.
602 var nns
= new NativeString(capacity
)
603 if _bytelen
!= 0 then _items
.copy_to
(nns
, _bytelen
, 0, 0)
608 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
610 # Internal only, does not modify _bytelen or length, this is the caller's responsability
611 private fun rshift_bytes
(from
: Int, len
: Int) do
615 if bt
+ len
> capacity
then
616 capacity
= capacity
* 2 + 2
617 nit
= new NativeString(capacity
)
618 oit
.copy_to
(nit
, 0, 0, from
)
620 oit
.copy_to
(nit
, bt
- from
, from
, from
+ len
)
623 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
625 # Internal only, does not modify _bytelen or length, this is the caller's responsability
626 private fun lshift_bytes
(from
: Int, len
: Int) do
628 it
.copy_to
(it
, _bytelen
- from
, from
, from
- len
)
631 redef fun []=(index
, item
)
633 assert index
>= 0 and index
<= length
634 if written
then reset
636 if index
== length
then
641 var ip
= it
.char_to_byte_index
(index
)
642 var c
= it
.char_at
(ip
)
643 var clen
= c
.u8char_len
644 var itemlen
= item
.u8char_len
645 var size_diff
= itemlen
- clen
646 if size_diff
> 0 then
647 rshift_bytes
(ip
+ clen
, size_diff
)
648 else if size_diff
< 0 then
649 lshift_bytes
(ip
+ clen
, -size_diff
)
651 _bytelen
+= size_diff
653 it
.set_char_at
(ip
, item
)
658 if written
then reset
660 var clen
= c
.u8char_len
663 _items
.set_char_at
(bt
, c
)
670 if written
then reset
675 redef fun empty
do return new Buffer
677 redef fun enlarge
(cap
)
680 if cap
<= c
then return
681 while c
<= cap
do c
= c
* 2 + 2
682 # The COW flag can be set at false here, since
683 # it does a copy of the current `Buffer`
686 var a
= new NativeString(c
+1)
689 if bln
> 0 then it
.copy_to
(a
, bln
, 0, 0)
699 if bln
== 0 then _items
= new NativeString(1)
700 return new FlatString.full
(_items
, bln
, 0, bln
- 1, length
)
707 var new_native
= new NativeString(bln
+ 1)
708 new_native
[bln
] = 0u8
709 if length
> 0 then _items
.copy_to
(new_native
, bln
, 0, 0)
710 real_items
= new_native
713 return real_items
.as(not null)
716 # Create a new empty string.
719 # Low-level creation a new buffer with given data.
721 # `_items` will be used as is, without copy, to store the characters of the buffer.
722 # Aliasing issues is the responsibility of the caller.
724 # If `_items` is shared, `written` should be set to true after the creation
725 # so that a modification will do a copy-on-write.
726 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
729 self.capacity
= capacity
730 self._bytelen
= bytelen
734 # Create a new string copied from `s`.
737 _items
= new NativeString(s
.bytelen
)
738 if s
isa FlatText then
741 for i
in substrings
do i
.as(FlatString)._items
.copy_to
(_items
, i
._bytelen
, 0, 0)
749 # Create a new empty string with a given capacity.
750 init with_capacity
(cap
: Int)
753 _items
= new NativeString(cap
+ 1)
760 if s
.is_empty
then return
763 var nln
= _bytelen
+ sl
765 if s
isa FlatText then
766 s
._items
.copy_to
(_items
, sl
, s
.first_byte
, _bytelen
)
768 for i
in s
.substrings
do append i
775 # Copies the content of self in `dest`
776 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
778 var self_chars
= self.chars
779 var dest_chars
= dest
.chars
780 for i
in [0..len-1
] do
781 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
785 redef fun substring
(from
, count
)
788 if from
< 0 then from
= 0
789 if (from
+ count
) > length
then count
= length
- from
792 var bytefrom
= its
.char_to_byte_index
(from
)
793 var byteto
= its
.char_to_byte_index
(count
+ from
- 1)
794 byteto
+= its
.char_at
(byteto
).u8char_len
- 1
795 var byte_length
= byteto
- bytefrom
+ 1
796 var r_items
= new NativeString(byte_length
)
797 its
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
798 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
807 var ns
= new FlatBuffer.with_capacity
(capacity
)
808 for i
in chars
.reverse_iterator
do ns
.add i
812 redef fun times
(repeats
)
815 var x
= new FlatString.full
(_items
, bln
, 0, bln
- 1, length
)
816 for i
in [1 .. repeats
[ do
823 if written
then reset
824 for i
in [0 .. length
[ do self[i
] = self[i
].to_upper
829 if written
then reset
830 for i
in [0 .. length
[ do self[i
] = self[i
].to_lower
834 private class FlatBufferByteReverseIterator
835 super IndexedIterator[Byte]
837 var target
: FlatBuffer
839 var target_items
: NativeString
843 init with_pos
(tgt
: FlatBuffer, pos
: Int)
845 init(tgt
, tgt
._items
, pos
)
848 redef fun index
do return curr_pos
850 redef fun is_ok
do return curr_pos
>= 0
852 redef fun item
do return target_items
[curr_pos
]
854 redef fun next
do curr_pos
-= 1
858 private class FlatBufferByteView
861 redef type SELFTYPE: FlatBuffer
863 redef fun [](index
) do return target
._items
[index
]
865 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator.with_pos
(target
, pos
)
867 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator.with_pos
(target
, pos
)
871 private class FlatBufferByteIterator
872 super IndexedIterator[Byte]
874 var target
: FlatBuffer
876 var target_items
: NativeString
880 init with_pos
(tgt
: FlatBuffer, pos
: Int)
882 init(tgt
, tgt
._items
, pos
)
885 redef fun index
do return curr_pos
887 redef fun is_ok
do return curr_pos
< target
._bytelen
889 redef fun item
do return target_items
[curr_pos
]
891 redef fun next
do curr_pos
+= 1
895 private class FlatBufferCharReverseIterator
896 super IndexedIterator[Char]
898 var target
: FlatBuffer
902 init with_pos
(tgt
: FlatBuffer, pos
: Int)
907 redef fun index
do return curr_pos
909 redef fun is_ok
do return curr_pos
>= 0
911 redef fun item
do return target
[curr_pos
]
913 redef fun next
do curr_pos
-= 1
917 private class FlatBufferCharView
920 redef type SELFTYPE: FlatBuffer
922 redef fun [](index
) do return target
[index
]
924 redef fun []=(index
, item
)
926 assert index
>= 0 and index
<= length
927 if index
== length
then
944 fun enlarge
(cap
: Int)
951 var s_length
= s
.length
952 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
953 for i
in s
do target
.add i
956 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator.with_pos
(target
, pos
)
958 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator.with_pos
(target
, pos
)
962 private class FlatBufferCharIterator
963 super IndexedIterator[Char]
965 var target
: FlatBuffer
971 init with_pos
(tgt
: FlatBuffer, pos
: Int)
973 init(tgt
, tgt
.length
- 1, pos
)
976 redef fun index
do return curr_pos
978 redef fun is_ok
do return curr_pos
<= max
980 redef fun item
do return target
[curr_pos
]
982 redef fun next
do curr_pos
+= 1
986 redef class NativeString
989 return to_s_with_length
(cstring_length
)
992 # Returns `self` as a String of `length`.
993 redef fun to_s_with_length
(length
): FlatString
996 var str
= new FlatString.with_infos
(self, length
, 0, length
- 1)
1000 redef fun to_s_full
(bytelen
, unilen
) do
1001 return new FlatString.full
(self, bytelen
, 0, bytelen
- 1, unilen
)
1004 # Returns `self` as a new String.
1005 redef fun to_s_with_copy
: FlatString
1007 var length
= cstring_length
1008 var new_self
= new NativeString(length
+ 1)
1009 copy_to
(new_self
, length
, 0, 0)
1010 var str
= new FlatString.with_infos
(new_self
, length
, 0, length
- 1)
1011 new_self
[length
] = 0u8
1012 str
.real_items
= new_self
1016 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
1018 # Very unsafe, make sure to have room for this char prior to calling this function.
1019 private fun set_char_at
(pos
: Int, c
: Char) do
1020 var ln
= c
.u8char_len
1021 native_set_char
(pos
, c
, ln
)
1024 private fun native_set_char
(pos
: Int, c
: Char, ln
: Int) `{
1025 char* dst = self + pos;
1031 dst[0] = 0xC0 | ((c & 0x7C0) >> 6);
1032 dst[1] = 0x80 | (c & 0x3F);
1035 dst[0] = 0xE0 | ((c & 0xF000) >> 12);
1036 dst[1] = 0x80 | ((c & 0xFC0) >> 6);
1037 dst[2] = 0x80 | (c & 0x3F);
1040 dst[0] = 0xF0 | ((c & 0x1C0000) >> 18);
1041 dst[1] = 0x80 | ((c & 0x3F000) >> 12);
1042 dst[2] = 0x80 | ((c & 0xFC0) >> 6);
1043 dst[3] = 0x80 | (c & 0x3F);
1050 redef fun to_base
(base
, signed
)
1052 var l
= digit_count
(base
)
1053 var s
= new FlatBuffer.from
(" " * l
)
1054 fill_buffer
(s
, base
, signed
)
1058 # return displayable int in base 10 and signed
1060 # assert 1.to_s == "1"
1061 # assert (-123).to_s == "-123"
1063 # Fast case for common numbers
1064 if self == 0 then return "0"
1065 if self == 1 then return "1"
1067 var nslen
= int_to_s_len
1068 var ns
= new NativeString(nslen
+ 1)
1070 native_int_to_s
(ns
, nslen
+ 1)
1071 return new FlatString.full
(ns
, nslen
, 0, nslen
- 1, nslen
)
1075 redef class Array[E
]
1077 # Fast implementation
1078 redef fun plain_to_s
1081 if l
== 0 then return ""
1082 if l
== 1 then if self[0] == null then return "" else return self[0].to_s
1084 var na
= new NativeArray[String](l
)
1090 if itsi
== null then
1100 var ns
= new NativeString(sl
+ 1)
1106 if tmp
isa FlatString then
1107 var tpl
= tmp
._bytelen
1108 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1111 for j
in tmp
.substrings
do
1112 var s
= j
.as(FlatString)
1113 var slen
= s
._bytelen
1114 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1120 return ns
.to_s_with_length
(sl
)
1124 redef class NativeArray[E
]
1125 redef fun native_to_s
do
1126 assert self isa NativeArray[String]
1137 var ns
= new NativeString(sl
+ 1)
1143 if tmp
isa FlatString then
1144 var tpl
= tmp
._bytelen
1145 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1148 for j
in tmp
.substrings
do
1149 var s
= j
.as(FlatString)
1150 var slen
= s
._bytelen
1151 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1157 return ns
.to_s_with_length
(sl
)
1161 redef class Map[K
,V
]
1162 redef fun join
(sep
, couple_sep
)
1164 if is_empty
then return ""
1166 var s
= new Buffer # Result
1172 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1174 # Concat other _items
1180 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")