/development/std/string/utf8.nct
utf8.nct
//! UTF-8 validation shared by owned-string construction.
see ./index.nct
func is_valid_utf8(candidate: &[u8]): bool {
var offset: usize = 0
while offset < candidate.len() {
let leading: u8 = candidate[offset]
if leading < 128 {
offset += 1
} else if leading >= 194 && leading <= 223 {
if offset + 1 >= candidate.len() || !is_continuation(candidate[offset + 1]) {
return false
}
offset += 2
} else if leading >= 224 && leading <= 239 {
if offset + 2 >= candidate.len() {
return false
}
let second: u8 = candidate[offset + 1]
if !is_continuation(second) || !is_continuation(candidate[offset + 2]) {
return false
}
if leading == 224 && second < 160 { return false }
if leading == 237 && second >= 160 { return false }
offset += 3
} else if leading >= 240 && leading <= 244 {
if offset + 3 >= candidate.len() {
return false
}
let second: u8 = candidate[offset + 1]
if !is_continuation(second) || !is_continuation(candidate[offset + 2]) || !is_continuation(candidate[offset + 3]) {
return false
}
if leading == 240 && second < 144 { return false }
if leading == 244 && second >= 144 { return false }
offset += 4
} else {
return false
}
}
return true
}
func is_continuation(byte: u8): bool {
return byte >= 128 && byte <= 191
}
func invalid_utf8(): error {
return error.new("std.string.invalid_utf8", "invalid UTF-8")
}