Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beginning of "tinystr" optimization #8

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/locale/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use std::fmt;

mod options;
mod parser;
mod tinystr;

use self::tinystr::{TinyStr4, TinyStr8};

/// A Locale object.
///
Expand Down Expand Up @@ -76,10 +79,10 @@ mod parser;
/// ```
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Locale {
language: Option<String>,
language: Option<TinyStr4>,
extlangs: Option<Vec<String>>,
script: Option<String>,
region: Option<String>,
script: Option<TinyStr4>,
region: Option<TinyStr4>,
variants: Option<Vec<String>>,
extensions: Option<BTreeMap<String, BTreeMap<String, String>>>,
privateuse: Vec<String>,
Expand Down
10 changes: 6 additions & 4 deletions src/locale/options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Locale;
use super::{Locale, TinyStr4};
use std::collections::BTreeMap;

pub fn option_name_for_key(key: &str) -> &'static str {
Expand All @@ -20,9 +20,11 @@ pub fn option_key_for_name(key: &str) -> &'static str {
pub fn apply_options(loc: &mut Locale, opts: BTreeMap<&str, &str>) {
for (key, value) in opts {
match key {
"language" => loc.language = Some(value.to_owned()),
"script" => loc.script = Some(value.to_owned()),
"region" => loc.region = Some(value.to_owned()),
// TODO: should we do something other than store None on strings
// that fail representation?
"language" => loc.language = TinyStr4::new(value).ok(),
"script" => loc.script = TinyStr4::new(value).ok(),
"region" => loc.region = TinyStr4::new(value).ok(),

_ => if let Some(ref mut exts) = loc.extensions {
let uext = exts
Expand Down
41 changes: 26 additions & 15 deletions src/locale/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::options;
use super::TinyStr4;
use super::Locale;
use std::collections::BTreeMap;
use std::error::Error as ErrorTrait;
Expand Down Expand Up @@ -61,32 +62,42 @@ pub fn ext_key_for_name(key: &str) -> &str {
}
}

pub fn parse_language_subtag(t: &str) -> Result<String> {
if t.len() < 2 || t.len() > 3 || t.chars().any(|c| !c.is_ascii_alphabetic()) {
pub fn parse_language_subtag(t: &str) -> Result<TinyStr4> {
let s = TinyStr4::new(t).map_err(|_| Error::InvalidLanguage)?;
if t.len() < 2 || t.len() > 3 || !s.is_all_ascii_alpha() {
return Err(Error::InvalidLanguage);
}

Ok(t.to_ascii_lowercase())
Ok(s.to_ascii_lowercase())
}

pub fn parse_script_subtag(t: &str) -> Result<String> {
if t.len() != 4 || t.chars().any(|c| !c.is_ascii_alphabetic()) {
pub fn parse_script_subtag(t: &str) -> Result<TinyStr4> {
let s = TinyStr4::new(t).map_err(|_| Error::InvalidSubtag)?;
if t.len() != 4 || !s.is_all_ascii_alpha() {
return Err(Error::InvalidSubtag);
}

let mut s = t.to_ascii_lowercase();
s[0..1].make_ascii_uppercase();

Ok(s)
Ok(s.to_ascii_titlecase())
}

pub fn parse_region_subtag(t: &str) -> Result<String> {
if (t.len() == 2 && t.chars().all(|c| c.is_ascii_alphabetic()))
|| (t.len() == 3 && t.chars().all(|c| c.is_ascii_digit()))
{
return Ok(t.to_ascii_uppercase());
pub fn parse_region_subtag(t: &str) -> Result<TinyStr4> {
match t.len() {
2 => {
let s = TinyStr4::new(t).map_err(|_| Error::InvalidSubtag)?;
if !s.is_all_ascii_alpha() {
return Err(Error::InvalidSubtag);
}
Ok(s.to_ascii_uppercase())
}
3 => {
if !t.chars().all(|c| c.is_ascii_digit()) {
return Err(Error::InvalidSubtag);
}
// This actually can't fail.
TinyStr4::new(t).map_err(|_| Error::InvalidSubtag)
}
_ => Err(Error::InvalidSubtag),
}
Err(Error::InvalidSubtag)
}

pub fn parse_language_tag(t: &str) -> Result<Locale> {
Expand Down
Loading