feat(Text): add ASCII-explicit helpers and bounded split/replace/repeat/join (M-TEXT-02)
This commit is contained in:
1 parent
68963fcd1d
commit
883bbdcf38
5 files changed
+468
No files matched your search
@@ -71,3 +71,4 @@ plans requiring updates. Do not record a proposal as user approval.
|
||||
| D-028 | Keep `docs/roadmap/` clean; author detailed per-task implementation specifications under `docs/agent-workflow/tasks/` prior to coding each wave | CONFIRMED by user |
|
||||
| D-029 | Follow `MIGRATION.md` Wave A+B: keep 1.x public APIs and existing tests compiling/passing while introducing NEW APIs and adapters beside them | CONFIRMED by user |
|
||||
| D-030 | Create dedicated git branch (`feat/roadmap-p0-p1-foundations`) and make focused commits per completed milestone/task; leave local untracked files (`.env`, `.serena/`, `WG.conf`, `keys`) untouched | CONFIRMED by user |
|
||||
| D-031 | Proceed continuously across roadmap waves without pausing for confirmation between waves, and merge verified waves locally into `main` (without pushing to remote) | CONFIRMED by user |
|
||||
@@ -0,0 +1,228 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Text/Str.hpp>
|
||||
|
||||
#include <concepts>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Text
|
||||
{
|
||||
|
||||
using Void::Str::iequals_ascii;
|
||||
using Void::Str::is_alnum_ascii;
|
||||
using Void::Str::is_alpha_ascii;
|
||||
using Void::Str::is_ascii_alnum;
|
||||
using Void::Str::is_ascii_alpha;
|
||||
using Void::Str::is_ascii_digit;
|
||||
using Void::Str::is_ascii_space;
|
||||
using Void::Str::is_digit_ascii;
|
||||
using Void::Str::to_lower_ascii;
|
||||
using Void::Str::to_lower_ascii_char;
|
||||
using Void::Str::to_upper_ascii;
|
||||
using Void::Str::to_upper_ascii_char;
|
||||
using Void::Str::trim_ascii;
|
||||
using Void::Str::trim_left_ascii;
|
||||
using Void::Str::trim_right_ascii;
|
||||
|
||||
struct TextLimits
|
||||
{
|
||||
std::size_t max_input = 16 * 1024 * 1024;
|
||||
std::size_t max_output = 16 * 1024 * 1024;
|
||||
std::size_t max_parts = 65536;
|
||||
};
|
||||
|
||||
struct TextError
|
||||
{
|
||||
enum class Code : std::uint8_t
|
||||
{
|
||||
invalid_encoding,
|
||||
invalid_rule,
|
||||
limit,
|
||||
overflow
|
||||
};
|
||||
|
||||
Code code{Code::invalid_rule};
|
||||
std::size_t offset{0};
|
||||
|
||||
[[nodiscard]] constexpr bool
|
||||
operator==(const TextError&) const noexcept = default;
|
||||
};
|
||||
|
||||
enum class EmptyParts : std::uint8_t
|
||||
{
|
||||
keep,
|
||||
drop
|
||||
};
|
||||
|
||||
[[nodiscard]] inline std::expected<std::vector<std::string_view>, TextError>
|
||||
split_views(std::string_view input,
|
||||
std::string_view delimiter,
|
||||
TextLimits limits = {},
|
||||
EmptyParts empty_parts = EmptyParts::keep)
|
||||
{
|
||||
if (delimiter.empty()) {
|
||||
return std::unexpected{TextError{TextError::Code::invalid_rule, 0}};
|
||||
}
|
||||
if (input.size() > limits.max_input) {
|
||||
return std::unexpected{TextError{TextError::Code::limit, limits.max_input}};
|
||||
}
|
||||
|
||||
std::vector<std::string_view> parts;
|
||||
std::size_t pos = 0;
|
||||
for (;;) {
|
||||
const std::size_t found = input.find(delimiter, pos);
|
||||
const std::size_t len =
|
||||
(found == std::string_view::npos) ? (input.size() - pos) : (found - pos);
|
||||
const std::string_view part = input.substr(pos, len);
|
||||
if (!part.empty() || empty_parts == EmptyParts::keep) {
|
||||
if (parts.size() >= limits.max_parts) {
|
||||
return std::unexpected{TextError{TextError::Code::limit, pos}};
|
||||
}
|
||||
parts.push_back(part);
|
||||
}
|
||||
if (found == std::string_view::npos) {
|
||||
break;
|
||||
}
|
||||
pos = found + delimiter.size();
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::expected<std::string, TextError>
|
||||
replace_bounded(std::string_view input,
|
||||
std::string_view needle,
|
||||
std::string_view replacement,
|
||||
TextLimits limits = {})
|
||||
{
|
||||
if (needle.empty()) {
|
||||
return std::unexpected{TextError{TextError::Code::invalid_rule, 0}};
|
||||
}
|
||||
if (input.size() > limits.max_input) {
|
||||
return std::unexpected{TextError{TextError::Code::limit, limits.max_input}};
|
||||
}
|
||||
|
||||
std::size_t matches = 0;
|
||||
std::size_t scan = 0;
|
||||
while (true) {
|
||||
const std::size_t found = input.find(needle, scan);
|
||||
if (found == std::string_view::npos) {
|
||||
break;
|
||||
}
|
||||
++matches;
|
||||
scan = found + needle.size();
|
||||
}
|
||||
|
||||
std::size_t out_size = input.size();
|
||||
if (replacement.size() >= needle.size()) {
|
||||
const std::size_t delta = replacement.size() - needle.size();
|
||||
constexpr std::size_t kMaxSize = std::numeric_limits<std::size_t>::max();
|
||||
if (delta != 0 && matches > (kMaxSize - input.size()) / delta) {
|
||||
return std::unexpected{TextError{TextError::Code::overflow, input.size()}};
|
||||
}
|
||||
out_size = input.size() + matches * delta;
|
||||
} else {
|
||||
const std::size_t shrink = needle.size() - replacement.size();
|
||||
out_size = input.size() - matches * shrink;
|
||||
}
|
||||
|
||||
if (out_size > limits.max_output) {
|
||||
return std::unexpected{TextError{TextError::Code::limit, limits.max_output}};
|
||||
}
|
||||
|
||||
std::string out;
|
||||
out.reserve(out_size);
|
||||
std::size_t pos = 0;
|
||||
while (true) {
|
||||
const std::size_t found = input.find(needle, pos);
|
||||
if (found == std::string_view::npos) {
|
||||
out.append(input.substr(pos));
|
||||
break;
|
||||
}
|
||||
out.append(input.substr(pos, found - pos));
|
||||
out.append(replacement);
|
||||
pos = found + needle.size();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::expected<std::string, TextError>
|
||||
repeat_bounded(std::string_view input, std::size_t count, TextLimits limits = {})
|
||||
{
|
||||
if (input.size() > limits.max_input) {
|
||||
return std::unexpected{TextError{TextError::Code::limit, limits.max_input}};
|
||||
}
|
||||
if (count == 0 || input.empty()) {
|
||||
return std::string{};
|
||||
}
|
||||
constexpr std::size_t kMaxSize = std::numeric_limits<std::size_t>::max();
|
||||
if (count > kMaxSize / input.size()) {
|
||||
return std::unexpected{TextError{TextError::Code::overflow, input.size()}};
|
||||
}
|
||||
const std::size_t out_size = input.size() * count;
|
||||
if (out_size > limits.max_output) {
|
||||
return std::unexpected{TextError{TextError::Code::limit, limits.max_output}};
|
||||
}
|
||||
|
||||
std::string out;
|
||||
out.reserve(out_size);
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
out.append(input);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
template <typename Range>
|
||||
requires requires(const Range& r) {
|
||||
std::begin(r);
|
||||
std::end(r);
|
||||
}
|
||||
[[nodiscard]] inline std::expected<std::string, TextError>
|
||||
join_bounded(const Range& parts, std::string_view sep, TextLimits limits = {})
|
||||
{
|
||||
constexpr std::size_t kMaxSize = std::numeric_limits<std::size_t>::max();
|
||||
std::size_t count = 0;
|
||||
std::size_t total_bytes = 0;
|
||||
bool first = true;
|
||||
for (const auto& raw_part : parts) {
|
||||
if (count >= limits.max_parts) {
|
||||
return std::unexpected{TextError{TextError::Code::limit, count}};
|
||||
}
|
||||
const std::string_view sv{raw_part};
|
||||
if (!first) {
|
||||
if (sep.size() > kMaxSize - total_bytes) {
|
||||
return std::unexpected{TextError{TextError::Code::overflow, count}};
|
||||
}
|
||||
total_bytes += sep.size();
|
||||
}
|
||||
if (sv.size() > kMaxSize - total_bytes) {
|
||||
return std::unexpected{TextError{TextError::Code::overflow, count}};
|
||||
}
|
||||
total_bytes += sv.size();
|
||||
if (total_bytes > limits.max_output) {
|
||||
return std::unexpected{TextError{TextError::Code::limit, limits.max_output}};
|
||||
}
|
||||
++count;
|
||||
first = false;
|
||||
}
|
||||
|
||||
std::string out;
|
||||
out.reserve(total_bytes);
|
||||
first = true;
|
||||
for (const auto& raw_part : parts) {
|
||||
if (!first) {
|
||||
out.append(sep);
|
||||
}
|
||||
out.append(std::string_view{raw_part});
|
||||
first = false;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace Void::Text
|
||||
@@ -3,6 +3,8 @@
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <charconv>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <system_error>
|
||||
@@ -208,4 +210,129 @@ template <typename Range>
|
||||
return ec == std::errc{} && ptr == s.data() + s.size();
|
||||
}
|
||||
|
||||
// ── ASCII-explicit helpers (locale-independent, bytes >= 128 preserved) ───────
|
||||
|
||||
[[nodiscard]] constexpr bool is_ascii_space(char c) noexcept
|
||||
{
|
||||
const auto uc = static_cast<unsigned char>(c);
|
||||
return uc == ' ' || (uc >= '\t' && uc <= '\r');
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool is_ascii_alpha(char c) noexcept
|
||||
{
|
||||
const auto uc = static_cast<unsigned char>(c);
|
||||
return (uc >= 'a' && uc <= 'z') || (uc >= 'A' && uc <= 'Z');
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool is_ascii_digit(char c) noexcept
|
||||
{
|
||||
const auto uc = static_cast<unsigned char>(c);
|
||||
return uc >= '0' && uc <= '9';
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool is_ascii_alnum(char c) noexcept
|
||||
{
|
||||
return is_ascii_alpha(c) || is_ascii_digit(c);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr char to_lower_ascii_char(char c) noexcept
|
||||
{
|
||||
const auto uc = static_cast<unsigned char>(c);
|
||||
if (uc >= 'A' && uc <= 'Z') {
|
||||
return static_cast<char>(uc + ('a' - 'A'));
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr char to_upper_ascii_char(char c) noexcept
|
||||
{
|
||||
const auto uc = static_cast<unsigned char>(c);
|
||||
if (uc >= 'a' && uc <= 'z') {
|
||||
return static_cast<char>(uc - ('a' - 'A'));
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::string_view trim_left_ascii(std::string_view s) noexcept
|
||||
{
|
||||
std::size_t pos = 0;
|
||||
while (pos < s.size() && is_ascii_space(s[pos])) {
|
||||
++pos;
|
||||
}
|
||||
s.remove_prefix(pos);
|
||||
return s;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::string_view trim_right_ascii(std::string_view s) noexcept
|
||||
{
|
||||
std::size_t len = s.size();
|
||||
while (len > 0 && is_ascii_space(s[len - 1])) {
|
||||
--len;
|
||||
}
|
||||
return s.substr(0, len);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::string_view trim_ascii(std::string_view s) noexcept
|
||||
{
|
||||
return trim_right_ascii(trim_left_ascii(s));
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::string to_lower_ascii(std::string_view s)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(s.size());
|
||||
for (const char c : s) {
|
||||
out.push_back(to_lower_ascii_char(c));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline std::string to_upper_ascii(std::string_view s)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(s.size());
|
||||
for (const char c : s) {
|
||||
out.push_back(to_upper_ascii_char(c));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool is_alpha_ascii(std::string_view s) noexcept
|
||||
{
|
||||
if (s.empty()) return false;
|
||||
for (const char c : s) {
|
||||
if (!is_ascii_alpha(c)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool is_digit_ascii(std::string_view s) noexcept
|
||||
{
|
||||
if (s.empty()) return false;
|
||||
for (const char c : s) {
|
||||
if (!is_ascii_digit(c)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool is_alnum_ascii(std::string_view s) noexcept
|
||||
{
|
||||
if (s.empty()) return false;
|
||||
for (const char c : s) {
|
||||
if (!is_ascii_alnum(c)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool iequals_ascii(std::string_view a, std::string_view b) noexcept
|
||||
{
|
||||
if (a.size() != b.size()) return false;
|
||||
for (std::size_t i = 0; i < a.size(); ++i) {
|
||||
if (to_lower_ascii_char(a[i]) != to_lower_ascii_char(b[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Void::Str
|
||||
@@ -0,0 +1,92 @@
|
||||
#include <Void/Text/Bounded.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
Void::Testing::Test bounded_split_views_keep_drop_and_limits()
|
||||
{
|
||||
using Void::Text::EmptyParts;
|
||||
using Void::Text::TextError;
|
||||
using Void::Text::TextLimits;
|
||||
using Void::Text::split_views;
|
||||
|
||||
const auto empty_delim = split_views("a,b", "");
|
||||
Void::Testing::expect(!empty_delim.has_value());
|
||||
Void::Testing::expect(empty_delim.error().code == TextError::Code::invalid_rule);
|
||||
|
||||
const auto kept = split_views(",a,,b,", ",");
|
||||
Void::Testing::require(kept.has_value());
|
||||
Void::Testing::expect_eq(kept->size(), std::size_t{5});
|
||||
Void::Testing::expect_eq((*kept)[0], std::string_view{""});
|
||||
Void::Testing::expect_eq((*kept)[1], std::string_view{"a"});
|
||||
Void::Testing::expect_eq((*kept)[4], std::string_view{""});
|
||||
|
||||
const auto dropped = split_views(",a,,b,", ",", {}, EmptyParts::drop);
|
||||
Void::Testing::require(dropped.has_value());
|
||||
Void::Testing::expect_eq(dropped->size(), std::size_t{2});
|
||||
Void::Testing::expect_eq((*dropped)[0], std::string_view{"a"});
|
||||
Void::Testing::expect_eq((*dropped)[1], std::string_view{"b"});
|
||||
|
||||
const auto too_big_input = split_views("abcdef", ",", TextLimits{.max_input = 5});
|
||||
Void::Testing::expect(!too_big_input.has_value());
|
||||
Void::Testing::expect(too_big_input.error().code == TextError::Code::limit);
|
||||
|
||||
const auto too_many_parts = split_views("a,b,c", ",", TextLimits{.max_parts = 2});
|
||||
Void::Testing::expect(!too_many_parts.has_value());
|
||||
Void::Testing::expect(too_many_parts.error().code == TextError::Code::limit);
|
||||
|
||||
// Embedded NUL and high bytes preserved across split views.
|
||||
const std::string bin_src{"\x80\0a::\xFF""b", 8};
|
||||
const auto bin_parts = split_views(bin_src, "::");
|
||||
Void::Testing::require(bin_parts.has_value());
|
||||
Void::Testing::expect_eq(bin_parts->size(), std::size_t{2});
|
||||
Void::Testing::expect_eq((*bin_parts)[0].size(), std::size_t{3});
|
||||
Void::Testing::expect_eq((*bin_parts)[1].size(), std::size_t{3});
|
||||
}
|
||||
|
||||
Void::Testing::Test bounded_replace_repeat_and_join_enforce_limits_and_overflow()
|
||||
{
|
||||
using Void::Text::TextError;
|
||||
using Void::Text::TextLimits;
|
||||
using Void::Text::join_bounded;
|
||||
using Void::Text::repeat_bounded;
|
||||
using Void::Text::replace_bounded;
|
||||
|
||||
// Invalid empty needle
|
||||
Void::Testing::expect(
|
||||
replace_bounded("abc", "", "x").error().code == TextError::Code::invalid_rule);
|
||||
|
||||
// Growing replacement exceeding max_output
|
||||
const auto grown_err = replace_bounded("aaa", "a", "xx", TextLimits{.max_output = 5});
|
||||
Void::Testing::expect(!grown_err.has_value());
|
||||
Void::Testing::expect(grown_err.error().code == TextError::Code::limit);
|
||||
|
||||
// Shrinking replacement succeeds even when input > max_output if max_input allows it
|
||||
const auto shrunk = replace_bounded(
|
||||
"longtoken_longtoken", "longtoken", "ok", TextLimits{.max_input = 64, .max_output = 5});
|
||||
Void::Testing::require(shrunk.has_value());
|
||||
Void::Testing::expect_eq(*shrunk, std::string{"ok_ok"});
|
||||
|
||||
// Multiplication overflow check on repeat_bounded
|
||||
const auto ovf = repeat_bounded("abcd", std::numeric_limits<std::size_t>::max() / 2);
|
||||
Void::Testing::expect(!ovf.has_value());
|
||||
Void::Testing::expect(ovf.error().code == TextError::Code::overflow);
|
||||
|
||||
const auto rep_ok = repeat_bounded("ab", 3, TextLimits{.max_output = 6});
|
||||
Void::Testing::require(rep_ok.has_value());
|
||||
Void::Testing::expect_eq(*rep_ok, std::string{"ababab"});
|
||||
|
||||
const std::vector<std::string_view> parts{"alpha", "beta", "gamma"};
|
||||
const auto joined_ok = join_bounded(parts, "-", TextLimits{.max_output = 16});
|
||||
Void::Testing::require(joined_ok.has_value());
|
||||
Void::Testing::expect_eq(*joined_ok, std::string{"alpha-beta-gamma"});
|
||||
|
||||
Void::Testing::expect(
|
||||
join_bounded(parts, "-", TextLimits{.max_parts = 2}).error().code == TextError::Code::limit);
|
||||
Void::Testing::expect(
|
||||
join_bounded(parts, "-", TextLimits{.max_output = 15}).error().code == TextError::Code::limit);
|
||||
}
|
||||
@@ -171,3 +171,23 @@ Void::Testing::Test str_to_double_valid()
|
||||
Void::Testing::expect(to_double("3.14", v));
|
||||
Void::Testing::expect(v > 3.13 && v < 3.15);
|
||||
}
|
||||
|
||||
Void::Testing::Test str_ascii_helpers_preserve_high_bytes_and_nuls()
|
||||
{
|
||||
static_assert(trim_ascii(" \t\r\nabc\n ") == "abc");
|
||||
static_assert(is_alpha_ascii("AbCd"));
|
||||
static_assert(!is_alpha_ascii("\xC3\xA9"));
|
||||
static_assert(is_digit_ascii("0123456789"));
|
||||
static_assert(is_alnum_ascii("Az09"));
|
||||
static_assert(iequals_ascii("HeLlO\xFF", "hElLo\xFF"));
|
||||
static_assert(!iequals_ascii("abc", "abcd"));
|
||||
|
||||
const std::string mixed{" \xC3\xA9""Ab\xFF""Z\0x\n", 10};
|
||||
Void::Testing::expect_eq(trim_left_ascii(mixed).size(), std::size_t{9});
|
||||
Void::Testing::expect_eq(trim_right_ascii(mixed).size(), std::size_t{9});
|
||||
Void::Testing::expect_eq(trim_ascii(mixed).size(), std::size_t{8});
|
||||
const std::string lower = to_lower_ascii("A\x80\xFFZ");
|
||||
Void::Testing::expect_eq(lower, std::string{"a\x80\xFFz"});
|
||||
const std::string upper = to_upper_ascii("a\x80\xFFz");
|
||||
Void::Testing::expect_eq(upper, std::string{"A\x80\xFFZ"});
|
||||
}
|
||||
Reference in new issue
Block a user