Skip to content

Commit 3ee4289

Browse files
committed
Fix the fast path for nameprep
char::is_ascii_lowercase() only returns true for alphabetical characters which are lowercase, so also add digits, '.' and '-' which are the only characters allowed in a non-IDN domain name. I have also added a test to not have that regress. This was found with this merge request in the jid crate: https://gitlab.com/xmpp-rs/xmpp-rs/-/merge_requests/205
1 parent 8887482 commit 3ee4289

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn is_prohibited_bidirectional_text(s: &str) -> bool {
126126
pub fn nameprep(s: &str) -> Result<Cow<'_, str>, Error> {
127127
// fast path for ascii text
128128
if s.chars()
129-
.all(|c| c.is_ascii_lowercase() && !tables::ascii_control_character(c))
129+
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '.' || c == '-')
130130
{
131131
return Ok(Cow::Borrowed(s));
132132
}
@@ -323,4 +323,17 @@ mod test {
323323
fn resourceprep_examples() {
324324
assert_eq!("foo@bar", resourceprep("foo@bar").unwrap());
325325
}
326+
327+
#[test]
328+
fn ascii_optimisations() {
329+
if let Cow::Owned(_) = nodeprep("nodepart").unwrap() {
330+
panic!("“nodepart” should get optimised as ASCII");
331+
}
332+
if let Cow::Owned(_) = nameprep("domainpart.example").unwrap() {
333+
panic!("“domainpart.example” should get optimised as ASCII");
334+
}
335+
if let Cow::Owned(_) = resourceprep("resourcepart").unwrap() {
336+
panic!("“resourcepart” should get optimised as ASCII");
337+
}
338+
}
326339
}

0 commit comments

Comments
 (0)