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

feat: Implement uloc_openAvailableByType() #295

Merged
merged 6 commits into from
Nov 13, 2023

Conversation

clydegerber
Copy link
Contributor

@clydegerber clydegerber commented Oct 31, 2023

  • Change the signature of ULoc::get_available() to more closely match the ICU4C uloc_getAvailable() function signature.
  • Add the function ULoc::get_available_locales() to return an (uncached) collection of available locales.
  • Add the function ULoc::get_available_locales_by_type() to return a collection of available locales for the specified type.
  • Correct some configuration tags that used the plural 'features' incorrectly and were not functioning correctly as a result.

}

/// Returns a vector of locales of the requested type.
pub fn get_available_by_type(locale_type: ULocAvailableType) -> &'static Vec<ULoc> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that more of this functionality will need to be pushed down to ICU.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you okay with adding the static vectors to this library? The underlying assumption is that the ICU library is loaded once so that it is safe to cache these static results. If dynamically opening and closing the ICU library underneath this library is required then they may not be a good idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, assuming static data is not going to work. ICU data can be loaded and unloaded.

In other places we've used this was to make it into iterator using Enumerate, but it seems that this particular API doesn't follow that pattern.

It might be OK to recompute the vector on every call, and have the user cache the results instead if they need to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the caching and recomputing on every call - have a look.

@clydegerber clydegerber changed the title Implement uloc_openAvailableByType and proide an API that returns a c… Implement uloc_openAvailableByType and provide an API that returns a c… Nov 1, 2023
@clydegerber
Copy link
Contributor Author

Looks file test failures against the 6.3 ICU version were due to the uloc_openAvailableByType() API not being available until 6.5. I've attempted to add a feature for the 6.5 version and make the new API's dependent on that feature. I would appreciate a thorough review of what I've done, as I simply followed the examples for the other version features. In the course of this I noticed an issue in my previous commits that I'm correcting as well.

@filmil
Copy link
Member

filmil commented Nov 3, 2023 via email

@clydegerber clydegerber changed the title Implement uloc_openAvailableByType and provide an API that returns a c… feat: Implement uloc_openAvailableByType() Nov 8, 2023
}

/// Implements `uloc_openAvailableByType`.
#[cfg(feature = "icu_version_65_plus")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we no longer support v65 anyways, you can use icu_version_67_plus as well, without the need to introduce a new (onerous) feature.

})
pub fn get_available(n: i32) -> Result<ULoc, common::Error> {
if (0 > n) || (n >= Self::count_available()) {
panic!("n is negative or greater than or equal to the value returned from count_available()");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably also print the value of n, to allow for self-evident error message here.

let mut vec = Vec::with_capacity(count as usize);
let mut index: i32 = 0;
while index < count {
let label = unsafe { ffi::CStr::from_ptr(versioned_function!(uloc_getAvailable)(index)).to_str().unwrap() };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to check for the ptr value here - sorry I missed this on the first go.

if (0 > n) || (n >= Self::count_available()) {
panic!("n is negative or greater than or equal to the value returned from count_available()");
}
let label = unsafe { ffi::CStr::from_ptr(versioned_function!(uloc_getAvailable)(n)).to_str().unwrap() };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to check for the ptr value here - sorry I missed this on the first go.

@filmil
Copy link
Member

filmil commented Nov 10, 2023

Sorry - I had these comments pending for a while but didn't send them out.

…collection of locales for the speficied type.
…le_by_type() API for that feature only since uloc_getAvailableByType() is not stable prior to 6.5. Correct some feature configurations which incorrectly used the plural 'features' and were thus not operating as desired.
- Add a new feature for ICU4C version 6.5 and above in which the new implementation will be available.
- Change the signature of ULoc::get_available() to more closely match the ICU4C uloc_getAvailable() function signature.
- Add the function ULoc::get_available_locales() to return an (uncached) collection of available locales.
- Add the function ULoc::get_available_locales_by_type() to return a collection of available locales for the specified type.
- Correct some configuration tags that used the plural 'features' incorrectly and were not functioning correctly as a result.
- Remove icu_version_65_plus feature.
- Use icu_version_67_plus for determining support for uloc_getAvailableByType.
- Ouput the invalid index value supplied to get_available()
- Check for null pointer returned from uloc_getAvailable()
- Change the signature of ULoc::get_available() to more closely match the ICU4C uloc_getAvailable() function signature.
- Add the function ULoc::get_available_locales() to return an (uncached) collection of available locales.
- Add the function ULoc::get_available_locales_by_type() to return a collection of available locales for the specified type.
- Correct some configuration tags that used the plural 'features' incorrectly and were not functioning correctly as a result.
@filmil filmil merged commit 1858980 into google:main Nov 13, 2023
@clydegerber clydegerber deleted the available_by_type branch December 4, 2023 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants