srctree

David Zero parent 9985bb46 39d56b95
util/string: Add more utility functions

Also removes the duplicate is_whitespace() implementation fromBaseParser.

This is preparation for a new URL parser.

inlinesplit
util/BUILD added: 37, removed: 9, total 28
@@ -1,9 +1,17 @@
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
 
dependencies = {
"base_parser": [":string"],
}
 
[cc_library(
name = hdr[:-2],
hdrs = [hdr],
visibility = ["//visibility:public"],
deps = dependencies.get(
hdr[:-2],
[],
),
) for hdr in glob(["*.h"])]
 
[cc_test(
 
util/base_parser.h added: 37, removed: 9, total 28
@@ -1,12 +1,13 @@
// SPDX-FileCopyrightText: 2021 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2022 David Zero <zero-one@zer0-one.net>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#ifndef UTIL_BASE_PARSER_H_
#define UTIL_BASE_PARSER_H_
 
#include <algorithm>
#include <array>
#include "util/string.h"
 
#include <concepts>
#include <cstddef>
#include <string_view>
@@ -32,6 +33,15 @@ public:
 
constexpr void advance(std::size_t n) { pos_ += n; }
 
constexpr void back(std::size_t n) { pos_ -= n; }
 
constexpr void reset() { pos_ = 0; }
 
constexpr void reset(std::string_view input) {
input_ = input;
pos_ = 0;
}
 
template<Predicate T>
constexpr std::string_view consume_while(T const &pred) {
std::size_t start = pos_;
@@ -42,15 +52,12 @@ public:
}
 
constexpr void skip_whitespace() {
while (!is_eof() && is_space(peek())) {
while (!is_eof() && util::is_whitespace(peek())) {
advance(1);
}
}
 
private:
static constexpr auto space_chars = std::array{' ', '\f', '\n', '\r', '\t', '\v'};
constexpr static bool is_space(char c) { return std::ranges::find(space_chars, c) != end(space_chars); }
 
std::string_view input_;
std::size_t pos_{0};
};
 
util/string.h added: 37, removed: 9, total 28
@@ -1,5 +1,6 @@
// SPDX-FileCopyrightText: 2021-2022 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021 Mikael Larsson <c.mikael.larsson@gmail.com>
// SPDX-FileCopyrightText: 2022 David Zero <zero-one@zer0-one.net>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -15,6 +16,18 @@
 
namespace util {
 
constexpr bool is_c0(char c) {
return c >= 0x00 && c <= 0x1f;
}
 
constexpr bool is_c0_or_space(char c) {
return is_c0(c) || c == ' ';
}
 
constexpr bool is_tab_or_newline(char c) {
return c == '\t' || c == '\n' || c == '\r';
}
 
constexpr bool is_upper_alpha(char c) {
return c >= 'A' && c <= 'Z';
}