Bug 1675069 - Use nsDependentCSubstring rather than ptr+length in unic-langid-ffi. r=zbraniecki

Turns out we have `nsCStr` just for this already. I wrote this to
confirm it works as expected and it does, so...

The code doesn't deal with raw pointers anymore so we can remove the
helpers module and so on.

Differential Revision: https://phabricator.services.mozilla.com/D95866
This commit is contained in:
Emilio Cobos Álvarez 2020-11-04 16:17:30 +00:00
parent 0ff315007b
commit d3853cf882
3 changed files with 20 additions and 53 deletions

View File

@ -22,21 +22,21 @@ const nsCString Locale::AsString() const {
}
const nsDependentCSubstring Locale::GetLanguage() const {
uint32_t len;
const uint8_t* chars = unic_langid_get_language(mRaw.get(), &len);
return nsDependentCSubstring(reinterpret_cast<const char*>(chars), len);
nsDependentCSubstring sub;
unic_langid_get_language(mRaw.get(), &sub);
return sub;
}
const nsDependentCSubstring Locale::GetScript() const {
uint32_t len;
const uint8_t* chars = unic_langid_get_script(mRaw.get(), &len);
return nsDependentCSubstring(reinterpret_cast<const char*>(chars), len);
nsDependentCSubstring sub;
unic_langid_get_script(mRaw.get(), &sub);
return sub;
}
const nsDependentCSubstring Locale::GetRegion() const {
uint32_t len;
const uint8_t* chars = unic_langid_get_region(mRaw.get(), &len);
return nsDependentCSubstring(reinterpret_cast<const char*>(chars), len);
nsDependentCSubstring sub;
unic_langid_get_region(mRaw.get(), &sub);
return sub;
}
void Locale::GetVariants(nsTArray<nsCString>& aRetVal) const {

View File

@ -19,3 +19,4 @@ include = ["unic-langid", "unic-langid-impl"]
[export.rename]
"ThinVec" = "nsTArray"
"nsCStr" = "nsDependentCSubstring"

View File

@ -2,32 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
use nsstring::nsACString;
use nsstring::nsCString;
use std::convert::TryInto;
use nsstring::{nsACString, nsCStr, nsCString};
use thin_vec::ThinVec;
pub use unic_langid::{subtags, CharacterDirection, LanguageIdentifier, LanguageIdentifierError};
mod helpers {
use unic_langid::LanguageIdentifier;
// Notice, we use `as_ref` here because `Script` is `Copy`
// and otherwise we'll end up with a copy which will then
// get lost on the FFI boundary.
//
// We use this helper function to ensure that lifetimes are
// maintained and we get a compiler error if we accidentally
// `Copy`.
pub(super) fn get_script_str(langid: &LanguageIdentifier) -> &str {
langid.script.as_ref().map(|s| s.as_str()).unwrap_or("")
}
pub(super) fn get_region_str(langid: &LanguageIdentifier) -> &str {
langid.region.as_ref().map(|s| s.as_str()).unwrap_or("")
}
}
pub fn new_langid_for_mozilla(
name: &nsACString,
) -> Result<LanguageIdentifier, LanguageIdentifierError> {
@ -76,13 +54,11 @@ pub extern "C" fn unic_langid_as_string(langid: &mut LanguageIdentifier, ret_val
}
#[no_mangle]
pub extern "C" fn unic_langid_get_language(
langid: &LanguageIdentifier,
len: &mut u32,
) -> *const u8 {
let lang = langid.language.as_str();
*len = lang.len() as u32;
lang.as_bytes().as_ptr()
pub extern "C" fn unic_langid_get_language<'a>(
langid: &'a LanguageIdentifier,
out: &mut nsCStr<'a>,
) {
*out = nsCStr::from(langid.language.as_str());
}
#[no_mangle]
@ -101,13 +77,8 @@ pub extern "C" fn unic_langid_clear_language(langid: &mut LanguageIdentifier) {
}
#[no_mangle]
pub extern "C" fn unic_langid_get_script(langid: &LanguageIdentifier, len: &mut u32) -> *const u8 {
let script: &str = helpers::get_script_str(langid);
*len = script
.len()
.try_into()
.expect("string should be at most 4 chars");
script.as_ptr()
pub extern "C" fn unic_langid_get_script<'a>(langid: &'a LanguageIdentifier, out: &mut nsCStr<'a>) {
*out = nsCStr::from(langid.script.as_ref().map_or("", |s| s.as_str()));
}
#[no_mangle]
@ -126,13 +97,8 @@ pub extern "C" fn unic_langid_clear_script(langid: &mut LanguageIdentifier) {
}
#[no_mangle]
pub extern "C" fn unic_langid_get_region(langid: &LanguageIdentifier, len: &mut u32) -> *const u8 {
let region: &str = helpers::get_region_str(langid);
*len = region
.len()
.try_into()
.expect("string should be at most 4 chars");
region.as_ptr()
pub extern "C" fn unic_langid_get_region<'a>(langid: &'a LanguageIdentifier, out: &mut nsCStr<'a>) {
*out = nsCStr::from(langid.region.as_ref().map_or("", |s| s.as_str()));
}
#[no_mangle]