From d8e45ea7c054b4ad6fb82ec3a9fcf1736b4d7260 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Wed, 2 Apr 2014 16:54:22 -0700 Subject: libstd: Implement `StrBuf`, a new string buffer type like `Vec`, and port all code over to use it. --- src/libserialize/json.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'src/libserialize') 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 = Result; 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> Parser { 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> Parser { 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!() } -- cgit 1.4.1-3-g733a5