1
- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
@@ -27,6 +27,21 @@ static CHARS: [char, ..64] = [
27
27
] ;
28
28
29
29
impl < ' self > ToBase64 for & ' self [ u8 ] {
30
+ /**
31
+ * Turn a vector of `u8` bytes into a base64 string.
32
+ *
33
+ * *Example*:
34
+ *
35
+ * ~~~~
36
+ * extern mod std;
37
+ * use std::base64::ToBase64;
38
+ *
39
+ * fn main () {
40
+ * let str = [52,32].to_base64();
41
+ * println(fmt!("%s", str));
42
+ * }
43
+ * ~~~~
44
+ */
30
45
fn to_base64 ( & self ) -> ~str {
31
46
let mut s = ~"";
32
47
let len = self . len ( ) ;
@@ -74,6 +89,23 @@ impl<'self> ToBase64 for &'self [u8] {
74
89
}
75
90
76
91
impl<'self> ToBase64 for &'self str {
92
+ /**
93
+ * Convert any string (literal, `@`, `&`, or `~`) to base64 encoding.
94
+ *
95
+ *
96
+ * *Example*:
97
+ *
98
+ * ~~~~
99
+ * extern mod std;
100
+ * use std::base64::ToBase64;
101
+ *
102
+ * fn main () {
103
+ * let str = " Hello , World ".to_base64();
104
+ * println(fmt!(" %s",str));
105
+ * }
106
+ * ~~~~
107
+ *
108
+ */
77
109
fn to_base64(&self) -> ~str {
78
110
str::to_bytes(*self).to_base64()
79
111
}
@@ -84,6 +116,25 @@ pub trait FromBase64 {
84
116
}
85
117
86
118
impl FromBase64 for ~[u8] {
119
+ /**
120
+ * Convert base64 `u8` vector into u8 byte values.
121
+ * Every 4 encoded characters is converted into 3 octets, modulo padding.
122
+ *
123
+ * *Example*:
124
+ *
125
+ * ~~~~
126
+ * extern mod std;
127
+ * use std::base64::ToBase64;
128
+ * use std::base64::FromBase64;
129
+ *
130
+ * fn main () {
131
+ * let str = [52,32].to_base64();
132
+ * println(fmt!(" %s", str));
133
+ * let bytes = str.from_base64();
134
+ * println(fmt!(" %?",bytes));
135
+ * }
136
+ * ~~~~
137
+ */
87
138
fn from_base64(&self) -> ~[u8] {
88
139
if self.len() % 4u != 0u { fail!(~" invalid base64 length"); }
89
140
@@ -144,6 +195,33 @@ impl FromBase64 for ~[u8] {
144
195
}
145
196
146
197
impl FromBase64 for ~str {
198
+ /**
199
+ * Convert any base64 encoded string (literal, `@`, `&`, or `~`)
200
+ * to the byte values it encodes.
201
+ *
202
+ * You can use the `from_bytes` function in `core::str`
203
+ * to turn a `[u8]` into a string with characters corresponding to those values.
204
+ *
205
+ * *Example*:
206
+ *
207
+ * This converts a string literal to base64 and back.
208
+ *
209
+ * ~~~~
210
+ * extern mod std;
211
+ * use std::base64::ToBase64;
212
+ * use std::base64::FromBase64;
213
+ * use core::str;
214
+ *
215
+ * fn main () {
216
+ * let hello_str = " Hello , World ".to_base64();
217
+ * println(fmt!(" %s",hello_str));
218
+ * let bytes = hello_str.from_base64();
219
+ * println(fmt!(" %?",bytes));
220
+ * let result_str = str::from_bytes(bytes);
221
+ * println(fmt!(" %s",result_str));
222
+ * }
223
+ * ~~~~
224
+ */
147
225
fn from_base64(&self) -> ~[u8] {
148
226
str::to_bytes(*self).from_base64()
149
227
}
0 commit comments