@@ -126,7 +126,7 @@ fn is_prohibited_bidirectional_text(s: &str) -> bool {
126
126
pub fn nameprep ( s : & str ) -> Result < Cow < ' _ , str > , Error > {
127
127
// fast path for ascii text
128
128
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 == '-' )
130
130
{
131
131
return Ok ( Cow :: Borrowed ( s) ) ;
132
132
}
@@ -179,12 +179,10 @@ pub fn nameprep(s: &str) -> Result<Cow<'_, str>, Error> {
179
179
///
180
180
/// [RFC 3920, Appendix A]: https://tools.ietf.org/html/rfc3920#appendix-A
181
181
pub fn nodeprep ( s : & str ) -> Result < Cow < ' _ , str > , Error > {
182
- // fast path for ascii text
183
- if s. chars ( ) . all ( |c| {
184
- c. is_ascii_lowercase ( )
185
- && !tables:: ascii_control_character ( c)
186
- && !prohibited_node_character ( c)
187
- } ) {
182
+ // fast path for common ascii text
183
+ if s. chars ( )
184
+ . all ( |c| matches ! ( c, '[' ..='~' | '0' ..='9' | '(' ..='.' | '#' ..='%' ) )
185
+ {
188
186
return Ok ( Cow :: Borrowed ( s) ) ;
189
187
}
190
188
@@ -248,7 +246,7 @@ fn prohibited_node_character(c: char) -> bool {
248
246
pub fn resourceprep ( s : & str ) -> Result < Cow < ' _ , str > , Error > {
249
247
// fast path for ascii text
250
248
if s. chars ( )
251
- . all ( |c| c . is_ascii ( ) && !tables :: ascii_control_character ( c ) )
249
+ . all ( |c| matches ! ( c , ' ' ..= '~' ) )
252
250
{
253
251
return Ok ( Cow :: Borrowed ( s) ) ;
254
252
}
@@ -323,4 +321,17 @@ mod test {
323
321
fn resourceprep_examples ( ) {
324
322
assert_eq ! ( "foo@bar" , resourceprep( "foo@bar" ) . unwrap( ) ) ;
325
323
}
324
+
325
+ #[ test]
326
+ fn ascii_optimisations ( ) {
327
+ if let Cow :: Owned ( _) = nodeprep ( "nodepart" ) . unwrap ( ) {
328
+ panic ! ( "“nodepart” should get optimised as ASCII" ) ;
329
+ }
330
+ if let Cow :: Owned ( _) = nameprep ( "domainpart.example" ) . unwrap ( ) {
331
+ panic ! ( "“domainpart.example” should get optimised as ASCII" ) ;
332
+ }
333
+ if let Cow :: Owned ( _) = resourceprep ( "resourcepart" ) . unwrap ( ) {
334
+ panic ! ( "“resourcepart” should get optimised as ASCII" ) ;
335
+ }
336
+ }
326
337
}
0 commit comments