Move HandleNumbers() from nsBidiPresUtils to nsBidiUtils. Bug 137857. r=ftang, sr=attinasi
authorsmontagu@netscape.com
Thu, 28 Jun 2007 13:02:44 -0700
changeset 2911 c3e03b30308adead4484a5cbd89b3b08cc23a7a5
parent 2910 72c5a641116deb0dcf44a55dd863a63f036ef1e0
child 2912 ae033ef51e4f7f856714b37ecc24036353af843c
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersftang, attinasi
bugs137857
milestone1.9a6pre
Move HandleNumbers() from nsBidiPresUtils to nsBidiUtils. Bug 137857. r=ftang, sr=attinasi
intl/unicharutil/util/nsBidiUtils.cpp
intl/unicharutil/util/nsBidiUtils.h
--- a/intl/unicharutil/util/nsBidiUtils.cpp
+++ b/intl/unicharutil/util/nsBidiUtils.cpp
@@ -223,16 +223,26 @@ 0x82E0, // 0xFE82 0xFEE0 -> 0xFEF6
 0x84DF, // 0xFE84 0xFEDF -> 0xFEF7
 0x84E0, // 0xFE84 0xFEE0 -> 0xFEF8
 0x88DF, // 0xFE88 0xFEDF -> 0xFEF9
 0x88E0, // 0xFE88 0xFEE0 -> 0xFEFA
 0x8EDF, // 0xFE8E 0xFEDF -> 0xFEFB
 0x8EE0  // 0xFE8E 0xFEE0 -> 0xFEFC
 };
 
+#define ARABIC_TO_HINDI_DIGIT_INCREMENT (START_HINDI_DIGITS - START_ARABIC_DIGITS)
+#define NUM_TO_ARABIC(c) \
+  ((((c)>=START_HINDI_DIGITS) && ((c)<=END_HINDI_DIGITS)) ? \
+   ((c) - (PRUint16)ARABIC_TO_HINDI_DIGIT_INCREMENT) : \
+   (c))
+#define NUM_TO_HINDI(c) \
+  ((((c)>=START_ARABIC_DIGITS) && ((c)<=END_ARABIC_DIGITS)) ? \
+   ((c) + (PRUint16)ARABIC_TO_HINDI_DIGIT_INCREMENT): \
+   (c))
+
 nsresult ArabicShaping(const PRUnichar* aString, PRUint32 aLen,
                        PRUnichar* aBuf, PRUint32 *aBufLen)
 {
   const PRUnichar* src = aString;
   const PRUnichar* p;
   PRUnichar* dest = aBuf;
   PRUnichar formB;
   PRInt8 leftJ, thisJ, rightJ;
@@ -454,9 +464,45 @@ nsresult Conv_06_FE_WithReverse(const ns
       }
     } else {
       aDst += aSrcUnichars[endArabic];
     }
     foundArabic=PR_FALSE;
   }// for : loop the buffer
   return NS_OK;
 }
+
+nsresult HandleNumbers(PRUnichar* aBuffer, PRUint32 aSize, PRUint32 aNumFlag)
+{
+  PRUint32 i;
+  // IBMBIDI_NUMERAL_REGULAR *
+  // IBMBIDI_NUMERAL_HINDICONTEXT
+  // IBMBIDI_NUMERAL_ARABIC
+  // IBMBIDI_NUMERAL_HINDI
+
+  switch (aNumFlag) {
+    case IBMBIDI_NUMERAL_HINDI:
+      for (i=0;i<aSize;i++)
+        aBuffer[i] = NUM_TO_HINDI(aBuffer[i]);
+      break;
+    case IBMBIDI_NUMERAL_ARABIC:
+      for (i=0;i<aSize;i++)
+        aBuffer[i] = NUM_TO_ARABIC(aBuffer[i]);
+      break;
+    default : // IBMBIDI_NUMERAL_REGULAR, IBMBIDI_NUMERAL_HINDICONTEXT for HandleNum() which is called for clipboard handling
+      for (i=1;i<aSize;i++) {
+        if (IS_ARABIC_CHAR(aBuffer[i-1])) 
+          aBuffer[i] = NUM_TO_HINDI(aBuffer[i]);
+        else 
+          aBuffer[i] = NUM_TO_ARABIC(aBuffer[i]);
+      }
+      break;
+  }
+  return NS_OK;
+}
+
+nsresult HandleNumbers(const nsString& aSrc, nsString& aDst)
+{
+  aDst = aSrc;
+  return HandleNumbers((PRUnichar *)aDst.get(),aDst.Length(), IBMBIDI_NUMERAL_REGULAR);
+}
+
 #endif //IBMBIDI
--- a/intl/unicharutil/util/nsBidiUtils.h
+++ b/intl/unicharutil/util/nsBidiUtils.h
@@ -62,16 +62,34 @@
    * @param aSrc is the input string
    * @param aDst is the output string
    * @param aDir indicates whether the string should be reversed
    *        IBMBIDI_TEXTDIRECTION_LTR: do not reverse the string
    *        IBMBIDI_TEXTDIRECTION_RTL: reverse the string
    */
   nsresult Conv_06_FE_WithReverse(const nsString& aSrc, nsString& aDst, PRUint32 aDir);
 
+  /**
+   * Scan a Unichar string, converting numbers to Arabic or Hindi forms in place
+   * @param aBuffer is the string
+   * @param aSize is the size of aBuffer
+   * @param aNumFlag specifies the conversion to perform:
+   *        IBMBIDI_NUMERAL_HINDI:        convert to Hindi forms (Unicode 0660-0669)
+   *        IBMBIDI_NUMERAL_ARABIC:       convert to Arabic forms (Unicode 0030-0039)
+   *        IBMBIDI_NUMERAL_HINDICONTEXT: convert numbers in Arabic text to Hindi, otherwise to Arabic
+   */
+  nsresult HandleNumbers(PRUnichar* aBuffer, PRUint32 aSize, PRUint32  aNumFlag);
+
+  /**
+   * Scan an nsString, converting numerals to Arabic or Hindi forms
+   * @param aSrc is the input string
+   * @param aDst is the output string
+   */
+  nsresult HandleNumbers(const nsString& aSrc, nsString& aDst);
+
 // --------------------------------------------------
 // IBMBIDI 
 // --------------------------------------------------
 //
 // These values are shared with Preferences dialog
 //  ------------------
 //  If Pref values are to be changed
 //  in the XUL file of Prefs. the values