From 1c7db5aaef2a91b3f87c999d4a21794b19d49ab6 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 20 Sep 2025 16:29:33 +0800 Subject: Add string_proc util crates --- crates/utils/string_proc/Cargo.toml | 6 ++ crates/utils/string_proc/src/lib.rs | 48 +++++++++ crates/utils/string_proc/src/macros.rs | 63 +++++++++++ crates/utils/string_proc/src/string_processer.rs | 131 +++++++++++++++++++++++ 4 files changed, 248 insertions(+) create mode 100644 crates/utils/string_proc/Cargo.toml create mode 100644 crates/utils/string_proc/src/lib.rs create mode 100644 crates/utils/string_proc/src/macros.rs create mode 100644 crates/utils/string_proc/src/string_processer.rs (limited to 'crates/utils/string_proc') diff --git a/crates/utils/string_proc/Cargo.toml b/crates/utils/string_proc/Cargo.toml new file mode 100644 index 0000000..cab0460 --- /dev/null +++ b/crates/utils/string_proc/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "string_proc" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/crates/utils/string_proc/src/lib.rs b/crates/utils/string_proc/src/lib.rs new file mode 100644 index 0000000..1f24028 --- /dev/null +++ b/crates/utils/string_proc/src/lib.rs @@ -0,0 +1,48 @@ +pub mod macros; +pub mod string_processer; + +#[cfg(test)] +mod tests { + use crate::string_processer::StringProcesser; + + #[test] + fn test_processer() { + let test_cases = vec![ + ("brew_coffee", "brewCoffee"), + ("brew, coffee", "brewCoffee"), + ("brew-coffee", "brewCoffee"), + ("Brew.Coffee", "brewCoffee"), + ("bRewCofFee", "bRewCofFee"), + ("brewCoffee", "brewCoffee"), + ("b&rewCoffee", "brewCoffee"), + ("BrewCoffee", "brewCoffee"), + ("brew.coffee", "brewCoffee"), + ("Brew_Coffee", "brewCoffee"), + ("BREW COFFEE", "brewCoffee"), + ]; + + for (input, expected) in test_cases { + let processor = StringProcesser::from(input); + assert_eq!( + processor.to_camel_case(), + expected, + "Failed for input: '{}'", + input + ); + } + } + + #[test] + fn test_conversions() { + let processor = StringProcesser::from("brewCoffee"); + + assert_eq!(processor.to_upper_case(), "BREW COFFEE"); + assert_eq!(processor.to_lower_case(), "brew coffee"); + assert_eq!(processor.to_title_case(), "Brew Coffee"); + assert_eq!(processor.to_dot_case(), "brew.coffee"); + assert_eq!(processor.to_snake_case(), "brew_coffee"); + assert_eq!(processor.to_kebab_case(), "brew-coffee"); + assert_eq!(processor.to_pascal_case(), "BrewCoffee"); + assert_eq!(processor.to_camel_case(), "brewCoffee"); + } +} diff --git a/crates/utils/string_proc/src/macros.rs b/crates/utils/string_proc/src/macros.rs new file mode 100644 index 0000000..9d85338 --- /dev/null +++ b/crates/utils/string_proc/src/macros.rs @@ -0,0 +1,63 @@ +#[macro_export] +macro_rules! camel_case { + ($input:expr) => {{ + use string_proc::string_processer::StringProcesser; + StringProcesser::from($input).to_camel_case() + }}; +} + +#[macro_export] +macro_rules! upper_case { + ($input:expr) => {{ + use string_proc::string_processer::StringProcesser; + StringProcesser::from($input).to_upper_case() + }}; +} + +#[macro_export] +macro_rules! lower_case { + ($input:expr) => {{ + use string_proc::string_processer::StringProcesser; + StringProcesser::from($input).to_lower_case() + }}; +} + +#[macro_export] +macro_rules! title_case { + ($input:expr) => {{ + use string_proc::string_processer::StringProcesser; + StringProcesser::from($input).to_title_case() + }}; +} + +#[macro_export] +macro_rules! dot_case { + ($input:expr) => {{ + use string_proc::string_processer::StringProcesser; + StringProcesser::from($input).to_dot_case() + }}; +} + +#[macro_export] +macro_rules! snake_case { + ($input:expr) => {{ + use string_proc::string_processer::StringProcesser; + StringProcesser::from($input).to_snake_case() + }}; +} + +#[macro_export] +macro_rules! kebab_case { + ($input:expr) => {{ + use string_proc::string_processer::StringProcesser; + StringProcesser::from($input).to_kebab_case() + }}; +} + +#[macro_export] +macro_rules! pascal_case { + ($input:expr) => {{ + use string_proc::string_processer::StringProcesser; + StringProcesser::from($input).to_pascal_case() + }}; +} diff --git a/crates/utils/string_proc/src/string_processer.rs b/crates/utils/string_proc/src/string_processer.rs new file mode 100644 index 0000000..6ad8768 --- /dev/null +++ b/crates/utils/string_proc/src/string_processer.rs @@ -0,0 +1,131 @@ +pub struct StringProcesser { + content: Vec, +} + +impl From for StringProcesser { + fn from(value: String) -> Self { + Self { + content: Self::process_string(value), + } + } +} + +impl From<&str> for StringProcesser { + fn from(value: &str) -> Self { + Self { + content: Self::process_string(value.to_string()), + } + } +} + +impl StringProcesser { + /// Process the string into an intermediate format + fn process_string(input: String) -> Vec { + let mut result = String::new(); + let mut prev_space = false; + + for c in input.chars() { + match c { + 'a'..='z' | 'A'..='Z' | '0'..='9' => { + result.push(c); + prev_space = false; + } + '_' | ',' | '.' | '-' | ' ' => { + if !prev_space { + result.push(' '); + prev_space = true; + } + } + _ => {} + } + } + + let mut processed = String::new(); + let mut chars = result.chars().peekable(); + + while let Some(c) = chars.next() { + processed.push(c); + if let Some(&next) = chars.peek() { + if c.is_lowercase() && next.is_uppercase() { + processed.push(' '); + } + } + } + + processed + .to_lowercase() + .split_whitespace() + .map(|s| s.to_string()) + .collect() + } + + /// Convert to camelCase format (brewCoffee) + pub fn to_camel_case(&self) -> String { + let mut result = String::new(); + for (i, word) in self.content.iter().enumerate() { + if i == 0 { + result.push_str(&word.to_lowercase()); + } else { + let mut chars = word.chars(); + if let Some(first) = chars.next() { + result.push_str(&first.to_uppercase().collect::()); + result.push_str(&chars.collect::().to_lowercase()); + } + } + } + result + } + + /// Convert to PascalCase format (BrewCoffee) + pub fn to_pascal_case(&self) -> String { + let mut result = String::new(); + for word in &self.content { + let mut chars = word.chars(); + if let Some(first) = chars.next() { + result.push_str(&first.to_uppercase().collect::()); + result.push_str(&chars.collect::().to_lowercase()); + } + } + result + } + + /// Convert to kebab-case format (brew-coffee) + pub fn to_kebab_case(&self) -> String { + self.content.join("-").to_lowercase() + } + + /// Convert to snake_case format (brew_coffee) + pub fn to_snake_case(&self) -> String { + self.content.join("_").to_lowercase() + } + + /// Convert to dot.case format (brew.coffee) + pub fn to_dot_case(&self) -> String { + self.content.join(".").to_lowercase() + } + + /// Convert to Title Case format (Brew Coffee) + pub fn to_title_case(&self) -> String { + let mut result = String::new(); + for word in &self.content { + let mut chars = word.chars(); + if let Some(first) = chars.next() { + result.push_str(&first.to_uppercase().collect::()); + result.push_str(&chars.collect::().to_lowercase()); + } + result.push(' '); + } + result.pop(); + result + } + + /// Convert to lower case format (brew coffee) + pub fn to_lower_case(&self) -> String { + self.content.join(" ").to_lowercase() + } + + /// Convert to UPPER CASE format (BREW COFFEE) + pub fn to_upper_case(&self) -> String { + self.content.join(" ").to_uppercase() + } +} -- cgit