about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-04-15 16:18:52 -0700
committerbors <bors@rust-lang.org>2013-04-15 16:18:52 -0700
commitdd2c7f61a9baa9551f6af4e4a76019c0c0198889 (patch)
tree3edbfa147910323a939d21b2465e5f96d9cae1ae /src/libstd
parentf10cf26e25c75e148d86dd151a210d9f4a7ece2f (diff)
parentb5c9990c387fff415a7de54f5870e5af92169a5d (diff)
downloadrust-dd2c7f61a9baa9551f6af4e4a76019c0c0198889.tar.gz
rust-dd2c7f61a9baa9551f6af4e4a76019c0c0198889.zip
auto merge of #5879 : astrieanna/rust/document_std_base64, r=catamorphism
This adds examples for the methods in std::base64.

Each example is complete in the sense that you can copy-paste it into a file and compile it successfully without adding anything (imports, etc). The hardest part of figuring out how to use this was figuring out the right import statements to put at the top.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/base64.rs80
1 files changed, 79 insertions, 1 deletions
diff --git a/src/libstd/base64.rs b/src/libstd/base64.rs
index 781a720b1a4..cd468411b5a 100644
--- a/src/libstd/base64.rs
+++ b/src/libstd/base64.rs
@@ -1,4 +1,4 @@
-// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -27,6 +27,21 @@ static CHARS: [char, ..64] = [
 ];
 
 impl<'self> ToBase64 for &'self [u8] {
+    /**
+     * Turn a vector of `u8` bytes into a base64 string.
+     *
+     * *Example*:
+     *
+     * ~~~~
+     * extern mod std;
+     * use std::base64::ToBase64;
+     *
+     * fn main () {
+     *     let str = [52,32].to_base64();
+     *     println(fmt!("%s", str));
+     * }
+     * ~~~~
+     */
     fn to_base64(&self) -> ~str {
         let mut s = ~"";
         let len = self.len();
@@ -74,6 +89,23 @@ impl<'self> ToBase64 for &'self [u8] {
 }
 
 impl<'self> ToBase64 for &'self str {
+    /**
+     * Convert any string (literal, `@`, `&`, or `~`) to base64 encoding.
+     *
+     *
+     * *Example*:
+     *
+     * ~~~~
+     * extern mod std;
+     * use std::base64::ToBase64;
+     *
+     * fn main () {
+     *     let str = "Hello, World".to_base64();
+     *     println(fmt!("%s",str));
+     * }
+     * ~~~~
+     *
+     */
     fn to_base64(&self) -> ~str {
         str::to_bytes(*self).to_base64()
     }
@@ -84,6 +116,25 @@ pub trait FromBase64 {
 }
 
 impl FromBase64 for ~[u8] {
+    /**
+     * Convert base64 `u8` vector into u8 byte values.
+     * Every 4 encoded characters is converted into 3 octets, modulo padding.
+     *
+     * *Example*:
+     *
+     * ~~~~
+     * extern mod std;
+     * use std::base64::ToBase64;
+     * use std::base64::FromBase64;
+     *
+     * fn main () {
+     *     let str = [52,32].to_base64();
+     *     println(fmt!("%s", str));
+     *     let bytes = str.from_base64();
+     *     println(fmt!("%?",bytes));
+     * }
+     * ~~~~
+     */
     fn from_base64(&self) -> ~[u8] {
         if self.len() % 4u != 0u { fail!(~"invalid base64 length"); }
 
@@ -144,6 +195,33 @@ impl FromBase64 for ~[u8] {
 }
 
 impl FromBase64 for ~str {
+    /**
+     * Convert any base64 encoded string (literal, `@`, `&`, or `~`)
+     * to the byte values it encodes.
+     *
+     * You can use the `from_bytes` function in `core::str`
+     * to turn a `[u8]` into a string with characters corresponding to those values.
+     *
+     * *Example*:
+     *
+     * This converts a string literal to base64 and back.
+     *
+     * ~~~~
+     * extern mod std;
+     * use std::base64::ToBase64;
+     * use std::base64::FromBase64;
+     * use core::str;
+     *
+     * fn main () {
+     *     let hello_str = "Hello, World".to_base64();
+     *     println(fmt!("%s",hello_str));
+     *     let bytes = hello_str.from_base64();
+     *     println(fmt!("%?",bytes));
+     *     let result_str = str::from_bytes(bytes);
+     *     println(fmt!("%s",result_str));
+     * }
+     * ~~~~
+     */
     fn from_base64(&self) -> ~[u8] {
         str::to_bytes(*self).from_base64()
     }