Skip to content

Commit

Permalink
Remove IanaToBcp47MapperBorrowed::get_bytes (#4398)
Browse files Browse the repository at this point in the history
Fixes #4368
  • Loading branch information
robertbastian authored Dec 1, 2023
1 parent e080ecd commit a9b01f0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
7 changes: 1 addition & 6 deletions components/timezone/src/iana_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,10 @@ impl<'a> IanaToBcp47MapperBorrowed<'a> {
///
/// [ECMAScript Temporal]: https://tc39.es/proposal-temporal/#sec-isavailabletimezonename
pub fn get(&self, iana_id: &str) -> Option<TimeZoneBcp47Id> {
self.get_bytes(iana_id.as_bytes())
}

#[doc(hidden)]
pub fn get_bytes(&self, iana_id: &[u8]) -> Option<TimeZoneBcp47Id> {
// The longest IANA name in CLDR appears to be "America/Argentina/ComodRivadavia"
// which is 32 characters long, so 48 should be plenty. Add a debug assertion
// just in case.
let name_for_lookup = match tinystr::TinyAsciiStr::<48>::from_bytes(iana_id) {
let name_for_lookup = match tinystr::TinyAsciiStr::<48>::from_bytes(iana_id.as_bytes()) {
Ok(tinystr) => tinystr.to_ascii_lowercase(),
Err(tinystr::TinyStrError::TooLarge { .. }) => {
debug_assert!(false, "IANA string too long for lookup");
Expand Down
16 changes: 9 additions & 7 deletions ffi/capi/src/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,15 @@ pub mod ffi {
mapper: &crate::iana_bcp47_mapper::ffi::ICU4XIanaToBcp47Mapper,
id: &DiplomatStr,
) -> Result<(), ICU4XError> {
match mapper.0.as_borrowed().get_bytes(id) {
Some(id) => {
self.0.time_zone_id = Some(id);
Ok(())
}
None => Err(ICU4XError::TimeZoneInvalidIdError),
}
let id = core::str::from_utf8(id).map_err(|_| ICU4XError::TimeZoneInvalidIdError)?;
self.0.time_zone_id = Some(
mapper
.0
.as_borrowed()
.get(id)
.ok_or(ICU4XError::TimeZoneInvalidIdError)?,
);
Ok(())
}

/// Clears the `time_zone_id` field.
Expand Down

0 comments on commit a9b01f0

Please sign in to comment.