1
1
use std:: borrow:: Cow ;
2
2
3
- /// https://tools.ietf.org/html/rfc7230#section-3.2.6
4
- pub ( crate ) fn parse_token ( input : & str ) -> ( Option < & str > , & str ) {
3
+ /// < https://tools.ietf.org/html/rfc7230#section-3.2.6>
4
+ pub ( crate ) fn parse_token ( input : & str ) -> Option < ( Cow < ' _ , str > , & str ) > {
5
5
let mut end_of_token = 0 ;
6
6
for ( i, c) in input. char_indices ( ) {
7
7
if tchar ( c) {
@@ -12,14 +12,15 @@ pub(crate) fn parse_token(input: &str) -> (Option<&str>, &str) {
12
12
}
13
13
14
14
if end_of_token == 0 {
15
- ( None , input )
15
+ None
16
16
} else {
17
- ( Some ( & input[ ..end_of_token] ) , & input[ end_of_token..] )
17
+ let ( token, rest) = input. split_at ( end_of_token) ;
18
+ Some ( ( Cow :: from ( token) , rest) )
18
19
}
19
20
}
20
21
21
- /// https://tools.ietf.org/html/rfc7230#section-3.2.6
22
- fn tchar ( c : char ) -> bool {
22
+ /// < https://tools.ietf.org/html/rfc7230#section-3.2.6>
23
+ pub ( crate ) fn tchar ( c : char ) -> bool {
23
24
matches ! (
24
25
c, 'a' ..='z'
25
26
| 'A' ..='Z'
@@ -42,16 +43,16 @@ fn tchar(c: char) -> bool {
42
43
)
43
44
}
44
45
45
- /// https://tools.ietf.org/html/rfc7230#section-3.2.6
46
+ /// < https://tools.ietf.org/html/rfc7230#section-3.2.6>
46
47
fn vchar ( c : char ) -> bool {
47
48
matches ! ( c as u8 , b'\t' | 32 ..=126 | 128 ..=255 )
48
49
}
49
50
50
- /// https://tools.ietf.org/html/rfc7230#section-3.2.6
51
- pub ( crate ) fn parse_quoted_string ( input : & str ) -> ( Option < Cow < ' _ , str > > , & str ) {
51
+ /// < https://tools.ietf.org/html/rfc7230#section-3.2.6>
52
+ pub ( crate ) fn parse_quoted_string ( input : & str ) -> Option < ( Cow < ' _ , str > , & str ) > {
52
53
// quoted-string must start with a DQUOTE
53
54
if !input. starts_with ( '"' ) {
54
- return ( None , input ) ;
55
+ return None ;
55
56
}
56
57
57
58
let mut end_of_string = None ;
@@ -61,7 +62,7 @@ pub(crate) fn parse_quoted_string(input: &str) -> (Option<Cow<'_, str>>, &str) {
61
62
if i > 1 && backslashes. last ( ) == Some ( & ( i - 2 ) ) {
62
63
if !vchar ( c) {
63
64
// only VCHARs can be escaped
64
- return ( None , input ) ;
65
+ return None ;
65
66
}
66
67
// otherwise, we skip over this character while parsing
67
68
} else {
@@ -81,7 +82,7 @@ pub(crate) fn parse_quoted_string(input: &str) -> (Option<Cow<'_, str>>, &str) {
81
82
b'\t' | b' ' | 15 | 35 ..=91 | 93 ..=126 | 128 ..=255 => { }
82
83
83
84
// unexpected character, bail
84
- _ => return ( None , input ) ,
85
+ _ => return None ,
85
86
}
86
87
}
87
88
}
@@ -110,10 +111,10 @@ pub(crate) fn parse_quoted_string(input: &str) -> (Option<Cow<'_, str>>, &str) {
110
111
. into ( )
111
112
} ;
112
113
113
- ( Some ( value) , & input[ end_of_string..] )
114
+ Some ( ( value, & input[ end_of_string..] ) )
114
115
} else {
115
116
// we never reached a closing DQUOTE, so we do not have a valid quoted-string
116
- ( None , input )
117
+ None
117
118
}
118
119
}
119
120
@@ -122,42 +123,51 @@ mod test {
122
123
use super :: * ;
123
124
#[ test]
124
125
fn token_successful_parses ( ) {
125
- assert_eq ! ( parse_token( "key=value" ) , ( Some ( "key" ) , "=value" ) ) ;
126
- assert_eq ! ( parse_token( "KEY=value" ) , ( Some ( "KEY" ) , "=value" ) ) ;
127
- assert_eq ! ( parse_token( "0123)=value" ) , ( Some ( "0123" ) , ")=value" ) ) ;
128
- assert_eq ! ( parse_token( "a=b" ) , ( Some ( "a" ) , "=b" ) ) ;
129
- assert_eq ! ( parse_token( "!#$%&'*+-.^_`|~=value" ) , ( Some ( "!#$%&'*+-.^_`|~" ) , "=value" , ) ) ;
126
+ assert_eq ! ( parse_token( "key=value" ) , Some ( ( "key" . into( ) , "=value" ) ) ) ;
127
+ assert_eq ! ( parse_token( "KEY=value" ) , Some ( ( "KEY" . into( ) , "=value" ) ) ) ;
128
+ assert_eq ! ( parse_token( "0123)=value" ) , Some ( ( "0123" . into( ) , ")=value" ) ) ) ;
129
+ assert_eq ! ( parse_token( "a=b" ) , Some ( ( "a" . into( ) , "=b" ) ) ) ;
130
+ assert_eq ! (
131
+ parse_token( "!#$%&'*+-.^_`|~=value" ) ,
132
+ Some ( ( "!#$%&'*+-.^_`|~" . into( ) , "=value" ) )
133
+ ) ;
130
134
}
131
135
132
136
#[ test]
133
137
fn token_unsuccessful_parses ( ) {
134
- assert_eq ! ( parse_token( "" ) , ( None , "" ) ) ;
135
- assert_eq ! ( parse_token( "=value" ) , ( None , "=value" ) ) ;
138
+ assert_eq ! ( parse_token( "" ) , None ) ;
139
+ assert_eq ! ( parse_token( "=value" ) , None ) ;
136
140
for c in r#"(),/:;<=>?@[\]{}"# . chars ( ) {
137
141
let s = c. to_string ( ) ;
138
- assert_eq ! ( parse_token( & s) , ( None , & * s ) ) ;
142
+ assert_eq ! ( parse_token( & s) , None ) ;
139
143
140
144
let s = format ! ( "match{}rest" , s) ;
141
- assert_eq ! ( parse_token( & s) , ( Some ( "match" ) , & * format!( "{}rest" , c) ) ) ;
145
+ assert_eq ! (
146
+ parse_token( & s) ,
147
+ Some ( ( "match" . into( ) , & * format!( "{}rest" , c) ) )
148
+ ) ;
142
149
}
143
150
}
144
151
145
152
#[ test]
146
153
fn qstring_successful_parses ( ) {
147
- assert_eq ! ( parse_quoted_string( r#""key"=value"# ) , ( Some ( Cow :: Borrowed ( "key" ) ) , "=value" ) ) ;
154
+ assert_eq ! (
155
+ parse_quoted_string( r#""key"=value"# ) ,
156
+ Some ( ( Cow :: Borrowed ( "key" ) , "=value" ) )
157
+ ) ;
148
158
149
159
assert_eq ! (
150
160
parse_quoted_string( r#""escaped \" quote \""rest"# ) ,
151
- ( Some ( Cow :: Owned ( String :: from( r#"escaped " quote ""# ) ) ) , r#"rest"# )
161
+ Some ( ( Cow :: Owned ( String :: from( r#"escaped " quote ""# ) ) , r#"rest"# ) )
152
162
) ;
153
163
}
154
164
155
165
#[ test]
156
166
fn qstring_unsuccessful_parses ( ) {
157
- assert_eq ! ( parse_quoted_string( r#""abc"# ) , ( None , " \" abc" ) ) ;
158
- assert_eq ! ( parse_quoted_string( r#"hello""# ) , ( None , "hello \" " , ) ) ;
159
- assert_eq ! ( parse_quoted_string( r#"=value\"# ) , ( None , "=value \\ " ) ) ;
160
- assert_eq ! ( parse_quoted_string( r#"\""# ) , ( None , r#"\""# ) ) ;
161
- assert_eq ! ( parse_quoted_string( r#""\""# ) , ( None , r#""\""# ) ) ;
167
+ assert_eq ! ( parse_quoted_string( r#""abc"# ) , None ) ;
168
+ assert_eq ! ( parse_quoted_string( r#"hello""# ) , None ) ;
169
+ assert_eq ! ( parse_quoted_string( r#"=value\"# ) , None ) ;
170
+ assert_eq ! ( parse_quoted_string( r#"\""# ) , None ) ;
171
+ assert_eq ! ( parse_quoted_string( r#""\""# ) , None ) ;
162
172
}
163
173
}
0 commit comments