From 3859b6cf2ac9d7c0aa6f20e9eff6df0c084a6c1a Mon Sep 17 00:00:00 2001 From: Felix Wiegand Date: Tue, 19 Jan 2021 00:48:53 +0100 Subject: [PATCH] Add function for utf-16 compression (#9) * Add method for compressing to utf-16 * Format * Use std::char::from_u32 * Format --- src/compress.rs | 8 ++++++++ src/lib.rs | 1 + tests/common_legacy.rs | 7 +++++++ 3 files changed, 16 insertions(+) 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";