diff --git a/src/compress.rs b/src/compress.rs index b79a7ae..e21cd96 100644 --- a/src/compress.rs +++ b/src/compress.rs @@ -97,6 +97,14 @@ pub fn compress_str(input: &str) -> Vec { compress(input, 16, |n| n) } +#[inline] +pub fn compress_to_utf16(input: &str) -> String { + let buf = compress(input, 15, |n| n + 32); + buf.iter() + .map(|i| std::char::from_u32(*i).unwrap()) + .collect() +} + #[inline] pub fn compress_uri(data: &str) -> Vec { compress(&data, 6, |n| { diff --git a/src/lib.rs b/src/lib.rs index d30581b..90ec8f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ pub use crate::{ compress::{ compress, compress_str, + compress_to_utf16, compress_uri, }, decompress::{ diff --git a/tests/common_legacy.rs b/tests/common_legacy.rs index 6dde301..362ec5e 100644 --- a/tests/common_legacy.rs +++ b/tests/common_legacy.rs @@ -1,5 +1,6 @@ use lz_string::{ compress_str, + compress_to_utf16, compress_uri, decompress_str, decompress_uri, @@ -29,6 +30,12 @@ pub fn compress_red() { assert_eq!(str_to_u32_vec("ᎅ〦䀀"), compressed); } +#[test] +pub fn compress_red_to_utf16() { + let compressed = compress_to_utf16(&RED_STR); + assert_eq!("\u{9e2}䰩䠠".to_string(), compressed); +} + #[test] pub fn compress_repeat() { let data = "aaaaabaaaaacaaaaadaaaaaeaaaaa";