tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

lib.rs (1400B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 use nsstring::nsACString;
      6 pub use unic_langid::{subtags, CharacterDirection, LanguageIdentifier, LanguageIdentifierError};
      7 
      8 pub fn new_langid_for_mozilla(
      9    name: &nsACString,
     10 ) -> Result<LanguageIdentifier, LanguageIdentifierError> {
     11    if name.eq_ignore_ascii_case(b"ja-jp-mac") {
     12        "ja-JP-macos".parse()
     13    } else {
     14        // Cut out any `.FOO` like `en-US.POSIX`.
     15        let mut name: &[u8] = name.as_ref();
     16        if let Some(ptr) = name.iter().position(|b| b == &b'.') {
     17            name = &name[..ptr];
     18        }
     19        LanguageIdentifier::from_bytes(name)
     20    }
     21 }
     22 
     23 #[no_mangle]
     24 pub extern "C" fn unic_langid_canonicalize(name: &mut nsACString) -> bool {
     25    let langid = new_langid_for_mozilla(name);
     26 
     27    let result = langid.is_ok();
     28 
     29    name.assign(&langid.unwrap_or_default().to_string());
     30 
     31    result
     32 }
     33 
     34 #[no_mangle]
     35 pub unsafe extern "C" fn unic_langid_destroy(langid: *mut LanguageIdentifier) {
     36    let _ = Box::from_raw(langid);
     37 }
     38 
     39 #[no_mangle]
     40 pub extern "C" fn unic_langid_is_rtl(name: &nsACString) -> bool {
     41    match new_langid_for_mozilla(name) {
     42        Ok(langid) => langid.character_direction() == CharacterDirection::RTL,
     43        Err(_) => false,
     44    }
     45 }