Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- let vv = vec![
- vec![0x41, 0x42, 0x43], // y
- vec![0x00, 0x01, 0x02], // n
- vec![0x80, 0x7F, 0x41], // n
- vec![0xD9, 0x84, 0x10], // y
- vec![0xF0, 0x81, 0x82, 0x41], // n
- vec![0xEF, 0x8A, 0xA7, 0x91], // n
- vec![0xE1, 0xE1, 0x01], // n
- vec![0xE0, 0x80, 0x87], //n
- vec![0xc0, 0x80], // y
- vec![0xF8, 0x42, 0x43], //n
- ];
- /*
- Bytes in the range of 0-0x7F, inclusive, are normally valid
- Bytes with the bit pattern 10XX XXXX are considered continuation bytes, with the six least significant bits being used to encode part of a codepoint. These must not appear unless they are expected by a preceding byte.
- Bytes with the pattern 110X XXXX expect one continuation byte afterward
- Bytes with the pattern 1110 XXXX expect two continuation bytes afterward
- Bytes with the pattern 1111 0XXX expect three continuation bytes afterward
- */
- let m=|v:&Vec<u8>|v.iter().fold(0, |s, b| match (s, b) {
- (0, 1..=0x7F) => 0,
- (0, 0xc0..=0xdf) => 1,
- (0, 0xe0..=0xef) => 2,
- (0, 0xf0..=0xf7) => 3,
- (2..=3, 0x80) => -1,
- (1..=3, 0x81..=0xbf) => s - 1,
- (1, 0x80) => 0,
- _ => -1,
- })==0;
- for v in vv {
- let x = m(&v);
- println!("{:?} {}", v, x);
- }
- }
Add Comment
Please, Sign In to add comment