Skip to main content

utf8_mixed/
utf8_mixed.rs

1// Example shows UTF-8 combinators intermixed with binary combinators
2
3use pom::parser::*;
4use pom::utf8;
5
6fn main() {
7	// A parser for MsgPack (but only messages encoding a string)
8	let testcases: [Vec<u8>; 6] = [
9		vec![0b10100100, 0b11110000, 0b10011111, 0b10100100, 0b10010100], // 🤔, max-size 31 format
10		vec![0xd9, 4, 0b11110000, 0b10011111, 0b10011000, 0b10101110],    // 😮, max-size 255 format
11		vec![0xda, 0, 4, 0b11110000, 0b10011111, 0b10100100, 0b10101111], // 🤯, max-size 2^16-1 format
12		vec![
13			0xdb, 0, 0, 0, 4, 0b11110000, 0b10011111, 0b10010010, 0b10100101,
14		], // 💥, max-size 2^32-1 format
15		vec![0xc4, 4, 0b11110000, 0b10011111, 0b10011000, 0b10101110], // Valid MsgPack, but not a string (binary)
16		vec![0b10100100, 0b10010100, 0b10100100, 0b10011111, 0b11110000], // A MsgPack string, but invalid UTF-8
17	];
18
19	const MASK: u8 = 0b11100000; // size 31 format is denoted by 3 high bits == 101
20	const SIZE_31: u8 = 0b10100000;
21
22	fn rest_as_str<'a>() -> utf8::Parser<'a, &'a str> {
23		utf8::any().repeat(0..).collect()
24	}
25
26	// Demo parser does not verify that the claimed length matches the actual length (but checking so is simple with >>)
27	let parser = (sym(0xdb) * any().repeat(4) * rest_as_str()) // 2^32-1 format
28		| (sym(0xda) * any().repeat(2) * rest_as_str()) // 2^16-1 format
29		| (sym(0xd9) * any()           * rest_as_str()) // 255 format
30		| (is_a(|x| x&MASK == SIZE_31) * rest_as_str()) // 31 format
31		- end();
32
33	for testcase in testcases.iter() {
34		println!("{:?}", parser.parse(testcase));
35	}
36}