Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Show impl for Tree{Map,Set} and cleanup #14447

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/libcollections/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,20 @@ mod test_map {
assert_eq!(m1, m2);
}

#[test]
fn test_show() {
let mut map: HashMap<int, int> = HashMap::new();
let empty: HashMap<int, int> = HashMap::new();

map.insert(1, 2);
map.insert(3, 4);

let map_str = format!("{}", map);

assert!(map_str == "{1: 2, 3: 4}".to_owned() || map_str == "{3: 4, 1: 2}".to_owned());
assert_eq!(format!("{}", empty), "{}".to_owned());
}

#[test]
fn test_expand() {
let mut m = HashMap::new();
Expand Down
60 changes: 58 additions & 2 deletions src/libcollections/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
//! trees. The only requirement for the types is that the key implements
//! `TotalOrd`.

use std::iter;
use std::iter::{Peekable};
use std::cmp::Ordering;
use std::fmt::Show;
use std::fmt;
use std::iter::Peekable;
use std::iter;
use std::mem::{replace, swap};
use std::ptr;

Expand Down Expand Up @@ -67,6 +69,19 @@ impl<K: Ord + TotalOrd, V: Ord> Ord for TreeMap<K, V> {
fn lt(&self, other: &TreeMap<K, V>) -> bool { lt(self, other) }
}

impl<K: TotalOrd + Show, V: Show> Show for TreeMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, r"\{"));

for (i, (k, v)) in self.iter().enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}: {}", *k, *v));
}

write!(f, r"\}")
}
}

impl<K: TotalOrd, V> Container for TreeMap<K, V> {
fn len(&self) -> uint { self.length }
}
Expand Down Expand Up @@ -547,6 +562,19 @@ impl<T: Ord + TotalOrd> Ord for TreeSet<T> {
fn lt(&self, other: &TreeSet<T>) -> bool { self.map < other.map }
}

impl<T: TotalOrd + Show> Show for TreeSet<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, r"\{"));

for (i, x) in self.iter().enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}", *x));
}

write!(f, r"\}")
}
}

impl<T: TotalOrd> Container for TreeSet<T> {
#[inline]
fn len(&self) -> uint { self.map.len() }
Expand Down Expand Up @@ -1328,6 +1356,20 @@ mod test_treemap {
assert!(a < b && a <= b);
}

#[test]
fn test_show() {
let mut map: TreeMap<int, int> = TreeMap::new();
let empty: TreeMap<int, int> = TreeMap::new();

map.insert(1, 2);
map.insert(3, 4);

let map_str = format!("{}", map);

assert!(map_str == "{1: 2, 3: 4}".to_owned());
assert_eq!(format!("{}", empty), "{}".to_owned());
}

#[test]
fn test_lazy_iterator() {
let mut m = TreeMap::new();
Expand Down Expand Up @@ -1723,4 +1765,18 @@ mod test_set {
assert!(set.contains(x));
}
}

#[test]
fn test_show() {
let mut set: TreeSet<int> = TreeSet::new();
let empty: TreeSet<int> = TreeSet::new();

set.insert(1);
set.insert(2);

let set_str = format!("{}", set);

assert!(set_str == "{1, 2}".to_owned());
assert_eq!(format!("{}", empty), "{}".to_owned());
}
}
30 changes: 0 additions & 30 deletions src/libserialize/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1434,36 +1434,6 @@ impl<T: Iterator<char>> Parser<T> {
},
},
_ => return self.error(InvalidEscape),
/*=======
'u' => {
// Parse \u1234.
let mut i = 0u;
let mut n = 0u;
while i < 4u && !self.eof() {
self.bump();
n = match self.ch_or_null() {
c @ '0' .. '9' => n * 16u + (c as uint) - ('0' as uint),
'a' | 'A' => n * 16u + 10u,
'b' | 'B' => n * 16u + 11u,
'c' | 'C' => n * 16u + 12u,
'd' | 'D' => n * 16u + 13u,
'e' | 'E' => n * 16u + 14u,
'f' | 'F' => n * 16u + 15u,
_ => return self.error(UnrecognizedHex)
};

i += 1u;
}

// Error out if we didn't parse 4 digits.
if i != 4u {
return self.error(NotFourDigit);
}

res.push_char(char::from_u32(n as u32).unwrap());
}
_ => return self.error(InvalidEscape),
>>>>>>> Add a streaming parser to serialize::json.*/
}
escape = false;
} else if self.ch_is('\\') {
Expand Down