Skip to content

Commit e617651

Browse files
committed
Removed convenience encoding trait impls
Encoding should really only be done from [u8]<->str. The extra convenience implementations don't really have a place, especially since they're so trivial. Also improved error messages in FromBase64.
1 parent 858e166 commit e617651

File tree

2 files changed

+63
-154
lines changed

2 files changed

+63
-154
lines changed

src/libextra/base64.rs

+41-84
Original file line numberDiff line numberDiff line change
@@ -150,52 +150,38 @@ impl<'self> ToBase64 for &'self [u8] {
150150
}
151151
}
152152

153-
impl<'self> ToBase64 for &'self str {
154-
/**
155-
* Convert any string (literal, `@`, `&`, or `~`) to base64 encoding.
156-
*
157-
*
158-
* # Example
159-
*
160-
* ~~~ {.rust}
161-
* extern mod extra;
162-
* use extra::base64::{ToBase64, standard};
163-
*
164-
* fn main () {
165-
* let str = "Hello, World".to_base64(standard);
166-
* printfln!("%s", str);
167-
* }
168-
* ~~~
169-
*
170-
*/
171-
fn to_base64(&self, config: Config) -> ~str {
172-
self.as_bytes().to_base64(config)
173-
}
174-
}
175-
176153
/// A trait for converting from base64 encoded values.
177154
pub trait FromBase64 {
178155
/// Converts the value of `self`, interpreted as base64 encoded data, into
179156
/// an owned vector of bytes, returning the vector.
180157
fn from_base64(&self) -> Result<~[u8], ~str>;
181158
}
182159

