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);
|
||||
}
|
||||
Reference in new issue
Block a user