make lib no-std compatible

main
pantonshire 3 years ago
parent effe6916d3
commit 9d6eae0bd0

@ -3,6 +3,9 @@ name = "utfdump"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[features]
std = []
[dependencies] [dependencies]
tap = "1.0.1" tap = "1.0.1"

@ -1,3 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std)]
pub mod character; pub mod character;
pub mod unicode_data; pub mod unicode_data;
pub mod utf8; pub mod utf8;

@ -197,64 +197,49 @@ mod tests {
#[test] #[test]
fn test_utf8_decoder() { fn test_utf8_decoder() {
assert_eq!( assert_decodes_to(&[
&decode_collect_lossy(&[
0x68, 0x65, 0x6c, 0x6c, 0x6f 0x68, 0x65, 0x6c, 0x6c, 0x6f
]), ], "hello");
"hello"
);
assert_eq!( assert_decodes_to(&[
&decode_collect_lossy(&[
0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5 0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5
]), ], "κόσμε");
"κόσμε"
);
assert_eq!( assert_decodes_to(&[
&decode_collect_lossy(&[
0xf0, 0x9f, 0x8f, 0xb3, 0xef, 0xb8, 0x8f, 0xe2, 0x80, 0x8d, 0xe2, 0x9a, 0xa7, 0xef, 0xf0, 0x9f, 0x8f, 0xb3, 0xef, 0xb8, 0x8f, 0xe2, 0x80, 0x8d, 0xe2, 0x9a, 0xa7, 0xef,
0xb8, 0x8f 0xb8, 0x8f
]), ], "\u{1f3f3}\u{fe0f}\u{200d}\u{26a7}\u{fe0f}");
"\u{1f3f3}\u{fe0f}\u{200d}\u{26a7}\u{fe0f}"
);
assert_eq!( assert_decodes_to(&[
&decode_collect_lossy(&[
0xce, 0x61 0xce, 0x61
]), ], "\u{fffd}a");
"\u{fffd}a"
);
assert_eq!( assert_decodes_to(&[
&decode_collect_lossy(&[
0xce, 0xc2 0xce, 0xc2
]), ], "\u{fffd}\u{fffd}");
"\u{fffd}\u{fffd}"
);
assert_eq!( assert_decodes_to(&[
&decode_collect_lossy(&[
0x80 0x80
]), ], "\u{fffd}");
"\u{fffd}"
);
assert_eq!( assert_decodes_to(&[
&decode_collect_lossy(&[
0x80, 0x80 0x80, 0x80
]), ], "\u{fffd}\u{fffd}");
"\u{fffd}\u{fffd}"
);
} }
fn decode_collect_lossy(bytes: &[u8]) -> String { fn assert_decodes_to(bytes: &[u8], expected: &str) {
bytes let mut decoded = bytes.decode_utf8();
.decode_utf8()
.map(|res| match res { for expected_char in expected.chars() {
Ok(c) => c, let decoded_char = match decoded.next() {
Err(_) => REPLACEMENT_CHARACTER, Some(Ok(c)) => Some(c),
}) Some(Err(_)) => Some(REPLACEMENT_CHARACTER),
.collect() None => None,
};
assert_eq!(decoded_char, Some(expected_char));
}
assert!(decoded.next().is_none());
} }
} }

Loading…
Cancel
Save