2025-02-28 23:16:25 +08:00
|
|
|
use std::char::{ToLowercase, ToUppercase};
|
|
|
|
|
2024-02-23 04:47:55 +08:00
|
|
|
use crate::Tendril;
|
|
|
|
|
|
|
|
// todo: should this be grapheme aware?
|
|
|
|
|
2025-01-12 01:51:45 +08:00
|
|
|
/// Whether there is a camelCase transition, such as at 'l' -> 'C'
|
|
|
|
fn has_camel_transition(prev: Option<char>, current: char) -> bool {
|
|
|
|
current.is_uppercase() && prev.is_some_and(|ch| ch.is_lowercase())
|
|
|
|
}
|
|
|
|
|
2025-01-12 01:04:04 +08:00
|
|
|
pub fn smart_case_conversion(
|
2025-01-12 01:43:57 +08:00
|
|
|
chars: impl Iterator<Item = char>,
|
2024-12-20 18:43:31 +08:00
|
|
|
buf: &mut Tendril,
|
2024-12-20 18:52:13 +08:00
|
|
|
capitalize_first: bool,
|
|
|
|
separator: Option<char>,
|
2024-12-20 18:43:31 +08:00
|
|
|
) {
|
2024-12-20 20:26:05 +08:00
|
|
|
let mut should_capitalize_current = capitalize_first;
|
2025-01-12 03:49:23 +08:00
|
|
|
let mut prev = None;
|
2024-12-20 18:43:31 +08:00
|
|
|
|
2025-01-12 01:49:01 +08:00
|
|
|
let add_separator_if_needed = |prev: Option<char>, buf: &mut Tendril| {
|
|
|
|
if let Some(separator) = separator {
|
|
|
|
// We do not want to add a separator when the previous char is not a separator
|
|
|
|
// For example, snake__case is invalid
|
|
|
|
if prev.is_some_and(|ch| ch != separator) {
|
|
|
|
buf.push(separator);
|
2024-12-20 20:10:05 +08:00
|
|
|
}
|
2025-01-12 01:49:01 +08:00
|
|
|
}
|
|
|
|
};
|
2025-01-12 01:43:57 +08:00
|
|
|
|
2025-01-12 01:49:01 +08:00
|
|
|
for current in chars.skip_while(|ch| ch.is_whitespace()) {
|
2025-01-12 03:56:18 +08:00
|
|
|
if !current.is_alphanumeric() {
|
2024-12-20 20:26:05 +08:00
|
|
|
should_capitalize_current = true;
|
2025-01-12 01:49:01 +08:00
|
|
|
add_separator_if_needed(prev, buf);
|
2025-01-12 03:56:18 +08:00
|
|
|
prev = Some(current);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if has_camel_transition(prev, current) {
|
|
|
|
add_separator_if_needed(prev, buf);
|
|
|
|
should_capitalize_current = true;
|
2024-12-20 15:55:43 +08:00
|
|
|
}
|
2025-01-12 03:56:18 +08:00
|
|
|
|
|
|
|
if should_capitalize_current {
|
2025-03-26 00:04:35 +08:00
|
|
|
buf.extend(current.to_uppercase());
|
2025-01-12 03:56:18 +08:00
|
|
|
should_capitalize_current = false;
|
|
|
|
} else {
|
2025-03-26 00:04:35 +08:00
|
|
|
buf.extend(current.to_lowercase());
|
2025-01-12 03:56:18 +08:00
|
|
|
}
|
|
|
|
|
2025-01-12 01:43:57 +08:00
|
|
|
prev = Some(current);
|
2024-12-20 15:55:43 +08:00
|
|
|
}
|
2024-12-20 20:04:48 +08:00
|
|
|
|
|
|
|
*buf = buf.trim_end().into();
|
2024-12-20 15:55:43 +08:00
|
|
|
}
|
|
|
|
|
2024-12-20 19:32:14 +08:00
|
|
|
pub fn separator_case_conversion(
|
2025-01-12 03:25:24 +08:00
|
|
|
chars: impl Iterator<Item = char>,
|
2024-12-20 17:00:56 +08:00
|
|
|
buf: &mut Tendril,
|
|
|
|
separator: char,
|
|
|
|
) {
|
2025-01-12 03:49:23 +08:00
|
|
|
let mut prev = None;
|
2024-12-20 17:00:56 +08:00
|
|
|
|
2025-01-12 03:25:24 +08:00
|
|
|
for current in chars.skip_while(|ch| ch.is_whitespace()) {
|
2025-01-12 01:51:45 +08:00
|
|
|
if !current.is_alphanumeric() {
|
|
|
|
prev = Some(current);
|
2024-12-20 20:23:53 +08:00
|
|
|
continue;
|
|
|
|
}
|
2024-12-20 17:00:56 +08:00
|
|
|
|
2024-12-20 20:23:53 +08:00
|
|
|
// "email@somewhere" => transition at 'l' -> '@'
|
|
|
|
// first character must not be separator, e.g. @emailSomewhere should not become -email-somewhere
|
|
|
|
let has_alphanum_transition = !prev.is_some_and(|p| p.is_alphanumeric()) && !buf.is_empty();
|
|
|
|
|
2025-01-12 01:51:45 +08:00
|
|
|
if has_camel_transition(prev, current) || has_alphanum_transition {
|
2024-12-20 20:23:53 +08:00
|
|
|
buf.push(separator);
|
2024-12-20 15:55:43 +08:00
|
|
|
}
|
2024-12-20 20:23:53 +08:00
|
|
|
|
2025-03-26 00:04:35 +08:00
|
|
|
buf.extend(current.to_lowercase());
|
2024-12-20 20:23:53 +08:00
|
|
|
|
2025-01-12 01:51:45 +08:00
|
|
|
prev = Some(current);
|
2024-12-20 15:55:43 +08:00
|
|
|
}
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
2024-12-20 15:55:43 +08:00
|
|
|
|
2025-02-28 23:16:25 +08:00
|
|
|
enum AlternateCase {
|
|
|
|
Upper(ToUppercase),
|
|
|
|
Lower(ToLowercase),
|
|
|
|
Keep(Option<char>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for AlternateCase {
|
|
|
|
type Item = char;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
match self {
|
|
|
|
AlternateCase::Upper(upper) => upper.next(),
|
|
|
|
AlternateCase::Lower(lower) => lower.next(),
|
|
|
|
AlternateCase::Keep(ch) => ch.take(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
match self {
|
|
|
|
AlternateCase::Upper(upper) => upper.size_hint(),
|
|
|
|
AlternateCase::Lower(lower) => lower.size_hint(),
|
|
|
|
AlternateCase::Keep(ch) => {
|
|
|
|
let n = if ch.is_some() { 1 } else { 0 };
|
|
|
|
(n, Some(n))
|
|
|
|
}
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
2025-02-28 23:16:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExactSizeIterator for AlternateCase {}
|
|
|
|
|
|
|
|
pub fn into_alternate_case(chars: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
*buf = chars
|
|
|
|
.flat_map(|ch| {
|
|
|
|
if ch.is_lowercase() {
|
|
|
|
AlternateCase::Upper(ch.to_uppercase())
|
|
|
|
} else if ch.is_uppercase() {
|
|
|
|
AlternateCase::Lower(ch.to_lowercase())
|
|
|
|
} else {
|
|
|
|
AlternateCase::Keep(Some(ch))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
|
|
|
|
2025-01-12 03:25:24 +08:00
|
|
|
pub fn into_uppercase(chars: impl Iterator<Item = char>, buf: &mut Tendril) {
|
2025-02-28 23:19:25 +08:00
|
|
|
*buf = chars.flat_map(char::to_uppercase).collect();
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
|
|
|
|
2025-01-12 03:25:24 +08:00
|
|
|
pub fn into_lowercase(chars: impl Iterator<Item = char>, buf: &mut Tendril) {
|
2025-02-28 23:19:25 +08:00
|
|
|
*buf = chars.flat_map(char::to_lowercase).collect();
|
2024-12-20 15:47:13 +08:00
|
|
|
}
|
|
|
|
|
2025-01-12 03:25:24 +08:00
|
|
|
pub fn into_kebab_case(chars: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
separator_case_conversion(chars, buf, '-');
|
2024-12-20 17:00:56 +08:00
|
|
|
}
|
|
|
|
|
2025-01-12 03:25:24 +08:00
|
|
|
pub fn into_snake_case(chars: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
separator_case_conversion(chars, buf, '_');
|
2024-12-20 17:00:56 +08:00
|
|
|
}
|
|
|
|
|
2025-01-12 03:25:24 +08:00
|
|
|
pub fn into_title_case(chars: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
smart_case_conversion(chars, buf, true, Some(' '));
|
2024-12-20 18:52:13 +08:00
|
|
|
}
|
|
|
|
|
2025-01-12 03:25:24 +08:00
|
|
|
pub fn into_camel_case(chars: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
smart_case_conversion(chars, buf, false, None);
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
2025-01-12 03:25:24 +08:00
|
|
|
pub fn into_pascal_case(chars: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
smart_case_conversion(chars, buf, true, None);
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
|
|
|
|
2025-03-25 23:56:37 +08:00
|
|
|
/// Create functional versions of the "into_*" case functions that take a `&mut Tendril`
|
|
|
|
macro_rules! to_case {
|
|
|
|
($($into_case:ident => $to_case:ident)*) => {
|
|
|
|
$(
|
|
|
|
pub fn $to_case(chars: impl Iterator<Item = char>) -> Tendril {
|
|
|
|
let mut res = Tendril::new();
|
|
|
|
$into_case(chars, &mut res);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
};
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
|
|
|
|
2025-03-25 23:56:37 +08:00
|
|
|
to_case! {
|
|
|
|
into_camel_case => to_camel_case
|
|
|
|
into_lowercase => to_lowercase
|
|
|
|
into_uppercase => to_uppercase
|
|
|
|
into_pascal_case => to_pascal_case
|
|
|
|
into_alternate_case => to_alternate_case
|
|
|
|
into_title_case => to_title_case
|
|
|
|
into_kebab_case => to_kebab_case
|
|
|
|
into_snake_case => to_snake_case
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_camel_case_conversion() {
|
2024-12-20 19:55:37 +08:00
|
|
|
let tests = [
|
|
|
|
("hello world", "helloWorld"),
|
|
|
|
("Hello World", "helloWorld"),
|
|
|
|
("hello_world", "helloWorld"),
|
|
|
|
("HELLO_WORLD", "helloWorld"),
|
|
|
|
("hello-world", "helloWorld"),
|
|
|
|
("hello world", "helloWorld"),
|
|
|
|
(" hello world", "helloWorld"),
|
|
|
|
("hello\tworld", "helloWorld"),
|
|
|
|
("HELLO WORLD", "helloWorld"),
|
|
|
|
("HELLO-world", "helloWorld"),
|
|
|
|
("hello WORLD ", "helloWorld"),
|
|
|
|
("helloWorld", "helloWorld"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (input, expected) in tests {
|
|
|
|
assert_eq!(to_camel_case(input.chars()), expected)
|
|
|
|
}
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lower_case_conversion() {
|
2024-12-20 19:55:37 +08:00
|
|
|
let tests = [
|
|
|
|
("HelloWorld", "helloworld"),
|
|
|
|
("HELLO WORLD", "hello world"),
|
|
|
|
("hello_world", "hello_world"),
|
|
|
|
("Hello-World", "hello-world"),
|
|
|
|
("Hello", "hello"),
|
|
|
|
("WORLD", "world"),
|
|
|
|
("hello world", "hello world"),
|
|
|
|
("HELLOworld", "helloworld"),
|
|
|
|
("hello-world", "hello-world"),
|
|
|
|
("hello_world_here", "hello_world_here"),
|
|
|
|
("HELLO_world", "hello_world"),
|
|
|
|
("MixEdCaseString", "mixedcasestring"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (input, expected) in tests {
|
2024-12-20 20:31:07 +08:00
|
|
|
assert_eq!(to_lowercase(input.chars()), expected)
|
2024-12-20 19:55:37 +08:00
|
|
|
}
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_upper_case_conversion() {
|
2024-12-20 19:55:37 +08:00
|
|
|
let tests = [
|
|
|
|
("helloWorld", "HELLOWORLD"),
|
|
|
|
("hello world", "HELLO WORLD"),
|
|
|
|
("hello_world", "HELLO_WORLD"),
|
|
|
|
("Hello-World", "HELLO-WORLD"),
|
|
|
|
("Hello", "HELLO"),
|
|
|
|
("world", "WORLD"),
|
|
|
|
("hello world", "HELLO WORLD"),
|
|
|
|
("helloworld", "HELLOWORLD"),
|
|
|
|
("hello-world", "HELLO-WORLD"),
|
|
|
|
("hello_world_here", "HELLO_WORLD_HERE"),
|
|
|
|
("hello_WORLD", "HELLO_WORLD"),
|
|
|
|
("mixedCaseString", "MIXEDCASESTRING"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (input, expected) in tests {
|
2024-12-20 20:31:07 +08:00
|
|
|
assert_eq!(to_uppercase(input.chars()), expected)
|
2024-12-20 19:55:37 +08:00
|
|
|
}
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pascal_case_conversion() {
|
2024-12-20 19:55:37 +08:00
|
|
|
let tests = [
|
|
|
|
("hello world", "HelloWorld"),
|
|
|
|
("Hello World", "HelloWorld"),
|
|
|
|
("hello_world", "HelloWorld"),
|
|
|
|
("HELLO_WORLD", "HelloWorld"),
|
|
|
|
("hello-world", "HelloWorld"),
|
|
|
|
("hello world", "HelloWorld"),
|
|
|
|
(" hello world", "HelloWorld"),
|
|
|
|
("hello\tworld", "HelloWorld"),
|
|
|
|
("HELLO WORLD", "HelloWorld"),
|
|
|
|
("HELLO-world", "HelloWorld"),
|
|
|
|
("hello WORLD ", "HelloWorld"),
|
|
|
|
("helloWorld", "HelloWorld"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (input, expected) in tests {
|
|
|
|
assert_eq!(to_pascal_case(input.chars()), expected)
|
|
|
|
}
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_alternate_case_conversion() {
|
2024-12-20 19:55:37 +08:00
|
|
|
let tests = [
|
|
|
|
("hello world", "HELLO WORLD"),
|
|
|
|
("Hello World", "hELLO wORLD"),
|
|
|
|
("helLo_woRlD", "HELlO_WOrLd"),
|
|
|
|
("HELLO_world", "hello_WORLD"),
|
|
|
|
("hello-world", "HELLO-WORLD"),
|
|
|
|
("Hello-world", "hELLO-WORLD"),
|
|
|
|
("hello", "HELLO"),
|
|
|
|
("HELLO", "hello"),
|
|
|
|
("hello123", "HELLO123"),
|
|
|
|
("hello WORLD", "HELLO world"),
|
|
|
|
("HELLO123 world", "hello123 WORLD"),
|
|
|
|
("world hello", "WORLD HELLO"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (input, expected) in tests {
|
|
|
|
assert_eq!(to_alternate_case(input.chars()), expected)
|
|
|
|
}
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_title_case_conversion() {
|
2024-12-20 19:55:37 +08:00
|
|
|
let tests = [
|
2024-12-20 20:10:05 +08:00
|
|
|
("hello world", "Hello World"),
|
|
|
|
("Hello World", "Hello World"),
|
|
|
|
("hello_world", "Hello World"),
|
|
|
|
("HELLO_WORLD", "Hello World"),
|
|
|
|
("hello-world", "Hello World"),
|
|
|
|
("hello world", "Hello World"),
|
|
|
|
(" hello world", "Hello World"),
|
|
|
|
("hello\tworld", "Hello World"),
|
|
|
|
("HELLO WORLD", "Hello World"),
|
|
|
|
("HELLO-world", "Hello World"),
|
2024-12-20 20:04:48 +08:00
|
|
|
("hello WORLD ", "Hello World"),
|
2024-12-20 20:10:05 +08:00
|
|
|
("helloWorld", "Hello World"),
|
2024-12-20 19:55:37 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
for (input, expected) in tests {
|
2024-12-20 20:04:48 +08:00
|
|
|
dbg!(input);
|
2024-12-20 19:55:37 +08:00
|
|
|
assert_eq!(to_title_case(input.chars()), expected)
|
|
|
|
}
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_kebab_case_conversion() {
|
2024-12-20 19:55:37 +08:00
|
|
|
let tests = [
|
|
|
|
("helloWorld", "hello-world"),
|
|
|
|
("HelloWorld", "hello-world"),
|
|
|
|
("hello_world", "hello-world"),
|
|
|
|
("HELLO_WORLD", "hello-world"),
|
|
|
|
("hello-world", "hello-world"),
|
|
|
|
("hello world", "hello-world"),
|
|
|
|
("hello\tworld", "hello-world"),
|
|
|
|
("HELLO WORLD", "hello-world"),
|
|
|
|
("HELLO-world", "hello-world"),
|
|
|
|
("hello WORLD ", "hello-world"),
|
|
|
|
("helloWorld", "hello-world"),
|
|
|
|
("HelloWorld123", "hello-world123"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (input, expected) in tests {
|
|
|
|
assert_eq!(to_kebab_case(input.chars()), expected)
|
|
|
|
}
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_snake_case_conversion() {
|
2024-12-20 19:55:37 +08:00
|
|
|
let tests = [
|
|
|
|
("helloWorld", "hello_world"),
|
|
|
|
("HelloWorld", "hello_world"),
|
|
|
|
("hello world", "hello_world"),
|
|
|
|
("HELLO WORLD", "hello_world"),
|
|
|
|
("hello-world", "hello_world"),
|
|
|
|
("hello world", "hello_world"),
|
|
|
|
("hello\tworld", "hello_world"),
|
|
|
|
("HELLO WORLD", "hello_world"),
|
|
|
|
("HELLO-world", "hello_world"),
|
|
|
|
("hello WORLD ", "hello_world"),
|
|
|
|
("helloWorld", "hello_world"),
|
|
|
|
("helloWORLD123", "hello_world123"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (input, expected) in tests {
|
|
|
|
assert_eq!(to_snake_case(input.chars()), expected)
|
|
|
|
}
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
2024-02-23 04:47:55 +08:00
|
|
|
}
|