diff options
| author | bors <bors@rust-lang.org> | 2014-04-10 21:01:41 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-04-10 21:01:41 -0700 |
| commit | cea8def62068b405495ecd1810124ebc88b4f90b (patch) | |
| tree | 65d5755bd532a9213799c52572db6d6683d5e942 /src/libserialize | |
| parent | 0156af156d70efd5a3c96d0c5b8fc9bec39a7ae5 (diff) | |
| parent | def90f43e2df9968cda730a2a30cb7ccb9513002 (diff) | |
| download | rust-cea8def62068b405495ecd1810124ebc88b4f90b.tar.gz rust-cea8def62068b405495ecd1810124ebc88b4f90b.zip | |
auto merge of #13440 : huonw/rust/strbuf, r=alexcrichton
libstd: Implement `StrBuf`, a new string buffer type like `Vec`, and port all code over to use it. Rebased & tests-fixed version of https://github.com/mozilla/rust/pull/13269
Diffstat (limited to 'src/libserialize')
| -rw-r--r-- | src/libserialize/json.rs | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index aff4b07f755..6c980f2f834 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -231,14 +231,15 @@ fn main() { */ +use collections::HashMap; use std::char; use std::f64; -use collections::HashMap; -use std::io; +use std::fmt; use std::io::MemWriter; +use std::io; use std::num; use std::str; -use std::fmt; +use std::strbuf::StrBuf; use Encodable; use collections::TreeMap; @@ -271,7 +272,7 @@ pub type EncodeResult = io::IoResult<()>; pub type DecodeResult<T> = Result<T, Error>; fn escape_str(s: &str) -> ~str { - let mut escaped = ~"\""; + let mut escaped = StrBuf::from_str("\""); for c in s.chars() { match c { '"' => escaped.push_str("\\\""), @@ -284,16 +285,16 @@ fn escape_str(s: &str) -> ~str { _ => escaped.push_char(c), } }; - escaped.push_char('"'); - - escaped + escaped.into_owned() } fn spaces(n: uint) -> ~str { - let mut ss = ~""; - for _ in range(0, n) { ss.push_str(" "); } - return ss; + let mut ss = StrBuf::new(); + for _ in range(0, n) { + ss.push_str(" "); + } + return ss.into_owned(); } /// A structure for implementing serialization to JSON. @@ -1130,7 +1131,7 @@ impl<T : Iterator<char>> Parser<T> { fn parse_str(&mut self) -> DecodeResult<~str> { let mut escape = false; - let mut res = ~""; + let mut res = StrBuf::new(); loop { self.bump(); @@ -1184,7 +1185,10 @@ impl<T : Iterator<char>> Parser<T> { escape = true; } else { match self.ch { - Some('"') => { self.bump(); return Ok(res); }, + Some('"') => { + self.bump(); + return Ok(res.into_owned()); + }, Some(c) => res.push_char(c), None => unreachable!() } |
