Debian Codegraph @

third_party/rust/sfv/src/utils.rs

raw
1 use data_encoding::{Encoding, Specification};
2 use std::iter::Peekable;
3 use std::str::Chars;
4
5 pub(crate) fn base64() -> Result<Encoding, &'static str> {
6 let mut spec = Specification::new();
7 spec.check_trailing_bits = false;
8 spec.symbols
9 .push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
10 spec.padding = None;
11 spec.ignore = "=".to_owned();
12 spec.encoding()
13 .map_err(|_err| "invalid base64 specification")
14 }
15
16 pub(crate) fn is_tchar(c: char) -> bool {
17 // See tchar values list in https://tools.ietf.org/html/rfc7230#section-3.2.6
18 let tchars = "!#$%&'*+-.^_`|~";
19 tchars.contains(c) || c.is_ascii_alphanumeric()
20 }
21
22 pub(crate) fn is_allowed_b64_content(c: char) -> bool {
23 c.is_ascii_alphanumeric() || c == '+' || c == '=' || c == '/'
24 }
25
26 pub(crate) fn consume_ows_chars(input_chars: &mut Peekable<Chars>) {
27 while let Some(c) = input_chars.peek() {
28 if c == &' ' || c == &'\t' {
29 input_chars.next();
30 } else {
31 break;
32 }
33 }
34 }
35
36 pub(crate) fn consume_sp_chars(input_chars: &mut Peekable<Chars>) {
37 while let Some(c) = input_chars.peek() {
38 if c == &' ' {
39 input_chars.next();
40 } else {
41 break;
42 }
43 }
44 }