use crate::pytables::{ALNUM, ALPHA, DECIMAL, PUNCT_SYMBOL, SPACE};
fn in_table(t: &[(u32, u32)], c: char) -> bool {
let c = c as u32;
t.binary_search_by(|&(lo, hi)| {
if c < lo {
std::cmp::Ordering::Greater
} else if c > hi {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Equal
}
})
.is_ok()
}
pub fn py_isalpha_char(c: char) -> bool {
if c.is_ascii() {
return c.is_ascii_alphabetic();
}
in_table(ALPHA, c)
}
pub fn py_isalnum_char(c: char) -> bool {
if c.is_ascii() {
return c.is_ascii_alphanumeric();
}
in_table(ALNUM, c)
}
pub fn py_isdecimal_char(c: char) -> bool {
if c.is_ascii() {
return c.is_ascii_digit();
}
in_table(DECIMAL, c)
}
pub fn py_is_word_char(c: char) -> bool {
c == '_' || py_isalnum_char(c)
}
pub fn py_is_word_start_char(c: char) -> bool {
py_is_word_char(c) && !py_isdecimal_char(c)
}
pub fn py_isspace_char(c: char) -> bool {
if c.is_ascii() {
return matches!(c, '\t'..='\r' | '\x1c'..='\x1f' | ' ');
}
in_table(SPACE, c)
}
#[doc(hidden)]
pub fn py_is_punct_or_symbol_char(c: char) -> bool {
if c.is_ascii() {
return c.is_ascii_punctuation();
}
in_table(PUNCT_SYMBOL, c)
}
pub fn py_isalpha(s: &str) -> bool {
!s.is_empty() && s.chars().all(py_isalpha_char)
}
pub fn py_isalnum(s: &str) -> bool {
!s.is_empty() && s.chars().all(py_isalnum_char)
}
pub fn py_isspace(s: &str) -> bool {
!s.is_empty() && s.chars().all(py_isspace_char)
}
pub fn py_has_alnum(s: &str) -> bool {
s.chars().any(py_isalnum_char)
}
pub fn py_has_alpha(s: &str) -> bool {
s.chars().any(py_isalpha_char)
}
pub fn py_strip(s: &str) -> &str {
s.trim_matches(py_isspace_char)
}
pub fn py_lstrip(s: &str) -> &str {
s.trim_start_matches(py_isspace_char)
}
pub fn py_rstrip(s: &str) -> &str {
s.trim_end_matches(py_isspace_char)
}
#[doc(hidden)]
pub fn py_is_line_break(c: char) -> bool {
matches!(c, '\n' | '\r' | '\u{0b}' | '\u{0c}' | '\u{1c}' | '\u{1d}' | '\u{1e}' | '\u{85}' | '\u{2028}' | '\u{2029}')
}
#[doc(hidden)]
pub fn py_splitlines_keepends(s: &str) -> Vec<&str> {
let mut out = Vec::new();
let mut start = 0;
let mut it = s.char_indices().peekable();
while let Some((i, c)) = it.next() {
if py_is_line_break(c) {
let mut end = i + c.len_utf8();
if c == '\r' && matches!(it.peek(), Some(&(_, '\n'))) {
it.next();
end += 1;
}
out.push(&s[start..end]);
start = end;
}
}
if start < s.len() {
out.push(&s[start..]);
}
out
}
#[doc(hidden)]
pub fn py_split_line_end(line: &str) -> (&str, &str) {
if let Some(body) = line.strip_suffix("\r\n") {
return (body, "\r\n");
}
match line.chars().next_back() {
Some(c) if py_is_line_break(c) => line.split_at(line.len() - c.len_utf8()),
_ => (line, ""),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn splitlines_is_pythons() {
assert_eq!(py_splitlines_keepends("a\r\nb\rc\u{2028}d\x0ce\n\nf"), ["a\r\n", "b\r", "c\u{2028}", "d\x0c", "e\n", "\n", "f"]);
assert!(py_splitlines_keepends("").is_empty());
assert_eq!(py_split_line_end("x\r\n"), ("x", "\r\n"));
assert_eq!(py_split_line_end("\n"), ("", "\n"));
assert_eq!(py_split_line_end("x\u{85}"), ("x", "\u{85}"));
assert_eq!(py_split_line_end("x"), ("x", ""));
}
#[test]
fn combining_marks_are_not_alphanumeric() {
assert!(!py_has_alnum("\u{0301}"));
assert!(!py_has_alnum("\u{0903}\u{200D}"));
assert!(!py_isalnum("\u{0301}"));
assert!(py_has_alnum("e\u{0301}"));
assert!('\u{0345}'.is_alphabetic() && !py_isalpha_char('\u{0345}'));
assert!('\u{05B0}'.is_alphabetic() && !py_isalpha_char('\u{05B0}'));
}
#[test]
fn numbers_count_as_alnum_but_not_alpha() {
for c in ['5', '\u{0665}', '\u{00B2}', '\u{00BD}', '\u{2160}', '\u{4E00}', '\u{3007}'] {
assert!(py_isalnum_char(c), "{c:?}");
}
assert!(!py_isalpha_char('5') && !py_isalpha_char('\u{00BD}'));
assert!(py_isalpha_char('\u{4E00}') && !py_isalpha_char('\u{3007}') );
}
#[test]
fn word_chars_follow_python_re() {
assert!(py_is_word_char('_') && py_is_word_char('\u{00B2}') && !py_is_word_char('-'));
assert!(py_is_word_start_char('\u{00B2}') && !py_is_word_start_char('\u{0665}') && !py_is_word_start_char('7'));
assert!(!py_is_word_char('\u{0301}'));
}
#[test]
fn whitespace_matches_python() {
for c in ['\t', '\n', '\x0b', '\x0c', '\r', '\x1c', '\x1d', '\x1e', '\x1f', ' ', '\u{85}', '\u{A0}', '\u{1680}', '\u{2003}', '\u{2028}', '\u{2029}', '\u{202F}', '\u{205F}', '\u{3000}'] {
assert!(py_isspace_char(c), "{c:?}");
}
for c in ['\u{200B}', '\u{FEFF}', '\u{180E}', '\u{2060}', 'a', '\u{0}'] {
assert!(!py_isspace_char(c), "{c:?}");
}
assert!(!'\x1c'.is_whitespace());
assert_eq!(py_strip("\x1c a \u{3000}"), "a");
assert!(!py_isspace("") && !py_isalnum("") && !py_isalpha(""));
}
#[test]
fn rust_and_python_differ_on_unicode_16_letters() {
assert!(!py_isalpha_char('\u{10D4A}'));
assert!(py_isalpha_char('\u{1E900}'));
}
#[test]
fn tables_are_sorted_and_the_ascii_fast_path_agrees() {
for t in [ALPHA, ALNUM, DECIMAL, SPACE] {
assert!(t.windows(2).all(|w| w[0].1 < w[1].0 && w[0].0 <= w[0].1));
}
for c in (0u8..128).map(char::from) {
assert_eq!(py_isalpha_char(c), in_table(ALPHA, c), "{c:?}");
assert_eq!(py_isalnum_char(c), in_table(ALNUM, c), "{c:?}");
assert_eq!(py_isspace_char(c), in_table(SPACE, c), "{c:?}");
}
for &(lo, hi) in ALPHA {
assert!(in_table(ALNUM, char::from_u32(lo).unwrap()) && in_table(ALNUM, char::from_u32(hi).unwrap()));
}
}
}