diff options
| author | Patrick Walton <pcwalton@mimiga.net> | 2014-04-02 16:54:22 -0700 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2014-04-10 22:10:10 +1000 |
| commit | d8e45ea7c054b4ad6fb82ec3a9fcf1736b4d7260 (patch) | |
| tree | 3ff220512aeae37710c8b1c783e1229e685bfce3 /src/libserialize | |
| parent | 7fbcb400f0697621ece9f9773b0f0bf1ec73e9c1 (diff) | |
| download | rust-d8e45ea7c054b4ad6fb82ec3a9fcf1736b4d7260.tar.gz rust-d8e45ea7c054b4ad6fb82ec3a9fcf1736b4d7260.zip | |
libstd: Implement `StrBuf`, a new string buffer type like `Vec`, and
port all code over to use it.
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!() } |
