Bug 1097499 part 8 - Move CountGraphemeClusters to mozilla::unicode. r=emk draft
authorXidorn Quan <quanxunzhen@gmail.com>
Mon, 21 Mar 2016 18:13:02 +0800
changeset 354603 ba323d6b6c32d9fc502edee4de49b1fd1970af3b
parent 354602 4de6a97985313152d09e0706ccc1098a6ca2ac78
child 354604 36338bcfa45d6503fe7af92fbf817cc81199da0e
push id16138
push userxquan@mozilla.com
push dateThu, 21 Apr 2016 11:18:53 +0000
reviewersemk
bugs1097499
milestone48.0a1
Bug 1097499 part 8 - Move CountGraphemeClusters to mozilla::unicode. r=emk MozReview-Commit-ID: J9yR8RPs5u8
intl/unicharutil/util/nsUnicodeProperties.cpp
intl/unicharutil/util/nsUnicodeProperties.h
layout/style/CounterStyleManager.cpp
--- a/intl/unicharutil/util/nsUnicodeProperties.cpp
+++ b/intl/unicharutil/util/nsUnicodeProperties.cpp
@@ -488,11 +488,23 @@ ClusterIterator::Next()
             mPos++;
         }
     }
 
     NS_ASSERTION(mText < mPos && mPos <= mLimit,
                  "ClusterIterator::Next has overshot the string!");
 }
 
+uint32_t
+CountGraphemeClusters(const char16_t* aText, uint32_t aLength)
+{
+  ClusterIterator iter(aText, aLength);
+  uint32_t result = 0;
+  while (!iter.AtEnd()) {
+    ++result;
+    iter.Next();
+  }
+  return result;
+}
+
 } // end namespace unicode
 
 } // end namespace mozilla
--- a/intl/unicharutil/util/nsUnicodeProperties.h
+++ b/intl/unicharutil/util/nsUnicodeProperties.h
@@ -146,13 +146,16 @@ public:
 private:
     const char16_t* mPos;
     const char16_t* mLimit;
 #ifdef DEBUG
     const char16_t* mText;
 #endif
 };
 
+// Count the number of grapheme clusters in the given string
+uint32_t CountGraphemeClusters(const char16_t* aText, uint32_t aLength);
+
 } // end namespace unicode
 
 } // end namespace mozilla
 
 #endif /* NS_UNICODEPROPERTIES_H */
--- a/layout/style/CounterStyleManager.cpp
+++ b/layout/style/CounterStyleManager.cpp
@@ -1841,29 +1841,16 @@ CounterStyle::IsDependentStyle() const
       return true;
 
     // BuiltinCounterStyle
     default:
       return false;
   }
 }
 
-static int32_t
-CountGraphemeClusters(const nsSubstring& aText)
-{
-  using mozilla::unicode::ClusterIterator;
-  ClusterIterator iter(aText.Data(), aText.Length());
-  int32_t result = 0;
-  while (!iter.AtEnd()) {
-    ++result;
-    iter.Next();
-  }
-  return result;
-}
-
 void
 CounterStyle::GetCounterText(CounterValue aOrdinal,
                              WritingMode aWritingMode,
                              nsSubstring& aResult,
                              bool& aIsRTL)
 {
   bool success = IsOrdinalInRange(aOrdinal);
   aIsRTL = false;
@@ -1884,17 +1871,19 @@ CounterStyle::GetCounterText(CounterValu
         ordinal, aWritingMode, initialText, aIsRTL);
 
     // add pad & negative, build the final result
     if (success) {
       PadType pad;
       GetPad(pad);
       // We have to calculate the difference here since suffix part of negative
       // sign may be appended to initialText later.
-      int32_t diff = pad.width - CountGraphemeClusters(initialText);
+      int32_t diff = pad.width -
+        unicode::CountGraphemeClusters(initialText.Data(),
+                                       initialText.Length());
       aResult.Truncate();
       if (useNegativeSign && aOrdinal < 0) {
         NegativeType negative;
         GetNegative(negative);
         aResult.Append(negative.before);
         // There is nothing between the suffix part of negative and initial
         // representation, so we append it directly here.
         initialText.Append(negative.after);