about summary refs log tree commit diff
path: root/src/libsyntax/parse/lexer/comments.rs
diff options
context:
space:
mode:
authorShotaro Yamada <sinkuu@sinkuu.xyz>2018-10-27 21:41:26 +0900
committerShotaro Yamada <sinkuu@sinkuu.xyz>2018-12-04 09:27:24 +0900
commit11af6f66cb62cb577bbd787000f0ca57fbe1ee4a (patch)
tree74717a03b6ddbdda1c335730534a8ffbe7688724 /src/libsyntax/parse/lexer/comments.rs
parent25c375413ab8e24b01bef1d80d61dff58ef7bc1c (diff)
downloadrust-11af6f66cb62cb577bbd787000f0ca57fbe1ee4a.tar.gz
rust-11af6f66cb62cb577bbd787000f0ca57fbe1ee4a.zip
Use iterator and pattern APIs instead of `char_at`
Diffstat (limited to 'src/libsyntax/parse/lexer/comments.rs')
-rw-r--r--src/libsyntax/parse/lexer/comments.rs28
1 files changed, 7 insertions, 21 deletions
diff --git a/src/libsyntax/parse/lexer/comments.rs b/src/libsyntax/parse/lexer/comments.rs
index 172a48ddba2..92990ae2ef0 100644
--- a/src/libsyntax/parse/lexer/comments.rs
+++ b/src/libsyntax/parse/lexer/comments.rs
@@ -16,7 +16,6 @@ use syntax_pos::{BytePos, CharPos, Pos, FileName};
 use parse::lexer::{is_block_doc_comment, is_pattern_whitespace};
 use parse::lexer::{self, ParseSess, StringReader, TokenAndSpan};
 use print::pprust;
-use str::char_at;
 
 use std::io::Read;
 use std::usize;
@@ -207,20 +206,14 @@ fn read_line_comments(rdr: &mut StringReader,
 /// Otherwise returns Some(k) where k is first char offset after that leading
 /// whitespace.  Note k may be outside bounds of s.
 fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
-    let len = s.len();
-    let mut col = col.to_usize();
-    let mut cursor: usize = 0;
-
-    while col > 0 && cursor < len {
-        let ch = char_at(s, cursor);
+    let mut idx = 0;
+    for (i, ch) in s.char_indices().take(col.to_usize()) {
         if !ch.is_whitespace() {
             return None;
         }
-        cursor += ch.len_utf8();
-        col -= 1;
+        idx = i;
     }
-
-    Some(cursor)
+    Some(idx)
 }
 
 fn trim_whitespace_prefix_and_push_line(lines: &mut Vec<String>, s: String, col: CharPos) {
@@ -228,7 +221,7 @@ fn trim_whitespace_prefix_and_push_line(lines: &mut Vec<String>, s: String, col:
     let s1 = match all_whitespace(&s[..], col) {
         Some(col) => {
             if col < len {
-                (&s[col..len]).to_string()
+                s[col..len].to_string()
             } else {
                 String::new()
             }
@@ -247,20 +240,13 @@ fn read_block_comment(rdr: &mut StringReader,
     let mut lines: Vec<String> = Vec::new();
 
     // Count the number of chars since the start of the line by rescanning.
-    let mut src_index = rdr.src_index(rdr.source_file.line_begin_pos(rdr.pos));
+    let src_index = rdr.src_index(rdr.source_file.line_begin_pos(rdr.pos));
     let end_src_index = rdr.src_index(rdr.pos);
     assert!(src_index <= end_src_index,
         "src_index={}, end_src_index={}, line_begin_pos={}",
         src_index, end_src_index, rdr.source_file.line_begin_pos(rdr.pos).to_u32());
-    let mut n = 0;
-
-    while src_index < end_src_index {
-        let c = char_at(&rdr.src, src_index);
-        src_index += c.len_utf8();
-        n += 1;
-    }
 
-    let col = CharPos(n);
+    let col = CharPos(rdr.src[src_index..end_src_index].chars().count());
 
     rdr.bump();
     rdr.bump();