229 lines
6.6 KiB
C++
229 lines
6.6 KiB
C++
#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
|