#utf-8 #slice #validation

vatfluid

UTF-8 slice validation, with incomplete results to support byte streams

3 releases (breaking)

Uses old Rust 2015

0.3.0 Mar 21, 2017
0.2.0 Mar 19, 2017
0.1.0 Mar 18, 2017

#156 in #utf-8


Used in 5 crates (via twist)

MIT/Apache

28KB
513 lines

vatfluid


lib.rs:

Zero allocation UTF-8 validation on &[u8] slices. Allows for incomplete UTF-8 sequences. Returns the position in the buffer that has been validated successfully.

#
use vatfluid::{Error, ErrorKind, Success, validate};

// A `Complete` result.
let buf = b"Hello, World!";
match validate(buf) {
    Ok(Success::Complete(pos)) => {
        assert!(pos == 13);
        assert!(pos == buf.len());
    }
    Ok(Success::Incomplete(_needed, _pos)) => {
        assert!(false);
    }
    Err(_e) => {
        assert!(false);
    }
}

// An `Incomplete` result.
let inc_buf = vec![0xc2, 0xa2, 0xc2];
match validate(&inc_buf) {
    Ok(Success::Complete(_pos)) => {
        assert!(false);
    }
    Ok(Success::Incomplete(needed, pos)) => {
        assert!(needed == 1);
        assert!(pos == 2);
    }
    Err(_e) => {
        assert!(false);
    }
}

// An `Error` result.
let error_buf = vec![0xf4, 0x90, 0x80, 0x80];
match validate(&error_buf) {
    Ok(Success::Complete(_pos)) => {
        assert!(false);
    }
    Ok(Success::Incomplete(_needed, _pos)) => {
        assert!(false);
    }
    Err(e) => {
        assert_matches!(e, Error(ErrorKind::BeyondMaximumCodePoint, _));
    }
}

Dependencies

~2.5–3.5MB
~73K SLoC