-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25e9094
commit f3b0573
Showing
53 changed files
with
2,098 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
components/locale_core/src/extensions/unicode/subdivision.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// This file is part of ICU4X. For terms of use, please see the file | ||
// called LICENSE at the top level of the ICU4X source tree | ||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). | ||
|
||
use core::str::FromStr; | ||
|
||
use crate::parser::ParseError; | ||
use crate::subtags::Region; | ||
|
||
impl_tinystr_subtag!( | ||
/// A subdivision suffix used in [`SubdivisionId`]. | ||
/// | ||
/// This suffix represents a specific subdivision code under a given [`Region`]. | ||
/// For example the value of [`SubdivisionId`] may be `gbsct`, where the [`SubdivisionSuffix`] | ||
/// is `sct` for Scotland. | ||
/// | ||
/// Such a value associated with a key `rg` means that the locale should use Unit Preferences | ||
/// (default calendar, currency, week data, time cycle, measurement system) for Scotland, even if the | ||
/// [`LanguageIdentifier`](crate::LanguageIdentifier) is `en-US`. | ||
/// | ||
/// A subdivision suffix has to be a sequence of alphanumerical characters no | ||
/// shorter than one and no longer than four characters. | ||
/// | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use icu::locale::extensions::unicode::{subdivision_suffix, SubdivisionSuffix}; | ||
/// | ||
/// let ss: SubdivisionSuffix = | ||
/// "sct".parse().expect("Failed to parse a SubdivisionSuffix."); | ||
/// | ||
/// assert_eq!(ss, subdivision_suffix!("sct")); | ||
/// ``` | ||
SubdivisionSuffix, | ||
extensions::unicode, | ||
subdivision_suffix, | ||
extensions_unicode_subdivision_suffix, | ||
1..=4, | ||
s, | ||
s.is_ascii_alphanumeric(), | ||
s.to_ascii_lowercase(), | ||
s.is_ascii_alphanumeric() && s.is_ascii_lowercase(), | ||
InvalidExtension, | ||
["sct"], | ||
["toolooong"], | ||
); | ||
|
||
/// A Subivision Id as defined in [`Unicode Locale Identifier`]. | ||
/// | ||
/// Subdivision Id is used in [`Unicode`] extensions: | ||
/// * `rg` - Regional Override | ||
/// * `sd` - Regional Subdivision | ||
/// | ||
/// In both cases the subdivision is composed of a [`Region`] and a [`SubdivisionSuffix`] which represents | ||
/// different meaning depending on the key. | ||
/// | ||
/// [`Unicode Locale Identifier`]: https://unicode.org/reports/tr35/tr35.html#unicode_subdivision_id | ||
/// [`Unicode`]: crate::extensions::unicode::Unicode | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use icu::locale::{ | ||
/// subtags::region, | ||
/// extensions::unicode::{subdivision_suffix, SubdivisionId} | ||
/// }; | ||
/// | ||
/// let ss = subdivision_suffix!("zzzz"); | ||
/// let region = region!("gb"); | ||
/// | ||
/// let si = SubdivisionId::new(region, ss); | ||
/// | ||
/// assert_eq!(si.to_string(), "gbzzzz"); | ||
/// ``` | ||
#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord)] | ||
#[non_exhaustive] | ||
pub struct SubdivisionId { | ||
/// A region field of a Subdivision Id. | ||
pub region: Region, | ||
/// A subdivision suffix field of a Subdivision Id. | ||
pub suffix: SubdivisionSuffix, | ||
} | ||
|
||
impl SubdivisionId { | ||
/// Returns a new [`SubdivisionId`]. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use icu::locale::{ | ||
/// subtags::region, | ||
/// extensions::unicode::{subdivision_suffix, SubdivisionId} | ||
/// }; | ||
/// | ||
/// let ss = subdivision_suffix!("zzzz"); | ||
/// let region = region!("gb"); | ||
/// | ||
/// let si = SubdivisionId::new(region, ss); | ||
/// | ||
/// assert_eq!(si.to_string(), "gbzzzz"); | ||
/// ``` | ||
pub const fn new(region: Region, suffix: SubdivisionSuffix) -> Self { | ||
Self { region, suffix } | ||
} | ||
|
||
pub(crate) fn try_from_bytes(input: &[u8]) -> Result<Self, ParseError> { | ||
let is_alpha = input | ||
.first() | ||
.and_then(|b| { | ||
b.is_ascii_alphabetic() | ||
.then_some(true) | ||
.or_else(|| b.is_ascii_digit().then_some(false)) | ||
}) | ||
.ok_or(ParseError::InvalidExtension)?; | ||
let region_len = if is_alpha { 2 } else { 3 }; | ||
if input.len() < region_len + 1 { | ||
return Err(ParseError::InvalidExtension); | ||
} | ||
let (region_bytes, suffix_bytes) = input.split_at(region_len); | ||
let region = | ||
Region::try_from_bytes(region_bytes).map_err(|_| ParseError::InvalidExtension)?; | ||
let suffix = SubdivisionSuffix::try_from_bytes(suffix_bytes)?; | ||
Ok(Self { region, suffix }) | ||
} | ||
} | ||
|
||
impl writeable::Writeable for SubdivisionId { | ||
#[inline] | ||
fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result { | ||
sink.write_str(self.region.into_tinystr().to_ascii_lowercase().as_str())?; | ||
sink.write_str(self.suffix.as_str()) | ||
} | ||
|
||
#[inline] | ||
fn writeable_length_hint(&self) -> writeable::LengthHint { | ||
self.region.writeable_length_hint() + self.suffix.writeable_length_hint() | ||
} | ||
} | ||
|
||
writeable::impl_display_with_writeable!(SubdivisionId); | ||
|
||
impl FromStr for SubdivisionId { | ||
type Err = ParseError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
Self::try_from_bytes(s.as_bytes()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_subdivisionid_fromstr() { | ||
let si: SubdivisionId = "gbzzzz".parse().expect("Failed to parse SubdivisionId"); | ||
assert_eq!(si.region.to_string(), "GB"); | ||
assert_eq!(si.suffix.to_string(), "zzzz"); | ||
assert_eq!(si.to_string(), "gbzzzz"); | ||
|
||
for sample in ["", "gb", "o"] { | ||
let oe: Result<SubdivisionId, _> = sample.parse(); | ||
assert!(oe.is_err(), "Should fail: {}", sample); | ||
} | ||
} | ||
} |
Oops, something went wrong.