Bug 1358275 - Preliminary cleanup: refactor nsTextFragment::UpdateBidiFlag(), moving its logic into nsBidiUtils function HasRTLChars() and making that function more precise in the process. r=dholbert

This commit is contained in:
Jonathan Kew 2017-04-26 23:25:48 +01:00
Родитель df1af0cf56
Коммит 26f06fc0de
3 изменённых файлов: 22 добавлений и 26 удалений

Просмотреть файл

@ -450,21 +450,8 @@ void
nsTextFragment::UpdateBidiFlag(const char16_t* aBuffer, uint32_t aLength) nsTextFragment::UpdateBidiFlag(const char16_t* aBuffer, uint32_t aLength)
{ {
if (mState.mIs2b && !mState.mIsBidi) { if (mState.mIs2b && !mState.mIsBidi) {
const char16_t* cp = aBuffer; if (HasRTLChars(aBuffer, aLength)) {
const char16_t* end = cp + aLength; mState.mIsBidi = true;
while (cp < end) {
char16_t ch1 = *cp++;
uint32_t utf32Char = ch1;
if (NS_IS_HIGH_SURROGATE(ch1) &&
cp < end &&
NS_IS_LOW_SURROGATE(*cp)) {
char16_t ch2 = *cp++;
utf32Char = SURROGATE_TO_UCS4(ch1, ch2);
}
if (UTF32_CHAR_IS_BIDI(utf32Char) || IsBidiControlRTL(utf32Char)) {
mState.mIsBidi = true;
break;
}
} }
} }
} }

Просмотреть файл

@ -82,16 +82,18 @@ nsresult HandleNumbers(char16_t* aBuffer, uint32_t aSize, uint32_t aNumFlag)
return NS_OK; return NS_OK;
} }
bool HasRTLChars(const nsAString& aString) bool HasRTLChars(const char16_t* aText, uint32_t aLength)
{ {
// This is used to determine whether to enable bidi if a string has // This is used to determine whether a string has right-to-left characters
// right-to-left characters. To simplify things, anything that could be a // that mean it will require bidi processing.
// surrogate or RTL presentation form is covered just by testing >= 0xD800). const char16_t* cp = aText;
// It's fine to enable bidi in rare cases where it actually isn't needed. const char16_t* end = cp + aLength;
int32_t length = aString.Length(); while (cp < end) {
for (int32_t i = 0; i < length; i++) { uint32_t ch = *cp++;
char16_t ch = aString.CharAt(i); if (NS_IS_HIGH_SURROGATE(ch) && cp < end && NS_IS_LOW_SURROGATE(*cp)) {
if (ch >= 0xD800 || IS_IN_BMP_RTL_BLOCK(ch)) { ch = SURROGATE_TO_UCS4(ch, *cp++);
}
if (UTF32_CHAR_IS_BIDI(ch) || IsBidiControlRTL(ch)) {
return true; return true;
} }
} }

Просмотреть файл

@ -147,10 +147,17 @@ typedef enum nsCharType nsCharType;
} }
/** /**
* Give an nsString. * Give a 16-bit (UTF-16) text buffer and length
* @return true if the string contains right-to-left characters * @return true if the string contains right-to-left characters
*/ */
bool HasRTLChars(const nsAString& aString); 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());
}
// These values are shared with Preferences dialog // These values are shared with Preferences dialog
// ------------------ // ------------------