提交 f74e1935 编写于 作者: B bors

auto merge of #7123 : huonw/rust/more-str, r=thestinger

Moves all the remaining functions that could reasonably be methods to be methods, except for some FFI ones (which I believe @erickt is working on, possibly) and `each_split_within`, since I'm not really sure the details of it (I believe @kimundi wrote the current implementation, so maybe he could convert it to an external iterator method on `StrSlice`, e.g. `word_wrap_iter(&self) -> WordWrapIterator<'self>`, where `WordWrapIterator` impls `Iterator<&'self str>`. It probably won't be too hard, since it's already a state machine.)

This also cleans up the comparison impls for the string types, except I'm not sure how the lang items `eq_str` and `eq_str_uniq` need to be handled, so they (`eq_slice` and `eq`) remain stand-alone functions.
......@@ -24,7 +24,6 @@
use core::io;
use core::os;
use core::str;
use core::uint;
use core::vec;
......
......@@ -229,8 +229,6 @@ fn from_base64(&self) -> ~[u8] {
#[cfg(test)]
mod tests {
use core::str;
#[test]
fn test_to_base64() {
assert_eq!("".to_base64(), ~"");
......
......@@ -1271,7 +1271,6 @@ mod tests {
use rope::*;
use core::str;
use core::uint;
use core::vec;
......
......@@ -24,7 +24,6 @@
use core::prelude::*;
use core::str;
use core::uint;
use core::vec;
......@@ -279,8 +278,6 @@ fn result_str(&mut self) -> ~str {
mod tests {
use sha1;
use core::vec;
#[test]
fn test() {
struct Test {
......
......@@ -294,11 +294,6 @@ mod tests {
use super::SmallIntMap;
use core::local_data;
use core::rand;
use core::uint;
use core::vec;
#[test]
fn test_find_mut() {
let mut m = SmallIntMap::new();
......
......@@ -751,7 +751,6 @@ fn shift_vec<T:Copy>(dest: &mut [T],
mod test_qsort3 {
use sort::*;
use core::vec;
fn check_sort(v1: &mut [int], v2: &mut [int]) {
let len = v1.len();
......@@ -861,8 +860,6 @@ mod tests {
use sort::*;
use core::vec;
fn check_sort(v1: &[int], v2: &[int]) {
let len = v1.len();
pub fn le(a: &int, b: &int) -> bool { *a <= *b }
......
......@@ -12,7 +12,6 @@
use core::prelude::*;
use core::vec;
use core::f64;
use core::cmp;
use core::num;
......
......@@ -37,6 +37,7 @@ not required in or otherwise suitable for the core library.
extern mod core(name = "std", vers = "0.7-pre");
#[cfg(stage0)]
use core::{str, unstable};
use core::str::{StrSlice, OwnedStr};
......
......@@ -36,7 +36,6 @@ mod tests {
use tempfile::mkdtemp;
use core::os;
use core::str;
#[test]
fn test_mkdtemp() {
......
......@@ -313,7 +313,6 @@ pub fn parse(file: @Reader, longnames: bool) -> Result<~TermInfo, ~str> {
#[cfg(test)]
mod test {
use super::*;
use p = core::path::Path;
#[test]
fn test_veclens() {
......
......@@ -26,7 +26,6 @@
use core::io;
use core::option;
use core::result;
use core::str;
use core::task;
use core::to_str::ToStr;
use core::uint;
......@@ -542,7 +541,7 @@ fn filter(test: TestDescAndFn) -> Option<TestDescAndFn> {
// Sort the tests alphabetically
fn lteq(t1: &TestDescAndFn, t2: &TestDescAndFn) -> bool {
str::le(t1.desc.name.to_str(), t2.desc.name.to_str())
t1.desc.name.to_str() < t2.desc.name.to_str()
}
sort::quick_sort(filtered, lteq);
......
......@@ -710,7 +710,6 @@ mod test_treemap {
use core::rand::RngUtil;
use core::rand;
use core::str;
use core::vec;
#[test]
......
......@@ -199,9 +199,6 @@ pub fn minimize_rpaths(rpaths: &[Path]) -> ~[Path] {
mod test {
use core::prelude::*;
use core::os;
use core::str;
// FIXME(#2119): the outer attribute should be #[cfg(unix, test)], then
// these redundant #[cfg(test)] blocks can be removed
#[cfg(test)]
......
......@@ -15,7 +15,6 @@
use driver::session::Session;
use e = metadata::encoder;
use metadata::decoder;
use metadata::encoder;
use metadata::tydecode;
use metadata::tydecode::{DefIdSource, NominalType, TypeWithId, TypeParameter};
use metadata::tyencode;
......
......@@ -4845,7 +4845,7 @@ pub fn find_best_match_for_name(@mut self,
let mut smallest = 0;
for maybes.eachi |i, &other| {
values[i] = str::levdistance(name, other);
values[i] = name.lev_distance(other);
if values[i] <= values[smallest] {
smallest = i;
......
......@@ -47,7 +47,6 @@
use core::container::Map;
use core::libc::c_ulonglong;
use core::option::{Option, Some, None};
use core::str;
use core::vec;
use lib::llvm::{ValueRef, TypeRef, True, IntEQ, IntNE};
......
......@@ -71,7 +71,6 @@
use core::str;
use core::uint;
use core::vec;
use core::local_data;
use extra::time;
use syntax::ast::ident;
use syntax::ast_map::{path, path_elt_to_str, path_name};
......@@ -3120,4 +3119,3 @@ pub fn trans_crate(sess: session::Session,
return (llcx, llmod, link_meta);
}
......@@ -32,8 +32,6 @@
use syntax::ast_map;
use syntax;
#[cfg(test)] use core::vec;
pub struct Ctxt {
ast: @ast::crate,
ast_map: ast_map::map
......
......@@ -24,7 +24,6 @@
use fold;
use pass::Pass;
use core::str;
use core::util;
pub fn mk_pass() -> Pass {
......@@ -129,25 +128,21 @@ fn first_sentence_(s: &str) -> ~str {
}
});
match idx {
Some(idx) if idx > 2u => {
str::to_owned(s.slice(0, idx - 1))
}
Some(idx) if idx > 2u => s.slice(0, idx - 1).to_owned(),
_ => {
if s.ends_with(".") {
str::to_owned(s)
s.to_owned()
} else {
str::to_owned(s)
s.to_owned()
}
}
}
}
pub fn paragraphs(s: &str) -> ~[~str] {
let mut lines = ~[];
for str::each_line_any(s) |line| { lines.push(line.to_owned()); }
let mut whitespace_lines = 0;
let mut accum = ~"";
let paras = do lines.iter().fold(~[]) |paras, line| {
let paras = do s.any_line_iter().fold(~[]) |paras, line| {
let mut res = paras;
if line.is_whitespace() {
......@@ -163,9 +158,9 @@ pub fn paragraphs(s: &str) -> ~[~str] {
whitespace_lines = 0;
accum = if accum.is_empty() {
copy *line
line.to_owned()
} else {
accum + "\n" + *line
fmt!("%s\n%s", accum, line)
}
}
......
......@@ -285,8 +285,6 @@ mod test {
use extract::{extract, from_srv};
use parse;
use core::vec;
fn mk_doc(source: @str) -> doc::Doc {
let ast = parse::from_str(source);
extract(ast, ~"")
......
......@@ -466,10 +466,7 @@ fn write_variant(ctxt: &Ctxt, doc: doc::VariantDoc) {
}
fn list_item_indent(item: &str) -> ~str {
let mut indented = ~[];
for str::each_line_any(item) |line| {
indented.push(line);
}
let indented = item.any_line_iter().collect::<~[&str]>();
// separate markdown elements within `*` lists must be indented by four
// spaces, or they will escape the list context. indenting everything
......@@ -539,8 +536,6 @@ mod test {
use tystr_pass;
use unindent_pass;
use core::str;
fn render(source: ~str) -> ~str {
let (srv, doc) = create_doc_srv(source);
let markdown = write_markdown_str_srv(srv, doc);
......
......@@ -157,7 +157,6 @@ mod test {
use doc;
use extract;
use page_pass::run;
use core::vec;
fn mk_doc_(
output_style: config::OutputStyle,
......
......@@ -77,8 +77,6 @@ fn mk_doc(source: ~str) -> doc::Doc {
#[test]
fn should_prune_hidden_items() {
use core::vec;
let doc = mk_doc(~"#[doc(hidden)] mod a { }");
assert!(doc.cratemod().mods().is_empty())
}
......
......@@ -162,7 +162,6 @@ mod test {
use extract;
use tystr_pass;
use prune_private_pass::run;
use core::vec;
fn mk_doc(source: ~str) -> doc::Doc {
do astsrv::from_str(copy source) |srv| {
......
......@@ -19,7 +19,7 @@
use fold;
use pass::Pass;
use core::str;
use core::iterator::IteratorUtil;
pub fn mk_pass() -> Pass {
Pass {
......@@ -104,21 +104,19 @@ fn sectionalize(desc: Option<~str>) -> (Option<~str>, ~[doc::Section]) {
if desc.is_none() {
return (None, ~[]);
}
let mut lines = ~[];
for str::each_line_any(*desc.get_ref()) |line| { lines.push(line.to_owned()); }
let mut new_desc = None::<~str>;
let mut current_section = None;
let mut sections = ~[];
for lines.each |line| {
match parse_header(copy *line) {
for desc.get_ref().any_line_iter().advance |line| {
match parse_header(line) {
Some(header) => {
if current_section.is_some() {
sections += [copy *current_section.get_ref()];
sections.push(copy *current_section.get_ref());
}
current_section = Some(doc::Section {
header: header,
header: header.to_owned(),
body: ~""
});
}
......@@ -126,17 +124,17 @@ fn sectionalize(desc: Option<~str>) -> (Option<~str>, ~[doc::Section]) {
match copy current_section {
Some(section) => {
current_section = Some(doc::Section {
body: section.body + "\n" + *line,
body: fmt!("%s\n%s", section.body, line),
.. section
});
}
None => {
new_desc = match copy new_desc {
Some(desc) => {
Some(desc + "\n" + *line)
Some(fmt!("%s\n%s", desc, line))
}
None => {
Some(copy *line)
Some(line.to_owned())
}
};
}
......@@ -146,15 +144,15 @@ fn sectionalize(desc: Option<~str>) -> (Option<~str>, ~[doc::Section]) {
}
if current_section.is_some() {
sections += [current_section.get()];
sections.push(current_section.unwrap());
}
(new_desc, sections)
}
fn parse_header(line: ~str) -> Option<~str> {
fn parse_header<'a>(line: &'a str) -> Option<&'a str> {
if line.starts_with("# ") {
Some(line.slice(2u, line.len()).to_owned())
Some(line.slice_from(2))
} else {
None
}
......@@ -172,9 +170,6 @@ mod test {
use extract;
use sectionalize_pass::run;
use core::str;
use core::vec;
fn mk_doc(source: ~str) -> doc::Doc {
do astsrv::from_str(copy source) |srv| {
let doc = extract::from_srv(srv.clone(), ~"");
......
......@@ -149,8 +149,6 @@ mod test {
use sectionalize_pass;
use text_pass::mk_pass;
use core::str;
fn mk_doc(source: ~str) -> doc::Doc {
do astsrv::from_str(copy source) |srv| {
let doc = extract::from_srv(srv.clone(), ~"");
......
......@@ -21,7 +21,6 @@
use core::prelude::*;
use core::str;
use core::uint;
use pass::Pass;
use text_pass;
......@@ -31,8 +30,7 @@ pub fn mk_pass() -> Pass {
}
fn unindent(s: &str) -> ~str {
let mut lines = ~[];
for str::each_line_any(s) |line| { lines.push(line.to_owned()); }
let lines = s.any_line_iter().collect::<~[&str]>();
let mut saw_first_line = false;
let mut saw_second_line = false;
let min_indent = do lines.iter().fold(uint::max_value)
......@@ -76,19 +74,20 @@ fn unindent(s: &str) -> ~str {
}
};
if !lines.is_empty() {
let unindented = ~[lines.head().trim().to_owned()]
+ do lines.tail().map |line| {
if line.is_whitespace() {
copy *line
} else {
assert!(line.len() >= min_indent);
line.slice(min_indent, line.len()).to_owned()
}
};
unindented.connect("\n")
} else {
s.to_str()
match lines {
[head, .. tail] => {
let mut unindented = ~[ head.trim() ];
unindented.push_all(do tail.map |&line| {
if line.is_whitespace() {
line
} else {
assert!(line.len() >= min_indent);
line.slice_from(min_indent)
}
});
unindented.connect("\n")
}
[] => s.to_owned()
}
}
......
......@@ -370,7 +370,7 @@ fn run_cmd(repl: &mut Repl, _in: @io::Reader, _out: @io::Writer,
if arg.ends_with(".rs") || arg.ends_with(".rc") {
(arg.slice_to(arg.len() - 3).to_owned(), copy *arg)
} else {
(copy *arg, arg + ".rs")
(copy *arg, *arg + ".rs")
};
match compile_crate(filename, copy repl.binary) {
Some(_) => loaded_crates.push(crate),
......
......@@ -144,7 +144,7 @@ pub fn fill_utf16_buf_and_decode(f: &fn(*mut u16, DWORD) -> DWORD)
}
pub fn as_utf16_p<T>(s: &str, f: &fn(*u16) -> T) -> T {
let mut t = str::to_utf16(s);
let mut t = s.to_utf16();
// Null terminate before passing on.
t += [0u16];
vec::as_imm_buf(t, |buf, _len| f(buf))
......
......@@ -515,7 +515,7 @@ fn with_filename(&self, f: &str) -> PosixPath {
fn with_filestem(&self, s: &str) -> PosixPath {
match self.filetype() {
None => self.with_filename(s),
Some(ref t) => self.with_filename(str::to_owned(s) + *t),
Some(ref t) => self.with_filename(s.to_owned() + *t),
}
}
......@@ -657,7 +657,7 @@ fn from_str(s: &str) -> WindowsPath {
(None, None) => {
host = None;
device = None;
rest = str::to_owned(s);
rest = s.to_owned();
}
}
......@@ -729,7 +729,7 @@ fn with_filename(&self, f: &str) -> WindowsPath {
fn with_filestem(&self, s: &str) -> WindowsPath {
match self.filetype() {
None => self.with_filename(s),
Some(ref t) => self.with_filename(str::to_owned(s) + *t),
Some(ref t) => self.with_filename(s.to_owned() + *t),
}
}
......@@ -947,7 +947,6 @@ pub fn extract_drive_prefix(s: &str) -> Option<(~str,~str)> {
mod tests {
use option::{None, Some};
use path::{PosixPath, WindowsPath, windows};
use str;
#[test]
fn test_double_slash_collapsing() {
......@@ -984,7 +983,7 @@ fn test_filetype_foo() {
fn test_posix_paths() {
fn t(wp: &PosixPath, s: &str) {
let ss = wp.to_str();
let sss = str::to_owned(s);
let sss = s.to_owned();
if (ss != sss) {
debug!("got %s", ss);
debug!("expected %s", sss);
......@@ -1042,7 +1041,7 @@ fn t(wp: &PosixPath, s: &str) {
fn test_normalize() {
fn t(wp: &PosixPath, s: &str) {
let ss = wp.to_str();
let sss = str::to_owned(s);
let sss = s.to_owned();
if (ss != sss) {
debug!("got %s", ss);
debug!("expected %s", sss);
......@@ -1105,7 +1104,7 @@ fn test_extract_drive_prefixes() {
fn test_windows_paths() {
fn t(wp: &WindowsPath, s: &str) {
let ss = wp.to_str();
let sss = str::to_owned(s);
let sss = s.to_owned();
if (ss != sss) {
debug!("got %s", ss);
debug!("expected %s", sss);
......
此差异已折叠。
......@@ -39,8 +39,6 @@ pub mod rt {
use parse;
use print::pprust;
use core::str;
pub use ast::*;
pub use parse::token::*;
pub use parse::new_parser_from_tts;
......
......@@ -969,7 +969,7 @@ fn fake_print_crate(s: @pprust::ps, crate: ast::crate) {
// change every identifier to "zz"
pub fn to_zz() -> @fn(ast::ident)->ast::ident {
let zz_id = token::str_to_ident("zz");
|id| {zz_id}
|_id| {zz_id}
}
// maybe add to expand.rs...
......
......@@ -89,12 +89,11 @@ fn block_trim(lines: ~[~str], chars: ~str, max: Option<uint>) -> ~[~str] {
}
return do lines.map |line| {
let mut chars = ~[];
for line.iter().advance |c| { chars.push(c) }
let chars = line.iter().collect::<~[char]>();
if i > chars.len() {
~""
} else {
str::from_chars(chars.slice(i, chars.len()).to_owned())
str::from_chars(chars.slice(i, chars.len()))
}
};
}
......@@ -103,14 +102,13 @@ fn block_trim(lines: ~[~str], chars: ~str, max: Option<uint>) -> ~[~str] {
// FIXME #5475:
// return comment.slice(3u, comment.len()).trim().to_owned();
let r = comment.slice(3u, comment.len()); return r.trim().to_owned();
}
if comment.starts_with("/*") {
let mut lines = ~[];
for str::each_line_any(comment.slice(3u, comment.len() - 2u)) |line| {
lines.push(line.to_owned())
}
let lines = comment.slice(3u, comment.len() - 2u)
.any_line_iter()
.transform(|s| s.to_owned())
.collect::<~[~str]>();
let lines = vertical_trim(lines);
let lines = block_trim(lines, ~"\t ", None);
let lines = block_trim(lines, ~"*", Some(1u));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册