this
in self
by by
var b = "String is string".to_bytes.replace(0x20, 0x41)
assert b.hexdigest == "537472696E6741697341737472696E67"
# Replaces all the occurences of `this` in `self` by `by`
#
# var b = "String is string".to_bytes.replace(0x20, 0x41)
# assert b.hexdigest == "537472696E6741697341737472696E67"
fun replace(pattern: BytePattern, bytes: BytePattern): Bytes do
if is_empty then return new Bytes.empty
var pos = pattern.search_all_in(self)
if pos.is_empty then return clone
var ret = new Bytes.with_capacity(length)
var prev = 0
for i in pos do
ret.append_ns(items.fast_cstring(prev), i - prev)
bytes.append_to ret
prev = i + pattern.pattern_length
end
ret.append(slice_from(pos.last + pattern.pattern_length))
return ret
end
lib/core/bytes.nit:690,2--707,4