Skip to content

Commit 4703bb4

Browse files
committed
liburl: Generic input for {en,de}code.
1 parent 465ec23 commit 4703bb4

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/liburl/lib.rs

+26-16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use std::fmt;
2626
use std::from_str::FromStr;
2727
use std::hash;
2828
use std::uint;
29+
use std::path::BytesContainer;
2930

3031
/// A Uniform Resource Locator (URL). A URL is a form of URI (Uniform Resource
3132
/// Identifier) that includes network location information, such as hostname or
@@ -182,8 +183,8 @@ impl UserInfo {
182183
}
183184
}
184185

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| {
187188
match b as char {
188189
// unreserved:
189190
'A' .. 'Z'
@@ -218,17 +219,17 @@ fn encode_inner(s: &str, full_url: bool) -> String {
218219
/// let url = encode("https://example.com/Rust (programming language)");
219220
/// println!("{}", url); // https://example.com/Rust%20(programming%20language)
220221
/// ```
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)
223224
}
224225

225226

226227
/// Encodes a URI component by replacing reserved characters with percent-
227228
/// encoded character sequences.
228229
///
229230
/// 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)
232233
}
233234

234235
pub type DecodeResult<T> = Result<T, String>;
@@ -245,18 +246,18 @@ pub type DecodeResult<T> = Result<T, String>;
245246
/// let url = decode("https://example.com/Rust%20(programming%20language)");
246247
/// println!("{}", url); // https://example.com/Rust (programming language)
247248
/// ```
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)
250251
}
251252

252253
/// 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)
255256
}
256257

257-
fn decode_inner(s: &str, full_url: bool) -> DecodeResult<String> {
258+
fn decode_inner<T: BytesContainer>(c: T, full_url: bool) -> DecodeResult<String> {
258259
let mut out = String::new();
259-
let mut iter = s.bytes();
260+
let mut iter = c.container_as_bytes().iter().map(|&b| b);
260261

261262
loop {
262263
match iter.next() {
@@ -864,6 +865,7 @@ mod tests {
864865
encode_component, decode_component, UserInfo, get_scheme, Url, Path};
865866

866867
use std::collections::HashMap;
868+
use std::path::BytesContainer;
867869

868870
#[test]
869871
fn test_url_parse() {
@@ -1057,7 +1059,7 @@ mod tests {
10571059

10581060
#[test]
10591061
fn test_encode() {
1060-
fn t(input: &str, expected: &str) {
1062+
fn t<T: BytesContainer>(input: T, expected: &str) {
10611063
assert_eq!(encode(input), expected.to_string())
10621064
}
10631065

@@ -1087,11 +1089,13 @@ mod tests {
10871089
t("]", "]");
10881090
t("\0", "%00");
10891091
t("\n", "%0A");
1092+
1093+
t(&[0u8, 10, 37], "%00%0A%25");
10901094
}
10911095

10921096
#[test]
10931097
fn test_encode_component() {
1094-
fn t(input: &str, expected: &str) {
1098+
fn t<T: BytesContainer>(input: T, expected: &str) {
10951099
assert_eq!(encode_component(input), expected.to_string())
10961100
}
10971101

@@ -1120,11 +1124,13 @@ mod tests {
11201124
t("]", "%5D");
11211125
t("\0", "%00");
11221126
t("\n", "%0A");
1127+
1128+
t(&[0u8, 10, 37], "%00%0A%25");
11231129
}
11241130

11251131
#[test]
11261132
fn test_decode() {
1127-
fn t(input: &str, expected: &str) {
1133+
fn t<T: BytesContainer>(input: T, expected: &str) {
11281134
assert_eq!(decode(input), Ok(expected.to_string()))
11291135
}
11301136

@@ -1154,11 +1160,13 @@ mod tests {
11541160
t("%40", "%40");
11551161
t("%5B", "%5B");
11561162
t("%5D", "%5D");
1163+
1164+
t("%00%0A%25".as_bytes(), "\0\n%");
11571165
}
11581166

11591167
#[test]
11601168
fn test_decode_component() {
1161-
fn t(input: &str, expected: &str) {
1169+
fn t<T: BytesContainer>(input: T, expected: &str) {
11621170
assert_eq!(decode_component(input), Ok(expected.to_string()))
11631171
}
11641172

@@ -1188,6 +1196,8 @@ mod tests {
11881196
t("%40", "@");
11891197
t("%5B", "[");
11901198
t("%5D", "]");
1199+
1200+
t("%00%0A%25".as_bytes(), "\0\n%");
11911201
}
11921202

11931203
#[test]

0 commit comments

Comments
 (0)