diff options
author | Peter Elliott <pelliott@ualberta.ca> | 2021-09-19 11:14:18 -0600 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-10-05 13:27:25 +0330 |
commit | 10f6f6a723b2cec608d9499e27c28b7343d6e51f (patch) | |
tree | 89c3947d3ce78c57f10b74ea7f07097cef4d610f /Userland/Libraries/LibMarkdown/LineIterator.cpp | |
parent | cd560d3ae3c5dd618f54f802f1efffe633ba75a5 (diff) | |
download | serenity-10f6f6a723b2cec608d9499e27c28b7343d6e51f.zip |
LibMarkdown: Add LineIterator
LineIterator wraps a vector's ConstIterator, to provide an iterator that
can work on indented container blocks (like lists and blockquotes).
Diffstat (limited to 'Userland/Libraries/LibMarkdown/LineIterator.cpp')
-rw-r--r-- | Userland/Libraries/LibMarkdown/LineIterator.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Userland/Libraries/LibMarkdown/LineIterator.cpp b/Userland/Libraries/LibMarkdown/LineIterator.cpp new file mode 100644 index 0000000000..6966112be8 --- /dev/null +++ b/Userland/Libraries/LibMarkdown/LineIterator.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibMarkdown/LineIterator.h> + +namespace Markdown { + +bool LineIterator::is_indented(StringView const& line) const +{ + if (line.is_whitespace()) + return true; + + if (line.length() < m_indent) + return false; + + for (size_t i = 0; i < m_indent; ++i) { + if (line[i] != ' ') + return false; + } + + return true; +} + +bool LineIterator::is_end() const +{ + return m_iterator.is_end() || (!m_ignore_prefix_mode && !is_indented(*m_iterator)); +} + +StringView LineIterator::operator*() const +{ + VERIFY(m_ignore_prefix_mode || is_indented(*m_iterator)); + if (m_iterator->is_whitespace()) + return *m_iterator; + + return m_iterator->substring_view(m_indent); +} + +} |