Bytes.endsWith
func Bytes.endsWith(b: Bytes, suffix: Bytes): boolBytes.endsWith reports whether b ends with suffix.
Details
- Compares bytes exactly, with no regard for text: a suffix may start in the middle of a UTF-8 character's bytes, and the two spellings of "é" differ.
- The empty
Bytesis a suffix of every value, soBytes.endsWith(b, String.toBytes(""))is always true.
Basic examples
let data = "main.ant".toBytes()
Bytes.endsWith(data, ".ant".toBytes()) // true
Bytes.endsWith(data, ".go".toBytes()) // falseCall it through . on any Bytes:
let jpeg = Bytes.fromArray([0xff, 0xd8, 0xff, 0xe0, 0xff, 0xd9])
jpeg.endsWith(Bytes.fromArray([0xff, 0xd9])) // true
jpeg.endsWith(Bytes.fromArray([0xff, 0xe0])) // falseScope
Every value ends with the empty Bytes, and the empty Bytes ends with nothing else:
let none = "".toBytes()
Bytes.fromArray([1, 2]).endsWith(none) // true
none.endsWith(none) // true
none.endsWith(Bytes.fromArray([2])) // falseA suffix longer than the value never matches, even when the value is inside it:
let tail = Bytes.fromArray([0xff, 0xd9])
tail.endsWith(Bytes.fromArray([0xd8, 0xff, 0xd9])) // false
Bytes.fromArray([0xd8, 0xff, 0xd9]).endsWith(tail) // trueBytes from any source compare the same way, here from hex text and from a slice:
let b = Encoding.fromHex("cafebabe").unwrap()
b.endsWith(Encoding.fromHex("babe").unwrap()) // true
b.endsWith(b.slice(2, 4)) // true
b.endsWith(b.slice(0, 2)) // falseThe suffix may be part of a UTF-8 character. "ñ" is the two bytes c3 b1, and the last of them followed by "o" is a suffix of "año":
let word = "año".toBytes()
word // Bytes(61 c3 b1 6f)
word.endsWith(Bytes.fromArray([0xb1, 0x6f])) // true
word.endsWith("ño".toBytes()) // trueIn a pipeline, the value is the first argument:
"index.html".toBytes() | Bytes.endsWith(".html".toBytes()) // trueApplications
Check that a JPEG file is complete: it starts with the marker ff d8 and ends with ff d9:
func isWholeJpeg(data: Bytes): bool {
let start = Bytes.fromArray([0xff, 0xd8])
let end = Bytes.fromArray([0xff, 0xd9])
data.startsWith(start) && data.endsWith(end)
}
isWholeJpeg(Bytes.fromArray([0xff, 0xd8, 0x00, 0xff, 0xd9])) // true
isWholeJpeg(Bytes.fromArray([0xff, 0xd8, 0x00, 0x00])) // falseStrip a network line ending, \r\n or \n, from a line read as bytes:
func chomp(line: Bytes): Bytes {
let crlf = Bytes.fromArray([0x0d, 0x0a])
let lf = Bytes.fromArray([0x0a])
if line.endsWith(crlf) {
return line.slice(0, len(line) - 2)
}
if line.endsWith(lf) {
return line.slice(0, len(line) - 1)
}
line
}
chomp("HELO ant\r\n".toBytes()) // Bytes(48 45 4c 4f 20 61 6e 74)
chomp("QUIT\n".toBytes()) // Bytes(51 55 49 54)
chomp("QUIT".toBytes()) // Bytes(51 55 49 54)Properties and relations
Bytes.endsWith agrees with slicing the last bytes of b:
let b = Bytes.fromArray([1, 2, 3, 4])
for s in [[4], [3, 4], [2, 4], [], [1, 2, 3, 4], [0, 1, 2, 3, 4]] {
let suffix = Bytes.fromArray(s)
let n = len(b)
let m = len(suffix)
assert(b.endsWith(suffix) == (m <= n && b.slice(n - m, n) == suffix))
}A value always ends with what Bytes.concat puts last:
let head = "ant".toBytes()
let tail = "hill".toBytes()
assert(Bytes.concat([head, tail]).endsWith(tail))
assert(Bytes.concat([head, tail]).endsWith(Bytes.concat([head, tail])))On the UTF-8 bytes of two strings, it gives the same answer as String.endsWith:
let s = "año"
for t in ["o", "ño", "ñ", ""] {
assert(s.endsWith(t) == s.toBytes().endsWith(t.toBytes()))
}Possible issues
The value comes first and the suffix second. Swapped, the call asks whether the suffix ends with the value:
let data = "main.ant".toBytes()
let ext = ".ant".toBytes()
Bytes.endsWith(ext, data) // false
Bytes.endsWith(data, ext) // trueThe precomposed "é" and "e" followed by a combining accent are different bytes, so text that looks the same can fail to match. Normalize the strings with String.normalize before converting them:
let word = "cafe\u{301}"
let nfc = word.normalize("NFC")
word.toBytes().endsWith("é".toBytes()) // false
nfc.toBytes().endsWith("é".toBytes()) // trueA string is not Bytes. Convert it with String.toBytes, or compare the strings with String.endsWith:
let data = "main.ant".toBytes()
println(Bytes.endsWith(data, ".ant")) // error: cannot use string where Bytes is expectedAn array of integers is not Bytes either. Convert it with Bytes.fromArray:
let data = Bytes.fromArray([0xff, 0xd8, 0xff, 0xd9])
println(Bytes.endsWith(data, [0xff, 0xd9])) // error: cannot use Array[int] where Bytes is expectedSee also
Bytes.startsWith · Bytes.indexOf · Bytes.slice · Bytes.concat · String.endsWith · String.toBytes
