Simplify needs_encoding() and add a test

This commit is contained in:
Alexander Krotov
2020-01-11 15:38:31 +03:00
committed by holger krekel
parent 06bc5513ae
commit a82f2a5df3

View File

@@ -1130,14 +1130,8 @@ fn encode_words(word: &str) -> String {
}
pub fn needs_encoding(to_check: impl AsRef<str>) -> bool {
let to_check = to_check.as_ref();
if to_check.is_empty() {
return false;
}
to_check.chars().any(|c| {
!c.is_ascii_alphanumeric() && c != '-' && c != '_' && c != '.' && c != '~' && c != '%'
!to_check.as_ref().chars().all(|c| {
c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.' || c == '~' || c == '%'
})
}
@@ -1196,4 +1190,12 @@ mod tests {
FBQUFBQUFBQQ==";
assert_eq!(wrapped_base64_encode(input), output);
}
#[test]
fn test_needs_encoding() {
assert!(!needs_encoding(""));
assert!(!needs_encoding("foobar"));
assert!(needs_encoding(" "));
assert!(needs_encoding("foo bar"));
}
}