Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Unified Diff: third_party/WebKit/Source/core/editing/iterators/TextAccumulator.h

Issue 1604783002: ALL-IN-ONE: Optimization to previousBoundary() and nextBoundary() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@progressive_accumulator
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/editing/iterators/TextAccumulator.h
diff --git a/third_party/WebKit/Source/core/editing/iterators/TextAccumulator.h b/third_party/WebKit/Source/core/editing/iterators/TextAccumulator.h
new file mode 100644
index 0000000000000000000000000000000000000000..e555b6bb6e023396ec87e8e9d5da14e510beb5bb
--- /dev/null
+++ b/third_party/WebKit/Source/core/editing/iterators/TextAccumulator.h
@@ -0,0 +1,76 @@
+// 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.
+
+#ifndef TextAccumulator_h
+#define TextAccumulator_h
+
+#include "core/editing/iterators/TextBuffer.h"
+
+namespace blink {
+
+template<typename Buffer>
+class TextAccumulator {
+ STACK_ALLOCATED();
+public:
+
+ TextAccumulator()
+#ifndef NDEBUG
+ : m_maxAccumulate(2)
+#else
+ : m_maxAccumulate(1024)
+#endif // NDEBUG
+ , m_accumulatedInCurrentNode(0)
+ {
+ }
+
+ const UChar* data() const { return m_buffer.data(); }
+ unsigned size() const { return m_buffer.size(); }
+ int accumulatedLengthInCurrentNode() const { return m_accumulatedInCurrentNode; }
+
+ void push(const UChar* other, unsigned length) { m_buffer.push(other, length); }
+
+ template<typename Iterator>
+ void extract(const Iterator& it)
+ {
+ ASSERT(m_maxAccumulate > 0);
+ ASSERT(m_accumulatedInCurrentNode >= 0);
+ ASSERT(m_accumulatedInCurrentNode <= it.length());
+ int accumulateLength = std::min(it.length() - m_accumulatedInCurrentNode, m_maxAccumulate);
+ if (it.isInTextSecurityMode()) {
+ // Treat bullets used in the text security mode as regular
+ // characters when looking for boundaries
+ m_buffer.push('x', accumulateLength);
+ } else {
+ accumulateLength = it.copyTextTo(m_buffer, m_accumulatedInCurrentNode, accumulateLength);
+ }
+ m_accumulatedInCurrentNode += accumulateLength;
+ if (accumulateLength >= m_maxAccumulate)
+ m_maxAccumulate *= 2;
+ }
+
+ template<typename Iterator>
+ void advance(Iterator& it)
+ {
+ ASSERT(m_accumulatedInCurrentNode >= 0);
+ ASSERT(m_accumulatedInCurrentNode <= it.length());
+ ASSERT(!it.atEnd());
+ if (m_accumulatedInCurrentNode == it.length()) {
+ m_accumulatedInCurrentNode = 0;
+ it.advance();
+ }
+ }
+
+private:
+
+ Buffer m_buffer;
+ int m_maxAccumulate;
+ int m_accumulatedInCurrentNode;
+};
+
+using ForwardsTextAccumulator = TextAccumulator<ForwardsTextBuffer>;
+using BackwardsTextAccumulator = TextAccumulator<BackwardsTextBuffer>;
+
+} // namespace blink
+
+#endif // TextAccumulator_h

Powered by Google App Engine
This is Rietveld 408576698