Skip to content

Commit

Permalink
refactor string ops module
Browse files Browse the repository at this point in the history
  • Loading branch information
CenekSanzak committed Jan 31, 2025
1 parent d52b076 commit 9e0f8a9
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 141 deletions.
10 changes: 3 additions & 7 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ mod image_ops;
mod string_ops;
use utils::set_panic_hook;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}

#[wasm_bindgen]
pub fn greet() {
alert("Hello, wasm!");
#[wasm_bindgen(start)]
fn start() {
set_panic_hook();
}
29 changes: 29 additions & 0 deletions wasm/src/string_ops/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use wasm_bindgen::prelude::*;
use crate::utils::process_lines;

#[wasm_bindgen]
pub fn reverse_string(s: &str) -> String {
process_lines(s, |line| line.chars().rev().collect())
}

#[wasm_bindgen]
pub fn count_vowels(s: &str) -> u32 {
s.chars()
.filter(|c| "aeiou".contains(*c))
.count() as u32
}

#[wasm_bindgen]
pub fn lowercase_string(s: &str) -> String {
process_lines(s, |line| line.to_ascii_lowercase())
}

#[wasm_bindgen]
pub fn uppercase_string(s: &str) -> String {
process_lines(s, |line| line.to_ascii_uppercase())
}

#[wasm_bindgen]
pub fn remove_whitespace(s: &str) -> String {
process_lines(s, |line| line.chars().filter(|c| !c.is_whitespace()).collect())
}
69 changes: 69 additions & 0 deletions wasm/src/string_ops/encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use wasm_bindgen::prelude::*;
use base64::{Engine as _, engine::general_purpose};
use data_encoding::{HEXLOWER as BASE16, BASE32};
use base85::{encode, decode};
use crate::utils::process_lines;

#[wasm_bindgen]
pub fn encode_base16(s: &str) -> String {
process_lines(s, |line| BASE16.encode(line.as_bytes()))
}

#[wasm_bindgen]
pub fn decode_base16(s: &str) -> String {
process_lines(s, |line| {
BASE16.decode(line.as_bytes())
.map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned())
})
}

#[wasm_bindgen]
pub fn encode_base32(s: &str) -> String {
process_lines(s, |line| BASE32.encode(line.as_bytes()))
}

#[wasm_bindgen]
pub fn decode_base32(s: &str) -> String {
process_lines(s, |line| {
BASE32.decode(line.as_bytes())
.map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned())
})
}

#[wasm_bindgen]
pub fn encode_base85(s: &str) -> String {
process_lines(s, |line| encode(line.as_bytes()))
}

#[wasm_bindgen]
pub fn decode_base85(s: &str) -> String {
process_lines(s, |line| {
decode(line).map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned())
})
}

#[wasm_bindgen]
pub fn encode_base64_standard(s: &str) -> String {
process_lines(s, |line| general_purpose::STANDARD.encode(line.as_bytes()))
}

#[wasm_bindgen]
pub fn decode_base64_standard(s: &str) -> String {
process_lines(s, |line| {
general_purpose::STANDARD.decode(line)
.map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned())
})
}

#[wasm_bindgen]
pub fn encode_base64_url(s: &str) -> String {
process_lines(s, |line| general_purpose::URL_SAFE.encode(line.as_bytes()))
}

#[wasm_bindgen]
pub fn decode_base64_url(s: &str) -> String {
process_lines(s, |line| {
general_purpose::URL_SAFE.decode(line)
.map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned())
})
}
32 changes: 32 additions & 0 deletions wasm/src/string_ops/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use wasm_bindgen::prelude::*;
use md5::{Md5, Digest};
use sha1::Sha1;
use sha2::Sha256;
use crate::utils::process_lines;

#[wasm_bindgen]
pub fn calculate_md5(s: &str) -> String {
process_lines(s, |line| {
let mut hasher = Md5::new();
hasher.update(line.as_bytes());
format!("{:x}", hasher.finalize())
})
}

#[wasm_bindgen]
pub fn calculate_sha1(s: &str) -> String {
process_lines(s, |line| {
let mut hasher = Sha1::new();
hasher.update(line.as_bytes());
format!("{:x}", hasher.finalize())
})
}

