String.toBytes
func String.toBytes(s: string): BytesString.toBytes returns the UTF-8 encoding of s.
Details
- Returns the UTF-8 encoding of
s, and never fails. - An escape writes a code point, not a byte:
"\u{89}"encodes as the two bytesc2 89.
Basic examples
String.toBytes("Ant") // Bytes(41 6e 74)
String.toBytes("é") // Bytes(c3 a9)
String.toBytes("") // Bytes()Call it through . on a string:
"hi".toBytes() // Bytes(68 69)Scope
ASCII takes one byte per code point; other code points take two, three, or four:
String.toBytes("a") // Bytes(61)
String.toBytes("ñ") // Bytes(c3 b1)
String.toBytes("€") // Bytes(e2 82 ac)
String.toBytes("🐜") // Bytes(f0 9f 90 9c)The code point U+0000 encodes as a zero byte, and the replacement character U+FFFD as its three bytes:
String.toBytes("\u{0}") // Bytes(00)
String.toBytes("\u{FFFD}") // Bytes(ef bf bd)String.toBytes encodes the code points as they are and does not normalize. An e followed by U+0301 and the single code point é look alike and encode differently:
String.toBytes("e\u{301}") // Bytes(65 cc 81)
String.toBytes("\u{e9}") // Bytes(c3 a9)The parameter is named s:
String.toBytes(s: "go") // Bytes(67 6f)In a pipe, the string is the first argument:
"naïve" | String.toBytes() | Bytes.length() // 6Index the result to read single bytes as byte values:
let b = String.toBytes("é")
b[0] // 195
b[1] // 169
b.toArray() // [195, 169]A function value works wherever a func(string): Bytes is expected:
let encode: func(string): Bytes = String.toBytes
encode("x") // Bytes(78)Applications
Digests and encodings of text
Crypto and Encoding work on Bytes. Turn text into bytes first:
let digest = "abc" | String.toBytes() | Crypto.sha256() | Encoding.toHex()
digest // "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad""hi?" | String.toBytes() | Encoding.toBase64() // "aGk/"Fit text into a byte limit
Keep as many whole code points as fit into limit bytes, so the cut never splits a code point:
func fitBytes(s: string, limit: int): string {
let kept: Array[string] = []
var used = 0
for c in s {
let n = c.toBytes().length()
if used + n > limit {
break
}
used += n
kept.push(c)
}
String.join(kept, "")
}
println(fitBytes("naïve café", 4))
println(fitBytes("🐜🐜🐜", 9))naï
🐜🐜A length-prefixed field
Write a name as one length byte followed by its UTF-8 bytes. The prefix counts bytes, not code points:
func field(name: string): Bytes {
let data = name.toBytes()
assert(data.length() <= 255)
Bytes.concat([Bytes.fromArray([data.length()]), data])
}
println(field("Zoë"))Bytes(04 5a 6f c3 ab)A one-byte prefix holds at most 255, and Bytes.fromArray panics on a longer name, so field checks the length first.
Write text as bytes
File.writeBytes takes Bytes, and File.readBytes gives back the same bytes:
let body = String.toBytes("name,city\nAnn,Zürich\n")
Result.unwrap(File.writeBytes("people.csv", body))
println(Result.unwrap(File.readBytes("people.csv")) == body)trueProperties and relations
String.fromBytes undoes String.toBytes for every string:
for s in ["", "ant", "straße", "🐜🐜", "e\u{301}", "\u{0}"] {
assert(String.fromBytes(s.toBytes()) == s)
}The bytes of a string are the bytes of its characters, joined:
let s = "Añ🐜"
let parts = s.chars().map(String.toBytes)
assert(Bytes.concat(parts) == s.toBytes())The byte length is at least the code point length and at most four times it:
for s in ["ant", "naïve", "€uro", "🐜"] {
let n = s.toBytes().length()
assert(n >= s.length() && n <= 4 * s.length())
}Equal strings give equal bytes, and different strings give different bytes:
assert(String.toBytes("ab") == String.toBytes("ab"))
assert(String.toBytes("ab") != String.toBytes("ba"))Possible issues
String.length counts code points, not bytes. Use Bytes.length of the encoding for the byte count. The builtin len does the same: it counts code points on a string and bytes on Bytes:
String.length("naïve") // 5
Bytes.length(String.toBytes("naïve")) // 6
len("naïve") // 5
len(String.toBytes("naïve")) // 6A string escape writes a code point, not a byte. To get the byte 0x89, use Bytes.fromArray:
String.toBytes("\u{89}") // Bytes(c2 89)
Bytes.fromArray([0x89]) // Bytes(89)Bytes and string are different types, so comparing them is a compile error:
println(String.toBytes("a") == "a")
// error: Bytes and string cannot be compared with ==: neither is assignable to the otherJson.stringify panics on Bytes. Write them as base64 or hex instead:
Json.stringify(String.toBytes("a")) // panic: Json.stringify: Bytes has no JSON form; write Encoding.toBase64 of it
Json.stringify(Encoding.toBase64(String.toBytes("a"))) // "\"YQ==\""See also
String.fromBytes · String.fromBytesLossy · String.toCodePoints · String.length · Bytes · Bytes.length · Encoding.toHex · Crypto.sha256 · File.writeBytes
