Skip to content

Commit

Permalink
buffer: add get_line_escaped api
Browse files Browse the repository at this point in the history
(cherry picked from commit 7de9fc1)
  • Loading branch information
lmichaelis committed Nov 1, 2022
1 parent 81986f3 commit c6a47e9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
8 changes: 8 additions & 0 deletions include/phoenix/buffer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,14 @@ namespace phoenix {
/// \see isspace
[[nodiscard]] std::string get_line(bool skip_whitespace = true);

/// \brief Get a line from the buffer, unescape all relevant escape sequences, and
/// advance the position accordingly.
/// \param skip_whitespace Set to `true` to skip whitespace characters immediately following the line.
/// \return The line just read.
/// \throws buffer_underflow if the string can't be read.
/// \see isspace
[[nodiscard]] std::string get_line_escaped(bool skip_whitespace = true);

/// \brief Get a line from the buffer.
/// \param index The index at which to start.
/// \return The line just read
Expand Down
26 changes: 25 additions & 1 deletion source/buffer.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright © 2022 Luis Michaelis <lmichaelis.all+dev@gmail.com>
// SPDX-License-Identifier: MIT
#include "phoenix/buffer.hh"
#include <phoenix/buffer.hh>

#include <fmt/format.h>
#include <mio/mmap.hpp>
Expand Down Expand Up @@ -312,6 +312,30 @@ namespace phoenix {
return tmp;
}

std::string buffer::get_line_escaped(bool skip_whitespace) {
auto tmp = this->get_line(skip_whitespace);
auto it = std::find(tmp.begin(), tmp.end(), '\\');

while (it != tmp.end()) {
auto next = it + 1;

switch (*next) {
case 'n':
*it = '\n';
tmp.erase(next);
break;
case 't':
*it = '\t';
tmp.erase(next);
break;
}

it = std::find(it, tmp.end(), '\\');
}

return tmp;
}

std::string buffer::get_line_at(std::uint64_t index) const {
auto lf = mismatch(index, [](char chr) { return chr == '\n' || chr == '\0'; });

Expand Down
14 changes: 10 additions & 4 deletions tests/test_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,8 @@ TEST_SUITE("buffer") {
'l',
'o',
',',
' ',
'\\',
't',
'W',
'o',
'r',
Expand All @@ -599,9 +600,14 @@ TEST_SUITE("buffer") {
SUBCASE("relative") {
CHECK(buf.get_line(true) == "Hi");
CHECK(buf.position() == 7);
buf.mark();

CHECK(buf.get_line(true) == "Hello, World!");
CHECK(buf.position() == 21);
CHECK(buf.get_line(true) == "Hello,\\tWorld!");
CHECK(buf.position() == 22);

buf.reset();
CHECK(buf.get_line_escaped(true) == "Hello,\tWorld!");
CHECK(buf.position() == 22);

CHECK_THROWS((void) buf.get_line());
}
Expand All @@ -610,7 +616,7 @@ TEST_SUITE("buffer") {
CHECK(buf.get_line_at(1) == "i");
CHECK(buf.position() == 0);

CHECK_THROWS((void) buf.get_line_at(21));
CHECK_THROWS((void) buf.get_line_at(22));
}
}

Expand Down

0 comments on commit c6a47e9

Please sign in to comment.