1- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+ // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22// file at the top-level directory of this distribution and at
33// http://rust-lang.org/COPYRIGHT.
44//
@@ -27,6 +27,21 @@ static CHARS: [char, ..64] = [
2727] ;
2828
2929impl < ' 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+ */
3045 fn to_base64 ( & self ) -> ~str {
3146 let mut s = ~"";
3247 let len = self . len ( ) ;
@@ -74,6 +89,23 @@ impl<'self> ToBase64 for &'self [u8] {
7489}
7590
7691impl<'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+ */
77109 fn to_base64(&self) -> ~str {
78110 str::to_bytes(*self).to_base64()
79111 }
@@ -84,6 +116,25 @@ pub trait FromBase64 {
84116}
85117
86118impl 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+ */
87138 fn from_base64(&self) -> ~[u8] {
88139 if self.len() % 4u != 0u { fail!(~" invalid base64 length"); }
89140
@@ -144,6 +195,33 @@ impl FromBase64 for ~[u8] {
144195}
145196
146197impl 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+ */
147225 fn from_base64(&self) -> ~[u8] {
148226 str::to_bytes(*self).from_base64()
149227 }
0 commit comments