From c446c5f6d8853e63dca8e666bd502c8e5fa5337d Mon Sep 17 00:00:00 2001 From: Sam Scott Date: Wed, 13 Jul 2022 10:30:29 -0500 Subject: Add a standalone serializer. --- src/utils.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/utils.rs (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..f8c1191 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,25 @@ +use percent_encoding::{AsciiSet, NON_ALPHANUMERIC}; +use std::borrow::Cow; + +pub const QS_ENCODE_SET: &AsciiSet = &NON_ALPHANUMERIC + .remove(b' ') + .remove(b'*') + .remove(b'-') + .remove(b'.') + .remove(b'_'); + +pub fn replace_space(input: &str) -> Cow { + match input.as_bytes().iter().position(|&b| b == b' ') { + None => Cow::Borrowed(input), + Some(first_position) => { + let mut replaced = input.as_bytes().to_owned(); + replaced[first_position] = b'+'; + for byte in &mut replaced[first_position + 1..] { + if *byte == b' ' { + *byte = b'+'; + } + } + Cow::Owned(String::from_utf8(replaced).expect("replacing ' ' with '+' cannot panic")) + } + } +} -- cgit v1.2.3