syntax: Copy unstable str::char_at into libsyntax

This commit is contained in:
Erick Tryzelaar 2015-04-21 10:19:53 -07:00
parent 7f9180fcb9
commit 19c8d70174
6 changed files with 39 additions and 22 deletions

View file

@ -92,6 +92,7 @@ pub mod syntax {
pub mod ptr;
pub mod show_span;
pub mod std_inject;
pub mod str;
pub mod test;
pub mod visit;

View file

@ -13,11 +13,12 @@
use ast;
use codemap::{BytePos, CharPos, CodeMap, Pos};
use diagnostic;
use parse::lexer::{is_whitespace, Reader};
use parse::lexer::{StringReader, TokenAndSpan};
use parse::lexer::is_block_doc_comment;
use parse::lexer::{StringReader, TokenAndSpan};
use parse::lexer::{is_whitespace, Reader};
use parse::lexer;
use print::pprust;
use str::char_at;
use std::io::Read;
use std::usize;
@ -209,7 +210,7 @@ fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
let mut col = col.to_usize();
let mut cursor: usize = 0;
while col > 0 && cursor < len {
let ch = s.char_at(cursor);
let ch = char_at(s, cursor);
if !ch.is_whitespace() {
return None;
}

View file

@ -13,8 +13,9 @@
use codemap;
use diagnostic::SpanHandler;
use ext::tt::transcribe::tt_next_token;
use parse::token;
use parse::token::str_to_ident;
use parse::token;
use str::char_at;
use std::borrow::Cow;
use std::char;
@ -289,10 +290,10 @@ fn translate_crlf<'b>(&self, start: BytePos,
s: &'b str, errmsg: &'b str) -> Cow<'b, str> {
let mut i = 0;
while i < s.len() {
let ch = s.char_at(i);
let ch = char_at(s, i);
let next = i + ch.len_utf8();
if ch == '\r' {
if next < s.len() && s.char_at(next) == '\n' {
if next < s.len() && char_at(s, next) == '\n' {
return translate_crlf_(self, start, s, errmsg, i).into();
}
let pos = start + BytePos(i as u32);
@ -308,12 +309,12 @@ fn translate_crlf_(rdr: &StringReader, start: BytePos,
let mut buf = String::with_capacity(s.len());
let mut j = 0;
while i < s.len() {
let ch = s.char_at(i);
let ch = char_at(s, i);
let next = i + ch.len_utf8();
if ch == '\r' {
if j < i { buf.push_str(&s[j..i]); }
j = next;
if next >= s.len() || s.char_at(next) != '\n' {
if next >= s.len() || char_at(s, next) != '\n' {
let pos = start + BytePos(i as u32);
let end_pos = start + BytePos(next as u32);
rdr.err_span_(pos, end_pos, errmsg);
@ -335,7 +336,7 @@ pub fn bump(&mut self) {
if current_byte_offset < self.source_text.len() {
assert!(self.curr.is_some());
let last_char = self.curr.unwrap();
let ch = self.source_text.char_at(current_byte_offset);
let ch = char_at(&self.source_text, current_byte_offset);
let next = current_byte_offset + ch.len_utf8();
let byte_offset_diff = next - current_byte_offset;
self.pos = self.pos + Pos::from_usize(byte_offset_diff);
@ -357,7 +358,7 @@ pub fn bump(&mut self) {
pub fn nextch(&self) -> Option<char> {
let offset = self.byte_offset(self.pos).to_usize();
if offset < self.source_text.len() {
Some(self.source_text.char_at(offset))
Some(char_at(&self.source_text, offset))
} else {
None
}
@ -371,9 +372,9 @@ pub fn nextnextch(&self) -> Option<char> {
let offset = self.byte_offset(self.pos).to_usize();
let s = &self.source_text[..];
if offset >= s.len() { return None }
let next = offset + s.char_at(offset).len_utf8();
let next = offset + char_at(s, offset).len_utf8();
if next < s.len() {
Some(s.char_at(next))
Some(char_at(s, next))
} else {
None
}

View file

@ -16,7 +16,7 @@
use parse::attr::ParserAttr;
use parse::parser::Parser;
use ptr::P;
use str::char_at;
use std::cell::{Cell, RefCell};
use std::fs::File;
@ -536,7 +536,7 @@ pub fn raw_str_lit(lit: &str) -> String {
// check if `s` looks like i32 or u1234 etc.
fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool {
s.len() > 1 &&
first_chars.contains(&s.char_at(0)) &&
first_chars.contains(&char_at(s, 0)) &&
s[1..].chars().all(|c| '0' <= c && c <= '9')
}
@ -673,8 +673,8 @@ pub fn integer_lit(s: &str, suffix: Option<&str>, sd: &SpanHandler, sp: Span) ->
let orig = s;
let mut ty = ast::UnsuffixedIntLit(ast::Plus);
if s.char_at(0) == '0' && s.len() > 1 {
match s.char_at(1) {
if char_at(s, 0) == '0' && s.len() > 1 {
match char_at(s, 1) {
'x' => base = 16,
'o' => base = 8,
'b' => base = 2,

13
src/libsyntax/str.rs Normal file
View file

@ -0,0 +1,13 @@
// 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.
pub fn char_at(s: &str, byte: usize) -> char {
s[byte..].chars().next().unwrap()
}

View file

@ -15,6 +15,7 @@
use parse::parser::Parser;
use parse::token;
use ptr::P;
use str::char_at;
/// Map a string to tts, using a made-up filename:
pub fn string_to_tts(source_str: String) -> Vec<ast::TokenTree> {
@ -96,24 +97,24 @@ pub fn matches_codepattern(a : &str, b : &str) -> bool {
else if idx_a == a.len() {return false;}
else if idx_b == b.len() {
// maybe the stuff left in a is all ws?
if is_whitespace(a.char_at(idx_a)) {
if is_whitespace(char_at(a, idx_a)) {
return scan_for_non_ws_or_end(a,idx_a) == a.len();
} else {
return false;
}
}
// ws in both given and pattern:
else if is_whitespace(a.char_at(idx_a))
&& is_whitespace(b.char_at(idx_b)) {
else if is_whitespace(char_at(a, idx_a))
&& is_whitespace(char_at(b, idx_b)) {
idx_a = scan_for_non_ws_or_end(a,idx_a);
idx_b = scan_for_non_ws_or_end(b,idx_b);
}
// ws in given only:
else if is_whitespace(a.char_at(idx_a)) {
else if is_whitespace(char_at(a, idx_a)) {
idx_a = scan_for_non_ws_or_end(a,idx_a);
}
// *don't* silently eat ws in expected only.
else if a.char_at(idx_a) == b.char_at(idx_b) {
else if char_at(a, idx_a) == char_at(b, idx_b) {
idx_a += 1;
idx_b += 1;
}
@ -129,7 +130,7 @@ pub fn matches_codepattern(a : &str, b : &str) -> bool {
fn scan_for_non_ws_or_end(a : &str, idx: usize) -> usize {
let mut i = idx;
let len = a.len();
while (i < len) && (is_whitespace(a.char_at(i))) {
while (i < len) && (is_whitespace(char_at(a, i))) {
i += 1;
}
i