Bug 898984 - Part 1: Add ClusterReverseIterator in nsUnicodeProperties. r=jfkthame
authorXidorn Quan <quanxunzhen@gmail.com>
Sun, 11 Aug 2013 03:37:00 +0900
changeset 315214 00a900605c52a89d263683f8a1edb2dcb34b37cb
parent 315213 bd99c422dca35d2ce039d3774710680095fe4977
child 315215 9a0a1b105571f3f29af09cd686ce88ca76d6f676
push id32563
push userihsiao@mozilla.com
push dateMon, 26 Sep 2016 11:18:33 +0000
treeherderautoland@eb840c87b5fd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjfkthame
bugs898984
milestone52.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 898984 - Part 1: Add ClusterReverseIterator in nsUnicodeProperties. r=jfkthame
intl/unicharutil/util/nsUnicodeProperties.cpp
intl/unicharutil/util/nsUnicodeProperties.h
--- a/intl/unicharutil/util/nsUnicodeProperties.cpp
+++ b/intl/unicharutil/util/nsUnicodeProperties.cpp
@@ -1,10 +1,11 @@
-/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
- * This Source Code Form is subject to the terms of the Mozilla Public
+/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* vim:set ts=4 sw=4 sts=4 et cindent: */
+/* 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 "nsUnicodeProperties.h"
 #include "nsUnicodePropertyData.cpp"
 
 #include "mozilla/ArrayUtils.h"
 #include "nsCharTraits.h"
@@ -498,16 +499,44 @@ ClusterIterator::Next()
             mPos++;
         }
     }
 
     NS_ASSERTION(mText < mPos && mPos <= mLimit,
                  "ClusterIterator::Next has overshot the string!");
 }
 
+void
+ClusterReverseIterator::Next()
+{
+    if (AtEnd()) {
+        NS_WARNING("ClusterReverseIterator has already reached the end");
+        return;
+    }
+
+    uint32_t ch;
+    do {
+        ch = *--mPos;
+
+        if (NS_IS_LOW_SURROGATE(ch) && mPos > mLimit &&
+            NS_IS_HIGH_SURROGATE(*(mPos - 1))) {
+            ch = SURROGATE_TO_UCS4(*--mPos, ch);
+        }
+
+        if (!IsClusterExtender(ch)) {
+            break;
+        }
+    } while (mPos > mLimit);
+
+    // XXX May need to handle conjoining Jamo
+
+    NS_ASSERTION(mPos >= mLimit,
+                 "ClusterReverseIterator::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();
--- a/intl/unicharutil/util/nsUnicodeProperties.h
+++ b/intl/unicharutil/util/nsUnicodeProperties.h
@@ -1,10 +1,11 @@
-/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
- * This Source Code Form is subject to the terms of the Mozilla Public
+/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* vim:set ts=4 sw=4 sts=4 et cindent: */
+/* 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/. */
 
 #ifndef NS_UNICODEPROPERTIES_H
 #define NS_UNICODEPROPERTIES_H
 
 #include "nsBidiUtils.h"
 #include "nsIUGenCategory.h"
@@ -151,13 +152,37 @@ private:
 #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);
 
+// A simple reverse iterator for a string of char16_t codepoints that
+// advances by Unicode grapheme clusters
+class ClusterReverseIterator
+{
+public:
+    ClusterReverseIterator(const char16_t* aText, uint32_t aLength)
+        : mPos(aText + aLength), mLimit(aText)
+    { }
+
+    operator const char16_t* () const {
+        return mPos;
+    }
+
+    bool AtEnd() const {
+        return mPos <= mLimit;
+    }
+
+    void Next();
+
+private:
+    const char16_t* mPos;
+    const char16_t* mLimit;
+};
+
 } // end namespace unicode
 
 } // end namespace mozilla
 
 #endif /* NS_UNICODEPROPERTIES_H */