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
This commit is contained in:
bors 2014-04-10 21:01:41 -07:00
commit cea8def620
66 changed files with 1070 additions and 983 deletions

View file

@ -12,6 +12,7 @@
use std::option;
use std::os;
use std::strbuf::StrBuf;
use std::task;
fn print_complements() {
@ -31,53 +32,62 @@ struct CreatureInfo {
color: color
}
fn show_color(cc: color) -> ~str {
fn show_color(cc: color) -> &'static str {
match cc {
Red => {~"red"}
Yellow => {~"yellow"}
Blue => {~"blue"}
Red => "red",
Yellow => "yellow",
Blue => "blue"
}
}
fn show_color_list(set: Vec<color>) -> ~str {
let mut out = ~"";
fn show_color_list(set: Vec<color>) -> StrBuf {
let mut out = StrBuf::new();
for col in set.iter() {
out.push_char(' ');
out.push_str(show_color(*col));
}
return out;
out
}
fn show_digit(nn: uint) -> ~str {
fn show_digit(nn: uint) -> &'static str {
match nn {
0 => {~"zero"}
1 => {~"one"}
2 => {~"two"}
3 => {~"three"}
4 => {~"four"}
5 => {~"five"}
6 => {~"six"}
7 => {~"seven"}
8 => {~"eight"}
9 => {~"nine"}
0 => {"zero"}
1 => {"one"}
2 => {"two"}
3 => {"three"}
4 => {"four"}
5 => {"five"}
6 => {"six"}
7 => {"seven"}
8 => {"eight"}
9 => {"nine"}
_ => {fail!("expected digits from 0 to 9...")}
}
}
fn show_number(nn: uint) -> ~str {
let mut out = ~"";
fn show_number(nn: uint) -> StrBuf {
let mut out = vec![];
let mut num = nn;
let mut dig;
if num == 0 { out = show_digit(0) };
let mut len = 0;
if num == 0 { out.push(show_digit(0)) };
while num != 0 {
dig = num % 10;
num = num / 10;
out = show_digit(dig) + " " + out;
out.push(" ");
let s = show_digit(dig);
out.push(s);
len += 1 + s.len();
}
len += 1;
out.push(" ");
return ~" " + out;
let mut ret = StrBuf::with_capacity(len);
for s in out.iter().rev() {
ret.push_str(*s);
}
ret
}
fn transform(aa: color, bb: color) -> color {
@ -124,7 +134,7 @@ fn creature(
option::None => {
// log creatures met and evil clones of self
let report = format!("{} {}",
creatures_met, show_number(evil_clones_met));
creatures_met, show_number(evil_clones_met).as_slice());
to_rendezvous_log.send(report);
break;
}

View file

@ -15,16 +15,11 @@
extern crate collections;
use std::cmp::Ord;
use std::comm;
use collections::HashMap;
use std::mem::replace;
use std::option;
use std::os;
use std::io;
use std::str;
use std::task;
use std::vec;
use std::strbuf::StrBuf;
fn f64_cmp(x: f64, y: f64) -> Ordering {
// arbitrarily decide that NaNs are larger than everything.
@ -63,19 +58,16 @@ fn sort_and_fmt(mm: &HashMap<Vec<u8> , uint>, total: uint) -> ~str {
let pairs_sorted = sortKV(pairs);
let mut buffer = ~"";
let mut buffer = StrBuf::new();
for &(ref k, v) in pairs_sorted.iter() {
unsafe {
buffer.push_str(format!("{} {:0.3f}\n",
k.as_slice()
.to_ascii()
.to_upper()
.into_str(), v));
}
buffer.push_str(format!("{} {:0.3f}\n",
k.as_slice()
.to_ascii()
.to_upper()
.into_str(), v));
}
return buffer;
return buffer.into_owned();
}
// given a map, search for the frequency of a pattern

View file

@ -11,8 +11,7 @@
// ignore-android see #10393 #13206
// ignore-pretty
use std::ascii::OwnedStrAsciiExt;
use std::str;
use std::strbuf::StrBuf;
use std::slice;
static TABLE: [u8, ..4] = [ 'A' as u8, 'C' as u8, 'G' as u8, 'T' as u8 ];
@ -49,8 +48,7 @@ impl Code {
string.bytes().fold(Code(0u64), |a, b| a.push_char(b))
}
// FIXME: Inefficient.
fn unpack(&self, frame: uint) -> ~str {
fn unpack(&self, frame: uint) -> StrBuf {
let mut key = self.hash();
let mut result = Vec::new();
for _ in range(0, frame) {
@ -59,7 +57,7 @@ impl Code {
}
result.reverse();
str::from_utf8_owned(result.move_iter().collect()).unwrap()
StrBuf::from_utf8(result).unwrap()
}
}
@ -238,7 +236,7 @@ fn print_frequencies(frequencies: &Table, frame: uint) {
for &(count, key) in vector.iter().rev() {
println!("{} {:.3f}",
key.unpack(frame),
key.unpack(frame).as_slice(),
(count as f32 * 100.0) / (total_count as f32));
}
println!("");
@ -248,14 +246,17 @@ fn print_occurrences(frequencies: &mut Table, occurrence: &'static str) {
frequencies.lookup(Code::pack(occurrence), PrintCallback(occurrence))
}
fn get_sequence<R: Buffer>(r: &mut R, key: &str) -> ~[u8] {
let mut res = ~"";
fn get_sequence<R: Buffer>(r: &mut R, key: &str) -> Vec<u8> {
let mut res = Vec::new();
for l in r.lines().map(|l| l.ok().unwrap())
.skip_while(|l| key != l.slice_to(key.len())).skip(1)
{
res.push_str(l.trim());
res.push_all(l.trim().as_bytes());
}
res.into_ascii_upper().into_bytes()
for b in res.mut_iter() {
*b = b.to_ascii().to_upper().to_byte();
}
res
}
fn main() {
@ -267,17 +268,17 @@ fn main() {
};
let mut frequencies = Table::new();
generate_frequencies(&mut frequencies, input, 1);
generate_frequencies(&mut frequencies, input.as_slice(), 1);
print_frequencies(&frequencies, 1);
frequencies = Table::new();
generate_frequencies(&mut frequencies, input, 2);
generate_frequencies(&mut frequencies, input.as_slice(), 2);
print_frequencies(&frequencies, 2);
for occurrence in OCCURRENCES.iter() {
frequencies = Table::new();
generate_frequencies(&mut frequencies,
input,
input.as_slice(),
occurrence.len());
print_occurrences(&mut frequencies, *occurrence);
}

View file

@ -16,7 +16,7 @@
use std::*;
fn main() {
str::with_capacity(10); // avoid an unused import message
str::from_byte('a' as u8); // avoid an unused import message
fail!("fail works")
}

View file

@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::strbuf::StrBuf;
fn test_stack_assign() {
let s: ~str = ~"a";
println!("{}", s.clone());
@ -43,21 +45,21 @@ fn test_heap_add() {
}
fn test_append() {
let mut s = ~"";
let mut s = StrBuf::new();
s.push_str("a");
assert_eq!(s, ~"a");
assert_eq!(s.as_slice(), "a");
let mut s = ~"a";
let mut s = StrBuf::from_str("a");
s.push_str("b");
println!("{}", s.clone());
assert_eq!(s, ~"ab");
assert_eq!(s.as_slice(), "ab");
let mut s = ~"c";
let mut s = StrBuf::from_str("c");
s.push_str("offee");
assert!(s == ~"coffee");
assert!(s.as_slice() == "coffee");
s.push_str("&tea");
assert!(s == ~"coffee&tea");
assert!(s.as_slice() == "coffee&tea");
}
pub fn main() {

View file

@ -8,8 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::strbuf::StrBuf;
struct StringBuffer {
s: ~str
s: StrBuf,
}
impl StringBuffer {
@ -18,14 +20,16 @@ impl StringBuffer {
}
}
fn to_str(sb: StringBuffer) -> ~str {
fn to_str(sb: StringBuffer) -> StrBuf {
sb.s
}
pub fn main() {
let mut sb = StringBuffer {s: ~""};
let mut sb = StringBuffer {
s: StrBuf::new(),
};
sb.append("Hello, ");
sb.append("World!");
let str = to_str(sb);
assert_eq!(str, ~"Hello, World!");
assert_eq!(str.as_slice(), "Hello, World!");
}

View file

@ -10,6 +10,7 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::strbuf::StrBuf;
#[deriving(Eq, Show)]
struct Point {
@ -33,7 +34,7 @@ pub fn main() {
assert!(s.equiv(&("foo")));
assert_eq!(s.as_slice(), "foo");
let mut_s = Rc::new(RefCell::new(~"foo"));
let mut_s = Rc::new(RefCell::new(StrBuf::from_str("foo")));
mut_s.borrow_mut().push_str("bar");
// HACK assert_eq! would fail here because it stores the LHS and RHS in two locals.
assert!(mut_s.borrow().as_slice() == "foobar");

View file

@ -10,6 +10,7 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::strbuf::StrBuf;
#[deriving(Eq, Show)]
struct Point {
@ -31,7 +32,7 @@ pub fn main() {
assert_eq!(*s, ~"foo");
assert_eq!((*s).as_slice(), "foo");
let mut_s = Rc::new(RefCell::new(~"foo"));
let mut_s = Rc::new(RefCell::new(StrBuf::from_str("foo")));
(*(*mut_s).borrow_mut()).push_str("bar");
// assert_eq! would fail here because it stores the LHS and RHS in two locals.
assert!((*(*mut_s).borrow()).as_slice() == "foobar");

View file

@ -1,30 +0,0 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn test1() {
let mut s: ~str = ~"hello";
s.push_str("world");
println!("{}", s.clone());
assert_eq!(s[9], 'd' as u8);
}
fn test2() {
// This tests for issue #163
let ff: ~str = ~"abc";
let a: ~str = ff + "ABC" + ff;
let b: ~str = ~"ABC" + ff + "ABC";
println!("{}", a.clone());
println!("{}", b.clone());
assert_eq!(a, ~"abcABCabc");
assert_eq!(b, ~"ABCabcABC");
}
pub fn main() { test1(); test2(); }

View file

@ -1,24 +0,0 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
pub fn main() {
let mut s = ~"a";
s.push_char('b');
assert_eq!(s[0], 'a' as u8);
assert_eq!(s[1], 'b' as u8);
s.push_char('c');
s.push_char('d');
assert_eq!(s[0], 'a' as u8);
assert_eq!(s[1], 'b' as u8);
assert_eq!(s[2], 'c' as u8);
assert_eq!(s[3], 'd' as u8);
}

View file

@ -38,14 +38,4 @@ pub fn main() {
assert!((!str::is_utf8([0xf0_u8, 0x10_u8])));
assert!((!str::is_utf8([0xf0_u8, 0xff_u8, 0x10_u8])));
assert!((!str::is_utf8([0xf0_u8, 0xff_u8, 0xff_u8, 0x10_u8])));
let mut stack = ~"a×c€";
assert_eq!(stack.pop_char(), Some('€'));
assert_eq!(stack.pop_char(), Some('c'));
stack.push_char('u');
assert!(stack == ~"a×u");
assert_eq!(stack.shift_char(), Some('a'));
assert_eq!(stack.shift_char(), Some('×'));
stack.unshift_char('ß');
assert!(stack == ~"ßu");
}

View file

@ -8,13 +8,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::strbuf::StrBuf;
#[deriving(Eq)]
enum t { a, b(~str), }
enum t { a, b(StrBuf), }
fn make(i: int) -> t {
if i > 10 { return a; }
let mut s = ~"hello";
let mut s = StrBuf::from_str("hello");
// Ensure s is non-const.
s.push_str("there");