redef class FlatText
- # First byte of the NativeString
+ # First byte of the CString
protected fun first_byte: Int do return 0
- # Last byte of the NativeString
+ # Last byte of the CString
protected fun last_byte: Int do return first_byte + _byte_length - 1
# Cache of the latest position (char) explored in the string
var position: Int = 0
- # Cached position (bytes) in the NativeString underlying the String
+ # Cached position (bytes) in the CString underlying the String
var bytepos: Int = 0
# Index of the character `index` in `_items`
var max = last_byte
var pos = first_byte
var nlen = extra + _byte_length
- var nits = new NativeString(nlen)
+ var nits = new CString(nlen)
var outpos = 0
while pos <= max do
var c = its[pos]
var its = _items
var max = last_byte
var nlen = _byte_length + ln_extra
- var nns = new NativeString(nlen)
+ var nns = new CString(nlen)
var pos = first_byte
var opos = 0
while pos <= max do
end
pos += 1
end
- return nns.to_s_unsafe(nlen)
+ return nns.to_s_unsafe(nlen, copy=false, clean=false)
end
redef fun [](index) do
redef fun to_cstring do
var blen = _byte_length
- var new_items = new NativeString(blen + 1)
+ var new_items = new CString(blen + 1)
_items.copy_to(new_items, blen, _first_byte, 0)
new_items[blen] = 0u8
return new_items
#
# `_items` will be used as is, without copy, to retrieve the characters of the string.
# Aliasing issues is the responsibility of the caller.
- private new with_infos(items: NativeString, byte_length, from: Int)
+ private new with_infos(items: CString, byte_length, from: Int)
do
var len = items.utf8_length(from, byte_length)
if byte_length == len then return new ASCIIFlatString.full_data(items, byte_length, from, len)
#
# `_items` will be used as is, without copy, to retrieve the characters of the string.
# Aliasing issues is the responsibility of the caller.
- private new full(items: NativeString, byte_length, from, length: Int)
+ private new full(items: CString, byte_length, from, length: Int)
do
if byte_length == length then return new ASCIIFlatString.full_data(items, byte_length, from, length)
return new UnicodeFlatString.full_data(items, byte_length, from, length)
if s isa FlatText then
var sits = s._items
var sifrom = s.first_byte
- var ns = new NativeString(nlen + 1)
+ var ns = new CString(nlen + 1)
mits.copy_to(ns, mlen, mifrom, 0)
sits.copy_to(ns, slen, sifrom, mlen)
return new FlatString.full(ns, nlen, 0, _length + o.length)
var newlen = mylen * i
var its = _items
var fb = _first_byte
- var ns = new NativeString(new_byte_length + 1)
+ var ns = new CString(new_byte_length + 1)
ns[new_byte_length] = 0u8
var offset = 0
while i > 0 do
private class UnicodeFlatString
super FlatString
- init full_data(items: NativeString, byte_length, from, length: Int) do
+ init full_data(items: CString, byte_length, from, length: Int) do
self._items = items
self._length = length
self._byte_length = byte_length
private class ASCIIFlatString
super FlatString
- init full_data(items: NativeString, byte_length, from, length: Int) do
+ init full_data(items: CString, byte_length, from, length: Int) do
self._items = items
self._length = length
self._byte_length = byte_length
var target: FlatString
- var target_items: NativeString is noautoinit
+ var target_items: CString is noautoinit
var curr_pos: Int
var target: FlatString
- var target_items: NativeString is noautoinit
+ var target_items: CString is noautoinit
var curr_pos: Int
redef fun substrings do return new FlatSubstringsIter(self)
- # Re-copies the `NativeString` into a new one and sets it as the new `Buffer`
+ # Re-copies the `CString` into a new one and sets it as the new `Buffer`
#
# This happens when an operation modifies the current `Buffer` and
# the Copy-On-Write flag `written` is set at true.
private fun reset do
- var nns = new NativeString(capacity)
+ var nns = new CString(capacity)
if _byte_length != 0 then _items.copy_to(nns, _byte_length, 0, 0)
_items = nns
written = false
var bt = _byte_length
if bt + len > capacity then
capacity = capacity * 2 + 2
- nit = new NativeString(capacity)
+ nit = new CString(capacity)
oit.copy_to(nit, 0, 0, from)
end
oit.copy_to(nit, bt - from, from, from + len)
# it does a copy of the current `Buffer`
written = false
var bln = _byte_length
- var a = new NativeString(c)
+ var a = new CString(c)
if bln > 0 then
var it = _items
if bln > 0 then it.copy_to(a, bln, 0, 0)
do
written = true
var bln = _byte_length
- if bln == 0 then _items = new NativeString(1)
+ if bln == 0 then _items = new CString(1)
return new FlatString.full(_items, bln, 0, _length)
end
redef fun to_cstring
do
var bln = _byte_length
- var new_native = new NativeString(bln + 1)
+ var new_native = new CString(bln + 1)
new_native[bln] = 0u8
if _length > 0 then _items.copy_to(new_native, bln, 0, 0)
return new_native
#
# If `_items` is shared, `written` should be set to true after the creation
# so that a modification will do a copy-on-write.
- private init with_infos(items: NativeString, capacity, byte_length, length: Int)
+ private init with_infos(items: CString, capacity, byte_length, length: Int)
do
self._items = items
self.capacity = capacity
# Create a new string copied from `s`.
init from(s: Text)
do
- _items = new NativeString(s.byte_length)
+ _items = new CString(s.byte_length)
for i in s.substrings do i._items.copy_to(_items, i._byte_length, first_byte, 0)
_byte_length = s.byte_length
_length = s.length
init with_capacity(cap: Int)
do
assert cap >= 0
- _items = new NativeString(cap)
+ _items = new CString(cap)
capacity = cap
_byte_length = 0
end
var byteto = its.char_to_byte_index(count + from - 1)
byteto += its.char_at(byteto).u8char_len - 1
var byte_length = byteto - bytefrom + 1
- var r_items = new NativeString(byte_length)
+ var r_items = new CString(byte_length)
its.copy_to(r_items, byte_length, bytefrom, 0)
return new FlatBuffer.with_infos(r_items, byte_length, byte_length, count)
end
var target: FlatBuffer
- var target_items: NativeString is noautoinit
+ var target_items: CString is noautoinit
var curr_pos: Int
var target: FlatBuffer
- var target_items: NativeString is noautoinit
+ var target_items: CString is noautoinit
var curr_pos: Int
end
-redef class NativeString
- redef fun to_s
- do
- return to_s_with_length(cstring_length)
- end
+redef class CString
- redef fun to_s_with_length(length)
- do
- assert length >= 0
- return clean_utf8(length)
- end
+ # Get a `String` from the data at `self` copied into Nit memory
+ #
+ # Require: `self` is a null-terminated string.
+ redef fun to_s do return to_s_unsafe
- redef fun to_s_full(byte_length, unilen) do
- return new FlatString.full(self, byte_length, 0, unilen)
- end
+ # Get a `String` from `byte_length` bytes at `self` copied into Nit memory
+ #
+ # The string is cleaned.
+ fun to_s_with_length(byte_length: Int): String do return to_s_unsafe(byte_length)
- redef fun to_s_unsafe(len) do
- if len == null then len = cstring_length
- return new FlatString.with_infos(self, len, 0)
- end
+ redef fun to_s_unsafe(byte_length, char_length, copy, clean)
+ do
+ byte_length = byte_length or else cstring_length
+ clean = clean or else true
+ copy = copy or else true
+
+ # Clean?
+ var str = null
+ if clean then
+ str = clean_utf8(byte_length)
+ char_length = str.length
+ else
+ char_length = char_length or else utf8_length(0, byte_length)
+ end
- redef fun to_s_with_copy do return to_s_with_copy_and_length(cstring_length)
+ # Copy? (if not already copied by `clean_utf8`)
+ if copy and (str == null or str.items == self) then
+ var new_cstr = new CString(byte_length + 1)
+ copy_to(new_cstr, byte_length, 0, 0)
+ new_cstr[byte_length] = 0u8
+ str = new FlatString.full(new_cstr, byte_length, 0, char_length)
+ end
+
+ if str == null then
+ str = new FlatString.full(self, byte_length, 0, char_length)
+ end
- # Get a `String` from `length` bytes at `self` copied into Nit memory
- fun to_s_with_copy_and_length(length: Int): String
- do
- var r = clean_utf8(length)
- if r.items != self then return r
- var new_self = new NativeString(length + 1)
- copy_to(new_self, length, 0, 0)
- var str = new FlatString.with_infos(new_self, length, 0)
- new_self[length] = 0u8
return str
end
- # Cleans a NativeString if necessary
+ # Cleans a CString if necessary
fun clean_utf8(len: Int): FlatString do
var replacements: nullable Array[Int] = null
var end_length = len
while rem > 0 do
while rem >= 4 do
var i = fetch_4_chars(pos)
- if i & 0x80808080 != 0 then break
+ if i & 0x80808080u32 != 0u32 then break
pos += 4
chr_ln += 4
rem -= 4
end
var ret = self
if end_length != len then
- ret = new NativeString(end_length)
+ ret = new CString(end_length)
var old_repl = 0
var off = 0
var repls = replacements.as(not null)
if self == 1 then return "1"
var nslen = int_to_s_len
- var ns = new NativeString(nslen + 1)
+ var ns = new CString(nslen + 1)
ns[nslen] = 0u8
native_int_to_s(ns, nslen + 1)
return new FlatString.full(ns, nslen, 0, nslen)
i += 1
mypos += 1
end
- var ns = new NativeString(sl + 1)
+ var ns = new CString(sl + 1)
ns[sl] = 0u8
i = 0
var off = 0
i += 1
mypos += 1
end
- var ns = new NativeString(sl + 1)
+ var ns = new CString(sl + 1)
ns[sl] = 0u8
i = 0
var off = 0