diff --git a/intl/encoding_glue/src/lib.rs b/intl/encoding_glue/src/lib.rs index 508d71550e2b..a2d2a721b841 100644 --- a/intl/encoding_glue/src/lib.rs +++ b/intl/encoding_glue/src/lib.rs @@ -28,11 +28,9 @@ use std::slice; macro_rules! try_start_bulk_write { ($needed:expr, $dst:ident, - $ret:expr) => ({ + $ret:expr) => {{ let needed = match $needed { - Some(needed) => { - needed - } + Some(needed) => needed, None => { return $ret; } @@ -40,48 +38,61 @@ macro_rules! try_start_bulk_write { match unsafe { $dst.bulk_write(needed, 0, false) } { Err(_) => { return $ret; - }, - Ok(handle) => { - handle } + Ok(handle) => handle, } - }) + }}; } #[no_mangle] -pub unsafe extern "C" fn mozilla_encoding_decode_to_nsstring(encoding: *mut *const Encoding, - src: *const u8, - src_len: usize, - dst: *mut nsAString) - -> nsresult { +pub unsafe extern "C" fn mozilla_encoding_decode_to_nsstring( + encoding: *mut *const Encoding, + src: *const u8, + src_len: usize, + dst: *mut nsAString, +) -> nsresult { let (rv, enc) = decode_to_nsstring(&**encoding, slice::from_raw_parts(src, src_len), &mut *dst); *encoding = enc as *const Encoding; rv } -pub fn decode_to_nsstring(encoding: &'static Encoding, - src: &[u8], - dst: &mut nsAString) - -> (nsresult, &'static Encoding) { +pub fn decode_to_nsstring( + encoding: &'static Encoding, + src: &[u8], + dst: &mut nsAString, +) -> (nsresult, &'static Encoding) { if let Some((enc, bom_length)) = Encoding::for_bom(src) { - return (decode_to_nsstring_without_bom_handling(enc, &src[bom_length..], dst), enc); + return ( + decode_to_nsstring_without_bom_handling(enc, &src[bom_length..], dst), + enc, + ); } - (decode_to_nsstring_without_bom_handling(encoding, src, dst), encoding) + ( + decode_to_nsstring_without_bom_handling(encoding, src, dst), + encoding, + ) } #[no_mangle] -pub unsafe extern "C" fn mozilla_encoding_decode_to_nsstring_with_bom_removal(encoding: *const Encoding, src: *const u8, src_len: usize, dst: *mut nsAString) -> nsresult{ +pub unsafe extern "C" fn mozilla_encoding_decode_to_nsstring_with_bom_removal( + encoding: *const Encoding, + src: *const u8, + src_len: usize, + dst: *mut nsAString, +) -> nsresult { decode_to_nsstring_with_bom_removal(&*encoding, slice::from_raw_parts(src, src_len), &mut *dst) } -pub fn decode_to_nsstring_with_bom_removal(encoding: &'static Encoding, - src: &[u8], - dst: &mut nsAString) - -> nsresult { +pub fn decode_to_nsstring_with_bom_removal( + encoding: &'static Encoding, + src: &[u8], + dst: &mut nsAString, +) -> nsresult { let without_bom = if encoding == UTF_8 && src.starts_with(b"\xEF\xBB\xBF") { &src[3..] - } else if (encoding == UTF_16LE && src.starts_with(b"\xFF\xFE")) || - (encoding == UTF_16BE && src.starts_with(b"\xFE\xFF")) { + } else if (encoding == UTF_16LE && src.starts_with(b"\xFF\xFE")) + || (encoding == UTF_16BE && src.starts_with(b"\xFE\xFF")) + { &src[2..] } else { src @@ -90,21 +101,32 @@ pub fn decode_to_nsstring_with_bom_removal(encoding: &'static Encoding, } #[no_mangle] -pub unsafe extern "C" fn mozilla_encoding_decode_to_nsstring_without_bom_handling(encoding: *const Encoding, src: *const u8, src_len: usize, dst: *mut nsAString) -> nsresult{ - decode_to_nsstring_without_bom_handling(&*encoding, - slice::from_raw_parts(src, src_len), - &mut *dst) +pub unsafe extern "C" fn mozilla_encoding_decode_to_nsstring_without_bom_handling( + encoding: *const Encoding, + src: *const u8, + src_len: usize, + dst: *mut nsAString, +) -> nsresult { + decode_to_nsstring_without_bom_handling( + &*encoding, + slice::from_raw_parts(src, src_len), + &mut *dst, + ) } -pub fn decode_to_nsstring_without_bom_handling(encoding: &'static Encoding, - src: &[u8], - dst: &mut nsAString) - -> nsresult { +pub fn decode_to_nsstring_without_bom_handling( + encoding: &'static Encoding, + src: &[u8], + dst: &mut nsAString, +) -> nsresult { let mut decoder = encoding.new_decoder_without_bom_handling(); - let mut handle = try_start_bulk_write!(decoder.max_utf16_buffer_length(src.len()), - dst, - NS_ERROR_OUT_OF_MEMORY); - let (result, read, written, had_errors) = decoder.decode_to_utf16(src, handle.as_mut_slice(), true); + let mut handle = try_start_bulk_write!( + decoder.max_utf16_buffer_length(src.len()), + dst, + NS_ERROR_OUT_OF_MEMORY + ); + let (result, read, written, had_errors) = + decoder.decode_to_utf16(src, handle.as_mut_slice(), true); debug_assert_eq!(result, CoderResult::InputEmpty); debug_assert_eq!(read, src.len()); debug_assert!(written <= handle.as_mut_slice().len()); @@ -116,25 +138,32 @@ pub fn decode_to_nsstring_without_bom_handling(encoding: &'static Encoding, } #[no_mangle] -pub unsafe extern "C" fn mozilla_encoding_decode_to_nsstring_without_bom_handling_and_without_replacement - (encoding: *const Encoding, - src: *const u8, - src_len: usize, - dst: *mut nsAString) - -> nsresult { - decode_to_nsstring_without_bom_handling_and_without_replacement(&*encoding, - slice::from_raw_parts(src, - src_len), - &mut *dst) +pub unsafe extern "C" fn mozilla_encoding_decode_to_nsstring_without_bom_handling_and_without_replacement( + encoding: *const Encoding, + src: *const u8, + src_len: usize, + dst: *mut nsAString, +) -> nsresult { + decode_to_nsstring_without_bom_handling_and_without_replacement( + &*encoding, + slice::from_raw_parts(src, src_len), + &mut *dst, + ) } -pub fn decode_to_nsstring_without_bom_handling_and_without_replacement(encoding: &'static Encoding, src: &[u8], dst: &mut nsAString) -> nsresult{ +pub fn decode_to_nsstring_without_bom_handling_and_without_replacement( + encoding: &'static Encoding, + src: &[u8], + dst: &mut nsAString, +) -> nsresult { let mut decoder = encoding.new_decoder_without_bom_handling(); - let mut handle = try_start_bulk_write!(decoder.max_utf16_buffer_length(src.len()), - dst, - NS_ERROR_OUT_OF_MEMORY); - let (result, read, written) = decoder - .decode_to_utf16_without_replacement(src, handle.as_mut_slice(), true); + let mut handle = try_start_bulk_write!( + decoder.max_utf16_buffer_length(src.len()), + dst, + NS_ERROR_OUT_OF_MEMORY + ); + let (result, read, written) = + decoder.decode_to_utf16_without_replacement(src, handle.as_mut_slice(), true); match result { DecoderResult::InputEmpty => { debug_assert_eq!(read, src.len()); @@ -151,34 +180,39 @@ pub fn decode_to_nsstring_without_bom_handling_and_without_replacement(encoding: } #[no_mangle] -pub unsafe extern "C" fn mozilla_encoding_encode_from_utf16(encoding: *mut *const Encoding, - src: *const u16, - src_len: usize, - dst: *mut nsACString) - -> nsresult { +pub unsafe extern "C" fn mozilla_encoding_encode_from_utf16( + encoding: *mut *const Encoding, + src: *const u16, + src_len: usize, + dst: *mut nsACString, +) -> nsresult { let (rv, enc) = encode_from_utf16(&**encoding, slice::from_raw_parts(src, src_len), &mut *dst); *encoding = enc as *const Encoding; rv } -pub fn encode_from_utf16(encoding: &'static Encoding, - src: &[u16], - dst: &mut nsACString) - -> (nsresult, &'static Encoding) { +pub fn encode_from_utf16( + encoding: &'static Encoding, + src: &[u16], + dst: &mut nsACString, +) -> (nsresult, &'static Encoding) { let output_encoding = encoding.output_encoding(); let mut encoder = output_encoding.new_encoder(); - let mut handle = try_start_bulk_write!(encoder.max_buffer_length_from_utf16_if_no_unmappables(src.len()), - dst, - (NS_ERROR_OUT_OF_MEMORY, output_encoding)); + let mut handle = try_start_bulk_write!( + encoder.max_buffer_length_from_utf16_if_no_unmappables(src.len()), + dst, + (NS_ERROR_OUT_OF_MEMORY, output_encoding) + ); let mut total_read = 0; let mut total_written = 0; let mut total_had_errors = false; loop { - let (result, read, written, had_errors) = encoder - .encode_from_utf16(&src[total_read..], - &mut (handle.as_mut_slice())[total_written..], - true); + let (result, read, written, had_errors) = encoder.encode_from_utf16( + &src[total_read..], + &mut (handle.as_mut_slice())[total_written..], + true, + ); total_read += read; total_written += written; total_had_errors |= had_errors; @@ -193,11 +227,15 @@ pub fn encode_from_utf16(encoding: &'static Encoding, return (NS_OK, output_encoding); } CoderResult::OutputFull => { - if let Some(needed) = - checked_add(total_written, - encoder.max_buffer_length_from_utf16_if_no_unmappables(src.len() - - total_read)) { - if unsafe { handle.restart_bulk_write(needed, total_written, false).is_ok() } { + if let Some(needed) = checked_add( + total_written, + encoder.max_buffer_length_from_utf16_if_no_unmappables(src.len() - total_read), + ) { + if unsafe { + handle + .restart_bulk_write(needed, total_written, false) + .is_ok() + } { continue; } } @@ -208,44 +246,54 @@ pub fn encode_from_utf16(encoding: &'static Encoding, } #[no_mangle] -pub unsafe extern "C" fn mozilla_encoding_decode_to_nscstring(encoding: *mut *const Encoding, - src: *const nsACString, - dst: *mut nsACString) - -> nsresult { +pub unsafe extern "C" fn mozilla_encoding_decode_to_nscstring( + encoding: *mut *const Encoding, + src: *const nsACString, + dst: *mut nsACString, +) -> nsresult { debug_assert_ne!(src as usize, dst as usize); let (rv, enc) = decode_to_nscstring(&**encoding, &*src, &mut *dst); *encoding = enc as *const Encoding; rv } -pub fn decode_to_nscstring(encoding: &'static Encoding, - src: &nsACString, - dst: &mut nsACString) - -> (nsresult, &'static Encoding) { +pub fn decode_to_nscstring( + encoding: &'static Encoding, + src: &nsACString, + dst: &mut nsACString, +) -> (nsresult, &'static Encoding) { if let Some((enc, bom_length)) = Encoding::for_bom(src) { - return (decode_from_slice_to_nscstring_without_bom_handling(enc, - &src[bom_length..], - dst, - 0), - enc); + return ( + decode_from_slice_to_nscstring_without_bom_handling(enc, &src[bom_length..], dst, 0), + enc, + ); } - (decode_to_nscstring_without_bom_handling(encoding, src, dst), encoding) + ( + decode_to_nscstring_without_bom_handling(encoding, src, dst), + encoding, + ) } #[no_mangle] -pub unsafe extern "C" fn mozilla_encoding_decode_to_nscstring_with_bom_removal(encoding: *const Encoding, src: *const nsACString, dst: *mut nsACString) -> nsresult{ +pub unsafe extern "C" fn mozilla_encoding_decode_to_nscstring_with_bom_removal( + encoding: *const Encoding, + src: *const nsACString, + dst: *mut nsACString, +) -> nsresult { debug_assert_ne!(src as usize, dst as usize); decode_to_nscstring_with_bom_removal(&*encoding, &*src, &mut *dst) } -pub fn decode_to_nscstring_with_bom_removal(encoding: &'static Encoding, - src: &nsACString, - dst: &mut nsACString) - -> nsresult { +pub fn decode_to_nscstring_with_bom_removal( + encoding: &'static Encoding, + src: &nsACString, + dst: &mut nsACString, +) -> nsresult { let without_bom = if encoding == UTF_8 && src.starts_with(b"\xEF\xBB\xBF") { &src[3..] - } else if (encoding == UTF_16LE && src.starts_with(b"\xFF\xFE")) || - (encoding == UTF_16BE && src.starts_with(b"\xFE\xFF")) { + } else if (encoding == UTF_16LE && src.starts_with(b"\xFF\xFE")) + || (encoding == UTF_16BE && src.starts_with(b"\xFE\xFF")) + { &src[2..] } else { return decode_to_nscstring_without_bom_handling(encoding, src, dst); @@ -254,15 +302,20 @@ pub fn decode_to_nscstring_with_bom_removal(encoding: &'static Encoding, } #[no_mangle] -pub unsafe extern "C" fn mozilla_encoding_decode_to_nscstring_without_bom_handling(encoding: *const Encoding, src: *const nsACString, dst: *mut nsACString) -> nsresult{ +pub unsafe extern "C" fn mozilla_encoding_decode_to_nscstring_without_bom_handling( + encoding: *const Encoding, + src: *const nsACString, + dst: *mut nsACString, +) -> nsresult { debug_assert_ne!(src as usize, dst as usize); decode_to_nscstring_without_bom_handling(&*encoding, &*src, &mut *dst) } -pub fn decode_to_nscstring_without_bom_handling(encoding: &'static Encoding, - src: &nsACString, - dst: &mut nsACString) - -> nsresult { +pub fn decode_to_nscstring_without_bom_handling( + encoding: &'static Encoding, + src: &nsACString, + dst: &mut nsACString, +) -> nsresult { let bytes = &src[..]; let valid_up_to = if encoding == UTF_8 { Encoding::utf8_valid_up_to(bytes) @@ -283,32 +336,44 @@ pub fn decode_to_nscstring_without_bom_handling(encoding: &'static Encoding, } #[no_mangle] -pub unsafe extern "C" fn mozilla_encoding_decode_from_slice_to_nscstring_without_bom_handling(encoding: *const Encoding, src: *const u8, src_len: usize, dst: *mut nsACString, already_validated: usize) -> nsresult { - decode_from_slice_to_nscstring_without_bom_handling(&*encoding, slice::from_raw_parts(src, src_len), &mut *dst, already_validated) +pub unsafe extern "C" fn mozilla_encoding_decode_from_slice_to_nscstring_without_bom_handling( + encoding: *const Encoding, + src: *const u8, + src_len: usize, + dst: *mut nsACString, + already_validated: usize, +) -> nsresult { + decode_from_slice_to_nscstring_without_bom_handling( + &*encoding, + slice::from_raw_parts(src, src_len), + &mut *dst, + already_validated, + ) } -fn decode_from_slice_to_nscstring_without_bom_handling(encoding: &'static Encoding, - src: &[u8], - dst: &mut nsACString, - already_validated: usize) - -> nsresult { +fn decode_from_slice_to_nscstring_without_bom_handling( + encoding: &'static Encoding, + src: &[u8], + dst: &mut nsACString, + already_validated: usize, +) -> nsresult { let bytes = src; let mut decoder = encoding.new_decoder_without_bom_handling(); - let mut handle = try_start_bulk_write!(Some(src.len()), - dst, - NS_ERROR_OUT_OF_MEMORY); + let mut handle = try_start_bulk_write!(Some(src.len()), dst, NS_ERROR_OUT_OF_MEMORY); if already_validated != 0 { - &mut (handle.as_mut_slice())[..already_validated].copy_from_slice(&bytes[..already_validated]); + &mut (handle.as_mut_slice())[..already_validated] + .copy_from_slice(&bytes[..already_validated]); } let mut total_read = already_validated; let mut total_written = already_validated; let mut total_had_errors = false; loop { - let (result, read, written, had_errors) = - decoder.decode_to_utf8(&bytes[total_read..], - &mut (handle.as_mut_slice())[total_written..], - true); + let (result, read, written, had_errors) = decoder.decode_to_utf8( + &bytes[total_read..], + &mut (handle.as_mut_slice())[total_written..], + true, + ); total_read += read; total_written += written; total_had_errors |= had_errors; @@ -324,11 +389,15 @@ fn decode_from_slice_to_nscstring_without_bom_handling(encoding: &'static Encodi CoderResult::OutputFull => { // Allocate for the worst case. That is, we should come // here at most once per invocation of this method. - if let Some(needed) = - checked_add(total_written, - decoder.max_utf8_buffer_length(bytes.len() - - total_read)) { - if unsafe { handle.restart_bulk_write(needed, total_written, false).is_ok() } { + if let Some(needed) = checked_add( + total_written, + decoder.max_utf8_buffer_length(bytes.len() - total_read), + ) { + if unsafe { + handle + .restart_bulk_write(needed, total_written, false) + .is_ok() + } { continue; } } @@ -339,15 +408,19 @@ fn decode_from_slice_to_nscstring_without_bom_handling(encoding: &'static Encodi } #[no_mangle] -pub unsafe extern "C" fn mozilla_encoding_decode_to_nscstring_without_bom_handling_and_without_replacement - (encoding: *const Encoding, - src: *const nsACString, - dst: *mut nsACString) - -> nsresult { +pub unsafe extern "C" fn mozilla_encoding_decode_to_nscstring_without_bom_handling_and_without_replacement( + encoding: *const Encoding, + src: *const nsACString, + dst: *mut nsACString, +) -> nsresult { decode_to_nscstring_without_bom_handling_and_without_replacement(&*encoding, &*src, &mut *dst) } -pub fn decode_to_nscstring_without_bom_handling_and_without_replacement(encoding: &'static Encoding, src: &nsACString, dst: &mut nsACString) -> nsresult{ +pub fn decode_to_nscstring_without_bom_handling_and_without_replacement( + encoding: &'static Encoding, + src: &nsACString, + dst: &mut nsACString, +) -> nsresult { let bytes = &src[..]; if encoding == UTF_8 { let valid_up_to = Encoding::utf8_valid_up_to(bytes); @@ -373,16 +446,22 @@ pub fn decode_to_nscstring_without_bom_handling_and_without_replacement(encoding return NS_OK; } let mut decoder = encoding.new_decoder_without_bom_handling(); - let mut handle = try_start_bulk_write!(checked_add(valid_up_to, - decoder.max_utf8_buffer_length_without_replacement(bytes.len() - - valid_up_to)), - dst, - NS_ERROR_OUT_OF_MEMORY); + let mut handle = try_start_bulk_write!( + checked_add( + valid_up_to, + decoder.max_utf8_buffer_length_without_replacement(bytes.len() - valid_up_to) + ), + dst, + NS_ERROR_OUT_OF_MEMORY + ); let (result, read, written) = { let dest = handle.as_mut_slice(); dest[..valid_up_to].copy_from_slice(&bytes[..valid_up_to]); - decoder - .decode_to_utf8_without_replacement(&src[valid_up_to..], &mut dest[valid_up_to..], true) + decoder.decode_to_utf8_without_replacement( + &src[valid_up_to..], + &mut dest[valid_up_to..], + true, + ) }; match result { DecoderResult::InputEmpty => { @@ -400,19 +479,21 @@ pub fn decode_to_nscstring_without_bom_handling_and_without_replacement(encoding } #[no_mangle] -pub unsafe extern "C" fn mozilla_encoding_encode_from_nscstring(encoding: *mut *const Encoding, - src: *const nsACString, - dst: *mut nsACString) - -> nsresult { +pub unsafe extern "C" fn mozilla_encoding_encode_from_nscstring( + encoding: *mut *const Encoding, + src: *const nsACString, + dst: *mut nsACString, +) -> nsresult { let (rv, enc) = encode_from_nscstring(&**encoding, &*src, &mut *dst); *encoding = enc as *const Encoding; rv } -pub fn encode_from_nscstring(encoding: &'static Encoding, - src: &nsACString, - dst: &mut nsACString) - -> (nsresult, &'static Encoding) { +pub fn encode_from_nscstring( + encoding: &'static Encoding, + src: &nsACString, + dst: &mut nsACString, +) -> (nsresult, &'static Encoding) { let output_encoding = encoding.output_encoding(); let bytes = &src[..]; if output_encoding == UTF_8 { @@ -447,10 +528,14 @@ pub fn encode_from_nscstring(encoding: &'static Encoding, }; let mut encoder = output_encoding.new_encoder(); - let mut handle = try_start_bulk_write!(checked_add(valid_up_to, - encoder.max_buffer_length_from_utf8_if_no_unmappables(trail.len())), - dst, - (NS_ERROR_OUT_OF_MEMORY, output_encoding)); + let mut handle = try_start_bulk_write!( + checked_add( + valid_up_to, + encoder.max_buffer_length_from_utf8_if_no_unmappables(trail.len()) + ), + dst, + (NS_ERROR_OUT_OF_MEMORY, output_encoding) + ); if valid_up_to != 0 { // to_mut() shouldn't fail right after setting length. @@ -464,10 +549,11 @@ pub fn encode_from_nscstring(encoding: &'static Encoding, let mut total_written = valid_up_to; let mut total_had_errors = false; loop { - let (result, read, written, had_errors) = encoder - .encode_from_utf8(&trail[total_read..], - &mut (handle.as_mut_slice())[total_written..], - true); + let (result, read, written, had_errors) = encoder.encode_from_utf8( + &trail[total_read..], + &mut (handle.as_mut_slice())[total_written..], + true, + ); total_read += read; total_written += written; total_had_errors |= had_errors; @@ -482,12 +568,15 @@ pub fn encode_from_nscstring(encoding: &'static Encoding, return (NS_OK, output_encoding); } CoderResult::OutputFull => { - if let Some(needed) = - checked_add(total_written, - encoder - .max_buffer_length_from_utf8_if_no_unmappables(trail.len() - - total_read)) { - if unsafe { handle.restart_bulk_write(needed, total_written, false).is_ok() } { + if let Some(needed) = checked_add( + total_written, + encoder.max_buffer_length_from_utf8_if_no_unmappables(trail.len() - total_read), + ) { + if unsafe { + handle + .restart_bulk_write(needed, total_written, false) + .is_ok() + } { continue; } } diff --git a/intl/locale/rust/unic-langid-ffi/src/lib.rs b/intl/locale/rust/unic-langid-ffi/src/lib.rs index 1114bfdf6fca..e42b1413e135 100644 --- a/intl/locale/rust/unic-langid-ffi/src/lib.rs +++ b/intl/locale/rust/unic-langid-ffi/src/lib.rs @@ -5,9 +5,11 @@ use nsstring::nsACString; use nsstring::nsCString; use thin_vec::ThinVec; -pub use unic_langid::{LanguageIdentifier, LanguageIdentifierError, CharacterDirection}; +pub use unic_langid::{CharacterDirection, LanguageIdentifier, LanguageIdentifierError}; -pub fn new_langid_for_mozilla(name: &nsACString) -> Result { +pub fn new_langid_for_mozilla( + name: &nsACString, +) -> Result { if name.eq_ignore_ascii_case(b"ja-jp-mac") { "ja-JP-macos".parse() } else { @@ -31,7 +33,6 @@ pub unsafe extern "C" fn unic_langid_canonicalize(name: &mut nsACString) -> bool result } - #[no_mangle] pub unsafe extern "C" fn unic_langid_new( name: &nsACString, @@ -167,6 +168,6 @@ pub unsafe extern "C" fn unic_langid_maximize(langid: &mut LanguageIdentifier) - pub unsafe extern "C" fn unic_langid_is_rtl(name: &nsACString) -> bool { match new_langid_for_mozilla(name) { Ok(langid) => langid.character_direction() == CharacterDirection::RTL, - Err(_) => false + Err(_) => false, } }