Merge pull request #13 from bitonic/fast-str-read

Read strings in one go, not byte-by-byte.
This commit is contained in:
Ty 2015-01-06 21:08:33 -08:00
commit ee09eb19da
2 changed files with 2 additions and 4 deletions

View File

@ -71,10 +71,7 @@ impl<'a, R: Reader+Buffer> Decoder for DecoderReader<'a, R> {
} }
fn read_str(&mut self) -> IoResult<String> { fn read_str(&mut self) -> IoResult<String> {
let len = try!(self.read_uint()); let len = try!(self.read_uint());
let mut vector = Vec::with_capacity(len as uint); let vector = try!(self.reader.read_exact(len));
for _ in range(0, len) {
vector.push(try!(self.reader.read_u8()));
}
Ok(String::from_utf8(vector).unwrap()) Ok(String::from_utf8(vector).unwrap())
} }
fn read_enum<T, F>(&mut self, _: &str, f: F) -> IoResult<T> where fn read_enum<T, F>(&mut self, _: &str, f: F) -> IoResult<T> where

View File

@ -49,6 +49,7 @@ fn test_numbers() {
fn test_string() { fn test_string() {
the_same("".to_string()); the_same("".to_string());
the_same("a".to_string()); the_same("a".to_string());
the_same("ƒoo".to_string());
} }
#[test] #[test]