#[wasm_bindgen]
pub fn calculate_sha256(s: &str) -> String {
process_lines(s, |line| {
let mut hasher = Sha256::new();
hasher.update(line.as_bytes());
format!("{:x}", hasher.finalize())
})
}
129 changes: 3 additions & 126 deletions wasm/src/string_ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,126 +1,3 @@
use wasm_bindgen::prelude::*;
use base64::{Engine as _, engine::general_purpose};
use data_encoding::{HEXLOWER as BASE16, BASE32};
use base85::{encode, decode};
use crate::utils::process_lines;
use md5::{Md5, Digest as Md5Digest};
use sha1::{Sha1, Digest as Sha1Digest};
use sha2::{Sha256, Digest as Sha256Digest};

#[wasm_bindgen]
pub fn reverse_string(s: &str) -> String {
process_lines(s, |line| line.chars().rev().collect())
}

#[wasm_bindgen]
pub fn count_vowels(s: &str) -> u32 {
s.chars()
.filter(|c| "aeiou".contains(*c))
.count() as u32
}

#[wasm_bindgen]
pub fn lowercase_string(s: &str) -> String {
process_lines(s, |line| line.to_ascii_lowercase())
}

#[wasm_bindgen]
pub fn uppercase_string(s: &str) -> String {
process_lines(s, |line| line.to_ascii_uppercase())
}

#[wasm_bindgen]
pub fn remove_whitespace(s: &str) -> String {
process_lines(s, |line| line.chars().filter(|c| !c.is_whitespace()).collect())
}

#[wasm_bindgen]
pub fn encode_base16(s: &str) -> String {
process_lines(s, |line| BASE16.encode(line.as_bytes()))
}

#[wasm_bindgen]
pub fn decode_base16(s: &str) -> String {
process_lines(s, |line| {
BASE16.decode(line.as_bytes())
.map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned())
})
}

#[wasm_bindgen]
pub fn encode_base32(s: &str) -> String {
process_lines(s, |line| BASE32.encode(line.as_bytes()))
}

#[wasm_bindgen]
pub fn decode_base32(s: &str) -> String {
process_lines(s, |line| {
BASE32.decode(line.as_bytes())
.map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned())
})
}

#[wasm_bindgen]
pub fn encode_base85(s: &str) -> String {
process_lines(s, |line| encode(line.as_bytes()))
}

#[wasm_bindgen]
pub fn decode_base85(s: &str) -> String {
process_lines(s, |line| {
decode(line).map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned())
})
}

#[wasm_bindgen]
pub fn encode_base64_standard(s: &str) -> String {
process_lines(s, |line| general_purpose::STANDARD.encode(line.as_bytes()))
}

#[wasm_bindgen]
pub fn decode_base64_standard(s: &str) -> String {
process_lines(s, |line| {
general_purpose::STANDARD.decode(line)
.map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned())
})
}

#[wasm_bindgen]
pub fn encode_base64_url(s: &str) -> String {
process_lines(s, |line| general_purpose::URL_SAFE.encode(line.as_bytes()))
}

#[wasm_bindgen]
pub fn decode_base64_url(s: &str) -> String {
process_lines(s, |line| {
general_purpose::URL_SAFE.decode(line)
.map_or(String::new(), |v| String::from_utf8_lossy(&v).into_owned())
})
}

#[wasm_bindgen]
pub fn calculate_md5(s: &str) -> String {
process_lines(s, |line| {
let mut hasher = Md5::new();
hasher.update(line.as_bytes());
format!("{:x}", hasher.finalize())
})
}

#[wasm_bindgen]
pub fn calculate_sha1(s: &str) -> String {
process_lines(s, |line| {
let mut hasher = Sha1::new();
hasher.update(line.as_bytes());
format!("{:x}", hasher.finalize())
})
}

#[wasm_bindgen]
pub fn calculate_sha256(s: &str) -> String {
process_lines(s, |line| {
let mut hasher = Sha256::new();
hasher.update(line.as_bytes());
format!("{:x}", hasher.finalize())
})
}
mod basic;
mod encoding;
mod hash;
8 changes: 0 additions & 8 deletions wasm/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
use wasm_bindgen::prelude::*;

pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
Expand Down

0 comments on commit 9e0f8a9

Please sign in to comment.