Bug 1431025 - Use encoding_rs::mem::is_utf16_bidi() as the implementation of HasRTLChars(). r=jfkthame

MozReview-Commit-ID: KnaI7wIndVf

--HG--
extra : rebase_source : 6a00c8fc140c1cf070e8fa668e7e099f98621820
This commit is contained in:
Henri Sivonen 2018-01-18 14:28:15 +02:00
parent 226dd89197
commit 5b05e804a9
6 changed files with 37 additions and 43 deletions

View File

@ -349,7 +349,7 @@ nsGenericDOMDataNode::SetTextInternal(uint32_t aOffset, uint32_t aCount,
if (aLength) {
to.Append(aBuffer, aLength);
if (!bidi && (!document || !document->GetBidiEnabled())) {
bidi = HasRTLChars(aBuffer, aLength);
bidi = HasRTLChars(MakeSpan(aBuffer, aLength));
}
}
if (endOffset != textLength) {

View File

@ -508,7 +508,7 @@ void
nsTextFragment::UpdateBidiFlag(const char16_t* aBuffer, uint32_t aLength)
{
if (mState.mIs2b && !mState.mIsBidi) {
if (HasRTLChars(aBuffer, aLength)) {
if (HasRTLChars(MakeSpan(aBuffer, aLength))) {
mState.mIsBidi = true;
}
}

View File

@ -583,3 +583,10 @@ fn checked_min(one: Option<usize>, other: Option<usize>) -> Option<usize> {
other
}
}
// Bindings for encoding_rs::mem. These may move to a separate crate in the future.
#[no_mangle]
pub unsafe extern "C" fn encoding_mem_is_utf16_bidi(buffer: *const u16, len: usize) -> bool {
encoding_rs::mem::is_utf16_bidi(::std::slice::from_raw_parts(buffer, len))
}

View File

@ -85,21 +85,3 @@ nsresult HandleNumbers(char16_t* aBuffer, uint32_t aSize, uint32_t aNumFlag)
}
return NS_OK;
}
bool HasRTLChars(const char16_t* aText, uint32_t aLength)
{
// This is used to determine whether a string has right-to-left characters
// that mean it will require bidi processing.
const char16_t* cp = aText;
const char16_t* end = cp + aLength;
while (cp < end) {
char16_t ch = *cp++;
if (ch < mozilla::kMinRTLChar) {
continue;
}
if (UTF16_CODE_UNIT_IS_BIDI(ch) || IsBidiControlRTL(ch)) {
return true;
}
}
return false;
}

View File

@ -8,6 +8,14 @@
#include "nsString.h"
extern "C" {
bool
encoding_mem_is_utf16_bidi(char16_t const* buffer,
size_t len);
}
/**
* Read ftp://ftp.unicode.org/Public/UNIDATA/ReadMe-Latest.txt
* section BIDIRECTIONAL PROPERTIES
@ -125,12 +133,12 @@ typedef enum nsCharType nsCharType;
#define PDI_CHAR 0x2069
#define ALM_CHAR 0x061C
inline bool IsBidiControl(uint32_t aChar) {
return ((LRE_CHAR <= aChar && aChar <= RLO_CHAR) ||
(LRI_CHAR <= aChar && aChar <= PDI_CHAR) ||
(aChar == ALM_CHAR) ||
(aChar & 0xfffffe) == LRM_CHAR);
}
inline bool IsBidiControl(uint32_t aChar) {
return ((LRE_CHAR <= aChar && aChar <= RLO_CHAR) ||
(LRI_CHAR <= aChar && aChar <= PDI_CHAR) ||
(aChar == ALM_CHAR) ||
(aChar & 0xfffffe) == LRM_CHAR);
}
/**
* Give a UTF-32 codepoint
@ -138,26 +146,23 @@ typedef enum nsCharType nsCharType;
* in RTL directionality and therefore needs to trigger bidi resolution;
* return false otherwise.
*/
inline bool IsBidiControlRTL(uint32_t aChar) {
return aChar == RLM_CHAR ||
aChar == RLE_CHAR ||
aChar == RLO_CHAR ||
aChar == RLI_CHAR ||
aChar == ALM_CHAR;
}
inline bool IsBidiControlRTL(uint32_t aChar) {
return aChar == RLM_CHAR ||
aChar == RLE_CHAR ||
aChar == RLO_CHAR ||
aChar == RLI_CHAR ||
aChar == ALM_CHAR;
}
/**
* Give a 16-bit (UTF-16) text buffer and length
* Give a 16-bit (UTF-16) text buffer
* @return true if the string contains right-to-left characters
*/
bool HasRTLChars(const char16_t* aText, uint32_t aLength);
/**
* Convenience function to call the above on an nsAString.
*/
inline bool HasRTLChars(const nsAString& aString) {
return HasRTLChars(aString.BeginReading(), aString.Length());
}
inline bool HasRTLChars(mozilla::Span<const char16_t> aBuffer) {
// Span ensures we never pass a nullptr to Rust--even if the
// length of the buffer is zero.
return encoding_mem_is_utf16_bidi(aBuffer.Elements(), aBuffer.Length());
}
// These values are shared with Preferences dialog
// ------------------

View File

@ -1333,7 +1333,7 @@ nsBidiPresUtils::ChildListMayRequireBidi(nsIFrame* aFirstChild,
if (content != *aCurrContent) {
*aCurrContent = content;
const nsTextFragment* txt = content->GetText();
if (txt->Is2b() && HasRTLChars(txt->Get2b(), txt->GetLength())) {
if (txt->Is2b() && HasRTLChars(MakeSpan(txt->Get2b(), txt->GetLength()))) {
return true;
}
}