srctree

Robin Linden parent aef48eea 490ff755
util/base_parser: Fix unchecked-optional-access violation

inlinesplit
util/base_parser.h added: 9, removed: 8, total 1
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2023 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2022 David Zero <zero-one@zer0-one.net>
//
// SPDX-License-Identifier: BSD-2-Clause
@@ -18,7 +18,6 @@ namespace util {
template<typename T>
concept Predicate = std::predicate<T, char>;
 
// NOLINTBEGIN(bugprone-unchecked-optional-access)
class BaseParser {
public:
constexpr explicit BaseParser(std::string_view input) : input_{input} {}
@@ -72,7 +71,7 @@ public:
}
 
constexpr void skip_whitespace() {
while (!is_eof() && util::is_whitespace(*peek())) {
for (auto c = peek(); c && util::is_whitespace(*c); c = peek()) {
advance(1);
}
}
@@ -81,7 +80,6 @@ private:
std::string_view input_;
std::size_t pos_{0};
};
// NOLINTEND(bugprone-unchecked-optional-access)
 
} // namespace util
 
 
util/base_parser_test.cpp added: 9, removed: 8, total 1
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2022 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -55,11 +55,14 @@ int main() {
});
 
etest::test("skip_whitespace, consume_char", [] {
auto p = BaseParser(" \t \n h \n\n\ni");
auto p = BaseParser(" \t \n h \n\n\ni ");
p.skip_whitespace();
expect_eq(p.consume_char(), 'h');
 
p.skip_whitespace();
expect_eq(p.consume_char(), 'i');
 
p.skip_whitespace();
expect(p.is_eof());
});
}