+ redef fun first_index_in_from(b, from) do
+ if is_empty then return -1
+ var fst = self[0]
+ var bpos = fst.first_index_in_from(self, from)
+ for i in [0 .. length[ do
+ if self[i] != b[bpos] then return first_index_in_from(b, bpos + 1)
+ bpos += 1
+ end
+ return bpos
+ end
+
+ redef fun last_index_in_from(b, from) do
+ if is_empty then return -1
+ var lst = self[length - 1]
+ var bpos = lst.last_index_in_from(b, from)
+ for i in [0 .. length[.step(-1) do
+ if self[i] != b[bpos] then return last_index_in_from(b, bpos - 1)
+ bpos -= 1
+ end
+ return bpos
+ end
+
+ redef fun search_all_in(b) do
+ var ret = new Array[Int]
+ var pos = first_index_in_from(b, 0)
+ if pos == -1 then return ret
+ pos = pos + 1
+ ret.add pos
+ loop
+ pos = first_index_in_from(b, pos)
+ if pos == -1 then return ret
+ ret.add pos
+ pos += length
+ end
+ end
+
+ # Splits the content on self when encountering `b`
+ #
+ # var a = "String is string".to_bytes.split_with(u's')
+ # assert a.length == 3
+ # assert a[0].hexdigest == "537472696E672069"
+ # assert a[1].hexdigest == "20"
+ # assert a[2].hexdigest == "7472696E67"
+ fun split_with(b: BytePattern): Array[Bytes] do
+ var fst = b.search_all_in(self)
+ if fst.is_empty then return [clone]
+ var retarr = new Array[Bytes]
+ var prev = 0
+ for i in fst do
+ retarr.add(slice(prev, i - prev))
+ prev = i + b.pattern_length
+ end
+ retarr.add slice_from(prev)
+ return retarr
+ end
+
+ # Splits `self` in two parts at the first occurence of `b`
+ #
+ # var a = "String is string".to_bytes.split_once_on(u's')
+ # assert a[0].hexdigest == "537472696E672069"
+ # assert a[1].hexdigest == "20737472696E67"
+ fun split_once_on(b: BytePattern): Array[Bytes] do
+ var spl = b.first_index_in(self)
+ if spl == -1 then return [clone]
+ var ret = new Array[Bytes].with_capacity(2)
+ ret.add(slice(0, spl))
+ ret.add(slice_from(spl + b.pattern_length))
+ return ret
+ end
+
+ # 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
+
+ # Decode `self` from percent (or URL) encoding to a clear string
+ #
+ # Invalid '%' are not decoded.
+ #
+ # assert "aBc09-._~".to_bytes.from_percent_encoding == "aBc09-._~".to_bytes
+ # assert "%25%28%29%3c%20%3e".to_bytes.from_percent_encoding == "%()< >".to_bytes
+ # assert ".com%2fpost%3fe%3dasdf%26f%3d123".to_bytes.from_percent_encoding == ".com/post?e=asdf&f=123".to_bytes
+ # assert "%25%28%29%3C%20%3E".to_bytes.from_percent_encoding == "%()< >".to_bytes
+ # 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
+ while pos < length do
+ var b = self[pos]
+ if b != u'%' then
+ tmp.add b
+ pos += 1
+ continue
+ end
+ if length - pos < 2 then
+ tmp.add u'%'
+ pos += 1
+ continue
+ end
+ var bn = self[pos + 1]
+ var bnn = self[pos + 2]
+ if not bn.is_valid_hexdigit or not bnn.is_valid_hexdigit then
+ tmp.add u'%'
+ pos += 1
+ continue
+ end
+ tmp.add((bn.hexdigit_to_byteval << 4) + bnn.hexdigit_to_byteval)
+ pos += 3
+ end
+ return tmp
+ end
+
+ # Is `b` a prefix of `self` ?
+ fun has_prefix(b: BytePattern): Bool do return b.is_prefix(self)
+
+ # Is `b` a suffix of `self` ?
+ fun has_suffix(b: BytePattern): Bool do return b.is_suffix(self)
+
+ redef fun is_suffix(b) do
+ if length > b.length then return false
+ var j = b.length - 1
+ var i = length - 1
+ while i > 0 do
+ if self[i] != b[j] then return false
+ i -= 1
+ j -= 1
+ end
+ return true
+ end
+
+ redef fun is_prefix(b) do
+ if length > b.length then return false
+ for i in [0 .. length[ do if self[i] != b[i] then return false
+ return true
+ end