aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock1
-rw-r--r--macros/Cargo.toml1
-rw-r--r--macros/src/util/string.rs39
3 files changed, 29 insertions, 12 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6464af3..cd03ac1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -539,7 +539,6 @@ dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
- "regex",
"syn 1.0.109",
"syrette",
"thiserror",
diff --git a/macros/Cargo.toml b/macros/Cargo.toml
index b9d2e68..6f27311 100644
--- a/macros/Cargo.toml
+++ b/macros/Cargo.toml
@@ -25,7 +25,6 @@ syn = { version = "1.0.96", features = ["full"] }
quote = "1.0.18"
proc-macro2 = "1.0.40"
uuid = { version = "0.8", features = ["v4"] }
-regex = "1.6.0"
once_cell = "1.13.1"
thiserror = "1.0.37"
proc-macro-error = "1.0.4"
diff --git a/macros/src/util/string.rs b/macros/src/util/string.rs
index a04a021..b29fe21 100644
--- a/macros/src/util/string.rs
+++ b/macros/src/util/string.rs
@@ -1,14 +1,28 @@
-use once_cell::sync::Lazy;
-use regex::Regex;
-
-static CAMELCASE_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"([a-z])([A-Z])").unwrap());
-
pub fn camelcase_to_snakecase(camelcased: &str) -> String
{
- CAMELCASE_RE
- .replace(camelcased, "${1}_$2")
- .to_string()
- .to_lowercase()
+ let mut prev_char_was_lowercase = false;
+
+ camelcased
+ .chars()
+ .fold(String::new(), |mut acc, character| {
+ if character.is_lowercase() {
+ prev_char_was_lowercase = true;
+
+ acc.push(character);
+
+ return acc;
+ }
+
+ if character.is_uppercase() && prev_char_was_lowercase {
+ prev_char_was_lowercase = false;
+
+ acc.push('_');
+ }
+
+ acc.push(character.to_ascii_lowercase());
+
+ acc
+ })
}
#[cfg(test)]
@@ -21,6 +35,11 @@ mod tests
{
assert_eq!(camelcase_to_snakecase("LoginHandler"), "login_handler");
- assert_eq!(camelcase_to_snakecase("Regex"), "regex");
+ assert_eq!(camelcase_to_snakecase("Transient"), "transient");
+
+ assert_eq!(
+ camelcase_to_snakecase("SystemInfoManager"),
+ "system_info_manager"
+ );
}
}