Skip to content

Commit

Permalink
Add validate_char to Value and use it in parser
Browse files Browse the repository at this point in the history
  • Loading branch information
SRv6d committed Sep 28, 2024
1 parent 97ea70c commit 929ed47
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,14 @@ impl<'a> Value<'a> {
}

fn validate(value: &str) -> Result<(), InvalidValueError> {
if value.chars().any(|c| !matches!(c, '\u{0000}'..='\u{00FF}')) {
value.chars().try_for_each(Self::validate_char)
}

#[inline]
pub(crate) fn validate_char(c: char) -> Result<(), InvalidValueError> {
if !is_extended_ascii(c) {
return Err(InvalidValueError::NonExtendedAscii);
} else if value.chars().any(|c| c.is_ascii_control()) {
} else if c.is_ascii_control() {
return Err(InvalidValueError::ContainsControlChar);
}

Expand Down Expand Up @@ -413,6 +418,12 @@ where
}
}

/// Checks if the given char is part of the extended ASCII set.
#[inline]
fn is_extended_ascii(char: char) -> bool {
matches!(char, '\u{0000}'..='\u{00FF}')
}

#[cfg(test)]
mod tests {
use proptest::prelude::*;
Expand Down
7 changes: 2 additions & 5 deletions src/parser/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use winnow::{
PResult, Parser,
};

use crate::Attribute;
use crate::{Attribute, Value};

// A response code or message sent by the whois server.
// Starts with the "%" character and extends until the end of the line.
Expand Down Expand Up @@ -56,10 +56,7 @@ pub fn attribute_name<'s>(input: &mut &'s str) -> PResult<&'s str> {

// An extended ASCII sequence of characters, excluding control.
pub fn attribute_value<'s>(input: &mut &'s str) -> PResult<&'s str> {
take_while(0.., |c: char| {
matches!(c, '\u{0000}'..='\u{00FF}') && !c.is_ascii_control()
})
.parse_next(input)
take_while(0.., |c| Value::validate_char(c).is_ok()).parse_next(input)
}

// Extends an attributes value over multiple lines.
Expand Down

0 comments on commit 929ed47

Please sign in to comment.