Guest User

Untitled

a guest
Jan 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. fn main() {
  2. let vv = vec![
  3. vec![0x41, 0x42, 0x43], // y
  4. vec![0x00, 0x01, 0x02], // n
  5. vec![0x80, 0x7F, 0x41], // n
  6. vec![0xD9, 0x84, 0x10], // y
  7. vec![0xF0, 0x81, 0x82, 0x41], // n
  8. vec![0xEF, 0x8A, 0xA7, 0x91], // n
  9. vec![0xE1, 0xE1, 0x01], // n
  10. vec![0xE0, 0x80, 0x87], //n
  11. vec![0xc0, 0x80], // y
  12. vec![0xF8, 0x42, 0x43], //n
  13. ];
  14. /*
  15. Bytes in the range of 0-0x7F, inclusive, are normally valid
  16. 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.
  17. Bytes with the pattern 110X XXXX expect one continuation byte afterward
  18. Bytes with the pattern 1110 XXXX expect two continuation bytes afterward
  19. Bytes with the pattern 1111 0XXX expect three continuation bytes afterward
  20. */
  21. let m=|v:&Vec<u8>|v.iter().fold(0, |s, b| match (s, b) {
  22. (0, 1..=0x7F) => 0,
  23. (0, 0xc0..=0xdf) => 1,
  24. (0, 0xe0..=0xef) => 2,
  25. (0, 0xf0..=0xf7) => 3,
  26. (2..=3, 0x80) => -1,
  27. (1..=3, 0x81..=0xbf) => s - 1,
  28. (1, 0x80) => 0,
  29. _ => -1,
  30. })==0;
  31. for v in vv {
  32. let x = m(&v);
  33. println!("{:?} {}", v, x);
  34. }
  35. }
Add Comment
Please, Sign In to add comment