Skip to content

Commit 36c2c56

Browse files
committed
auto merge of #14535 : sfackler/rust/bitv-show, r=alexcrichton
Closes #14531
2 parents 874b56d + 8e8f6a0 commit 36c2c56

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

src/libcollections/bitv.rs

+35-20
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313

1414
use std::cmp;
15+
use std::fmt;
1516
use std::iter::RandomAccessIterator;
1617
use std::iter::{Enumerate, Repeat, Map, Zip};
1718
use std::ops;
1819
use std::slice;
19-
use std::string::String;
2020
use std::uint;
2121

2222
#[deriving(Clone)]
@@ -526,25 +526,6 @@ impl Bitv {
526526
Vec::from_fn(self.nbits, |i| self[i])
527527
}
528528

529-
/**
530-
* Converts `self` to a string.
531-
*
532-
* The resulting string has the same length as `self`, and each
533-
* character is either '0' or '1'.
534-
*/
535-
pub fn to_str(&self) -> String {
536-
let mut rs = String::new();
537-
for i in self.iter() {
538-
if i {
539-
rs.push_char('1');
540-
} else {
541-
rs.push_char('0');
542-
}
543-
};
544-
rs
545-
}
546-
547-
548529
/**
549530
* Compare a bitvector to a vector of `bool`.
550531
*
@@ -604,6 +585,15 @@ impl ops::Index<uint,bool> for Bitv {
604585
}
605586
}
606587

588+
impl fmt::Show for Bitv {
589+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
590+
for bit in self.iter() {
591+
try!(write!(fmt, "{}", if bit { 1 } else { 0 }));
592+
}
593+
Ok(())
594+
}
595+
}
596+
607597
#[inline]
608598
fn iterate_bits(base: uint, bits: uint, f: |uint| -> bool) -> bool {
609599
if bits == 0 {
@@ -827,6 +817,21 @@ impl cmp::Eq for BitvSet {
827817
fn ne(&self, other: &BitvSet) -> bool { !self.eq(other) }
828818
}
829819

820+
impl fmt::Show for BitvSet {
821+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
822+
try!(write!(fmt, r"\{"));
823+
let mut first = true;
824+
for n in self.iter() {
825+
if !first {
826+
try!(write!(fmt, ", "));
827+
}
828+
try!(write!(fmt, "{}", n));
829+
first = false;
830+
}
831+
write!(fmt, r"\}")
832+
}
833+
}
834+
830835
impl Container for BitvSet {
831836
#[inline]
832837
fn len(&self) -> uint { self.size }
@@ -1629,6 +1634,16 @@ mod tests {
16291634
assert!(!v.none());
16301635
}
16311636

1637+
#[test]
1638+
fn test_bitv_set_show() {
1639+
let mut s = BitvSet::new();
1640+
s.insert(1);
1641+
s.insert(10);
1642+
s.insert(50);
1643+
s.insert(2);
1644+
assert_eq!("{1, 2, 10, 50}".to_string(), s.to_str());
1645+
}
1646+
16321647
fn rng() -> rand::IsaacRng {
16331648
let seed = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
16341649
rand::SeedableRng::from_seed(seed)

0 commit comments

Comments
 (0)