| 1 |
use utf8parse::{Parser, Receiver}; |
| 2 |
|
| 3 |
static UTF8_DEMO: &[u8] = include_bytes!("UTF-8-demo.txt"); |
| 4 |
|
| 5 |
#[derive(Debug, PartialEq)] |
| 6 |
struct StringWrapper(String); |
| 7 |
|
| 8 |
impl Receiver for StringWrapper { |
| 9 |
fn codepoint(&mut self, c: char) { |
| 10 |
self.0.push(c); |
| 11 |
} |
| 12 |
|
| 13 |
fn invalid_sequence(&mut self) {} |
| 14 |
} |
| 15 |
|
| 16 |
#[test] |
| 17 |
fn utf8parse_test() { |
| 18 |
let mut parser = Parser::new(); |
| 19 |
|
| 20 |
// utf8parse implementation |
| 21 |
let mut actual = StringWrapper(String::new()); |
| 22 |
|
| 23 |
for byte in UTF8_DEMO { |
| 24 |
parser.advance(&mut actual, *byte) |
| 25 |
} |
| 26 |
|
| 27 |
// standard library implementation |
| 28 |
let expected = String::from_utf8_lossy(UTF8_DEMO); |
| 29 |
|
| 30 |
assert_eq!(actual.0, expected); |
| 31 |
} |