Bytes.startsWith
func Bytes.startsWith(b: Bytes, prefix: Bytes, at: int = 0): boolBytes.startsWith reports whether prefix occurs in b at index at. It panics if at is outside 0 ..= Bytes.length(b).
Details
- Every
Bytesvalue starts with the empty bytes. - The match is byte by byte. It knows nothing of text, so a prefix may end in the middle of a UTF-8 character.
Basic examples
let png = Bytes.fromArray([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
Bytes.startsWith(png, Bytes.fromArray([0x89, 0x50, 0x4e, 0x47])) // true
Bytes.startsWith(png, Bytes.fromArray([0xff, 0xd8])) // falseCall it through . on any Bytes value:
let data = String.toBytes("GIF89a")
data.startsWith(String.toBytes("GIF")) // true
data.startsWith(String.toBytes("GIF87")) // falseScope
Every value starts with the empty bytes, and the empty bytes start with nothing else:
let empty = String.toBytes("")
String.toBytes("ant").startsWith(empty) // true
empty.startsWith(empty) // true
empty.startsWith(String.toBytes("a")) // falseA prefix longer than the bytes never matches, and a value starts with itself:
let ant = String.toBytes("ant")
ant.startsWith(String.toBytes("anthill")) // false
ant.startsWith(ant) // trueAny byte value matches, zero and 0xff included:
let b = Bytes.fromArray([0, 0xff, 7])
b.startsWith(Bytes.fromArray([0])) // true
b.startsWith(Bytes.fromArray([0, 0xff])) // true
b.startsWith(Bytes.fromArray([0xff])) // falseIn a pipeline, the bytes are the first argument:
String.toBytes("PK\u{3}\u{4}") | Bytes.startsWith(String.toBytes("PK")) // trueNaming the arguments makes clear which value is the prefix, in either order:
let header = String.toBytes("%PDF-1.7")
Bytes.startsWith(b: header, prefix: String.toBytes("%PDF")) // true
Bytes.startsWith(prefix: String.toBytes("%PDF"), b: header) // trueOn bytes that can be null, use ?. and supply a default with ??:
let body: Bytes | null = null
body?.startsWith(String.toBytes("{")) ?? false // falseApplications
Guess a file type from its magic number:
func kind(data: Bytes): string {
if data.startsWith(Bytes.fromArray([0x89, 0x50, 0x4e, 0x47])) {
"png"
} else if data.startsWith(Bytes.fromArray([0xff, 0xd8, 0xff])) {
"jpeg"
} else if data.startsWith(String.toBytes("%PDF")) {
"pdf"
} else {
"unknown"
}
}
println(kind(Bytes.fromArray([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a])))
println(kind(Bytes.fromArray([0xff, 0xd8, 0xff, 0xe0])))
println(kind(String.toBytes("%PDF-1.7")))
println(kind(String.toBytes("hello")))png
jpeg
pdf
unknownDrop the UTF-8 byte order mark that some editors write at the start of a file:
let bom = Bytes.fromArray([0xef, 0xbb, 0xbf])
func withoutBom(data: Bytes): Bytes {
if data.startsWith(bom) {
data.slice(bom.length(), data.length())
} else {
data
}
}
let raw = Bytes.concat([bom, String.toBytes("name,age")])
println(String.fromBytes(withoutBom(raw)))
println(String.fromBytes(withoutBom(String.toBytes("name,age"))))name,age
name,ageProperties and relations
Bytes.startsWith agrees with slicing the first bytes of b:
let b = String.toBytes("anthill")
for t in ["ant", "", "anthill", "anthills", "hill"] {
let prefix = String.toBytes(t)
let m = prefix.length()
assert(b.startsWith(prefix) == (m <= b.length() && b.slice(0, m) == prefix))
}It holds exactly when Bytes.indexOf finds the prefix at index 0:
let b = String.toBytes("banana")
for t in ["ban", "an", "", "x"] {
let prefix = String.toBytes(t)
assert(b.startsWith(prefix) == (b.indexOf(prefix) == 0))
}Bytes always start with what is joined in front of them with Bytes.concat:
let head = Bytes.fromArray([1, 2])
let tail = Bytes.fromArray([3])
assert(Bytes.concat([head, tail]).startsWith(head))On UTF-8 text, it gives the answer String.startsWith gives:
let s = "año"
for t in ["a", "añ", "ñ", ""] {
assert(Bytes.startsWith(String.toBytes(s), String.toBytes(t)) == s.startsWith(t))
}Possible issues
The bytes come first and the prefix second. Swapped, the call asks whether the prefix begins with the bytes:
let data = String.toBytes("GIF89a")
let magic = String.toBytes("GIF")
Bytes.startsWith(magic, data) // false
Bytes.startsWith(data, magic) // trueThe prefix must be Bytes. Convert a string with String.toBytes, and integers with Bytes.fromArray:
let data = String.toBytes("GIF89a")
println(data.startsWith("GIF")) // error: cannot use string where Bytes is expectedlet data = String.toBytes("GIF89a")
println(data.startsWith([0x47])) // error: cannot use Array[int] where Bytes is expectedEncoding.fromHex returns a Result, not Bytes. Unwrap it first:
let data = Bytes.fromArray([0xef, 0xbb, 0xbf, 0x61])
println(data.startsWith(Encoding.fromHex("efbbbf")))
// error: cannot use Result[Bytes, Error] where Bytes is expectedlet data = Bytes.fromArray([0xef, 0xbb, 0xbf, 0x61])
data.startsWith(Encoding.fromHex("efbbbf").unwrap()) // trueThe match works on bytes, not characters. "é" is two bytes in UTF-8, so its first byte alone is a prefix of the encoded text, though no string ends halfway through a character:
let word = String.toBytes("école")
word.startsWith(Bytes.fromArray([0xc3])) // trueThe same text in two spellings is two byte sequences. The precomposed "é" and "e" followed by a combining accent do not match:
let word = String.toBytes("e\u{301}cole")
word.startsWith(String.toBytes("é")) // false
word.startsWith(String.toBytes(String.normalize("é", "NFD"))) // trueSee also
Bytes.endsWith · Bytes.indexOf · Bytes.slice · Bytes.concat · String.startsWith
