Skip to content

Commit 19c8d70

Browse files
committed
syntax: Copy unstable str::char_at into libsyntax
1 parent 7f9180f commit 19c8d70

File tree

6 files changed

+39
-22
lines changed

6 files changed

+39
-22
lines changed

src/libsyntax/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub mod parse;
9292
pub mod ptr;
9393
pub mod show_span;
9494
pub mod std_inject;
95+
pub mod str;
9596
pub mod test;
9697
pub mod visit;
9798

src/libsyntax/parse/lexer/comments.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ pub use self::CommentStyle::*;
1313
use ast;
1414
use codemap::{BytePos, CharPos, CodeMap, Pos};
1515
use diagnostic;
16-
use parse::lexer::{is_whitespace, Reader};
17-
use parse::lexer::{StringReader, TokenAndSpan};
1816
use parse::lexer::is_block_doc_comment;
17+
use parse::lexer::{StringReader, TokenAndSpan};
18+
use parse::lexer::{is_whitespace, Reader};
1919
use parse::lexer;
2020
use print::pprust;
21+
use str::char_at;
2122

2223
use std::io::Read;
2324
use std::usize;
@@ -209,7 +210,7 @@ fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
209210
let mut col = col.to_usize();
210211
let mut cursor: usize = 0;
211212
while col > 0 && cursor < len {
212-
let ch = s.char_at(cursor);
213+
let ch = char_at(s, cursor);
213214
if !ch.is_whitespace() {
214215
return None;
215216
}

src/libsyntax/parse/lexer/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ use codemap::{BytePos, CharPos, CodeMap, Pos, Span};
1313
use codemap;
1414
use diagnostic::SpanHandler;
1515
use ext::tt::transcribe::tt_next_token;
16-
use parse::token;
1716
use parse::token::str_to_ident;
17+
use parse::token;
18+
use str::char_at;
1819

1920
use std::borrow::Cow;
2021
use std::char;
@@ -289,10 +290,10 @@ impl<'a> StringReader<'a> {
289290
s: &'b str, errmsg: &'b str) -> Cow<'b, str> {
290291
let mut i = 0;
291292
while i < s.len() {
292-
let ch = s.char_at(i);
293+
let ch = char_at(s, i);
293294
let next = i + ch.len_utf8();
294295
if ch == '\r' {
295-
if next < s.len() && s.char_at(next) == '\n' {
296+
if next < s.len() && char_at(s, next) == '\n' {
296297
return translate_crlf_(self, start, s, errmsg, i).into();
297298
}
298299
let pos = start + BytePos(i as u32);
@@ -308,12 +309,12 @@ impl<'a> StringReader<'a> {
308309
let mut buf = String::with_capacity(s.len());
309310
let mut j = 0;
310311
while i < s.len() {
311-
let ch = s.char_at(i);
312+
let ch = char_at(s, i);
312313
let next = i + ch.len_utf8();
313314
if ch == '\r' {
314315
if j < i { buf.push_str(&s[j..i]); }
315316
j = next;
316-
if next >= s.len() || s.char_at(next) != '\n' {
317+
if next >= s.len() || char_at(s, next) != '\n' {
317318
let pos = start + BytePos(i as u32);
318319
let end_pos = start + BytePos(next as u32);
319320
rdr.err_span_(pos, end_pos, errmsg);
@@ -335,7 +336,7 @@ impl<'a> StringReader<'a> {
335336
if current_byte_offset < self.source_text.len() {
336337
assert!(self.curr.is_some());
337338
let last_char = self.curr.unwrap();
338-
let ch = self.source_text.char_at(current_byte_offset);
339+
let ch = char_at(&self.source_text, current_byte_offset);
339340
let next = current_byte_offset + ch.len_utf8();
340341
let byte_offset_diff = next - current_byte_offset;
341342
self.pos = self.pos + Pos::from_usize(byte_offset_diff);
@@ -357,7 +358,7 @@ impl<'a> StringReader<'a> {
357358
pub fn nextch(&self) -> Option<char> {
358359
let offset = self.byte_offset(self.pos).to_usize();
359360
if offset < self.source_text.len() {
360-
Some(self.source_text.char_at(offset))
361+
Some(char_at(&self.source_text, offset))
361362
} else {
362363
None
363364
}
@@ -371,9 +372,9 @@ impl<'a> StringReader<'a> {
371372
let offset = self.byte_offset(self.pos).to_usize();
372373
let s = &self.source_text[..];
373374
if offset >= s.len() { return None }
374-
let next = offset + s.char_at(offset).len_utf8();
375+
let next = offset + char_at(s, offset).len_utf8();
375376
if next < s.len() {
376-
Some(s.char_at(next))
377+
Some(char_at(s, next))
377378
} else {
378379
None
379380
}

src/libsyntax/parse/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use diagnostic::{SpanHandler, mk_span_handler, default_handler, Auto, FatalError
1616
use parse::attr::ParserAttr;
1717
use parse::parser::Parser;
1818
use ptr::P;
19-
19+
use str::char_at;
2020

2121
use std::cell::{Cell, RefCell};
2222
use std::fs::File;
@@ -536,7 +536,7 @@ pub fn raw_str_lit(lit: &str) -> String {
536536
// check if `s` looks like i32 or u1234 etc.
537537
fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool {
538538
s.len() > 1 &&
539-
first_chars.contains(&s.char_at(0)) &&
539+
first_chars.contains(&char_at(s, 0)) &&
540540
s[1..].chars().all(|c| '0' <= c && c <= '9')
541541
}
542542

@@ -673,8 +673,8 @@ pub fn integer_lit(s: &str, suffix: Option<&str>, sd: &SpanHandler, sp: Span) ->
673673
let orig = s;
674674
let mut ty = ast::UnsuffixedIntLit(ast::Plus);
675675

676-
if s.char_at(0) == '0' && s.len() > 1 {
677-
match s.char_at(1) {
676+
if char_at(s, 0) == '0' && s.len() > 1 {
677+
match char_at(s, 1) {
678678
'x' => base = 16,
679679
'o' => base = 8,
680680
'b' => base = 2,

src/libsyntax/str.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn char_at(s: &str, byte: usize) -> char {
12+
s[byte..].chars().next().unwrap()
13+
}

src/libsyntax/util/parser_testing.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use parse::new_parser_from_source_str;
1515
use parse::parser::Parser;
1616
use parse::token;
1717
use ptr::P;
18+
use str::char_at;
1819

1920
/// Map a string to tts, using a made-up filename:
2021
pub fn string_to_tts(source_str: String) -> Vec<ast::TokenTree> {
@@ -96,24 +97,24 @@ pub fn matches_codepattern(a : &str, b : &str) -> bool {
9697
else if idx_a == a.len() {return false;}
9798
else if idx_b == b.len() {
9899
// maybe the stuff left in a is all ws?
99-
if is_whitespace(a.char_at(idx_a)) {
100+
if is_whitespace(char_at(a, idx_a)) {
100101
return scan_for_non_ws_or_end(a,idx_a) == a.len();
101102
} else {
102103
return false;
103104
}
104105
}
105106
// ws in both given and pattern:
106-
else if is_whitespace(a.char_at(idx_a))
107-
&& is_whitespace(b.char_at(idx_b)) {
107+
else if is_whitespace(char_at(a, idx_a))
108+
&& is_whitespace(char_at(b, idx_b)) {
108109
idx_a = scan_for_non_ws_or_end(a,idx_a);
109110
idx_b = scan_for_non_ws_or_end(b,idx_b);
110111
}
111112
// ws in given only:
112-
else if is_whitespace(a.char_at(idx_a)) {
113+
else if is_whitespace(char_at(a, idx_a)) {
113114
idx_a = scan_for_non_ws_or_end(a,idx_a);
114115
}
115116
// *don't* silently eat ws in expected only.
116-
else if a.char_at(idx_a) == b.char_at(idx_b) {
117+
else if char_at(a, idx_a) == char_at(b, idx_b) {
117118
idx_a += 1;
118119
idx_b += 1;
119120
}
@@ -129,7 +130,7 @@ pub fn matches_codepattern(a : &str, b : &str) -> bool {
129130
fn scan_for_non_ws_or_end(a : &str, idx: usize) -> usize {
130131
let mut i = idx;
131132
let len = a.len();
132-
while (i < len) && (is_whitespace(a.char_at(i))) {
133+
while (i < len) && (is_whitespace(char_at(a, i))) {
133134
i += 1;
134135
}
135136
i

0 commit comments

Comments
 (0)