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
55 # Find best insertion point
56 var delta_begin
= index
57 var delta_end
= (ln
- 1) - index
58 var delta_cache
= (position
- index
).abs
62 if delta_cache
< min
then min
= delta_cache
63 if delta_end
< min
then min
= delta_end
68 if min
== delta_begin
then
71 else if min
== delta_cache
then
75 ns_i
= its
.find_beginning_of_char_at
(last_byte
)
79 ns_i
= its
.char_to_byte_index_cached
(index
, my_i
, ns_i
)
87 redef fun [](index
) do return items
.char_at
(char_to_byte_index
(index
))
90 # Immutable strings of characters.
95 # Index at which `self` begins in `items`, inclusively
96 redef var first_byte
is noinit
98 # Index at which `self` ends in `items`, inclusively
99 redef var last_byte
is noinit
101 redef var chars
= new FlatStringCharView(self) is lazy
103 redef var bytes
= new FlatStringByteView(self) is lazy
105 redef var length
is lazy
do
106 if bytelen
== 0 then return 0
112 st
+= its
.length_of_char_at
(st
)
120 var b
= new FlatBuffer.with_capacity
(bytelen
+ 1)
121 for i
in [length
- 1 .. 0].step
(-1) do
124 var s
= b
.to_s
.as(FlatString)
125 s
.length
= self.length
129 redef fun fast_cstring
do return items
.fast_cstring
(first_byte
)
131 redef fun substring
(from
, count
)
137 if count
< 0 then count
= 0
141 if (count
+ from
) > length
then count
= length
- from
142 if count
<= 0 then return ""
143 var end_index
= from
+ count
- 1
145 var bytefrom
= char_to_byte_index
(from
)
146 var byteto
= char_to_byte_index
(end_index
)
147 byteto
+= items
.length_of_char_at
(byteto
) - 1
149 var s
= new FlatString.full
(items
, byteto
- bytefrom
+ 1, bytefrom
, byteto
, count
)
153 redef fun empty
do return "".as(FlatString)
157 var outstr
= new FlatBuffer.with_capacity
(self.bytelen
+ 1)
163 outstr
.add
(chars
[pos
].to_upper
)
172 var outstr
= new FlatBuffer.with_capacity
(self.bytelen
+ 1)
178 outstr
.add
(chars
[pos
].to_lower
)
187 for i
in chars
do i
.output
190 ##################################################
191 # String Specific Methods #
192 ##################################################
194 # Low-level creation of a new string with minimal data.
196 # `items` will be used as is, without copy, to retrieve the characters of the string.
197 # Aliasing issues is the responsibility of the caller.
198 private init with_infos
(items
: NativeString, bytelen
, from
, to
: Int)
201 self.bytelen
= bytelen
206 # Low-level creation of a new string with all the data.
208 # `items` will be used as is, without copy, to retrieve the characters of the string.
209 # Aliasing issues is the responsibility of the caller.
210 private init full
(items
: NativeString, bytelen
, from
, to
, length
: Int)
214 self.bytelen
= bytelen
219 redef fun to_cstring
do
220 if real_items
!= null then return real_items
.as(not null)
221 var new_items
= new NativeString(bytelen
+ 1)
222 self.items
.copy_to
(new_items
, bytelen
, first_byte
, 0)
223 new_items
[bytelen
] = 0u8
224 real_items
= new_items
230 if not other
isa FlatString then return super
232 if self.object_id
== other
.object_id
then return true
234 var my_length
= bytelen
236 if other
.bytelen
!= my_length
then return false
238 var my_index
= first_byte
239 var its_index
= other
.first_byte
241 var last_iteration
= my_index
+ my_length
243 var itsitems
= other
.items
244 var myitems
= self.items
246 while my_index
< last_iteration
do
247 if myitems
[my_index
] != itsitems
[its_index
] then return false
257 if not other
isa FlatString then return super
259 if self.object_id
== other
.object_id
then return false
261 var my_length
= self.bytelen
262 var its_length
= other
.bytelen
264 var max
= if my_length
< its_length
then my_length
else its_length
266 var myits
= self.bytes
267 var itsits
= other
.bytes
269 for i
in [0 .. max
[ do
270 var my_curr_char
= myits
[i
]
271 var its_curr_char
= itsits
[i
]
273 if my_curr_char
!= its_curr_char
then
274 if my_curr_char
< its_curr_char
then return true
279 return my_length
< its_length
286 var nlen
= mlen
+ slen
288 var mifrom
= first_byte
289 if s
isa FlatText then
291 var sifrom
= s
.first_byte
292 var ns
= new NativeString(nlen
+ 1)
293 mits
.copy_to
(ns
, mlen
, mifrom
, 0)
294 sits
.copy_to
(ns
, slen
, sifrom
, mlen
)
295 return new FlatString.full
(ns
, nlen
, 0, nlen
- 1, length
+ o
.length
)
302 var mybtlen
= bytelen
303 var new_bytelen
= mybtlen
* i
305 var newlen
= mylen
* i
306 var ns
= new NativeString(new_bytelen
+ 1)
307 ns
[new_bytelen
] = 0u8
310 items
.copy_to
(ns
, bytelen
, first_byte
, offset
)
314 return new FlatString.full
(ns
, new_bytelen
, 0, new_bytelen
- 1, newlen
)
320 if hash_cache
== null then
321 # djb2 hash algorithm
327 while i
<= last_byte
do
328 h
= (h
<< 5) + h
+ myitems
[i
].to_i
335 return hash_cache
.as(not null)
338 redef fun substrings
do return new FlatSubstringsIter(self)
341 private class FlatStringCharReverseIterator
342 super IndexedIterator[Char]
344 var target
: FlatString
348 init with_pos
(tgt
: FlatString, pos
: Int)
353 redef fun is_ok
do return curr_pos
>= 0
355 redef fun item
do return target
[curr_pos
]
357 redef fun next
do curr_pos
-= 1
359 redef fun index
do return curr_pos
363 private class FlatStringCharIterator
364 super IndexedIterator[Char]
366 var target
: FlatString
372 init with_pos
(tgt
: FlatString, pos
: Int)
374 init(tgt
, tgt
.length
- 1, pos
)
377 redef fun is_ok
do return curr_pos
<= max
379 redef fun item
do return target
[curr_pos
]
381 redef fun next
do curr_pos
+= 1
383 redef fun index
do return curr_pos
387 private class FlatStringCharView
390 redef type SELFTYPE: FlatString
392 redef fun [](index
) do return target
[index
]
394 redef fun iterator_from
(start
) do return new FlatStringCharIterator.with_pos
(target
, start
)
396 redef fun reverse_iterator_from
(start
) do return new FlatStringCharReverseIterator.with_pos
(target
, start
)
400 private class FlatStringByteReverseIterator
401 super IndexedIterator[Byte]
403 var target
: FlatString
405 var target_items
: NativeString
409 init with_pos
(tgt
: FlatString, pos
: Int)
411 init(tgt
, tgt
.items
, pos
+ tgt
.first_byte
)
414 redef fun is_ok
do return curr_pos
>= target
.first_byte
416 redef fun item
do return target_items
[curr_pos
]
418 redef fun next
do curr_pos
-= 1
420 redef fun index
do return curr_pos
- target
.first_byte
424 private class FlatStringByteIterator
425 super IndexedIterator[Byte]
427 var target
: FlatString
429 var target_items
: NativeString
433 init with_pos
(tgt
: FlatString, pos
: Int)
435 init(tgt
, tgt
.items
, pos
+ tgt
.first_byte
)
438 redef fun is_ok
do return curr_pos
<= target
.last_byte
440 redef fun item
do return target_items
[curr_pos
]
442 redef fun next
do curr_pos
+= 1
444 redef fun index
do return curr_pos
- target
.first_byte
448 private class FlatStringByteView
451 redef type SELFTYPE: FlatString
455 # Check that the index (+ first_byte) is not larger than last_byte
456 # In other terms, if the index is valid
458 var target
= self.target
459 assert (index
+ target
.first_byte
) <= target
.last_byte
460 return target
.items
[index
+ target
.first_byte
]
463 redef fun iterator_from
(start
) do return new FlatStringByteIterator.with_pos
(target
, start
)
465 redef fun reverse_iterator_from
(start
) do return new FlatStringByteReverseIterator.with_pos
(target
, start
)
470 redef new do return new FlatBuffer
472 redef new with_cap
(i
) do return new FlatBuffer.with_capacity
(i
)
475 # Mutable strings of characters.
480 redef var chars
: Sequence[Char] = new FlatBufferCharView(self) is lazy
482 redef var bytes
= new FlatBufferByteView(self) is lazy
484 redef var bytelen
= 0
488 private var char_cache
: Int = -1
490 private var byte_cache
: Int = -1
492 private var capacity
= 0
494 redef fun fast_cstring
do return items
.fast_cstring
(0)
496 redef fun substrings
do return new FlatSubstringsIter(self)
498 # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
500 # This happens when an operation modifies the current `Buffer` and
501 # the Copy-On-Write flag `written` is set at true.
503 var nns
= new NativeString(capacity
)
504 items
.copy_to
(nns
, bytelen
, 0, 0)
509 # Shifts the content of the buffer by `len` bytes to the right, starting at byte `from`
511 # Internal only, does not modify bytelen or length, this is the caller's responsability
512 private fun rshift_bytes
(from
: Int, len
: Int) do
515 if bytelen
+ len
> capacity
then
516 capacity
= capacity
* 2 + 2
517 nit
= new NativeString(capacity
)
518 oit
.copy_to
(nit
, 0, 0, from
)
520 oit
.copy_to
(nit
, bytelen
- from
, from
, from
+ len
)
523 # Shifts the content of the buffer by `len` bytes to the left, starting at `from`
525 # Internal only, does not modify bytelen or length, this is the caller's responsability
526 private fun lshift_bytes
(from
: Int, len
: Int) do
527 items
.copy_to
(items
, bytelen
- from
, from
, from
- len
)
530 redef fun []=(index
, item
)
532 assert index
>= 0 and index
<= length
533 if written
then reset
535 if index
== length
then
539 var ip
= items
.char_to_byte_index
(index
)
540 var c
= items
.char_at
(ip
)
541 var clen
= c
.u8char_len
542 var itemlen
= item
.u8char_len
543 var size_diff
= itemlen
- clen
544 if size_diff
> 0 then
545 rshift_bytes
(ip
+ clen
, size_diff
)
546 else if size_diff
< 0 then
547 lshift_bytes
(ip
+ clen
, -size_diff
)
551 items
.set_char_at
(ip
, item
)
556 if written
then reset
558 var clen
= c
.u8char_len
559 enlarge
(bytelen
+ clen
)
560 items
.set_char_at
(bytelen
, c
)
567 if written
then reset
572 redef fun empty
do return new Buffer
574 redef fun enlarge
(cap
)
577 if cap
<= c
then return
578 while c
<= cap
do c
= c
* 2 + 2
579 # The COW flag can be set at false here, since
580 # it does a copy of the current `Buffer`
582 var a
= new NativeString(c
+1)
583 if bytelen
> 0 then items
.copy_to
(a
, bytelen
, 0, 0)
591 if bytelen
== 0 then items
= new NativeString(1)
592 return new FlatString.full
(items
, bytelen
, 0, bytelen
- 1, length
)
598 var new_native
= new NativeString(bytelen
+ 1)
599 new_native
[bytelen
] = 0u8
600 if length
> 0 then items
.copy_to
(new_native
, bytelen
, 0, 0)
601 real_items
= new_native
604 return real_items
.as(not null)
607 # Create a new empty string.
610 # Low-level creation a new buffer with given data.
612 # `items` will be used as is, without copy, to store the characters of the buffer.
613 # Aliasing issues is the responsibility of the caller.
615 # If `items` is shared, `written` should be set to true after the creation
616 # so that a modification will do a copy-on-write.
617 private init with_infos
(items
: NativeString, capacity
, bytelen
, length
: Int)
620 self.capacity
= capacity
621 self.bytelen
= bytelen
625 # Create a new string copied from `s`.
628 items
= new NativeString(s
.bytelen
)
629 if s
isa FlatText then
632 for i
in substrings
do i
.as(FlatString).items
.copy_to
(items
, i
.bytelen
, 0, 0)
640 # Create a new empty string with a given capacity.
641 init with_capacity
(cap
: Int)
644 items
= new NativeString(cap
+ 1)
651 if s
.is_empty
then return
654 enlarge
(bytelen
+ sl
)
655 if s
isa FlatText then
656 s
.items
.copy_to
(items
, sl
, s
.first_byte
, bytelen
)
658 for i
in s
.substrings
do append i
665 # Copies the content of self in `dest`
666 fun copy
(start
: Int, len
: Int, dest
: Buffer, new_start
: Int)
668 var self_chars
= self.chars
669 var dest_chars
= dest
.chars
670 for i
in [0..len-1
] do
671 dest_chars
[new_start
+i
] = self_chars
[start
+i
]
675 redef fun substring
(from
, count
)
678 if from
< 0 then from
= 0
679 if (from
+ count
) > length
then count
= length
- from
681 var bytefrom
= items
.char_to_byte_index
(from
)
682 var byteto
= items
.char_to_byte_index
(count
+ from
- 1)
683 byteto
+= items
.char_at
(byteto
).u8char_len
- 1
684 var byte_length
= byteto
- bytefrom
+ 1
685 var r_items
= new NativeString(byte_length
)
686 items
.copy_to
(r_items
, byte_length
, bytefrom
, 0)
687 return new FlatBuffer.with_infos
(r_items
, byte_length
, byte_length
, count
)
696 var ns
= new FlatBuffer.with_capacity
(capacity
)
697 for i
in chars
.reverse_iterator
do ns
.add i
701 redef fun times
(repeats
)
703 var x
= new FlatString.full
(items
, bytelen
, 0, bytelen
- 1, length
)
704 for i
in [1 .. repeats
[ do
711 if written
then reset
712 for i
in [0 .. length
[ do self[i
] = self[i
].to_upper
717 if written
then reset
718 for i
in [0 .. length
[ do self[i
] = self[i
].to_lower
722 private class FlatBufferByteReverseIterator
723 super IndexedIterator[Byte]
725 var target
: FlatBuffer
727 var target_items
: NativeString
731 init with_pos
(tgt
: FlatBuffer, pos
: Int)
733 init(tgt
, tgt
.items
, pos
)
736 redef fun index
do return curr_pos
738 redef fun is_ok
do return curr_pos
>= 0
740 redef fun item
do return target_items
[curr_pos
]
742 redef fun next
do curr_pos
-= 1
746 private class FlatBufferByteView
749 redef type SELFTYPE: FlatBuffer
751 redef fun [](index
) do return target
.items
[index
]
753 redef fun iterator_from
(pos
) do return new FlatBufferByteIterator.with_pos
(target
, pos
)
755 redef fun reverse_iterator_from
(pos
) do return new FlatBufferByteReverseIterator.with_pos
(target
, pos
)
759 private class FlatBufferByteIterator
760 super IndexedIterator[Byte]
762 var target
: FlatBuffer
764 var target_items
: NativeString
768 init with_pos
(tgt
: FlatBuffer, pos
: Int)
770 init(tgt
, tgt
.items
, pos
)
773 redef fun index
do return curr_pos
775 redef fun is_ok
do return curr_pos
< target
.bytelen
777 redef fun item
do return target_items
[curr_pos
]
779 redef fun next
do curr_pos
+= 1
783 private class FlatBufferCharReverseIterator
784 super IndexedIterator[Char]
786 var target
: FlatBuffer
790 init with_pos
(tgt
: FlatBuffer, pos
: Int)
795 redef fun index
do return curr_pos
797 redef fun is_ok
do return curr_pos
>= 0
799 redef fun item
do return target
[curr_pos
]
801 redef fun next
do curr_pos
-= 1
805 private class FlatBufferCharView
808 redef type SELFTYPE: FlatBuffer
810 redef fun [](index
) do return target
[index
]
812 redef fun []=(index
, item
)
814 assert index
>= 0 and index
<= length
815 if index
== length
then
832 fun enlarge
(cap
: Int)
839 var s_length
= s
.length
840 if target
.capacity
< s
.length
then enlarge
(s_length
+ target
.length
)
841 for i
in s
do target
.add i
844 redef fun iterator_from
(pos
) do return new FlatBufferCharIterator.with_pos
(target
, pos
)
846 redef fun reverse_iterator_from
(pos
) do return new FlatBufferCharReverseIterator.with_pos
(target
, pos
)
850 private class FlatBufferCharIterator
851 super IndexedIterator[Char]
853 var target
: FlatBuffer
859 init with_pos
(tgt
: FlatBuffer, pos
: Int)
861 init(tgt
, tgt
.length
- 1, pos
)
864 redef fun index
do return curr_pos
866 redef fun is_ok
do return curr_pos
<= max
868 redef fun item
do return target
[curr_pos
]
870 redef fun next
do curr_pos
+= 1
874 redef class NativeString
877 return to_s_with_length
(cstring_length
)
880 # Returns `self` as a String of `length`.
881 redef fun to_s_with_length
(length
): FlatString
884 var str
= new FlatString.with_infos
(self, length
, 0, length
- 1)
888 redef fun to_s_full
(bytelen
, unilen
) do
889 return new FlatString.full
(self, bytelen
, 0, bytelen
- 1, unilen
)
892 # Returns `self` as a new String.
893 redef fun to_s_with_copy
: FlatString
895 var length
= cstring_length
896 var new_self
= new NativeString(length
+ 1)
897 copy_to
(new_self
, length
, 0, 0)
898 var str
= new FlatString.with_infos
(new_self
, length
, 0, length
- 1)
899 new_self
[length
] = 0u8
900 str
.real_items
= new_self
904 # Sets the next bytes at position `pos` to the value of `c`, encoded in UTF-8
906 # Very unsafe, make sure to have room for this char prior to calling this function.
907 private fun set_char_at
(pos
: Int, c
: Char) do
908 var ln
= c
.u8char_len
909 native_set_char
(pos
, c
, ln
)
912 private fun native_set_char
(pos
: Int, c
: Char, ln
: Int) `{
913 char* dst = self + pos;
919 dst[0] = 0xC0 | ((c & 0x7C0) >> 6);
920 dst[1] = 0x80 | (c & 0x3F);
923 dst[0] = 0xE0 | ((c & 0xF000) >> 12);
924 dst[1] = 0x80 | ((c & 0xFC0) >> 6);
925 dst[2] = 0x80 | (c & 0x3F);
928 dst[0] = 0xF0 | ((c & 0x1C0000) >> 18);
929 dst[1] = 0x80 | ((c & 0x3F000) >> 12);
930 dst[2] = 0x80 | ((c & 0xFC0) >> 6);
931 dst[3] = 0x80 | (c & 0x3F);
938 redef fun to_base
(base
, signed
)
940 var l
= digit_count
(base
)
941 var s
= new FlatBuffer.from
(" " * l
)
942 fill_buffer
(s
, base
, signed
)
946 # return displayable int in base 10 and signed
948 # assert 1.to_s == "1"
949 # assert (-123).to_s == "-123"
951 # Fast case for common numbers
952 if self == 0 then return "0"
953 if self == 1 then return "1"
955 var nslen
= int_to_s_len
956 var ns
= new NativeString(nslen
+ 1)
958 native_int_to_s
(ns
, nslen
+ 1)
959 return new FlatString.full
(ns
, nslen
, 0, nslen
- 1, nslen
)
965 # Fast implementation
969 if l
== 0 then return ""
970 if l
== 1 then if self[0] == null then return "" else return self[0].to_s
972 var na
= new NativeArray[String](l
)
988 var ns
= new NativeString(sl
+ 1)
994 if tmp
isa FlatString then
995 var tpl
= tmp
.bytelen
996 tmp
.items
.copy_to
(ns
, tpl
, tmp
.first_byte
, off
)
999 for j
in tmp
.substrings
do
1000 var s
= j
.as(FlatString)
1001 var slen
= s
.bytelen
1002 s
.items
.copy_to
(ns
, slen
, s
.first_byte
, off
)
1008 return ns
.to_s_with_length
(sl
)
1012 redef class NativeArray[E
]
1013 redef fun native_to_s
do
1014 assert self isa NativeArray[String]
1025 var ns
= new NativeString(sl
+ 1)
1031 if tmp
isa FlatString then
1032 var tpl
= tmp
.bytelen
1033 tmp
.items
.copy_to
(ns
, tpl
, tmp
.first_byte
, off
)
1036 for j
in tmp
.substrings
do
1037 var s
= j
.as(FlatString)
1038 var slen
= s
.bytelen
1039 s
.items
.copy_to
(ns
, slen
, s
.first_byte
, off
)
1045 return ns
.to_s_with_length
(sl
)
1049 redef class Map[K
,V
]
1050 redef fun join
(sep
, couple_sep
)
1052 if is_empty
then return ""
1054 var s
= new Buffer # Result
1060 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
1062 # Concat other items
1068 s
.append
("{k or else "<null>"}{couple_sep}{e or else "<null>"}")