Use iterator and pattern APIs instead of char_at
This commit is contained in:
parent
25c375413a
commit
11af6f66cb
5 changed files with 32 additions and 62 deletions
|
@ -173,7 +173,6 @@ pub mod parse;
|
||||||
pub mod ptr;
|
pub mod ptr;
|
||||||
pub mod show_span;
|
pub mod show_span;
|
||||||
pub mod std_inject;
|
pub mod std_inject;
|
||||||
pub mod str;
|
|
||||||
pub use syntax_pos::edition;
|
pub use syntax_pos::edition;
|
||||||
pub use syntax_pos::symbol;
|
pub use syntax_pos::symbol;
|
||||||
pub mod test;
|
pub mod test;
|
||||||
|
|
|
@ -16,7 +16,6 @@ use syntax_pos::{BytePos, CharPos, Pos, FileName};
|
||||||
use parse::lexer::{is_block_doc_comment, is_pattern_whitespace};
|
use parse::lexer::{is_block_doc_comment, is_pattern_whitespace};
|
||||||
use parse::lexer::{self, ParseSess, StringReader, TokenAndSpan};
|
use parse::lexer::{self, ParseSess, StringReader, TokenAndSpan};
|
||||||
use print::pprust;
|
use print::pprust;
|
||||||
use str::char_at;
|
|
||||||
|
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::usize;
|
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
|
/// Otherwise returns Some(k) where k is first char offset after that leading
|
||||||
/// whitespace. Note k may be outside bounds of s.
|
/// whitespace. Note k may be outside bounds of s.
|
||||||
fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
|
fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
|
||||||
let len = s.len();
|
let mut idx = 0;
|
||||||
let mut col = col.to_usize();
|
for (i, ch) in s.char_indices().take(col.to_usize()) {
|
||||||
let mut cursor: usize = 0;
|
|
||||||
|
|
||||||
while col > 0 && cursor < len {
|
|
||||||
let ch = char_at(s, cursor);
|
|
||||||
if !ch.is_whitespace() {
|
if !ch.is_whitespace() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
cursor += ch.len_utf8();
|
idx = i;
|
||||||
col -= 1;
|
|
||||||
}
|
}
|
||||||
|
Some(idx)
|
||||||
Some(cursor)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn trim_whitespace_prefix_and_push_line(lines: &mut Vec<String>, s: String, col: CharPos) {
|
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) {
|
let s1 = match all_whitespace(&s[..], col) {
|
||||||
Some(col) => {
|
Some(col) => {
|
||||||
if col < len {
|
if col < len {
|
||||||
(&s[col..len]).to_string()
|
s[col..len].to_string()
|
||||||
} else {
|
} else {
|
||||||
String::new()
|
String::new()
|
||||||
}
|
}
|
||||||
|
@ -247,20 +240,13 @@ fn read_block_comment(rdr: &mut StringReader,
|
||||||
let mut lines: Vec<String> = Vec::new();
|
let mut lines: Vec<String> = Vec::new();
|
||||||
|
|
||||||
// Count the number of chars since the start of the line by rescanning.
|
// 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);
|
let end_src_index = rdr.src_index(rdr.pos);
|
||||||
assert!(src_index <= end_src_index,
|
assert!(src_index <= end_src_index,
|
||||||
"src_index={}, end_src_index={}, line_begin_pos={}",
|
"src_index={}, end_src_index={}, line_begin_pos={}",
|
||||||
src_index, end_src_index, rdr.source_file.line_begin_pos(rdr.pos).to_u32());
|
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 col = CharPos(rdr.src[src_index..end_src_index].chars().count());
|
||||||
let c = char_at(&rdr.src, src_index);
|
|
||||||
src_index += c.len_utf8();
|
|
||||||
n += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
let col = CharPos(n);
|
|
||||||
|
|
||||||
rdr.bump();
|
rdr.bump();
|
||||||
rdr.bump();
|
rdr.bump();
|
||||||
|
|
|
@ -13,12 +13,12 @@ use syntax_pos::{self, BytePos, CharPos, Pos, Span, NO_EXPANSION};
|
||||||
use source_map::{SourceMap, FilePathMapping};
|
use source_map::{SourceMap, FilePathMapping};
|
||||||
use errors::{Applicability, FatalError, Diagnostic, DiagnosticBuilder};
|
use errors::{Applicability, FatalError, Diagnostic, DiagnosticBuilder};
|
||||||
use parse::{token, ParseSess};
|
use parse::{token, ParseSess};
|
||||||
use str::char_at;
|
|
||||||
use symbol::{Symbol, keywords};
|
use symbol::{Symbol, keywords};
|
||||||
use core::unicode::property::Pattern_White_Space;
|
use core::unicode::property::Pattern_White_Space;
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::char;
|
use std::char;
|
||||||
|
use std::iter;
|
||||||
use std::mem::replace;
|
use std::mem::replace;
|
||||||
use rustc_data_structures::sync::Lrc;
|
use rustc_data_structures::sync::Lrc;
|
||||||
|
|
||||||
|
@ -459,45 +459,42 @@ impl<'a> StringReader<'a> {
|
||||||
|
|
||||||
/// Converts CRLF to LF in the given string, raising an error on bare CR.
|
/// Converts CRLF to LF in the given string, raising an error on bare CR.
|
||||||
fn translate_crlf<'b>(&self, start: BytePos, s: &'b str, errmsg: &'b str) -> Cow<'b, str> {
|
fn translate_crlf<'b>(&self, start: BytePos, s: &'b str, errmsg: &'b str) -> Cow<'b, str> {
|
||||||
let mut i = 0;
|
let mut chars = s.char_indices().peekable();
|
||||||
while i < s.len() {
|
while let Some((i, ch)) = chars.next() {
|
||||||
let ch = char_at(s, i);
|
|
||||||
let next = i + ch.len_utf8();
|
|
||||||
if ch == '\r' {
|
if ch == '\r' {
|
||||||
if next < s.len() && char_at(s, next) == '\n' {
|
if let Some((lf_idx, '\n')) = chars.peek() {
|
||||||
return translate_crlf_(self, start, s, errmsg, i).into();
|
return translate_crlf_(self, start, s, *lf_idx, chars, errmsg).into();
|
||||||
}
|
}
|
||||||
let pos = start + BytePos(i as u32);
|
let pos = start + BytePos(i as u32);
|
||||||
let end_pos = start + BytePos(next as u32);
|
let end_pos = start + BytePos((i + ch.len_utf8()) as u32);
|
||||||
self.err_span_(pos, end_pos, errmsg);
|
self.err_span_(pos, end_pos, errmsg);
|
||||||
}
|
}
|
||||||
i = next;
|
|
||||||
}
|
}
|
||||||
return s.into();
|
return s.into();
|
||||||
|
|
||||||
fn translate_crlf_(rdr: &StringReader,
|
fn translate_crlf_(rdr: &StringReader,
|
||||||
start: BytePos,
|
start: BytePos,
|
||||||
s: &str,
|
s: &str,
|
||||||
errmsg: &str,
|
mut j: usize,
|
||||||
mut i: usize)
|
mut chars: iter::Peekable<impl Iterator<Item = (usize, char)>>,
|
||||||
|
errmsg: &str)
|
||||||
-> String {
|
-> String {
|
||||||
let mut buf = String::with_capacity(s.len());
|
let mut buf = String::with_capacity(s.len());
|
||||||
let mut j = 0;
|
// Skip first CR
|
||||||
while i < s.len() {
|
buf.push_str(&s[.. j - 1]);
|
||||||
let ch = char_at(s, i);
|
while let Some((i, ch)) = chars.next() {
|
||||||
let next = i + ch.len_utf8();
|
|
||||||
if ch == '\r' {
|
if ch == '\r' {
|
||||||
if j < i {
|
if j < i {
|
||||||
buf.push_str(&s[j..i]);
|
buf.push_str(&s[j..i]);
|
||||||
}
|
}
|
||||||
|
let next = i + ch.len_utf8();
|
||||||
j = next;
|
j = next;
|
||||||
if next >= s.len() || char_at(s, next) != '\n' {
|
if chars.peek().map(|(_, ch)| *ch) != Some('\n') {
|
||||||
let pos = start + BytePos(i as u32);
|
let pos = start + BytePos(i as u32);
|
||||||
let end_pos = start + BytePos(next as u32);
|
let end_pos = start + BytePos(next as u32);
|
||||||
rdr.err_span_(pos, end_pos, errmsg);
|
rdr.err_span_(pos, end_pos, errmsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i = next;
|
|
||||||
}
|
}
|
||||||
if j < s.len() {
|
if j < s.len() {
|
||||||
buf.push_str(&s[j..]);
|
buf.push_str(&s[j..]);
|
||||||
|
@ -1858,6 +1855,11 @@ fn ident_continue(c: Option<char>) -> bool {
|
||||||
(c > '\x7f' && c.is_xid_continue())
|
(c > '\x7f' && c.is_xid_continue())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn char_at(s: &str, byte: usize) -> char {
|
||||||
|
s[byte..].chars().next().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -19,7 +19,6 @@ use errors::{Handler, ColorConfig, Diagnostic, DiagnosticBuilder};
|
||||||
use feature_gate::UnstableFeatures;
|
use feature_gate::UnstableFeatures;
|
||||||
use parse::parser::Parser;
|
use parse::parser::Parser;
|
||||||
use ptr::P;
|
use ptr::P;
|
||||||
use str::char_at;
|
|
||||||
use symbol::Symbol;
|
use symbol::Symbol;
|
||||||
use tokenstream::{TokenStream, TokenTree};
|
use tokenstream::{TokenStream, TokenTree};
|
||||||
use diagnostics::plugin::ErrorMap;
|
use diagnostics::plugin::ErrorMap;
|
||||||
|
@ -436,9 +435,7 @@ fn raw_str_lit(lit: &str) -> String {
|
||||||
|
|
||||||
// check if `s` looks like i32 or u1234 etc.
|
// check if `s` looks like i32 or u1234 etc.
|
||||||
fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool {
|
fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool {
|
||||||
s.len() > 1 &&
|
s.starts_with(first_chars) && s[1..].chars().all(|c| c.is_ascii_digit())
|
||||||
first_chars.contains(&char_at(s, 0)) &&
|
|
||||||
s[1..].chars().all(|c| '0' <= c && c <= '9')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! err {
|
macro_rules! err {
|
||||||
|
@ -645,11 +642,11 @@ fn integer_lit(s: &str, suffix: Option<Symbol>, diag: Option<(Span, &Handler)>)
|
||||||
let orig = s;
|
let orig = s;
|
||||||
let mut ty = ast::LitIntType::Unsuffixed;
|
let mut ty = ast::LitIntType::Unsuffixed;
|
||||||
|
|
||||||
if char_at(s, 0) == '0' && s.len() > 1 {
|
if s.starts_with('0') && s.len() > 1 {
|
||||||
match char_at(s, 1) {
|
match s.as_bytes()[1] {
|
||||||
'x' => base = 16,
|
b'x' => base = 16,
|
||||||
'o' => base = 8,
|
b'o' => base = 8,
|
||||||
'b' => base = 2,
|
b'b' => base = 2,
|
||||||
_ => { }
|
_ => { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
// Copyright 2012-2014 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.
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn char_at(s: &str, byte: usize) -> char {
|
|
||||||
s[byte..].chars().next().unwrap()
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue