Skip to content

Commit

Permalink
Add function for utf-16 compression (#9)
Browse files Browse the repository at this point in the history
* Add method for compressing to utf-16

* Format

* Use std::char::from_u32

* Format
  • Loading branch information
fxwiegand authored Jan 18, 2021
1 parent 68a1d28 commit 3859b6c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ pub fn compress_str(input: &str) -> Vec<u32> {
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<u32> {
compress(&data, 6, |n| {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use crate::{
compress::{
compress,
compress_str,
compress_to_utf16,
compress_uri,
},
decompress::{
Expand Down
7 changes: 7 additions & 0 deletions tests/common_legacy.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use lz_string::{
compress_str,
compress_to_utf16,
compress_uri,
decompress_str,
decompress_uri,
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit 3859b6c

Please sign in to comment.