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
46 # Cached position (bytes) in the NativeString underlying the String
47 var bytepos
: Int = first_byte
is lateinit
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 private fun byte_to_char_index
(index
: Int): Int do
94 # Find best insertion point
95 var delta_begin
= index
96 var delta_end
= (ln
- 1) - index
97 var delta_cache
= (pos
- index
).abs
101 if delta_cache
< min
then min
= delta_cache
102 if delta_end
< min
then min
= delta_end
107 if min
== delta_begin
then
110 else if min
== delta_cache
then
114 ns_i
= its
.find_beginning_of_char_at
(last_byte
)
118 my_i
= its
.byte_to_char_index_cached
(index
, my_i
, ns_i
)
126 redef fun [](index
) do return _items
.char_at
(char_to_byte_index
(index
))
129 # Immutable strings of characters.
134 # Index at which `self` begins in `_items`, inclusively
135 redef var first_byte
is noinit
137 # Index at which `self` ends in `_items`, inclusively
138 redef var last_byte
is noinit
140 redef var chars
= new FlatStringCharView(self) is lazy
142 redef var bytes
= new FlatStringByteView(self) is lazy
144 redef var length
is lazy
do
145 if _bytelen
== 0 then return 0
151 st
+= its
.length_of_char_at
(st
)
159 var b
= new FlatBuffer.with_capacity
(_bytelen
+ 1)
160 for i
in [length
- 1 .. 0].step
(-1) do
163 var s
= b
.to_s
.as(FlatString)
164 s
.length
= self.length
168 redef fun fast_cstring
do return _items
.fast_cstring
(_first_byte
)
170 redef fun substring
(from
, count
)
176 if count
< 0 then count
= 0
180 if (count
+ from
) > length
then count
= length
- from
181 if count
<= 0 then return ""
182 var end_index
= from
+ count
- 1
184 var bytefrom
= char_to_byte_index
(from
)
185 var byteto
= char_to_byte_index
(end_index
)
187 byteto
+= its
.length_of_char_at
(byteto
) - 1
189 var s
= new FlatString.full
(its
, byteto
- bytefrom
+ 1, bytefrom
, byteto
, count
)
193 redef fun empty
do return "".as(FlatString)
197 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
203 outstr
.add
(chars
[pos
].to_upper
)
212 var outstr
= new FlatBuffer.with_capacity
(self._bytelen
+ 1)
218 outstr
.add
(chars
[pos
].to_lower
)
227 for i
in chars
do i
.output
230 ##################################################
231 # String Specific Methods #
232 ##################################################
234 # Low-level creation of a new string with minimal data.
236 # `_items` will be used as is, without copy, to retrieve the characters of the string.
237 # Aliasing issues is the responsibility of the caller.
238 private init with_infos
(items
: NativeString, bytelen
, from
, to
: Int)
241 self._bytelen
= bytelen
246 # Low-level creation of a new string with all the data.
248 # `_items` will be used as is, without copy, to retrieve the characters of the string.
249 # Aliasing issues is the responsibility of the caller.
250 private init full
(items
: NativeString, bytelen
, from
, to
, length
: Int)
254 self._bytelen
= bytelen
259 redef fun to_cstring
do
260 if real_items
!= null then return real_items
.as(not null)
262 var new_items
= new NativeString(blen
+ 1)
263 _items
.copy_to
(new_items
, blen
, _first_byte
, 0)
264 new_items
[blen
] = 0u8
265 real_items
= new_items
271 if not other
isa FlatString then return super
273 if self.object_id
== other
.object_id
then return true
275 var my_length
= _bytelen
277 if other
._bytelen
!= my_length
then return false
279 var my_index
= _first_byte
280 var its_index
= other
._first_byte
282 var last_iteration
= my_index
+ my_length
284 var its_items
= other
._items
285 var my_items
= self._items
287 while my_index
< last_iteration
do
288 if my_items
[my_index
] != its_items
[its_index
] then return false
298 if not other
isa FlatString then return super
300 if self.object_id
== other
.object_id
then return false
302 var my_length
= self._bytelen
303 var its_length
= other
._bytelen
305 var max
= if my_length
< its_length
then my_length
else its_length
307 var myits
= self.bytes
308 var itsits
= other
.bytes
310 for i
in [0 .. max
[ do
311 var my_curr_char
= myits
[i
]
312 var its_curr_char
= itsits
[i
]
314 if my_curr_char
!= its_curr_char
then
315 if my_curr_char
< its_curr_char
then return true
320 return my_length
< its_length
327 var nlen
= mlen
+ slen
329 var mifrom
= _first_byte
330 if s
isa FlatText then
332 var sifrom
= s
.first_byte
333 var ns
= new NativeString(nlen
+ 1)
334 mits
.copy_to
(ns
, mlen
, mifrom
, 0)
335 sits
.copy_to
(ns
, slen
, sifrom
, mlen
)
336 return new FlatString.full
(ns
, nlen
, 0, nlen
- 1, length
+ o
.length
)
343 var mybtlen
= _bytelen
344 var new_bytelen
= mybtlen
* i
346 var newlen
= mylen
* i
349 var ns
= new NativeString(new_bytelen
+ 1)
350 ns
[new_bytelen
] = 0u8
353 its
.copy_to
(ns
, mybtlen
, fb
, offset
)
357 return new FlatString.full
(ns
, new_bytelen
, 0, new_bytelen
- 1, newlen
)
363 if hash_cache
== null then
364 # djb2 hash algorithm
368 var my_items
= _items
372 h
= (h
<< 5) + h
+ my_items
[i
].to_i
379 return hash_cache
.as(not null)
382 redef fun substrings
do return new FlatSubstringsIter(self)
385 private class FlatStringCharReverseIterator
386 super IndexedIterator[Char]
388 var target
: FlatString
392 init with_pos
(tgt
: FlatString, pos
: Int)
397 redef fun is_ok
do return curr_pos
>= 0
399 redef fun item
do return target
[curr_pos
]
401 redef fun next
do curr_pos
-= 1
403 redef fun index
do return curr_pos
407 private class FlatStringCharIterator
408 super IndexedIterator[Char]
410 var target
: FlatString
416 init with_pos
(tgt
: FlatString, pos
: Int)
418 init(tgt
, tgt
.length
- 1, pos
)
421 redef fun is_ok
do return curr_pos
<= max
423 redef fun item
do return target
[curr_pos
]
425 redef fun next
do curr_pos
+= 1
427 redef fun index
do return curr_pos
431 private class FlatStringCharView
434 redef type SELFTYPE: FlatString
436 redef fun [](index
) do return target
[index
]
438 redef fun iterator_from
(start
) do return new FlatStringCharIterator.with_pos
(target
, start
)
440 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator.with_pos
(target
, start
)
444 private class FlatStringByteReverseIterator
445 super IndexedIterator[Byte]
447 var target
: FlatString
449 var target_items
: NativeString
453 init with_pos
(tgt
: FlatString, pos
: Int)
455 init(tgt
, tgt
._items
, pos
+ tgt
._first_byte
)
458 redef fun is_ok
do return curr_pos
>= target
._first_byte
460 redef fun item
do return target_items
[curr_pos
]
462 redef fun next
do curr_pos
-= 1
464 redef fun index
do return curr_pos
- target
._first_byte
468 private class FlatStringByteIterator
469 super IndexedIterator[Byte]
471 var target
: FlatString
473 var target_items
: NativeString
477 init with_pos
(tgt
: FlatString, pos
: Int)
479 init(tgt
, tgt
._items
, pos
+ tgt
._first_byte
)
482 redef fun is_ok
do return curr_pos
<= target
._last_byte
484 redef fun item
do return target_items
[curr_pos
]
486 redef fun next
do curr_pos
+= 1
488 redef fun index
do return curr_pos
- target
._first_byte
492 private class FlatStringByteView
495 redef type SELFTYPE: FlatString
499 # Check that the index (+ _first_byte) is not larger than _last_byte
500 # In other terms, if the index is valid
502 var target
= self.target
503 var ind
= index
+ target
._first_byte
504 assert ind
<= target
._last_byte
505 return target
._items
[ind
]
508 redef fun iterator_from
(start
) do return new FlatStringByteIterator.with_pos
(target
, start
)
510 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator.with_pos
(target
, start
)
515 redef new do return new FlatBuffer
517 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
520 # Mutable strings of characters.
525 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
527 redef var bytes
= new FlatBufferByteView(self) is lazy
531 private var char_cache
: Int = -1
533 private var byte_cache
: Int = -1
535 private var capacity
= 0
537 redef fun fast_cstring
do return _items
.fast_cstring
(0)
539 redef fun substrings
do return new FlatSubstringsIter(self)
541 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
543 # This happens when an operation modifies the current `Buffer` and
544 # the Copy-On-Write flag `written` is set at true.
546 var nns
= new NativeString(capacity
)
547 if _bytelen
!= 0 then _items
.copy_to
(nns
, _bytelen
, 0, 0)
552 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
554 # Internal only, does not modify _bytelen or length, this is the caller's responsability
555 private fun rshift_bytes
(from
: Int, len
: Int) do
559 if bt
+ len
> capacity
then
560 capacity
= capacity
* 2 + 2
561 nit
= new NativeString(capacity
)
562 oit
.copy_to
(nit
, 0, 0, from
)
564 oit
.copy_to
(nit
, bt
- from
, from
, from
+ len
)
567 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
569 # Internal only, does not modify _bytelen or length, this is the caller's responsability
570 private fun lshift_bytes
(from
: Int, len
: Int) do
572 it
.copy_to
(it
, _bytelen
- from
, from
, from
- len
)
575 redef fun []=(index
, item
)
577 assert index
>= 0 and index
<= length
578 if written
then reset
580 if index
== length
then
585 var ip
= it
.char_to_byte_index
(index
)
586 var c
= it
.char_at
(ip
)
587 var clen
= c
.u8char_len
588 var itemlen
= item
.u8char_len
589 var size_diff
= itemlen
- clen
590 if size_diff
> 0 then
591 rshift_bytes
(ip
+ clen
, size_diff
)
592 else if size_diff
< 0 then
593 lshift_bytes
(ip
+ clen
, -size_diff
)
595 _bytelen
+= size_diff
597 it
.set_char_at
(ip
, item
)
602 if written
then reset
604 var clen
= c
.u8char_len
607 _items
.set_char_at
(bt
, c
)
614 if written
then reset
619 redef fun empty
do return new Buffer
621 redef fun enlarge
(cap
)
624 if cap
<= c
then return
625 while c
<= cap
do c
= c
* 2 + 2
626 # The COW flag can be set at false here, since
627 # it does a copy of the current `Buffer`
630 var a
= new NativeString(c
+1)
633 if bln
> 0 then it
.copy_to
(a
, bln
, 0, 0)
643 if bln
== 0 then _items
= new NativeString(1)
644 return new FlatString.full
(_items
, bln
, 0, bln
- 1, length
)
651 var new_native
= new NativeString(bln
+ 1)
652 new_native
[bln
] = 0u8
653 if length
> 0 then _items
.copy_to
(new_native
, bln
, 0, 0)
654 real_items
= new_native
657 return real_items
.as(not null)
660 # Create a new empty string.
663 # Low-level creation a new buffer with given data.
665 # `_items` will be used as is, without copy, to store the characters of the buffer.
666 # Aliasing issues is the responsibility of the caller.
668 # If `_items` is shared, `written` should be set to true after the creation
669 # so that a modification will do a copy-on-write.
670 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
673 self.capacity
= capacity
674 self._bytelen
= bytelen
678 # Create a new string copied from `s`.
681 _items
= new NativeString(s
.bytelen
)
682 if s
isa FlatText then
685 for i
in substrings
do i
.as(FlatString)._items
.copy_to
(_items
, i
._bytelen
, 0, 0)
693 # Create a new empty string with a given capacity.
694 init with_capacity
(cap
: Int)
697 _items
= new NativeString(cap
+ 1)
704 if s
.is_empty
then return
707 var nln
= _bytelen
+ sl
709 if s
isa FlatText then
710 s
._items
.copy_to
(_items
, sl
, s
.first_byte
, _bytelen
)
712 for i
in s
.substrings
do append i
719 # Copies the content of self in `dest`
720 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
722 var self_chars
= self.chars
723 var dest_chars
= dest
.chars
724 for i
in [0..len-1
] do
725 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
729 redef fun substring
(from
, count
)
732 if from
< 0 then from
= 0
733 if (from
+ count
) > length
then count
= length
- from
736 var bytefrom
= its
.char_to_byte_index
(from
)
737 var byteto
= its
.char_to_byte_index
(count
+ from
- 1)
738 byteto
+= its
.char_at
(byteto
).u8char_len
- 1
739 var byte_length
= byteto
- bytefrom
+ 1
740 var r_items
= new NativeString(byte_length
)
741 its
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
742 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
751 var ns
= new FlatBuffer.with_capacity
(capacity
)
752 for i
in chars
.reverse_iterator
do ns
.add i
756 redef fun times
(repeats
)
759 var x
= new FlatString.full
(_items
, bln
, 0, bln
- 1, length
)
760 for i
in [1 .. repeats
[ do
767 if written
then reset
768 for i
in [0 .. length
[ do self[i
] = self[i
].to_upper
773 if written
then reset
774 for i
in [0 .. length
[ do self[i
] = self[i
].to_lower
778 private class FlatBufferByteReverseIterator
779 super IndexedIterator[Byte]
781 var target
: FlatBuffer
783 var target_items
: NativeString
787 init with_pos
(tgt
: FlatBuffer, pos
: Int)
789 init(tgt
, tgt
._items
, pos
)
792 redef fun index
do return curr_pos
794 redef fun is_ok
do return curr_pos
>= 0
796 redef fun item
do return target_items
[curr_pos
]
798 redef fun next
do curr_pos
-= 1
802 private class FlatBufferByteView
805 redef type SELFTYPE: FlatBuffer
807 redef fun [](index
) do return target
._items
[index
]
809 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator.with_pos
(target
, pos
)
811 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator.with_pos
(target
, pos
)
815 private class FlatBufferByteIterator
816 super IndexedIterator[Byte]
818 var target
: FlatBuffer
820 var target_items
: NativeString
824 init with_pos
(tgt
: FlatBuffer, pos
: Int)
826 init(tgt
, tgt
._items
, pos
)
829 redef fun index
do return curr_pos
831 redef fun is_ok
do return curr_pos
< target
._bytelen
833 redef fun item
do return target_items
[curr_pos
]
835 redef fun next
do curr_pos
+= 1
839 private class FlatBufferCharReverseIterator
840 super IndexedIterator[Char]
842 var target
: FlatBuffer
846 init with_pos
(tgt
: FlatBuffer, pos
: Int)
851 redef fun index
do return curr_pos
853 redef fun is_ok
do return curr_pos
>= 0
855 redef fun item
do return target
[curr_pos
]
857 redef fun next
do curr_pos
-= 1
861 private class FlatBufferCharView
864 redef type SELFTYPE: FlatBuffer
866 redef fun [](index
) do return target
[index
]
868 redef fun []=(index
, item
)
870 assert index
>= 0 and index
<= length
871 if index
== length
then
888 fun enlarge
(cap
: Int)
895 var s_length
= s
.length
896 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
897 for i
in s
do target
.add i
900 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator.with_pos
(target
, pos
)
902 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator.with_pos
(target
, pos
)
906 private class FlatBufferCharIterator
907 super IndexedIterator[Char]
909 var target
: FlatBuffer
915 init with_pos
(tgt
: FlatBuffer, pos
: Int)
917 init(tgt
, tgt
.length
- 1, pos
)
920 redef fun index
do return curr_pos
922 redef fun is_ok
do return curr_pos
<= max
924 redef fun item
do return target
[curr_pos
]
926 redef fun next
do curr_pos
+= 1
930 redef class NativeString
933 return to_s_with_length
(cstring_length
)
936 # Returns `self` as a String of `length`.
937 redef fun to_s_with_length
(length
): FlatString
940 var str
= new FlatString.with_infos
(self, length
, 0, length
- 1)
944 redef fun to_s_full
(bytelen
, unilen
) do
945 return new FlatString.full
(self, bytelen
, 0, bytelen
- 1, unilen
)
948 # Returns `self` as a new String.
949 redef fun to_s_with_copy
: FlatString
951 var length
= cstring_length
952 var new_self
= new NativeString(length
+ 1)
953 copy_to
(new_self
, length
, 0, 0)
954 var str
= new FlatString.with_infos
(new_self
, length
, 0, length
- 1)
955 new_self
[length
] = 0u8
956 str
.real_items
= new_self
960 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
962 # Very unsafe, make sure to have room for this char prior to calling this function.
963 private fun set_char_at
(pos
: Int, c
: Char) do
964 var ln
= c
.u8char_len
965 native_set_char
(pos
, c
, ln
)
968 private fun native_set_char
(pos
: Int, c
: Char, ln
: Int) `{
969 char* dst = self + pos;
975 dst[0] = 0xC0 | ((c & 0x7C0) >> 6);
976 dst[1] = 0x80 | (c & 0x3F);
979 dst[0] = 0xE0 | ((c & 0xF000) >> 12);
980 dst[1] = 0x80 | ((c & 0xFC0) >> 6);
981 dst[2] = 0x80 | (c & 0x3F);
984 dst[0] = 0xF0 | ((c & 0x1C0000) >> 18);
985 dst[1] = 0x80 | ((c & 0x3F000) >> 12);
986 dst[2] = 0x80 | ((c & 0xFC0) >> 6);
987 dst[3] = 0x80 | (c & 0x3F);
994 redef fun to_base
(base
, signed
)
996 var l
= digit_count
(base
)
997 var s
= new FlatBuffer.from
(" " * l
)
998 fill_buffer
(s
, base
, signed
)
1002 # return displayable int in base 10 and signed
1004 # assert 1.to_s == "1"
1005 # assert (-123).to_s == "-123"
1007 # Fast case for common numbers
1008 if self == 0 then return "0"
1009 if self == 1 then return "1"
1011 var nslen
= int_to_s_len
1012 var ns
= new NativeString(nslen
+ 1)
1014 native_int_to_s
(ns
, nslen
+ 1)
1015 return new FlatString.full
(ns
, nslen
, 0, nslen
- 1, nslen
)
1019 redef class Array[E
]
1021 # Fast implementation
1022 redef fun plain_to_s
1025 if l
== 0 then return ""
1026 if l
== 1 then if self[0] == null then return "" else return self[0].to_s
1028 var na
= new NativeArray[String](l
)
1034 if itsi
== null then
1044 var ns
= new NativeString(sl
+ 1)
1050 if tmp
isa FlatString then
1051 var tpl
= tmp
._bytelen
1052 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1055 for j
in tmp
.substrings
do
1056 var s
= j
.as(FlatString)
1057 var slen
= s
._bytelen
1058 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1064 return ns
.to_s_with_length
(sl
)
1068 redef class NativeArray[E
]
1069 redef fun native_to_s
do
1070 assert self isa NativeArray[String]
1081 var ns
= new NativeString(sl
+ 1)
1087 if tmp
isa FlatString then
1088 var tpl
= tmp
._bytelen
1089 tmp
._items
.copy_to
(ns
, tpl
, tmp
._first_byte
, off
)
1092 for j
in tmp
.substrings
do
1093 var s
= j
.as(FlatString)
1094 var slen
= s
._bytelen
1095 s
._items
.copy_to
(ns
, slen
, s
._first_byte
, off
)
1101 return ns
.to_s_with_length
(sl
)
1105 redef class Map[K
,V
]
1106 redef fun join
(sep
, couple_sep
)
1108 if is_empty
then return ""
1110 var s
= new Buffer # Result
1116 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1118 # Concat other _items
1124 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")