Skip to content

Commit ff9ab51

Browse files
authored
Merge pull request rust-lang#2550 from sinkuu/chars_count_index
Don't index a string with chars().count()/position()
2 parents 0bc0241 + 83c8d23 commit ff9ab51

File tree

6 files changed

+18
-17
lines changed

6 files changed

+18
-17
lines changed

src/attr.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,17 @@ impl Rewrite for ast::NestedMetaItem {
167167

168168
fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) {
169169
// Look at before and after comment and see if there are any empty lines.
170-
let comment_begin = comment.chars().position(|c| c == '/');
170+
let comment_begin = comment.find('/');
171171
let len = comment_begin.unwrap_or_else(|| comment.len());
172172
let mlb = count_newlines(&comment[..len]) > 1;
173173
let mla = if comment_begin.is_none() {
174174
mlb
175175
} else {
176-
let comment_end = comment.chars().rev().position(|c| !c.is_whitespace());
177-
let len = comment_end.unwrap();
178176
comment
179177
.chars()
180178
.rev()
181-
.take(len)
182-
.filter(|c| *c == '\n')
179+
.take_while(|c| c.is_whitespace())
180+
.filter(|&c| c == '\n')
183181
.count() > 1
184182
};
185183
(if mlb { "\n" } else { "" }, if mla { "\n" } else { "" })

src/comment.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub fn combine_strs_with_missing_comments(
187187
// expression and the second expression or the missing comment. We will preserve the original
188188
// layout whenever possible.
189189
let original_snippet = context.snippet(span);
190-
let prefer_same_line = if let Some(pos) = original_snippet.chars().position(|c| c == '/') {
190+
let prefer_same_line = if let Some(pos) = original_snippet.find('/') {
191191
!original_snippet[..pos].contains('\n')
192192
} else {
193193
!original_snippet.contains('\n')
@@ -523,7 +523,7 @@ pub fn recover_missing_comment_in_span(
523523
Some(String::new())
524524
} else {
525525
let missing_snippet = context.snippet(span);
526-
let pos = missing_snippet.chars().position(|c| c == '/').unwrap_or(0);
526+
let pos = missing_snippet.find('/').unwrap_or(0);
527527
// 1 = ` `
528528
let total_width = missing_comment.len() + used_width + 1;
529529
let force_new_line_before_comment =

src/issues.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,13 @@ fn find_unnumbered_issue() {
227227
let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
228228
assert_eq!(
229229
Some(failing_pos),
230-
text.chars().position(|c| seeker.inspect(c).is_some())
230+
text.find(|c| seeker.inspect(c).is_some())
231231
);
232232
}
233233

234234
fn check_pass(text: &str) {
235235
let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
236-
assert_eq!(None, text.chars().position(|c| seeker.inspect(c).is_some()));
236+
assert_eq!(None, text.find(|c| seeker.inspect(c).is_some()));
237237
}
238238

239239
check_fail("TODO\n", 4);

src/items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
977977
result.push_str(&where_clause_str);
978978
} else {
979979
let item_snippet = context.snippet(item.span);
980-
if let Some(lo) = item_snippet.chars().position(|c| c == '/') {
980+
if let Some(lo) = item_snippet.find('/') {
981981
// 1 = `{`
982982
let comment_hi = body_lo - BytePos(1);
983983
let comment_lo = item.span.lo() + BytePos(lo as u32);

src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,14 @@ impl FormattingError {
154154
match self.kind {
155155
ErrorKind::LineOverflow(found, max) => (max, found - max),
156156
ErrorKind::TrailingWhitespace => {
157-
let trailing_ws_len = self.line_buffer
158-
.chars()
159-
.rev()
160-
.take_while(|c| c.is_whitespace())
161-
.count();
162-
(self.line_buffer.len() - trailing_ws_len, trailing_ws_len)
157+
let trailing_ws_start = self.line_buffer
158+
.rfind(|c: char| !c.is_whitespace())
159+
.map(|pos| pos + 1)
160+
.unwrap_or(0);
161+
(
162+
trailing_ws_start,
163+
self.line_buffer.len() - trailing_ws_start,
164+
)
163165
}
164166
_ => unreachable!(),
165167
}

src/utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ pub fn stmt_expr(stmt: &ast::Stmt) -> Option<&ast::Expr> {
251251

252252
#[inline]
253253
pub fn count_newlines(input: &str) -> usize {
254-
input.chars().filter(|&c| c == '\n').count()
254+
// Using `as_bytes` to omit UTF-8 decoding
255+
input.as_bytes().iter().filter(|&&c| c == b'\n').count()
255256
}
256257

257258
macro_rules! msg {

0 commit comments

Comments
 (0)