From 86d0bdddb289c574c273c823f3e7c6be64202d57 Mon Sep 17 00:00:00 2001 From: gcarq Date: Mon, 15 Jun 2020 23:36:21 +0200 Subject: [PATCH] Replace manual ASCII checks with 'std::char' calls. --- core/http/src/parse/uri/parser.rs | 4 ++-- core/http/src/route.rs | 7 ++----- core/lib/src/config/toml_ext.rs | 5 +---- core/lib/src/request/param.rs | 2 +- examples/pastebin/src/paste_id.rs | 6 +----- 5 files changed, 7 insertions(+), 17 deletions(-) diff --git a/core/http/src/parse/uri/parser.rs b/core/http/src/parse/uri/parser.rs index 88a4e9ea..8a3852cd 100644 --- a/core/http/src/parse/uri/parser.rs +++ b/core/http/src/parse/uri/parser.rs @@ -64,7 +64,7 @@ fn port_from<'a>(input: &mut RawInput<'a>, bytes: &IndexedBytes<'a>) -> Result<' let source = Some(input.cow_source()); let string = bytes.from_cow_source(&source); for (&b, i) in string.iter().rev().zip(&[1, 10, 100, 1000, 10000]) { - if b < b'0' || b > b'9' { + if !b.is_ascii_digit() { return Err(pear_error!("port byte is out of range")); } @@ -80,7 +80,7 @@ fn port_from<'a>(input: &mut RawInput<'a>, bytes: &IndexedBytes<'a>) -> Result<' #[parser] fn port<'a>(input: &mut RawInput<'a>) -> Result<'a, u16> { - let port_str = take_n_while(5, |c| c >= b'0' && c <= b'9')?; + let port_str = take_n_while(5, |c| c.is_ascii_digit())?; port_from(&port_str)? } diff --git a/core/http/src/route.rs b/core/http/src/route.rs index b24c5584..f6362e3e 100644 --- a/core/http/src/route.rs +++ b/core/http/src/route.rs @@ -63,18 +63,15 @@ pub type SResult<'a, P> = Result, (&'a str, Error<'a>)>; #[inline] fn is_ident_start(c: char) -> bool { - ('a' <= c && c <= 'z') - || ('A' <= c && c <= 'Z') + c.is_ascii_alphabetic() || c == '_' || (c > '\x7f' && UnicodeXID::is_xid_start(c)) } #[inline] fn is_ident_continue(c: char) -> bool { - ('a' <= c && c <= 'z') - || ('A' <= c && c <= 'Z') + c.is_ascii_alphanumeric() || c == '_' - || ('0' <= c && c <= '9') || (c > '\x7f' && UnicodeXID::is_xid_continue(c)) } diff --git a/core/lib/src/config/toml_ext.rs b/core/lib/src/config/toml_ext.rs index 09bd4e68..c0ec75e1 100644 --- a/core/lib/src/config/toml_ext.rs +++ b/core/lib/src/config/toml_ext.rs @@ -23,10 +23,7 @@ fn is_not_separator(byte: char) -> bool { // FIXME: Be more permissive here? #[inline(always)] fn is_ident_char(byte: char) -> bool { - match byte { - '0'..='9' | 'A'..='Z' | 'a'..='z' | '_' | '-' => true, - _ => false - } + byte.is_ascii_alphanumeric() || byte == '_' || byte == '-' } #[parser] diff --git a/core/lib/src/request/param.rs b/core/lib/src/request/param.rs index bcbdd9a5..a1d3eaa6 100644 --- a/core/lib/src/request/param.rs +++ b/core/lib/src/request/param.rs @@ -154,7 +154,7 @@ use crate::http::{RawStr, uri::{Segments, SegmentError}}; /// _ => return Err(param) /// }; /// -/// if !key.chars().all(|c| (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { +/// if !key.chars().all(|c| c.is_ascii_alphabetic()) { /// return Err(param); /// } /// diff --git a/examples/pastebin/src/paste_id.rs b/examples/pastebin/src/paste_id.rs index 736155bd..c3ec2032 100644 --- a/examples/pastebin/src/paste_id.rs +++ b/examples/pastebin/src/paste_id.rs @@ -35,11 +35,7 @@ impl<'a> fmt::Display for PasteID<'a> { /// Returns `true` if `id` is a valid paste ID and `false` otherwise. fn valid_id(id: &str) -> bool { - id.chars().all(|c| { - (c >= 'a' && c <= 'z') - || (c >= 'A' && c <= 'Z') - || (c >= '0' && c <= '9') - }) + id.chars().all(|c| c.is_ascii_alphanumeric()) } /// Returns an instance of `PasteID` if the path segment is a valid ID.