serde_qs/
utils.rs

1use percent_encoding::{AsciiSet, NON_ALPHANUMERIC};
2use std::borrow::Cow;
3
4pub const QS_ENCODE_SET: &AsciiSet = &NON_ALPHANUMERIC
5    .remove(b' ')
6    .remove(b'*')
7    .remove(b'-')
8    .remove(b'.')
9    .remove(b'_');
10
11pub fn replace_space(input: &str) -> Cow<str> {
12    match input.as_bytes().iter().position(|&b| b == b' ') {
13        None => Cow::Borrowed(input),
14        Some(first_position) => {
15            let mut replaced = input.as_bytes().to_owned();
16            replaced[first_position] = b'+';
17            for byte in &mut replaced[first_position + 1..] {
18                if *byte == b' ' {
19                    *byte = b'+';
20                }
21            }
22            Cow::Owned(String::from_utf8(replaced).expect("replacing ' ' with '+' cannot panic"))
23        }
24    }
25}