Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: to_{capitalized,titlecase}, to_{capitalized,titlecase}_into, make_ascii_{capitalized,titlecase} #81

Closed
lopopolo opened this issue Jan 14, 2021 · 1 comment

Comments

@lopopolo
Copy link
Contributor

lopopolo commented Jan 14, 2021

I'm particularly interested in titlecase support since it is not provided by the std.

For the capitalize cases, I believe they could be implemented like:

trait ByteSlice {
    fn make_ascii_capitalized(&mut self) {
        if let Some((head, tail)) = self.as_bytes().split_first_mut() {
            head.make_ascii_uppercase();
            tail.make_ascii_lowercase();
        }
    }

    fn to_capitalized_into(&self, buf: &mut Vec<u8>) {
        // This allocation assumes that in the common case, capitalizing
        // and lowercasing `char`s do not change the length of the
        // `Vec`.
        buf.reserve(bytes.len());
        let mut bytes = self.as_bytes();
        match bstr::decode_utf8(bytes) {
            (Some(ch), size) => {
                // Converting a UTF-8 character to uppercase may yield
                // multiple codepoints.
                for ch in ch.to_uppercase() {
                    buf.push_char(ch)
                }
                bytes = &bytes[size..];
            }
            (None, size) if size == 0 => return,
            (None, size) => {
                let (substring, remainder) = bytes.split_at(size);
                buf.extend_from_slice(substring);
                bytes = remainder;
            }
        }
        while !bytes.is_empty() {
            let (ch, size) = bstr::decode_utf8(bytes);
            if let Some(ch) = ch {
                // Converting a UTF-8 character to lowercase may yield
                // multiple codepoints.
                for ch in ch.to_lowercase() {
                    buf.push_char(ch);
                }
                bytes = &bytes[size..];
            } else {
                let (substring, remainder) = bytes.split_at(size);
                buf.extend_from_slice(substring);
                bytes = remainder;
            }
        }
        self = replacement;
    }
}

The above code is MIT licensed and comes from https://github.com/artichoke/artichoke/blob/fc87277afda4ac3db59ea9c080bbb5f5170b6d10/spinoso-string/src/lib.rs#L1305-L1353.

@lopopolo lopopolo changed the title feat: make_{capitalized,lowercase,uppercase,titlecase}, make_ascii_{capitalized,lowercase,uppercase,titlecase} feat: make_{capitalized,lowercase,uppercase,titlecase}, make_ascii_{capitalized,titlecase} Jan 14, 2021
@lopopolo lopopolo changed the title feat: make_{capitalized,lowercase,uppercase,titlecase}, make_ascii_{capitalized,titlecase} feat: make_{capitalized,titlecase}, make_ascii_{capitalized,titlecase} Jan 14, 2021
@lopopolo lopopolo changed the title feat: make_{capitalized,titlecase}, make_ascii_{capitalized,titlecase} feat: to_{capitalized,titlecase}, to_{capitalized,titlecase}_into, make_ascii_{capitalized,titlecase} Jan 14, 2021
@lopopolo
Copy link
Contributor Author

I think my needs are a bit specialized here. I'm implementing what I need over here: https://github.com/artichoke/roe.

Sorry for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant