libsyntax: make matches_codepattern unicode aware

This commit is contained in:
Kevin Butler
2015-11-10 00:29:56 +00:00
parent 8a27230102
commit 9e3e43f3f6

View File

@@ -14,7 +14,7 @@ use parse::new_parser_from_source_str;
use parse::parser::Parser; use parse::parser::Parser;
use parse::token; use parse::token;
use ptr::P; use ptr::P;
use str::char_at; use std::iter::Peekable;
/// Map a string to tts, using a made-up filename: /// Map a string to tts, using a made-up filename:
pub fn string_to_tts(source_str: String) -> Vec<ast::TokenTree> { pub fn string_to_tts(source_str: String) -> Vec<ast::TokenTree> {
@@ -87,57 +87,55 @@ pub fn strs_to_idents(ids: Vec<&str> ) -> Vec<ast::Ident> {
/// Does the given string match the pattern? whitespace in the first string /// Does the given string match the pattern? whitespace in the first string
/// may be deleted or replaced with other whitespace to match the pattern. /// may be deleted or replaced with other whitespace to match the pattern.
/// this function is Unicode-ignorant; fortunately, the careful design of /// This function is relatively Unicode-ignorant; fortunately, the careful design
/// UTF-8 mitigates this ignorance. In particular, this function only collapses /// of UTF-8 mitigates this ignorance. It doesn't do NKF-normalization(?).
/// sequences of \n, \r, ' ', and \t, but it should otherwise tolerate Unicode
/// chars. Unsurprisingly, it doesn't do NKF-normalization(?).
pub fn matches_codepattern(a : &str, b : &str) -> bool { pub fn matches_codepattern(a : &str, b : &str) -> bool {
let mut idx_a = 0; let mut a_iter = a.chars().peekable();
let mut idx_b = 0; let mut b_iter = b.chars().peekable();
loop { loop {
if idx_a == a.len() && idx_b == b.len() { let (a, b) = match (a_iter.peek(), b_iter.peek()) {
return true; (None, None) => return true,
} (None, _) => return false,
else if idx_a == a.len() {return false;} (Some(a), None) => {
else if idx_b == b.len() { if a.is_whitespace() {
// maybe the stuff left in a is all ws? break // trailing whitespace check is out of loop for borrowck
if is_whitespace(char_at(a, idx_a)) {
return scan_for_non_ws_or_end(a,idx_a) == a.len();
} else { } else {
return false; return false
} }
} }
// ws in both given and pattern: (Some(&a), Some(&b)) => (a, 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); if a.is_whitespace() && b.is_whitespace() {
idx_b = scan_for_non_ws_or_end(b,idx_b); // skip whitespace for a and b
} scan_for_non_ws_or_end(&mut a_iter);
// ws in given only: scan_for_non_ws_or_end(&mut b_iter);
else if is_whitespace(char_at(a, idx_a)) { } else if a.is_whitespace() {
idx_a = scan_for_non_ws_or_end(a,idx_a); // skip whitespace for a
} scan_for_non_ws_or_end(&mut a_iter);
// *don't* silently eat ws in expected only. } else if a == b {
else if char_at(a, idx_a) == char_at(b, idx_b) { a_iter.next();
idx_a += 1; b_iter.next();
idx_b += 1; } else {
} return false
else {
return false;
}
} }
} }
/// Given a string and an index, return the first usize >= idx // check if a has *only* trailing whitespace
/// that is a non-ws-char or is outside of the legal range of a_iter.all(|c| c.is_whitespace())
/// the string. }
fn scan_for_non_ws_or_end(a : &str, idx: usize) -> usize {
let mut i = idx; /// Advances the given peekable `Iterator` until it reaches a non-whitespace character
let len = a.len(); fn scan_for_non_ws_or_end<I: Iterator<Item= char>>(iter: &mut Peekable<I>) {
while (i < len) && (is_whitespace(char_at(a, i))) { loop {
i += 1; match iter.peek() {
Some(c) if c.is_whitespace() => {} // fall through; borrowck
_ => return
}
iter.next();
} }
i
} }
pub fn is_whitespace(c: char) -> bool { pub fn is_whitespace(c: char) -> bool {
@@ -148,7 +146,8 @@ pub fn is_whitespace(c: char) -> bool {
mod tests { mod tests {
use super::*; use super::*;
#[test] fn eqmodws() { #[test]
fn eqmodws() {
assert_eq!(matches_codepattern("",""),true); assert_eq!(matches_codepattern("",""),true);
assert_eq!(matches_codepattern("","a"),false); assert_eq!(matches_codepattern("","a"),false);
assert_eq!(matches_codepattern("a",""),false); assert_eq!(matches_codepattern("a",""),false);
@@ -159,5 +158,18 @@ mod tests {
assert_eq!(matches_codepattern("a b","a b"),true); assert_eq!(matches_codepattern("a b","a b"),true);
assert_eq!(matches_codepattern("ab","a b"),false); assert_eq!(matches_codepattern("ab","a b"),false);
assert_eq!(matches_codepattern("a b","ab"),true); assert_eq!(matches_codepattern("a b","ab"),true);
assert_eq!(matches_codepattern(" a b","ab"),true);
}
#[test]
fn more_whitespace() {
assert_eq!(matches_codepattern("","\x0C"), false);
assert_eq!(matches_codepattern("a b","a\u{2002}b"),true);
assert_eq!(matches_codepattern("a b ","a \u{0085}\n\t\r b"),true);
assert_eq!(matches_codepattern("a b","a \u{0085}\n\t\r b "),false);
assert_eq!(matches_codepattern("a b","a\u{2002}b"),true);
assert_eq!(matches_codepattern("ab","a\u{2003}b"),false);
assert_eq!(matches_codepattern("a \u{3000}b","ab"),true);
assert_eq!(matches_codepattern("\u{205F}a b","ab"),true);
} }
} }