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
@@ -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