Index: third_party/WebKit/Source/core/editing/iterators/ProgressiveTextAccumulator.cpp |
diff --git a/third_party/WebKit/Source/core/editing/iterators/ProgressiveTextAccumulator.cpp b/third_party/WebKit/Source/core/editing/iterators/ProgressiveTextAccumulator.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6c4c7e4f505134afb14e495f398ef2017a408524 |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/editing/iterators/ProgressiveTextAccumulator.cpp |
@@ -0,0 +1,57 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "core/editing/iterators/ProgressiveTextAccumulator.h" |
+ |
+namespace blink { |
+ |
+template<typename Iterator> |
+ProgressiveTextAccumulator<Iterator>::ProgressiveTextAccumulator(const PositionType& start, const PositionType& end, TextIteratorBehaviorFlags behavior) |
+ : m_iterator(start, end, behavior) |
+ , m_maxAccumulate(1024) |
+ , m_accumulatedInCurrentNode(0) |
+{ |
+} |
+ |
+template<typename Iterator> |
+void ProgressiveTextAccumulator<Iterator>::advance() |
+{ |
+ ASSERT(!m_iterator.atEnd()); |
+ ASSERT(m_accumulatedInCurrentNode <= m_iterator.length()); |
+ |
+ if (m_accumulatedInCurrentNode == m_iterator.length()) { |
+ m_accumulatedInCurrentNode = 0; |
+ m_iterator.advance(); |
+ } |
+} |
+ |
+template<typename Iterator> |
+int ProgressiveTextAccumulator<Iterator>::expandToValidLength(int accumulateLength) |
+{ |
+ int ans = accumulateLength; |
+ // Make sure we copy whole code points (on well-formed UTF-16). |
+ if (m_accumulatedInCurrentNode + accumulateLength < m_iterator.length()) { |
+ UChar last = m_iterator.characterAt(m_accumulatedInCurrentNode + accumulateLength - 1); |
+ UChar next = m_iterator.characterAt(m_accumulatedInCurrentNode + accumulateLength); |
+ if (arePairingSurrogates(last, next)) |
+ ans = accumulateLength + 1; |
+ } |
+ // TODO(xiaochengh): Do we have to copy whole grapheme clusters? |
+ return ans; |
+} |
+ |
+template<typename Iterator> |
+bool ProgressiveTextAccumulator<Iterator>::arePairingSurrogates(UChar first, UChar second) |
+{ |
+ if (Iterator::direction == TextIteratorDirection::TextIteratorForward) |
+ return U16_IS_LEAD(first) && U16_IS_TRAIL(second); |
+ return U16_IS_TRAIL(first) && U16_IS_LEAD(second); |
+} |
+ |
+template class CORE_TEMPLATE_EXPORT ProgressiveTextAccumulator<TextIteratorAlgorithm<EditingStrategy>>; |
+template class CORE_TEMPLATE_EXPORT ProgressiveTextAccumulator<TextIteratorAlgorithm<EditingInComposedTreeStrategy>>; |
+template class CORE_TEMPLATE_EXPORT ProgressiveTextAccumulator<SimplifiedBackwardsTextIteratorAlgorithm<EditingStrategy>>; |
+template class CORE_TEMPLATE_EXPORT ProgressiveTextAccumulator<SimplifiedBackwardsTextIteratorAlgorithm<EditingInComposedTreeStrategy>>; |
+ |
+} // namespace blink |