gecko-dev/intl/unicharutil/util/nsBidiUtils.cpp
Ehsan Akhgari e368dc9c85 Bug 579517 - Part 1: Automated conversion of NSPR numeric types to stdint types in Gecko; r=bsmedberg
This patch was generated by a script.  Here's the source of the script for
future reference:

function convert() {
echo "Converting $1 to $2..."
find . ! -wholename "*nsprpub*" \
       ! -wholename "*security/nss*" \
       ! -wholename "*/.hg*" \
       ! -wholename "obj-ff-dbg*" \
       ! -name nsXPCOMCID.h \
       ! -name prtypes.h \
         -type f \
      \( -iname "*.cpp" \
         -o -iname "*.h" \
         -o -iname "*.c" \
         -o -iname "*.cc" \
         -o -iname "*.idl" \
         -o -iname "*.ipdl" \
         -o -iname "*.ipdlh" \
         -o -iname "*.mm" \) | \
    xargs -n 1 sed -i -e "s/\b$1\b/$2/g"
}

convert PRInt8 int8_t
convert PRUint8 uint8_t
convert PRInt16 int16_t
convert PRUint16 uint16_t
convert PRInt32 int32_t
convert PRUint32 uint32_t
convert PRInt64 int64_t
convert PRUint64 uint64_t

convert PRIntn int
convert PRUintn unsigned

convert PRSize size_t

convert PROffset32 int32_t
convert PROffset64 int64_t

convert PRPtrdiff ptrdiff_t

convert PRFloat64 double
2012-08-22 11:56:38 -04:00

113 lines
3.8 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
#include "nsBidiUtils.h"
#include "nsCharTraits.h"
#include "nsUnicodeProperties.h"
#define ARABIC_TO_HINDI_DIGIT_INCREMENT (START_HINDI_DIGITS - START_ARABIC_DIGITS)
#define PERSIAN_TO_HINDI_DIGIT_INCREMENT (START_HINDI_DIGITS - START_FARSI_DIGITS)
#define ARABIC_TO_PERSIAN_DIGIT_INCREMENT (START_FARSI_DIGITS - START_ARABIC_DIGITS)
#define NUM_TO_ARABIC(c) \
((((c)>=START_HINDI_DIGITS) && ((c)<=END_HINDI_DIGITS)) ? \
((c) - (uint16_t)ARABIC_TO_HINDI_DIGIT_INCREMENT) : \
((((c)>=START_FARSI_DIGITS) && ((c)<=END_FARSI_DIGITS)) ? \
((c) - (uint16_t)ARABIC_TO_PERSIAN_DIGIT_INCREMENT) : \
(c)))
#define NUM_TO_HINDI(c) \
((((c)>=START_ARABIC_DIGITS) && ((c)<=END_ARABIC_DIGITS)) ? \
((c) + (uint16_t)ARABIC_TO_HINDI_DIGIT_INCREMENT): \
((((c)>=START_FARSI_DIGITS) && ((c)<=END_FARSI_DIGITS)) ? \
((c) + (uint16_t)PERSIAN_TO_HINDI_DIGIT_INCREMENT) : \
(c)))
#define NUM_TO_PERSIAN(c) \
((((c)>=START_HINDI_DIGITS) && ((c)<=END_HINDI_DIGITS)) ? \
((c) - (uint16_t)PERSIAN_TO_HINDI_DIGIT_INCREMENT) : \
((((c)>=START_ARABIC_DIGITS) && ((c)<=END_ARABIC_DIGITS)) ? \
((c) + (uint16_t)ARABIC_TO_PERSIAN_DIGIT_INCREMENT) : \
(c)))
PRUnichar HandleNumberInChar(PRUnichar aChar, bool aPrevCharArabic, uint32_t aNumFlag)
{
// IBMBIDI_NUMERAL_NOMINAL *
// IBMBIDI_NUMERAL_REGULAR
// IBMBIDI_NUMERAL_HINDICONTEXT
// IBMBIDI_NUMERAL_ARABIC
// IBMBIDI_NUMERAL_HINDI
switch (aNumFlag) {
case IBMBIDI_NUMERAL_HINDI:
return NUM_TO_HINDI(aChar);
case IBMBIDI_NUMERAL_ARABIC:
return NUM_TO_ARABIC(aChar);
case IBMBIDI_NUMERAL_PERSIAN:
return NUM_TO_PERSIAN(aChar);
case IBMBIDI_NUMERAL_REGULAR:
case IBMBIDI_NUMERAL_HINDICONTEXT:
case IBMBIDI_NUMERAL_PERSIANCONTEXT:
// for clipboard handling
//XXX do we really want to convert numerals when copying text?
if (aPrevCharArabic) {
if (aNumFlag == IBMBIDI_NUMERAL_PERSIANCONTEXT)
return NUM_TO_PERSIAN(aChar);
else
return NUM_TO_HINDI(aChar);
}
else
return NUM_TO_ARABIC(aChar);
case IBMBIDI_NUMERAL_NOMINAL:
default:
return aChar;
}
}
nsresult HandleNumbers(PRUnichar* aBuffer, uint32_t aSize, uint32_t aNumFlag)
{
uint32_t i;
switch (aNumFlag) {
case IBMBIDI_NUMERAL_HINDI:
case IBMBIDI_NUMERAL_ARABIC:
case IBMBIDI_NUMERAL_PERSIAN:
case IBMBIDI_NUMERAL_REGULAR:
case IBMBIDI_NUMERAL_HINDICONTEXT:
case IBMBIDI_NUMERAL_PERSIANCONTEXT:
for (i=0;i<aSize;i++)
aBuffer[i] = HandleNumberInChar(aBuffer[i], !!(i>0 ? aBuffer[i-1] : 0), aNumFlag);
break;
case IBMBIDI_NUMERAL_NOMINAL:
default:
break;
}
return NS_OK;
}
#define LRM_CHAR 0x200e
#define LRE_CHAR 0x202a
#define RLO_CHAR 0x202e
bool IsBidiControl(uint32_t aChar)
{
// This method is used when stripping Bidi control characters for
// display, so it will return TRUE for LRM, RLM, LRE, RLE, PDF, LRO and RLO
return ((LRE_CHAR <= aChar && aChar <= RLO_CHAR) ||
((aChar)&0xfffffe)==LRM_CHAR);
}
bool HasRTLChars(const nsAString& aString)
{
// This is used to determine whether to enable bidi if a string has
// right-to-left characters. To simplify things, anything that could be a
// surrogate or RTL presentation form is covered just by testing >= 0xD800).
// It's fine to enable bidi in rare cases where it actually isn't needed.
int32_t length = aString.Length();
for (int32_t i = 0; i < length; i++) {
PRUnichar ch = aString.CharAt(i);
if (ch >= 0xD800 || IS_IN_BMP_RTL_BLOCK(ch)) {
return true;
}
}
return false;
}