@@ -26,6 +26,7 @@ use std::fmt;
26
26
use std:: from_str:: FromStr ;
27
27
use std:: hash;
28
28
use std:: uint;
29
+ use std:: path:: BytesContainer ;
29
30
30
31
/// A Uniform Resource Locator (URL). A URL is a form of URI (Uniform Resource
31
32
/// Identifier) that includes network location information, such as hostname or
@@ -182,8 +183,8 @@ impl UserInfo {
182
183
}
183
184
}
184
185
185
- fn encode_inner ( s : & str , full_url : bool ) -> String {
186
- s . bytes ( ) . fold ( String :: new ( ) , |mut out, b| {
186
+ fn encode_inner < T : BytesContainer > ( c : T , full_url : bool ) -> String {
187
+ c . container_as_bytes ( ) . iter ( ) . fold ( String :: new ( ) , |mut out, & b| {
187
188
match b as char {
188
189
// unreserved:
189
190
'A' .. 'Z'
@@ -218,17 +219,17 @@ fn encode_inner(s: &str, full_url: bool) -> String {
218
219
/// let url = encode("https://example.com/Rust (programming language)");
219
220
/// println!("{}", url); // https://example.com/Rust%20(programming%20language)
220
221
/// ```
221
- pub fn encode ( s : & str ) -> String {
222
- encode_inner ( s , true )
222
+ pub fn encode < T : BytesContainer > ( container : T ) -> String {
223
+ encode_inner ( container , true )
223
224
}
224
225
225
226
226
227
/// Encodes a URI component by replacing reserved characters with percent-
227
228
/// encoded character sequences.
228
229
///
229
230
/// This function is compliant with RFC 3986.
230
- pub fn encode_component ( s : & str ) -> String {
231
- encode_inner ( s , false )
231
+ pub fn encode_component < T : BytesContainer > ( container : T ) -> String {
232
+ encode_inner ( container , false )
232
233
}
233
234
234
235
pub type DecodeResult < T > = Result < T , String > ;
@@ -245,18 +246,18 @@ pub type DecodeResult<T> = Result<T, String>;
245
246
/// let url = decode("https://example.com/Rust%20(programming%20language)");
246
247
/// println!("{}", url); // https://example.com/Rust (programming language)
247
248
/// ```
248
- pub fn decode ( s : & str ) -> DecodeResult < String > {
249
- decode_inner ( s , true )
249
+ pub fn decode < T : BytesContainer > ( container : T ) -> DecodeResult < String > {
250
+ decode_inner ( container , true )
250
251
}
251
252
252
253
/// Decode a string encoded with percent encoding.
253
- pub fn decode_component ( s : & str ) -> DecodeResult < String > {
254
- decode_inner ( s , false )
254
+ pub fn decode_component < T : BytesContainer > ( container : T ) -> DecodeResult < String > {
255
+ decode_inner ( container , false )
255
256
}
256
257
257
- fn decode_inner ( s : & str , full_url : bool ) -> DecodeResult < String > {
258
+ fn decode_inner < T : BytesContainer > ( c : T , full_url : bool ) -> DecodeResult < String > {
258
259
let mut out = String :: new ( ) ;
259
- let mut iter = s . bytes ( ) ;
260
+ let mut iter = c . container_as_bytes ( ) . iter ( ) . map ( | & b| b ) ;
260
261
261
262
loop {
262
263
match iter. next ( ) {
@@ -864,6 +865,7 @@ mod tests {
864
865
encode_component, decode_component, UserInfo , get_scheme, Url , Path } ;
865
866
866
867
use std:: collections:: HashMap ;
868
+ use std:: path:: BytesContainer ;
867
869
868
870
#[ test]
869
871
fn test_url_parse ( ) {
@@ -1057,7 +1059,7 @@ mod tests {
1057
1059
1058
1060
#[ test]
1059
1061
fn test_encode ( ) {
1060
- fn t ( input : & str , expected : & str ) {
1062
+ fn t < T : BytesContainer > ( input : T , expected : & str ) {
1061
1063
assert_eq ! ( encode( input) , expected. to_string( ) )
1062
1064
}
1063
1065
@@ -1087,11 +1089,13 @@ mod tests {
1087
1089
t ( "]" , "]" ) ;
1088
1090
t ( "\0 " , "%00" ) ;
1089
1091
t ( "\n " , "%0A" ) ;
1092
+
1093
+ t ( & [ 0u8 , 10 , 37 ] , "%00%0A%25" ) ;
1090
1094
}
1091
1095
1092
1096
#[ test]
1093
1097
fn test_encode_component ( ) {
1094
- fn t ( input : & str , expected : & str ) {
1098
+ fn t < T : BytesContainer > ( input : T , expected : & str ) {
1095
1099
assert_eq ! ( encode_component( input) , expected. to_string( ) )
1096
1100
}
1097
1101
@@ -1120,11 +1124,13 @@ mod tests {
1120
1124
t ( "]" , "%5D" ) ;
1121
1125
t ( "\0 " , "%00" ) ;
1122
1126
t ( "\n " , "%0A" ) ;
1127
+
1128
+ t ( & [ 0u8 , 10 , 37 ] , "%00%0A%25" ) ;
1123
1129
}
1124
1130
1125
1131
#[ test]
1126
1132
fn test_decode ( ) {
1127
- fn t ( input : & str , expected : & str ) {
1133
+ fn t < T : BytesContainer > ( input : T , expected : & str ) {
1128
1134
assert_eq ! ( decode( input) , Ok ( expected. to_string( ) ) )
1129
1135
}
1130
1136
@@ -1154,11 +1160,13 @@ mod tests {
1154
1160
t ( "%40" , "%40" ) ;
1155
1161
t ( "%5B" , "%5B" ) ;
1156
1162
t ( "%5D" , "%5D" ) ;
1163
+
1164
+ t ( "%00%0A%25" . as_bytes ( ) , "\0 \n %" ) ;
1157
1165
}
1158
1166
1159
1167
#[ test]
1160
1168
fn test_decode_component ( ) {
1161
- fn t ( input : & str , expected : & str ) {
1169
+ fn t < T : BytesContainer > ( input : T , expected : & str ) {
1162
1170
assert_eq ! ( decode_component( input) , Ok ( expected. to_string( ) ) )
1163
1171
}
1164
1172
@@ -1188,6 +1196,8 @@ mod tests {
1188
1196
t ( "%40" , "@" ) ;
1189
1197
t ( "%5B" , "[" ) ;
1190
1198
t ( "%5D" , "]" ) ;
1199
+
1200
+ t ( "%00%0A%25" . as_bytes ( ) , "\0 \n %" ) ;
1191
1201
}
1192
1202
1193
1203
#[ test]
0 commit comments