about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/hash/sip.rs2
-rw-r--r--src/libstd/strbuf.rs21
2 files changed, 21 insertions, 2 deletions
diff --git a/src/libstd/hash/sip.rs b/src/libstd/hash/sip.rs
index 69b35df50e4..4a523e3d09e 100644
--- a/src/libstd/hash/sip.rs
+++ b/src/libstd/hash/sip.rs
@@ -291,7 +291,7 @@ mod tests {
     use iter::Iterator;
     use num::ToStrRadix;
     use option::{Some, None};
-    use str::{Str, OwnedStr};
+    use str::Str;
     use strbuf::StrBuf;
     use slice::{Vector, ImmutableVector, OwnedVector};
     use self::test::BenchHarness;
diff --git a/src/libstd/strbuf.rs b/src/libstd/strbuf.rs
index e9e50f0a07a..1fcc9c6465a 100644
--- a/src/libstd/strbuf.rs
+++ b/src/libstd/strbuf.rs
@@ -20,9 +20,11 @@ use iter::{Extendable, FromIterator, Iterator, range};
 use option::{None, Option, Some};
 use ptr::RawPtr;
 use slice::{OwnedVector, Vector};
+use str;
 use str::{OwnedStr, Str, StrSlice};
 use vec::Vec;
 
+/// A growable string stored as a UTF-8 encoded buffer.
 #[deriving(Clone, Eq, Ord, TotalEq, TotalOrd)]
 pub struct StrBuf {
     vec: Vec<u8>,
@@ -69,6 +71,23 @@ impl StrBuf {
         }
     }
 
+    /// Tries to create a new string buffer from the given byte
+    /// vector, validating that the vector is UTF-8 encoded.
+    #[inline]
+    pub fn from_utf8(vec: Vec<u8>) -> Option<StrBuf> {
+        if str::is_utf8(vec.as_slice()) {
+            Some(StrBuf { vec: vec })
+        } else {
+            None
+        }
+    }
+
+    /// Return the underlying byte buffer, encoded as UTF-8.
+    #[inline]
+    pub fn into_bytes(self) -> Vec<u8> {
+        self.vec
+    }
+
     /// Pushes the given string onto this buffer; then, returns `self` so that it can be used
     /// again.
     #[inline]
@@ -100,6 +119,7 @@ impl StrBuf {
         self.vec.push_all(string.as_bytes())
     }
 
+    /// Push `ch` onto the given string `count` times.
     #[inline]
     pub fn grow(&mut self, count: uint, ch: char) {
         for _ in range(0, count) {
@@ -352,4 +372,3 @@ mod tests {
         s.truncate(1);
     }
 }
-