Skip to content

Commit

Permalink
HeaderName parse function implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdecimus committed Oct 21, 2022
1 parent fbd53cb commit 58b1bc8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
10 changes: 0 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,16 +509,6 @@ impl<'x> Hash for HeaderName<'x> {
impl Eq for HeaderName<'_> {}

impl<'x> HeaderName<'x> {
/// Parse a header name
pub fn parse(header_name: impl Into<Cow<'x, str>>) -> Self {
let header_name = header_name.into();
if let Some(rfc_header) = RfcHeader::parse(header_name.as_ref()) {
HeaderName::Rfc(rfc_header)
} else {
HeaderName::Other(header_name)
}
}

pub fn as_str(&self) -> &str {
match self {
HeaderName::Rfc(header) => header.as_str(),
Expand Down
40 changes: 26 additions & 14 deletions src/parsers/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* except according to those terms.
*/

use std::borrow::Cow;

use crate::{Header, HeaderName, RfcHeader};

use super::MessageStream;
Expand Down Expand Up @@ -166,25 +168,31 @@ impl<'x> MessageStream<'x> {
}
}

impl RfcHeader {
pub fn parse(data: &str) -> Option<RfcHeader> {
impl<'x> HeaderName<'x> {
/// Parse a header name
pub fn parse(data: impl Into<Cow<'x, str>>) -> Option<HeaderName<'x>> {
let mut token_hash: usize = 0;
let mut last_ch: u8 = 0;
let data = data.into();
let data_ = data.as_bytes();

for (pos, &ch) in data_.iter().enumerate() {
if let 0 | 9 = pos {
token_hash += {
#[cfg(feature = "ludicrous_mode")]
unsafe {
*HDR_HASH.get_unchecked(ch.to_ascii_lowercase() as usize)
}
if ch.is_ascii_alphanumeric() || [b'_', b'-'].contains(&ch) {
if let 0 | 9 = pos {
token_hash += {
#[cfg(feature = "ludicrous_mode")]
unsafe {
*HDR_HASH.get_unchecked(ch.to_ascii_lowercase() as usize)
}

#[cfg(not(feature = "ludicrous_mode"))]
HDR_HASH[ch.to_ascii_lowercase() as usize]
} as usize;
#[cfg(not(feature = "ludicrous_mode"))]
HDR_HASH[ch.to_ascii_lowercase() as usize]
} as usize;
}
last_ch = ch;
} else {
return None;
}
last_ch = ch;
}

if (2..=25).contains(&data.len()) {
Expand All @@ -202,12 +210,16 @@ impl RfcHeader {
let token_hash = token_hash - 4;

if data_.eq_ignore_ascii_case(HDR_NAMES[token_hash]) {
return HDR_MAP[token_hash].into();
return HeaderName::Rfc(HDR_MAP[token_hash]).into();
}
}
}

None
if !data.is_empty() {
HeaderName::Other(data).into()
} else {
None
}
}
}

Expand Down

0 comments on commit 58b1bc8

Please sign in to comment.