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_decodes_to(&[
); 0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5
], "κόσμε");
assert_eq!(
&decode_collect_lossy(&[ assert_decodes_to(&[
0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5 0xf0, 0x9f, 0x8f, 0xb3, 0xef, 0xb8, 0x8f, 0xe2, 0x80, 0x8d, 0xe2, 0x9a, 0xa7, 0xef,
]), 0xb8, 0x8f
"κόσμε" ], "\u{1f3f3}\u{fe0f}\u{200d}\u{26a7}\u{fe0f}");
);
assert_decodes_to(&[
assert_eq!( 0xce, 0x61
&decode_collect_lossy(&[ ], "\u{fffd}a");
0xf0, 0x9f, 0x8f, 0xb3, 0xef, 0xb8, 0x8f, 0xe2, 0x80, 0x8d, 0xe2, 0x9a, 0xa7, 0xef,
0xb8, 0x8f assert_decodes_to(&[
]), 0xce, 0xc2
"\u{1f3f3}\u{fe0f}\u{200d}\u{26a7}\u{fe0f}" ], "\u{fffd}\u{fffd}");
);
assert_decodes_to(&[
assert_eq!( 0x80
&decode_collect_lossy(&[ ], "\u{fffd}");
0xce, 0x61
]), assert_decodes_to(&[
"\u{fffd}a" 0x80, 0x80
); ], "\u{fffd}\u{fffd}");
assert_eq!(
&decode_collect_lossy(&[
0xce, 0xc2
]),
"\u{fffd}\u{fffd}"
);
assert_eq!(
&decode_collect_lossy(&[
0x80
]),
"\u{fffd}"
);
assert_eq!(
&decode_collect_lossy(&[
0x80, 0x80
]),
"\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