Skip to content

Commit 4b18fff

Browse files
committed
std: convert str::{map,levdistance,subslice_offset} to methods.
The first two become map_chars and lev_distance. Also, remove a few allocations in rustdoc.
1 parent c989b79 commit 4b18fff

File tree

7 files changed

+148
-155
lines changed

7 files changed

+148
-155
lines changed

src/librustc/middle/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4845,7 +4845,7 @@ impl Resolver {
48454845
let mut smallest = 0;
48464846
for maybes.eachi |i, &other| {
48474847

4848-
values[i] = str::levdistance(name, other);
4848+
values[i] = name.lev_distance(other);
48494849

48504850
if values[i] <= values[smallest] {
48514851
smallest = i;

src/librustdoc/desc_to_brief_pass.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,9 @@ fn first_sentence_(s: &str) -> ~str {
143143
}
144144

145145
pub fn paragraphs(s: &str) -> ~[~str] {
146-
let mut lines = ~[];
147-
for str::each_line_any(s) |line| { lines.push(line.to_owned()); }
148146
let mut whitespace_lines = 0;
149147
let mut accum = ~"";
150-
let paras = do lines.iter().fold(~[]) |paras, line| {
148+
let paras = do s.any_line_iter().fold(~[]) |paras, line| {
151149
let mut res = paras;
152150

153151
if line.is_whitespace() {
@@ -163,9 +161,9 @@ pub fn paragraphs(s: &str) -> ~[~str] {
163161
whitespace_lines = 0;
164162

165163
accum = if accum.is_empty() {
166-
copy *line
164+
line.to_owned()
167165
} else {
168-
accum + "\n" + *line
166+
fmt!("%s\n%s", accum, line)
169167
}
170168
}
171169

src/librustdoc/markdown_pass.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use markdown_writer::WriterFactory;
2222
use pass::Pass;
2323
use sort_pass;
2424

25+
use core::iterator::IteratorUtil;
2526
use core::cell::Cell;
2627
use core::str;
2728
use core::vec;
@@ -466,10 +467,7 @@ fn write_variant(ctxt: &Ctxt, doc: doc::VariantDoc) {
466467
}
467468

468469
fn list_item_indent(item: &str) -> ~str {
469-
let mut indented = ~[];
470-
for str::each_line_any(item) |line| {
471-
indented.push(line);
472-
}
470+
let indented = item.any_line_iter().collect::<~[&str]>();
473471

474472
// separate markdown elements within `*` lists must be indented by four
475473
// spaces, or they will escape the list context. indenting everything

src/librustdoc/sectionalize_pass.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use fold::Fold;
1919
use fold;
2020
use pass::Pass;
2121

22-
use core::str;
22+
use core::iterator::IteratorUtil;
2323

2424
pub fn mk_pass() -> Pass {
2525
Pass {
@@ -104,39 +104,37 @@ fn sectionalize(desc: Option<~str>) -> (Option<~str>, ~[doc::Section]) {
104104
if desc.is_none() {
105105
return (None, ~[]);
106106
}
107-
let mut lines = ~[];
108-
for str::each_line_any(*desc.get_ref()) |line| { lines.push(line.to_owned()); }
109107

110108
let mut new_desc = None::<~str>;
111109
let mut current_section = None;
112110
let mut sections = ~[];
113111

114-
for lines.each |line| {
115-
match parse_header(copy *line) {
112+
for desc.get_ref().any_line_iter().advance |line| {
113+
match parse_header(line) {
116114
Some(header) => {
117115
if current_section.is_some() {
118-
sections += [copy *current_section.get_ref()];
116+
sections.push(copy *current_section.get_ref());
119117
}
120118
current_section = Some(doc::Section {
121-
header: header,
119+
header: header.to_owned(),
122120
body: ~""
123121
});
124122
}
125123
None => {
126124
match copy current_section {
127125
Some(section) => {
128126
current_section = Some(doc::Section {
129-
body: section.body + "\n" + *line,
127+
body: fmt!("%s\n%s", section.body, line),
130128
.. section
131129
});
132130
}
133131
None => {
134132
new_desc = match copy new_desc {
135133
Some(desc) => {
136-
Some(desc + "\n" + *line)
134+
Some(fmt!("%s\n%s", desc, line))
137135
}
138136
None => {
139-
Some(copy *line)
137+
Some(line.to_owned())
140138
}
141139
};
142140
}
@@ -146,15 +144,15 @@ fn sectionalize(desc: Option<~str>) -> (Option<~str>, ~[doc::Section]) {
146144
}
147145

148146
if current_section.is_some() {
149-
sections += [current_section.get()];
147+
sections.push(current_section.unwrap());
150148
}
151149

152150
(new_desc, sections)
153151
}
154152

155-
fn parse_header(line: ~str) -> Option<~str> {
153+
fn parse_header<'a>(line: &'a str) -> Option<&'a str> {
156154
if line.starts_with("# ") {
157-
Some(line.slice(2u, line.len()).to_owned())
155+
Some(line.slice_from(2))
158156
} else {
159157
None
160158
}

src/librustdoc/unindent_pass.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ middle of a line, and each of the following lines is indented.
2121

2222
use core::prelude::*;
2323

24-
use core::str;
2524
use core::uint;
2625
use pass::Pass;
2726
use text_pass;
@@ -31,8 +30,7 @@ pub fn mk_pass() -> Pass {
3130
}
3231

3332
fn unindent(s: &str) -> ~str {
34-
let mut lines = ~[];
35-
for str::each_line_any(s) |line| { lines.push(line.to_owned()); }
33+
let lines = s.any_line_iter().collect::<~[&str]>();
3634
let mut saw_first_line = false;
3735
let mut saw_second_line = false;
3836
let min_indent = do lines.iter().fold(uint::max_value)
@@ -76,19 +74,20 @@ fn unindent(s: &str) -> ~str {
7674
}
7775
};
7876

79-
if !lines.is_empty() {
80-
let unindented = ~[lines.head().trim().to_owned()]
81-
+ do lines.tail().map |line| {
82-
if line.is_whitespace() {
83-
copy *line
84-
} else {
85-
assert!(line.len() >= min_indent);
86-
line.slice(min_indent, line.len()).to_owned()
87-
}
88-
};
89-
unindented.connect("\n")
90-
} else {
91-
s.to_str()
77+
match lines {
78+
[head, .. tail] => {
79+
let mut unindented = ~[ head.trim() ];
80+
unindented.push_all(do tail.map |&line| {
81+
if line.is_whitespace() {
82+
line
83+
} else {
84+
assert!(line.len() >= min_indent);
85+
line.slice_from(min_indent)
86+
}
87+
});
88+
unindented.connect("\n")
89+
}
90+
[] => s.to_owned()
9291
}
9392
}
9493

0 commit comments

Comments
 (0)