1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use std::ascii::AsciiExt;
use std::borrow::Cow;

/// Internally used by the macro to hyphenate camel-cased attribute names.
pub fn identifier_to_tag_name<'a, T>(s: T) -> Cow<'a, str>
    where T: Into<Cow<'a, str>>
{
    let s = s.into();
    if s.contains(|c: char| c.is_ascii() && c.is_uppercase()) {
        let mut result = String::with_capacity(s.len() + 4);
        for c in s.chars() {
            if c.is_ascii() && c.is_uppercase() {
                result.push('-');
                result.push(c.to_ascii_lowercase());
            } else {
                result.push(c);
            }
        }
        Cow::Owned(result)
    } else {
        s
    }
}