| Index: third_party/WebKit/Source/core/editing/iterators/TextBuffer.h | 
| diff --git a/third_party/WebKit/Source/core/editing/iterators/TextBuffer.h b/third_party/WebKit/Source/core/editing/iterators/TextBuffer.h | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..319307eb5c44ea6abb1e5e12a4279883a5a75ac8 | 
| --- /dev/null | 
| +++ b/third_party/WebKit/Source/core/editing/iterators/TextBuffer.h | 
| @@ -0,0 +1,134 @@ | 
| +// 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 TextBuffer_h | 
| +#define TextBuffer_h | 
| + | 
| +#include "wtf/Vector.h" | 
| + | 
| +namespace blink { | 
| + | 
| +template<typename Direction, typename CharType, size_t inlineCapacity> | 
| +class TextBuffer { | 
| +    STACK_ALLOCATED(); | 
| +public: | 
| + | 
| +    TextBuffer() | 
| +        : m_size(0) | 
| +    { | 
| +        m_buffer.resize(m_buffer.capacity()); | 
| +    } | 
| + | 
| +    size_t size() const { return m_size; } | 
| +    bool isEmpty() const { return m_size == 0; } | 
| + | 
| +    CharType* data() { return Direction::dataStart(m_buffer, m_size); } | 
| +    const CharType* data() const { return Direction::dataStart(m_buffer, m_size); } | 
| + | 
| +    CharType& operator[](size_t index) { return data()[index]; } | 
| +    const CharType& operator[](size_t index) const { return data()[index]; } | 
| + | 
| +    void clear() { m_size = 0; } | 
| + | 
| +    void push(CharType c, size_t length = 1) | 
| +    { | 
| +        if (length == 0) | 
| +            return; | 
| +        std::fill_n(pushInternal(length), length, c); | 
| +    } | 
| + | 
| +    template<typename T> | 
| +    void push(const T* other, size_t length) | 
| +    { | 
| +        if (length == 0) | 
| +            return; | 
| +        std::copy(other, other + length, pushInternal(length)); | 
| +    } | 
| + | 
| +private: | 
| + | 
| +    CharType* pushInternal(size_t length) | 
| +    { | 
| +        if (m_size + length > m_buffer.capacity()) | 
| +            grow(m_size + length); | 
| +        CharType* ans = Direction::pushDestination(m_buffer, m_size, length); | 
| +        m_size += length; | 
| +        return ans; | 
| +    } | 
| + | 
| +    void grow(size_t demand) | 
| +    { | 
| +        size_t oldCapacity = m_buffer.capacity(); | 
| +        m_buffer.resize(demand); | 
| +        m_buffer.resize(m_buffer.capacity()); | 
| +        Direction::shiftData(m_buffer, m_size, oldCapacity); | 
| +    } | 
| + | 
| +    // I don't think |size_t| can introduce any performance issue, since | 
| +    // the wrapped |WTF::Vector| is already using |size_t|. | 
| +    size_t m_size; | 
| +    Vector<CharType, inlineCapacity> m_buffer; | 
| +}; | 
| + | 
| +class TextBufferForwards { | 
| +    STATIC_ONLY(TextBufferForwards); | 
| +public: | 
| +    template<typename Vec> | 
| +    static typename Vec::const_iterator dataStart(const Vec& vector, size_t) | 
| +    { | 
| +        return vector.begin(); | 
| +    } | 
| + | 
| +    template<typename Vec> | 
| +    static typename Vec::iterator dataStart(Vec& vector, size_t) | 
| +    { | 
| +        return vector.begin(); | 
| +    } | 
| + | 
| +    template<typename Vec> | 
| +    static typename Vec::iterator pushDestination(Vec& vector, size_t size, size_t) | 
| +    { | 
| +        return vector.begin() + size; | 
| +    } | 
| + | 
| +    template<typename Vec> | 
| +    static void shiftData(Vec&, size_t, size_t) | 
| +    { | 
| +    } | 
| +}; | 
| + | 
| +class TextBufferBackwards { | 
| +    STATIC_ONLY(TextBufferBackwards); | 
| +public: | 
| +    template<typename Vec> | 
| +    static typename Vec::const_iterator dataStart(const Vec& vector, size_t size) | 
| +    { | 
| +        return vector.end() - size; | 
| +    } | 
| + | 
| +    template<typename Vec> | 
| +    static typename Vec::iterator dataStart(Vec& vector, size_t size) | 
| +    { | 
| +        return vector.end() - size; | 
| +    } | 
| + | 
| +    template<typename Vec> | 
| +    static typename Vec::iterator pushDestination(Vec& vector, size_t size, size_t length) | 
| +    { | 
| +        return vector.end() - size - length; | 
| +    } | 
| + | 
| +    template<typename Vec> | 
| +    static void shiftData(Vec& vector, size_t size, size_t oldCapacity) | 
| +    { | 
| +        std::copy_backward(vector.begin() + oldCapacity - size, vector.begin() + oldCapacity, vector.end()); | 
| +    } | 
| +}; | 
| + | 
| +using ForwardsTextBuffer = TextBuffer<TextBufferForwards, UChar, 1024>; | 
| +using BackwardsTextBuffer = TextBuffer<TextBufferBackwards, UChar, 1024>; | 
| + | 
| +} // namespace blink | 
| + | 
| +#endif // TextBuffer_h | 
|  |