From 760c95792cf81cf1d4dbddf872aa651065d5b11a Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 20 Apr 2020 14:30:56 -0400 Subject: [PATCH] refactor char utils to separate file --- src/utils/chars.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++ src/utils/mod.rs | 51 ++++--------------------------------------- 2 files changed, 58 insertions(+), 47 deletions(-) create mode 100644 src/utils/chars.rs diff --git a/src/utils/chars.rs b/src/utils/chars.rs new file mode 100644 index 0000000..6f428e0 --- /dev/null +++ b/src/utils/chars.rs @@ -0,0 +1,54 @@ +use std::iter::Iterator; + +use peekmore::PeekMoreIterator; + +use super::read_until_closing_quote; + +use crate::Token; + +pub(crate) fn read_until_char>( + toks: &mut PeekMoreIterator, + c: char, +) -> Vec { + let mut v = Vec::new(); + while let Some(tok) = toks.next() { + match tok.kind { + '"' | '\'' => { + v.push(tok.clone()); + v.extend(read_until_closing_quote(toks, tok.kind)); + continue; + } + t if t == c => break, + _ => {} + } + v.push(tok) + } + v +} + +pub(crate) fn is_ident_char(c: char) -> bool { + c.is_ascii_alphabetic() || c == '_' || c == '\\' || (!c.is_ascii() && !c.is_control()) +} + +pub(crate) fn hex_char_for(number: u32) -> char { + assert!(number < 0x10); + std::char::from_u32(if number < 0xA { + 0x30 + number + } else { + 0x61 - 0xA + number + }) + .unwrap() +} + +pub(crate) fn is_name(c: char) -> bool { + is_name_start(c) || c.is_digit(10) || c == '-' +} + +pub(crate) fn is_name_start(c: char) -> bool { + // NOTE: in the dart-sass implementation, identifiers cannot start + // with numbers. We explicitly differentiate from the reference + // implementation here in order to support selectors beginning with numbers. + // This can be considered a hack and in the future it would be nice to refactor + // how this is handled. + c == '_' || c.is_alphanumeric() || c as u32 >= 0x0080 +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index f48bc74..5fd00a1 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -10,6 +10,10 @@ use crate::selector::Selector; use crate::value::Value; use crate::{Scope, Token}; +pub(crate) use chars::*; + +mod chars; + pub(crate) trait IsWhitespace { fn is_whitespace(&self) -> bool; } @@ -416,19 +420,6 @@ fn interpolated_ident_body>( Ok(Spanned { node: buf, span }) } -fn is_name(c: char) -> bool { - is_name_start(c) || c.is_digit(10) || c == '-' -} - -fn is_name_start(c: char) -> bool { - // NOTE: in the dart-sass implementation, identifiers cannot start - // with numbers. We explicitly differentiate from the reference - // implementation here in order to support selectors beginning with numbers. - // This can be considered a hack and in the future it would be nice to refactor - // how this is handled. - c == '_' || c.is_alphanumeric() || c as u32 >= 0x0080 -} - fn escape>( toks: &mut PeekMoreIterator, identifier_start: bool, @@ -822,37 +813,3 @@ pub(crate) fn read_until_closing_square_brace>( } v } - -pub(crate) fn read_until_char>( - toks: &mut PeekMoreIterator, - c: char, -) -> Vec { - let mut v = Vec::new(); - while let Some(tok) = toks.next() { - match tok.kind { - '"' | '\'' => { - v.push(tok.clone()); - v.extend(read_until_closing_quote(toks, tok.kind)); - continue; - } - t if t == c => break, - _ => {} - } - v.push(tok) - } - v -} - -pub(crate) fn is_ident_char(c: char) -> bool { - c.is_ascii_alphabetic() || c == '_' || c == '\\' || (!c.is_ascii() && !c.is_control()) -} - -pub(crate) fn hex_char_for(number: u32) -> char { - assert!(number < 0x10); - std::char::from_u32(if number < 0xA { - 0x30 + number - } else { - 0x61 - 0xA + number - }) - .unwrap() -}