String.fromBytesLossy
func String.fromBytesLossy(bytes: Bytes): stringString.fromBytesLossy decodes bytes as UTF-8, replacing each maximal subpart of an ill-formed sequence with U+FFFD. A byte order mark at the start is kept, as U+FEFF.
Details
- Each maximal subpart of an ill-formed sequence becomes one U+FFFD: the bytes
FF FEbecome two, andE2 82, a euro sign cut short, one. - It never fails.
- It decodes the way Ant decodes text from outside the program: a file read with
File.read, standard input, a network peer, a child process. - To learn whether the bytes are text at all, use String.fromBytes, which returns
nullinstead.
Basic examples
Valid UTF-8 decodes to the same text String.fromBytes gives:
String.fromBytesLossy("héllo".toBytes()) // "héllo"A byte that cannot start or continue a character becomes U+FFFD, which renders as �:
String.fromBytesLossy(Bytes.fromArray([0x61, 0xFF, 0x62])) // "a�b"Scope
Empty bytes give the empty string:
String.fromBytesLossy("".toBytes()) // ""Each maximal subpart becomes one U+FFFD. Two bytes that are each invalid on their own give two, and a sequence that starts well and is cut short gives one:
let twoBad = String.fromBytesLossy(Bytes.fromArray([0xFF, 0xFE]))
twoBad // "��"
twoBad.length() // 2
let cutShort = String.fromBytesLossy(Bytes.fromArray([0xE2, 0x82]))
cutShort.length() // 1The bytes after a broken sequence still decode. Here E2 82 begins a euro sign that never ends, and 41 is the letter A:
String.fromBytesLossy(Bytes.fromArray([0xE2, 0x82, 0x41])) // "�A"Bytes that look like UTF-8 but encode no scalar value are ill-formed too: an overlong encoding of /, an encoded surrogate, and a value above U+10FFFF. None of them reaches the string:
String.fromBytesLossy(Bytes.fromArray([0xC0, 0xAF])) // "��"
String.fromBytesLossy(Bytes.fromArray([0xED, 0xA0, 0x80])) // "���"
String.fromBytesLossy(Bytes.fromArray([0xF4, 0x90, 0x80, 0x80])) // "����"Characters outside the Basic Multilingual Plane decode whole when all four bytes are there, and give one U+FFFD when the last is missing:
String.fromBytesLossy(Bytes.fromArray([0xF0, 0x9F, 0x90, 0x9C])) // "🐜"
String.fromBytesLossy(Bytes.fromArray([0xF0, 0x9F, 0x90])) // "�"The zero byte is valid UTF-8, the code point U+0000, and stays:
String.fromBytesLossy(Bytes.fromArray([0x00])) == "\u{0}" // trueThe parameter is named bytes:
String.fromBytesLossy(bytes: "ant".toBytes()) // "ant"In a pipe, the bytes are the first argument:
"Ant 🐜" | String.toBytes() | Bytes.slice(0, 6) | String.fromBytesLossy() // "Ant �"A function value works wherever a func(Bytes): string is expected:
let decode: func(Bytes): string = String.fromBytesLossy
[Bytes.fromArray([0x6f, 0x6b]), Bytes.fromArray([0x80])].map(decode) // ["ok", "�"]Applications
Show the first bytes of a buffer as text, even when the cut lands inside a character:
func preview(b: Bytes, max: int): string {
if len(b) <= max {
return String.fromBytesLossy(b)
}
"${String.fromBytesLossy(b.slice(0, max))}…"
}
println(preview("naïve café".toBytes(), 3))
println(preview("naïve café".toBytes(), 4))na�…
naï…Read a file as bytes, use the bytes, and still log it as text. Here the file holds the Latin-1 byte E9 for é, which is not UTF-8:
let bytes = Result.unwrap(File.readBytes("name.txt"))
println("${len(bytes)} bytes: ${String.fromBytesLossy(bytes)}")4 bytes: Jos�Count how many U+FFFD a decode put in, to report how damaged the input was:
func damage(b: Bytes): int {
String.fromBytesLossy(b).count("\u{FFFD}")
}
damage("fine".toBytes()) // 0
damage(Bytes.fromArray([0x61, 0xFF, 0xE2, 0x82, 0x62])) // 2Properties and relations
On valid UTF-8, String.fromBytesLossy agrees with String.fromBytes and undoes String.toBytes:
for s in ["", "ant", "héllo", "🐜 \u{0} \u{10FFFF}"] {
let b = s.toBytes()
assert(String.fromBytesLossy(b) == s)
assert(String.fromBytes(b) == s)
}When String.fromBytes returns null, the lossy result holds at least one U+FFFD that the bytes did not encode:
let b = Bytes.fromArray([0x61, 0xFF])
assert(String.fromBytes(b) == null)
assert(String.fromBytesLossy(b) == "a\u{FFFD}")The result is always valid text, so encoding it and decoding again changes nothing:
let b = Bytes.fromArray([0xC0, 0xAF, 0x61, 0xED, 0xA0, 0x80])
let s = String.fromBytesLossy(b)
assert(String.fromBytes(s.toBytes()) == s)
assert(String.fromBytesLossy(s.toBytes()) == s)Possible issues
Decoding is lossy: the bytes that became U+FFFD are gone, and encoding the string again does not give them back. Keep the Bytes when you need them as they were:
let b = Bytes.fromArray([0xFF])
String.fromBytesLossy(b).toBytes() // Bytes(ef bf bd)
String.fromBytesLossy(b).toBytes() == b // falseA U+FFFD in the result does not prove the bytes were broken: valid UTF-8 can encode U+FFFD itself. Use String.fromBytes to tell the two apart:
let real = "\u{FFFD}".toBytes()
String.fromBytesLossy(real) == String.fromBytesLossy(Bytes.fromArray([0xFF])) // true
String.fromBytes(real) == null // falseThe function is in the String namespace and its argument is Bytes, so a call through . on the bytes is a compile error:
let b = "ant".toBytes()
println(b.fromBytesLossy()) // error: Bytes has no field or function fromBytesLossyWrite String.fromBytesLossy(b), or pipe the bytes into it: b | String.fromBytesLossy().
A leading byte order mark stays in the result, so the string does not equal the same text without it:
let s = String.fromBytesLossy(Bytes.fromArray([0xEF, 0xBB, 0xBF, 0x61]))
s == "a" // false
s.length() // 2The byte order mark is valid UTF-8 and decodes to U+FEFF. s.trimPrefix("\u{FEFF}") removes it.
See also
String.fromBytes · String.toBytes · Bytes · Bytes.fromArray · File.readBytes · String.fromCodePoints
