Skip to content

Commit a68d10e

Browse files
committed
std::str: safen and optimize is_utf8.
This uses a vector iterator to avoid the necessity for unsafe indexing, and makes this function slightly faster. Unfortunately #11751 means that the iterator comes with repeated `null` checks which means the pure-ASCII case still has room for significant improvement (and the other cases too, but it's most significant for just ASCII). Before: is_utf8_100_ascii ... bench: 143 ns/iter (+/- 6) is_utf8_100_multibyte ... bench: 134 ns/iter (+/- 4) After: is_utf8_100_ascii ... bench: 123 ns/iter (+/- 4) is_utf8_100_multibyte ... bench: 115 ns/iter (+/- 5)
1 parent c848906 commit a68d10e

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

src/libstd/str.rs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,84 @@ pub fn eq(a: &~str, b: &~str) -> bool {
731731
Section: Misc
732732
*/
733733

734-
/// Determines if a vector of bytes contains valid UTF-8
734+
/// Walk through `iter` checking that it's a valid UTF-8 sequence,
735+
/// returning `true` in that case, or, if it is invalid, `false` with
736+
/// `iter` reset such that it is pointing at the first byte in the
737+
/// invalid sequence.
738+
#[inline(always)]
739+
fn run_utf8_validation_iterator(iter: &mut vec::Items<u8>) -> bool {
740+
loop {
741+
// save the current thing we're pointing at.
742+
let old = *iter;
743+
744+
// restore the iterator we had at the start of this codepoint.
745+
macro_rules! err ( () => { {*iter = old; return false} });
746+
macro_rules! next ( () => {
747+
match iter.next() {
748+
Some(a) => *a,
749+
// we needed data, but there was none: error!
750+
None => err!()
751+
}
752+
});
753+
754+
let first = match iter.next() {
755+
Some(&b) => b,
756+
// we're at the end of the iterator and a codepoint
757+
// boundary at the same time, so this string is valid.
758+
None => return true
759+
};
760+
761+
// ASCII characters are always valid, so only large
762+
// bytes need more examination.
763+
if first >= 128 {
764+
let w = utf8_char_width(first);
765+
let second = next!();
766+
// 2-byte encoding is for codepoints \u0080 to \u07ff
767+
// first C2 80 last DF BF
768+
// 3-byte encoding is for codepoints \u0800 to \uffff
769+
// first E0 A0 80 last EF BF BF
770+
// excluding surrogates codepoints \ud800 to \udfff
771+
// ED A0 80 to ED BF BF
772+
// 4-byte encoding is for codepoints \u10000 to \u10ffff
773+
// first F0 90 80 80 last F4 8F BF BF
774+
//
775+
// Use the UTF-8 syntax from the RFC
776+
//
777+
// https://tools.ietf.org/html/rfc3629
778+
// UTF8-1 = %x00-7F
779+
// UTF8-2 = %xC2-DF UTF8-tail
780+
// UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
781+
// %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
782+
// UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
783+
// %xF4 %x80-8F 2( UTF8-tail )
784+
match w {
785+
2 => if second & 192 != TAG_CONT_U8 {err!()},
786+
3 => {
787+
match (first, second, next!() & 192) {
788+
(0xE0 , 0xA0 .. 0xBF, TAG_CONT_U8) |
789+
(0xE1 .. 0xEC, 0x80 .. 0xBF, TAG_CONT_U8) |
790+
(0xED , 0x80 .. 0x9F, TAG_CONT_U8) |
791+
(0xEE .. 0xEF, 0x80 .. 0xBF, TAG_CONT_U8) => {}
792+
_ => err!()
793+
}
794+
}
795+
4 => {
796+
match (first, second, next!() & 192, next!() & 192) {
797+
(0xF0 , 0x90 .. 0xBF, TAG_CONT_U8, TAG_CONT_U8) |
798+
(0xF1 .. 0xF3, 0x80 .. 0xBF, TAG_CONT_U8, TAG_CONT_U8) |
799+
(0xF4 , 0x80 .. 0x8F, TAG_CONT_U8, TAG_CONT_U8) => {}
800+
_ => err!()
801+
}
802+
}
803+
_ => err!()
804+
}
805+
}
806+
}
807+
}
808+
809+
/// Determines if a vector of bytes contains valid UTF-8.
735810
pub fn is_utf8(v: &[u8]) -> bool {
736-
first_non_utf8_index(v).is_none()
811+
run_utf8_validation_iterator(&mut v.iter())
737812
}
738813

739814
#[inline(always)]

0 commit comments

Comments
 (0)