Files
ziran/std/utf8.zi
T
waozi dc515999bf Return owned Text from the standard text producers
TextReplace, TextJoin, TextLower, TextHex, Float64Repr, the JsonWrite
family, JoinPath, ResolvePath, WorkingDirectory, CreateTemporaryDirectory,
CopyText, ReadStandardInput, DecodeReplace and the unicode case transforms
now return Text. Callers written for a string still get one; a caller that
keeps the result as Text frees it with its owner. 1000 rounds of four
producers leave 4000 allocations when used as strings and none as Text.
2026-11-06 13:41:28 -03:00

179 lines
6.0 KiB
Ziran

// Advance one Unicode scalar in an immutable UTF-8 string. An invalid byte
// advances by one so callers always make progress and never split a valid
// multibyte sequence.
using Vectors :: #import "std/vec";
Continuation :: (byte: u8) -> bool {
return byte >= cast(u8)128 && byte <= cast(u8)191
}
Decoded :: struct {
value: u32
after: s32
valid: bool
}
CopyResult :: struct {
count: s64
complete: bool
}
// Decode one scalar. A malformed sequence consumes its lead byte and yields
// U+FFFD so callers can continue scanning without splitting valid text.
#program_export
Decode :: (text: string, at: s32) -> Decoded {
result: Decoded
if at < 0 { return result }
if at >= cast(s32)text.count {
result.after = cast(s32)text.count
return result
}
first: u8 = text[at]
result.after = at + 1
if first < cast(u8)128 {
result.value = cast(u32)first
result.valid = true
return result
}
result.value = cast(u32)65533
count: s32 = 0
codepoint: u32 = cast(u32)0
minimum: u32 = cast(u32)0
if first >= cast(u8)194 && first <= cast(u8)223 {
count = 1
codepoint = cast(u32)(first & cast(u8)31)
minimum = cast(u32)128
} else if first >= cast(u8)224 && first <= cast(u8)239 {
count = 2
codepoint = cast(u32)(first & cast(u8)15)
minimum = cast(u32)2048
} else if first >= cast(u8)240 && first <= cast(u8)244 {
count = 3
codepoint = cast(u32)(first & cast(u8)7)
minimum = cast(u32)65536
}
if count == 0 || at + count >= cast(s32)text.count { return result }
index: s32 = 1
while index <= count {
part: u8 = text[at + index]
if !Continuation(part) { return result }
codepoint = (codepoint << cast(u32)6) | cast(u32)(part & cast(u8)63)
index += 1
}
if codepoint < minimum || codepoint > cast(u32)1114111 ||
(codepoint >= cast(u32)55296 && codepoint <= cast(u32)57343) {
return result
}
result.value = codepoint
result.after = at + count + 1
result.valid = true
return result
}
#program_export
Next :: (value: string, at: s32) -> s32 {
decoded: Decoded = Decode(value, at)
return decoded.after
}
// Validate every byte without allocating or decoding ordinary ASCII scalars.
// Multibyte windows borrow at most four bytes, keeping Decode's absolute
// 32-bit offset independent of the size of the complete string.
#program_export
Valid :: (text: string) -> bool {
at: s64 = 0
while at < text.count {
while at < text.count && text[at] < cast(u8)128 { at += 1 }
if at == text.count { return true }
end := at + 4
if end > text.count { end = text.count }
decoded := Decode(text[at:end], 0)
if !decoded.valid || decoded.after <= 0 { return false }
at += decoded.after
}
return true
}
#program_export
Count :: (value: string) -> s32 {
at: s32 = 0
count: s32 = 0
while at < (cast(s32)value.count) {
at = Next(value, at)
count = count + 1
}
return count
}
#scope_export
// Copy valid UTF-8 byte-for-byte into a separate bounded destination. Invalid
// input follows Decode: consume only the malformed lead byte and append one
// UTF-8 replacement character. Stop before a source sequence or replacement
// would be split; report the bytes written and whether all input was consumed.
#program_export
Copy :: (source: []u8, destination: []u8) -> CopyResult {
result: CopyResult
at: s64 = 0
while at < source.count {
end := at + 4
if end > source.count { end = source.count }
decoded: Decoded = Decode(TextView(source[at:end]), 0)
length := cast(s64)decoded.after
if length <= 0 { return result }
if decoded.valid {
if length > destination.count - result.count { return result }
index: s64 = 0
while index < length {
destination[result.count + index] = source[at + index]
index += 1
}
result.count += length
} else {
if 3 > destination.count - result.count { return result }
destination[result.count] = cast(u8)239
destination[result.count + 1] = cast(u8)191
destination[result.count + 2] = cast(u8)189
result.count += 3
}
at += length
}
result.complete = true
return result
}
// Decode a whole byte string with Python UTF-8 errors='replace' consumption:
// replace each maximal valid prefix of an ill-formed sequence once. A valid
// but truncated E2 82 is one replacement; forbidden ED A0 80 is three. The
// result has independent immutable storage and is always valid UTF-8.
// Decode and Copy retain their consume-one-malformed-lead contracts.
DecodeReplace :: (source: []u8) -> Text {
output: Vec(u8)
at: s64 = 0
while at < source.count {
first := source[at]
length: s64 = 1
if first >= 194 && first <= 223 { length = 2 }
else if first >= 224 && first <= 239 { length = 3 }
else if first >= 240 && first <= 244 { length = 4 }
consumed: s64 = 1
if length > 1 {
while consumed < length && at + consumed < source.count {
part := source[at + consumed]
if !Continuation(part) { break }
// Restrict the second byte before consuming it: overlong,
// surrogate and out-of-range prefixes are invalid at byte 2.
if consumed == 1 && ((first == 224 && part < 160) ||
(first == 237 && part >= 160) || (first == 240 && part < 144) ||
(first == 244 && part >= 144)) { break }
consumed += 1
}
}
if first < 128 || (length > 1 && consumed == length) {
index: s64 = 0
while index < consumed { VecPush(output, source[at + index]); index += 1 }
} else { BuilderAppend(output, "�") }
at += consumed
}
return BuilderFinish(output)
}