Skip to content

Commit

Permalink
Merge pull request #381 from epage/caseless
Browse files Browse the repository at this point in the history
refactor(token): Clear up tag code
  • Loading branch information
epage authored Dec 5, 2023
2 parents 9238dba + 9c76be5 commit 7014156
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 37 deletions.
8 changes: 8 additions & 0 deletions src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ use crate::Parser;
#[derive(Copy, Clone, Debug)]
pub struct Caseless<T>(pub T);

impl Caseless<&str> {
/// Get the byte-representation of this case-insensitive value
#[inline(always)]
pub fn as_bytes(&self) -> Caseless<&[u8]> {
Caseless(self.0.as_bytes())
}
}

/// Recognizes the string `"\r\n"`.
///
/// *Complete version*: Will return an error if there's not enough input data.
Expand Down
48 changes: 11 additions & 37 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1593,27 +1593,15 @@ pub trait Compare<T> {
fn compare_no_case(&self, t: T) -> CompareResult;
}

fn lowercase_byte(c: u8) -> u8 {
match c {
b'A'..=b'Z' => c - b'A' + b'a',
_ => c,
}
}

impl<'a, 'b> Compare<&'b [u8]> for &'a [u8] {
#[inline]
fn compare(&self, t: &'b [u8]) -> CompareResult {
let pos = self.iter().zip(t.iter()).position(|(a, b)| a != b);

match pos {
Some(_) => CompareResult::Error,
None => {
if self.len() >= t.len() {
CompareResult::Ok
} else {
CompareResult::Incomplete
}
}
if t.iter().zip(*self).any(|(a, b)| a != b) {
CompareResult::Error
} else if self.len() < t.slice_len() {
CompareResult::Incomplete
} else {
CompareResult::Ok
}
}

Expand All @@ -1627,13 +1615,13 @@ impl<'a, 'b> Compare<&'b [u8]> for &'a [u8] {
impl<'a, 'b> Compare<AsciiCaseless<&'b [u8]>> for &'a [u8] {
#[inline]
fn compare(&self, t: AsciiCaseless<&'b [u8]>) -> CompareResult {
if self
if t.0
.iter()
.zip(t.0)
.any(|(a, b)| lowercase_byte(*a) != lowercase_byte(*b))
.zip(*self)
.any(|(a, b)| !a.eq_ignore_ascii_case(b))
{
CompareResult::Error
} else if self.len() < t.0.len() {
} else if self.len() < t.slice_len() {
CompareResult::Incomplete
} else {
CompareResult::Ok
Expand Down Expand Up @@ -1739,21 +1727,7 @@ impl<'a, 'b> Compare<&'b str> for &'a str {
impl<'a, 'b> Compare<AsciiCaseless<&'b str>> for &'a str {
#[inline(always)]
fn compare(&self, t: AsciiCaseless<&'b str>) -> CompareResult {
let pos = self
.chars()
.zip(t.0.chars())
.position(|(a, b)| a.to_lowercase().ne(b.to_lowercase()));

match pos {
Some(_) => CompareResult::Error,
None => {
if self.len() >= t.0.len() {
CompareResult::Ok
} else {
CompareResult::Incomplete
}
}
}
self.as_bytes().compare(t.as_bytes())
}

#[inline(always)]
Expand Down

0 comments on commit 7014156

Please sign in to comment.