super BytePattern
# Write self as a string into `ns` at position `pos`
- private fun add_digest_at(ns: NativeString, pos: Int) do
+ private fun add_digest_at(ns: CString, pos: Int) do
var tmp = (0xF0u8 & self) >> 4
ns[pos] = if tmp >= 0x0Au8 then tmp + 0x37u8 else tmp + 0x30u8
tmp = 0x0Fu8 & self
super AbstractArray[Byte]
super BytePattern
- # A NativeString being a char*, it can be used as underlying representation here.
- var items: NativeString
+ # A CString being a char*, it can be used as underlying representation here.
+ var items: CString
# Number of bytes in the array
redef var length
# var b = new Bytes.empty
# assert b.to_s == ""
init empty do
- var ns = new NativeString(0)
+ var ns = new CString(0)
init(ns, 0, 0)
end
# Init a `Bytes` with capacity `cap`
init with_capacity(cap: Int) do
- var ns = new NativeString(cap)
+ var ns = new CString(cap)
init(ns, 0, cap)
end
# ~~~
fun hexdigest: String do
var elen = length * 2
- var ns = new NativeString(elen)
+ var ns = new CString(elen)
var i = 0
var oi = 0
while i < length do
# ~~~
fun chexdigest: String do
var elen = length * 4
- var ns = new NativeString(elen)
+ var ns = new CString(elen)
var i = 0
var oi = 0
while i < length do
# ~~~
fun binarydigest: String do
var elen = length * 8
- var ns = new NativeString(elen)
+ var ns = new CString(elen)
var i = 0
var oi = 0
while i < length do
# Regenerates the buffer, necessary when it was persisted
private fun regen do
- var nns = new NativeString(capacity)
+ var nns = new CString(capacity)
items.copy_to(nns, length, 0, 0)
persisted = false
end
# Appends the `ln` first bytes of `ns` to self
- fun append_ns(ns: NativeString, ln: Int) do
+ fun append_ns(ns: CString, ln: Int) do
if persisted then regen
var nlen = length + ln
if nlen > capacity then enlarge(nlen)
end
# Appends `ln` bytes from `ns` starting at index `from` to self
- fun append_ns_from(ns: NativeString, ln, from: Int) do
+ fun append_ns_from(ns: CString, ln, from: Int) do
if persisted then regen
var nlen = length + ln
if nlen > capacity then enlarge(nlen)
if capacity >= sz then return
persisted = false
while capacity < sz do capacity = capacity * 2 + 2
- var ns = new NativeString(capacity)
+ var ns = new CString(capacity)
items.copy_to(ns, length, 0, 0)
items = ns
end
redef fun to_s do
persisted = true
var b = self
- var r = b.items.to_s_with_length(length)
+ var r = b.items.to_s_unsafe(length, copy=false)
if r != items then persisted = false
return r
end
# assert "incomplete %".to_bytes.from_percent_encoding == "incomplete %".to_bytes
# assert "invalid % usage".to_bytes.from_percent_encoding == "invalid % usage".to_bytes
# assert "%c3%a9%e3%81%82%e3%81%84%e3%81%86".to_bytes.from_percent_encoding == "éあいう".to_bytes
+ # assert "%1 %A %C3%A9A9".to_bytes.from_percent_encoding == "%1 %A éA9".to_bytes
fun from_percent_encoding: Bytes do
var tmp = new Bytes.with_capacity(length)
var pos = 0
private class BytesIterator
super IndexedIterator[Byte]
- var tgt: NativeString
+ var tgt: CString
redef var index
# assert "<STRING/&rt;".hexdigest == "266C743B535452494E47262334373B2672743B"
fun hexdigest: String do
var ln = byte_length
- var outns = new NativeString(ln * 2)
+ var outns = new CString(ln * 2)
var oi = 0
for i in [0 .. ln[ do
bytes[i].add_digest_at(outns, oi)
end
end
-redef class NativeString
+redef class CString
# Creates a new `Bytes` object from `self` with `len` as length
#
# If `len` is null, strlen will determine the length of the Bytes
# If `len` is null, strlen will determine the length of the Bytes
fun to_bytes_with_copy(len: nullable Int): Bytes do
if len == null then len = cstring_length
- var nns = new NativeString(len)
+ var nns = new CString(len)
copy_to(nns, len, 0, 0)
return new Bytes(nns, len, len)
end