Skip to content

Commit

Permalink
Pull the separator character into a function where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Aug 16, 2023
1 parent 93f0969 commit 241b563
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
23 changes: 19 additions & 4 deletions provider/core/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl Writeable for DataLocale {
self.keywords.write_to(sink)?;
}
if let Some(aux) = self.aux.as_ref() {
sink.write_char('$')?;
sink.write_char(AuxiliaryKey::separator() as char)?;
aux.write_to(sink)?;
}
Ok(())
Expand Down Expand Up @@ -216,7 +216,7 @@ impl From<&Locale> for DataLocale {
impl FromStr for DataLocale {
type Err = DataError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut split_iter = s.split('$');
let mut split_iter = s.split(AuxiliaryKey::separator() as char);
let (locale_str, aux_str) = match (split_iter.next(), split_iter.next(), split_iter.next())
{
(Some(locale_str), aux_str, None) => (locale_str, aux_str),
Expand Down Expand Up @@ -335,7 +335,7 @@ impl DataLocale {
/// }
/// ```
pub fn strict_cmp(&self, other: &[u8]) -> Ordering {
let mut pipe_iter = other.split(|b| *b == b'$');
let mut pipe_iter = other.split(|b| *b == AuxiliaryKey::separator());
let Some(locale_str) = pipe_iter.next() else {
debug_assert!(other.is_empty());
return Ordering::Greater;
Expand Down Expand Up @@ -706,7 +706,8 @@ impl DataLocale {
/// The "auxiliary key" is an annotation on [`DataLocale`] that can contain an arbitrary
/// information that does not fit into the [`LanguageIdentifier`] or [`Keywords`].
///
/// It is represented as a string separated from the BCP-47 ID with a `$`.
/// It is represented as a string separated from the BCP-47 ID with the character returned by
/// [`AuxiliaryKey::separator()`].
///
/// An auxiliary key currently allows alphanumerics and `-`.
///
Expand Down Expand Up @@ -814,6 +815,20 @@ impl AuxiliaryKey {
self.value.as_str().as_bytes()
}

/// Returns the separator byte used for auxiliary keys in data locales.
///
/// # Examples
///
/// ```
/// use icu_provider::AuxiliaryKey;
///
/// assert_eq!(AuxiliaryKey::separator(), b'$');
/// ```
#[inline]
pub const fn separator() -> u8 {
b'$'
}

fn validate_str(s: &str) -> bool {
!s.is_empty()
&& s.bytes()
Expand Down
2 changes: 1 addition & 1 deletion provider/datagen/src/baked_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ impl BakedExporter {
for (bake, locales) in values {
let first_locale = locales.iter().next().unwrap();
let anchor = syn::parse_str::<syn::Ident>(
&first_locale.to_ascii_uppercase().replace('-', "_").replace('$', "__"),
&first_locale.to_ascii_uppercase().replace('-', "_").replace(AuxiliaryKey::separator(), "__"),
)
.unwrap();
let bake = bake.parse::<TokenStream>().unwrap();
Expand Down

0 comments on commit 241b563

Please sign in to comment.