Skip to content

Commit 8456f11

Browse files
ember-rosezbraniecki
authored andcommitted
Use std AsciiExt functions for Rust 1.24 (#4) (#5)
1 parent e4f6d79 commit 8456f11

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

src/locale/parser.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ use std::error::Error as ErrorTrait;
44
use super::Locale;
55
use super::options;
66

7-
fn is_ascii_alphabetic(s: &str) -> bool {
8-
s.chars()
9-
.all(|x| x >= 'A' && x <= 'Z' || x >= 'a' && x <= 'z')
10-
}
11-
12-
fn is_ascii_digit(s: &str) -> bool {
13-
s.chars().all(|x| x >= '0' && x <= '9')
14-
}
15-
167
pub type Result<T> = ::std::result::Result<T, Error>;
178

189
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -71,15 +62,15 @@ pub fn ext_key_for_name(key: &str) -> &str {
7162
}
7263

7364
pub fn parse_language_subtag(t: &str) -> Result<String> {
74-
if t.len() < 2 || t.len() > 3 || !is_ascii_alphabetic(t) {
65+
if t.len() < 2 || t.len() > 3 || t.chars().any(|c| !c.is_ascii_alphabetic()) {
7566
return Err(Error::InvalidLanguage);
7667
}
7768

7869
Ok(t.to_ascii_lowercase())
7970
}
8071

8172
pub fn parse_script_subtag(t: &str) -> Result<String> {
82-
if t.len() != 4 || !is_ascii_alphabetic(t) {
73+
if t.len() != 4 || t.chars().any(|c| !c.is_ascii_alphabetic()) {
8374
return Err(Error::InvalidSubtag);
8475
}
8576

@@ -90,7 +81,9 @@ pub fn parse_script_subtag(t: &str) -> Result<String> {
9081
}
9182

9283
pub fn parse_region_subtag(t: &str) -> Result<String> {
93-
if (t.len() == 2 && is_ascii_alphabetic(t)) || (t.len() == 3 && is_ascii_digit(t)) {
84+
if (t.len() == 2 && t.chars().all(|c| c.is_ascii_alphabetic()))
85+
|| (t.len() == 3 && t.chars().all(|c| c.is_ascii_digit()))
86+
{
9487
return Ok(t.to_ascii_uppercase());
9588
}
9689
Err(Error::InvalidSubtag)
@@ -145,7 +138,7 @@ pub fn parse_language_tag(t: &str) -> Result<Locale> {
145138
},
146139
None => {
147140
if slen == 1 {
148-
if !is_ascii_alphabetic(subtag) {
141+
if subtag.chars().any(|c| !c.is_ascii_alphabetic()) {
149142
return Err(Error::InvalidSubtag);
150143
}
151144
let ext_name = ext_name_for_key(subtag);
@@ -175,26 +168,30 @@ pub fn parse_language_tag(t: &str) -> Result<Locale> {
175168
} else {
176169
position = 2;
177170
}
178-
} else if position == 1 && slen == 3 && is_ascii_alphabetic(subtag) {
171+
} else if position == 1 && slen == 3
172+
&& subtag.chars().all(|c| c.is_ascii_alphabetic())
173+
{
179174
// extlangs
180175
if let Some(ref mut extlangs) = locale.extlangs {
181176
extlangs.push(subtag.to_owned());
182177
} else {
183178
locale.extlangs = Some(vec![subtag.to_owned()]);
184179
}
185-
} else if position <= 2 && slen == 4 && is_ascii_alphabetic(subtag) {
180+
} else if position <= 2 && slen == 4
181+
&& subtag.chars().all(|c| c.is_ascii_alphabetic())
182+
{
186183
// Script
187184
locale.set_script(subtag)?;
188185
position = 3;
189186
} else if position <= 3
190-
&& (slen == 2 && is_ascii_alphabetic(subtag)
191-
|| slen == 3 && is_ascii_digit(subtag))
187+
&& (slen == 2 && subtag.chars().all(|c| c.is_ascii_alphabetic())
188+
|| slen == 3 && subtag.chars().all(|c| c.is_ascii_digit()))
192189
{
193190
locale.set_region(subtag)?;
194191
position = 4;
195192
} else if position <= 4
196-
&& (slen >= 5 && is_ascii_alphabetic(&subtag[0..1])
197-
|| slen >= 4 && is_ascii_digit(&subtag[0..1]))
193+
&& (slen >= 5 && subtag[0..1].chars().all(|c| c.is_ascii_alphabetic())
194+
|| slen >= 4 && subtag[0..1].chars().all(|c| c.is_ascii_digit()))
198195
{
199196
// Variant
200197
if let Some(ref mut variants) = locale.variants {

0 commit comments

Comments
 (0)