yay fun with bitsets
fn change(hay: &str) -> String { let mut output = 0u32; for c in hay.chars() { match c { 'a'..='z' => output |= 1u32 << (b'z' - c as u8), 'A'..='Z' => output |= 1u32 << (b'Z' - c as u8), _ => {} } } format!("{:026b}", output) }
fn main() { assert_eq!(change("a **& cZ"), "10100000000000000000000001"); assert_eq!(change("Abc e $$ z"), "11101000000000000000000001"); let c = |x| println!(r#"{} <= "{}""#, change(x), x); // "Tests" c("!!a$%&RgTT"); c(""); c("abcdefghijklmnopqrstuvwxyz"); c("aaaaaaaaaaa"); }
On infinitely long strings it would be good to:
0b11111111111111111111111111
but on such short strings these would both be slower
I have stolen the ascii case sensitivity bit from @miketalbot and will now proceed to pleasure myself with this fish
fn change(hay: &str) -> String { let mut output = 0u32; for c in hay.chars() { if let c @ b'A'..=b'Z' = c as u8 & 0b11011111 { output |= 1u32 << (b'Z' - c) } } format!("{:026b}", output) }
Is this actually faster? No idea. Alternatively
if let c @ b'A'..=b'Z' | c @ b'a'..=b'z' = c as u8 { output |= 1u32 << (b'Z' - (c & 0b11011111)) }
or even just
if matches!(c, 'A'..='Z' | 'a'..='z') { output |= 1u32 << (b'Z' - (c as u8 & 0b11011111)) }
🤷
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Rust
yay fun with bitsets
// "Test"
On infinitely long strings it would be good to:
0b11111111111111111111111111
but on such short strings these would both be slower
Edit:
I have stolen the ascii case sensitivity bit from @miketalbot and will now proceed to pleasure myself with this fish
Is this actually faster?
No idea.
Alternatively
or even just
🤷