183-
impl<'self> FromBase64 for &'self [u8] {
160+
impl<'self> FromBase64 for &'self str {
184161
/**
185-
* Convert base64 `u8` vector into u8 byte values.
186-
* Every 4 encoded characters is converted into 3 octets, modulo padding.
162+
* Convert any base64 encoded string (literal, `@`, `&`, or `~`)
163+
* to the byte values it encodes.
164+
*
165+
* You can use the `from_bytes` function in `std::str`
166+
* to turn a `[u8]` into a string with characters corresponding to those
167+
* values.
187168
*
188169
* # Example
189170
*
171+
* This converts a string literal to base64 and back.
172+
*
190173
* ~~~ {.rust}
191174
* extern mod extra;
192175
* use extra::base64::{ToBase64, FromBase64, standard};
176+
* use std::str;
193177
*
194178
* fn main () {
195-
* let str = [52,32].to_base64(standard);
196-
* printfln!("%s", str);
197-
* let bytes = str.from_base64();
179+
* let hello_str = "Hello, World".to_base64(standard);
180+
* printfln!("%s", hello_str);
181+
* let bytes = hello_str.from_base64();
198182
* printfln!("%?", bytes);
183+
* let result_str = str::from_bytes(bytes);
184+
* printfln!("%s", result_str);
199185
* }
200186
* ~~~
201187
*/
@@ -204,20 +190,20 @@ impl<'self> FromBase64 for &'self [u8] {
204190
let mut buf: u32 = 0;
205191
let mut modulus = 0;
206192

207-
let mut it = self.iter();
208-
for &byte in it {
209-
let ch = byte as char;
193+
let mut it = self.byte_iter().enumerate();
194+
for (idx, byte) in it {
210195
let val = byte as u32;
211196

212-
match ch {
197+
match byte as char {
213198
'A'..'Z' => buf |= val - 0x41,
214199
'a'..'z' => buf |= val - 0x47,
215200
'0'..'9' => buf |= val + 0x04,
216201
'+'|'-' => buf |= 0x3E,
217202
'/'|'_' => buf |= 0x3F,
218203
'\r'|'\n' => loop,
219204
'=' => break,
220-
_ => return Err(~"Invalid Base64 character")
205+
_ => return Err(fmt!("Invalid character '%c' at position %u",
206+
self.char_at(idx), idx))
221207
}
222208

223209
buf <<= 6;
@@ -230,8 +216,11 @@ impl<'self> FromBase64 for &'self [u8] {
230216
}
231217
}
232218

233-
if !it.all(|&byte| {byte as char == '='}) {
234-
return Err(~"Invalid Base64 character");
219+
for (idx, byte) in it {
220+
if (byte as char) != '=' {
221+
return Err(fmt!("Invalid character '%c' at position %u",
222+
self.char_at(idx), idx));
223+
}
235224
}
236225

237226
match modulus {
@@ -250,67 +239,35 @@ impl<'self> FromBase64 for &'self [u8] {
250239
}
251240
}
252241

253-
impl<'self> FromBase64 for &'self str {
254-
/**
255-
* Convert any base64 encoded string (literal, `@`, `&`, or `~`)
256-
* to the byte values it encodes.
257-
*
258-
* You can use the `from_bytes` function in `std::str`
259-
* to turn a `[u8]` into a string with characters corresponding to those
260-
* values.
261-
*
262-
* # Example
263-
*
264-
* This converts a string literal to base64 and back.
265-
*
266-
* ~~~ {.rust}
267-
* extern mod extra;
268-
* use extra::base64::{ToBase64, FromBase64, standard};
269-
* use std::str;
270-
*
271-
* fn main () {
272-
* let hello_str = "Hello, World".to_base64(standard);
273-
* printfln!("%s", hello_str);
274-
* let bytes = hello_str.from_base64();
275-
* printfln!("%?", bytes);
276-
* let result_str = str::from_bytes(bytes);
277-
* printfln!("%s", result_str);
278-
* }
279-
* ~~~
280-
*/
281-
fn from_base64(&self) -> Result<~[u8], ~str> {
282-
self.as_bytes().from_base64()
283-
}
284-
}
285-
286242
#[cfg(test)]
287243
mod test {
288244
use test::BenchHarness;
289245
use base64::*;
290246

291247
#[test]
292248
fn test_to_base64_basic() {
293-
assert_eq!("".to_base64(STANDARD), ~"");
294-
assert_eq!("f".to_base64(STANDARD), ~"Zg==");
295-
assert_eq!("fo".to_base64(STANDARD), ~"Zm8=");
296-
assert_eq!("foo".to_base64(STANDARD), ~"Zm9v");
297-
assert_eq!("foob".to_base64(STANDARD), ~"Zm9vYg==");
298-
assert_eq!("fooba".to_base64(STANDARD), ~"Zm9vYmE=");
299-
assert_eq!("foobar".to_base64(STANDARD), ~"Zm9vYmFy");
249+
assert_eq!("".as_bytes().to_base64(STANDARD), ~"");
250+
assert_eq!("f".as_bytes().to_base64(STANDARD), ~"Zg==");
251+
assert_eq!("fo".as_bytes().to_base64(STANDARD), ~"Zm8=");
252+
assert_eq!("foo".as_bytes().to_base64(STANDARD), ~"Zm9v");
253+
assert_eq!("foob".as_bytes().to_base64(STANDARD), ~"Zm9vYg==");
254+
assert_eq!("fooba".as_bytes().to_base64(STANDARD), ~"Zm9vYmE=");
255+
assert_eq!("foobar".as_bytes().to_base64(STANDARD), ~"Zm9vYmFy");
300256
}
301257
302258
#[test]
303259
fn test_to_base64_line_break() {
304260
assert!(![0u8, 1000].to_base64(Config {line_length: None, ..STANDARD})
305261
.contains("\r\n"));
306-
assert_eq!("foobar".to_base64(Config {line_length: Some(4), ..STANDARD}),
262+
assert_eq!("foobar".as_bytes().to_base64(Config {line_length: Some(4),
263+
..STANDARD}),
307264
~"Zm9v\r\nYmFy");
308265
}
309266
310267
#[test]
311268
fn test_to_base64_padding() {
312-
assert_eq!("f".to_base64(Config {pad: false, ..STANDARD}), ~"Zg");
313-
assert_eq!("fo".to_base64(Config {pad: false, ..STANDARD}), ~"Zm8");
269+
assert_eq!("f".as_bytes().to_base64(Config {pad: false, ..STANDARD}), ~"Zg");
270+
assert_eq!("fo".as_bytes().to_base64(Config {pad: false, ..STANDARD}), ~"Zm8");
314271
}
315272
316273
#[test]
@@ -344,7 +301,7 @@ mod test {
344301
#[test]
345302
fn test_from_base64_invalid_char() {
346303
assert!("Zm$=".from_base64().is_err())
347-
assert!("Zg==$".from_base64().is_err());
304+
assert!("Zg==$".from_base64().is_err());
348305
}
349306
350307
#[test]
@@ -368,20 +325,20 @@ mod test {
368325
}
369326
370327
#[bench]
371-
pub fn to_base64(bh: & mut BenchHarness) {
328+
pub fn bench_to_base64(bh: & mut BenchHarness) {
372329
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
373330
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
374331
do bh.iter {
375-
s.to_base64(STANDARD);
332+
s.as_bytes().to_base64(STANDARD);
376333
}
377334
bh.bytes = s.len() as u64;
378335
}
379336
380337
#[bench]
381-
pub fn from_base64(bh: & mut BenchHarness) {
338+
pub fn bench_from_base64(bh: & mut BenchHarness) {
382339
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
383340
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
384-
let b = s.to_base64(STANDARD);
341+
let b = s.as_bytes().to_base64(STANDARD);
385342
do bh.iter {
386343
b.from_base64();
387344
}

src/libextra/hex.rs

+22-70
Original file line numberDiff line numberDiff line change
@@ -50,53 +50,38 @@ impl<'self> ToHex for &'self [u8] {
5050
}
5151
}
5252

53-
impl<'self> ToHex for &'self str {
54-
/**
55-
* Convert any string (literal, `@`, `&`, or `~`) to hexadecimal encoding.
56-
*
57-
*
58-
* # Example
59-
*
60-
* ~~~ {.rust}
61-
* extern mod extra;
62-
* use extra::ToHex;
63-
*
64-
* fn main () {
65-
* let str = "Hello, World".to_hex();
66-
* printfln!("%s", str);
67-
* }
68-
* ~~~
69-
*
70-
*/
71-
fn to_hex(&self) -> ~str {
72-
self.as_bytes().to_hex()
73-
}
74-
}
75-
7653
/// A trait for converting hexadecimal encoded values
7754
pub trait FromHex {
7855
/// Converts the value of `self`, interpreted as hexadecimal encoded data,
7956
/// into an owned vector of bytes, returning the vector.
8057
fn from_hex(&self) -> Result<~[u8], ~str>;
8158
}
8259

83-
impl<'self> FromHex for &'self [u8] {
60+
impl<'self> FromHex for &'self str {
8461
/**
85-
* Convert hexadecimal `u8` vector into u8 byte values.
86-
* Every 2 encoded characters is converted into 1 octet.
87-
* Whitespace is ignored.
62+
* Convert any hexadecimal encoded string (literal, `@`, `&`, or `~`)
63+
* to the byte values it encodes.
64+
*
65+
* You can use the `from_bytes` function in `std::str`
66+
* to turn a `[u8]` into a string with characters corresponding to those
67+
* values.
8868
*
8969
* # Example
9070
*
71+
* This converts a string literal to hexadecimal and back.
72+
*
9173
* ~~~ {.rust}
9274
* extern mod extra;
93-
* use extra::hex::{ToHex, FromHex};
75+
* use extra::hex::{FromHex, ToHex};
76+
* use std::str;
9477
*
9578
* fn main () {
96-
* let str = [52,32].to_hex();
97-
* printfln!("%s", str);
98-
* let bytes = str.from_hex().get();
79+
* let hello_str = "Hello, World".to_hex();
80+
* printfln!("%s", hello_str);
81+
* let bytes = hello_str.from_hex().get();
9982
* printfln!("%?", bytes);
83+
* let result_str = str::from_bytes(bytes);
84+
* printfln!("%s", result_str);
10085
* }
10186
* ~~~
10287
*/
@@ -106,7 +91,7 @@ impl<'self> FromHex for &'self [u8] {
10691
let mut modulus = 0;
10792
let mut buf = 0u8;
10893

109-
for (idx, &byte) in self.iter().enumerate() {
94+
for (idx, byte) in self.byte_iter().enumerate() {
11095
buf <<= 4;
11196

11297
match byte as char {
@@ -117,8 +102,8 @@ impl<'self> FromHex for &'self [u8] {
117102
buf >>= 4;
118103
loop
119104
}
120-
_ => return Err(fmt!("Invalid byte '%c' found at position %u",
121-
byte as char, idx))
105+
_ => return Err(fmt!("Invalid character '%c' at position %u",
106+
self.char_at(idx), idx))
122107
}
123108

124109
modulus += 1;
@@ -135,47 +120,14 @@ impl<'self> FromHex for &'self [u8] {
135120
}
136121
}
137122

138-
impl<'self> FromHex for &'self str {
139-
/**
140-
* Convert any hexadecimal encoded string (literal, `@`, `&`, or `~`)
141-
* to the byte values it encodes.
142-
*
143-
* You can use the `from_bytes` function in `std::str`
144-
* to turn a `[u8]` into a string with characters corresponding to those
145-
* values.
146-
*
147-
* # Example
148-
*
149-
* This converts a string literal to hexadecimal and back.
150-
*
151-
* ~~~ {.rust}
152-
* extern mod extra;
153-
* use extra::hex::{FromHex, ToHex};
154-
* use std::str;
155-
*
156-
* fn main () {
157-
* let hello_str = "Hello, World".to_hex();
158-
* printfln!("%s", hello_str);
159-
* let bytes = hello_str.from_hex().get();
160-
* printfln!("%?", bytes);
161-
* let result_str = str::from_bytes(bytes);
162-
* printfln!("%s", result_str);
163-
* }
164-
* ~~~
165-
*/
166-
fn from_hex(&self) -> Result<~[u8], ~str> {
167-
self.as_bytes().from_hex()
168-
}
169-
}
170-
171123
#[cfg(test)]
172124
mod tests {
173125
use test::BenchHarness;
174126
use hex::*;
175127

176128
#[test]
177129
pub fn test_to_hex() {
178-
assert_eq!("foobar".to_hex(), ~"666f6f626172");
130+
assert_eq!("foobar".as_bytes().to_hex(), ~"666f6f626172");
179131
}
180132
181133
#[test]
@@ -223,7 +175,7 @@ mod tests {
223175
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
224176
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
225177
do bh.iter {
226-
s.to_hex();
178+
s.as_bytes().to_hex();
227179
}
228180
bh.bytes = s.len() as u64;
229181
}
@@ -232,7 +184,7 @@ mod tests {
232184
pub fn bench_from_hex(bh: & mut BenchHarness) {
233185
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
234186
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
235-
let b = s.to_hex();
187+
let b = s.as_bytes().to_hex();
236188
do bh.iter {
237189
b.from_hex();
238190
}

0 commit comments

Comments
 (0)