Files
Library/include/Void/Text/Str.hpp
T
faumarayandClaude Fable 5 28b94883fd Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged:
- Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant
- Functional: Pipeline, Either, Result, Retry
- Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval
- Math: +Matrix, Numeric, Random, Histogram
- Text: Str, Format, Tokenizer, Diff, Encoding, Semver
- Data: Json, Csv, Ini
- Async: Coro, Concurrency, EventBus, Observable
- Time: +Bench
- Log: Log facade + Logging framework (facade now header-only, breaking
  the Stacktrace<->Log link cycle the merge would have created)
- System: FileSystem + Process
- Configuration: +Argparse
- Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail

Module system fixes:
- OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no
  longer depends on the alphabetical discovery order of src/ subdirs
  (Testing -> Text would have been silently dropped).
- Every header-only module now has a tracked module.cmake; previously the
  empty src/<Name> marker dirs were untracked, so a fresh clone would have
  silently skipped those modules.
- VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT.

Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files
prefixed: ResultBasic etc.). Test count verified identical (831) before and
after. README/CLAUDE.md module tables updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 18:50:02 +03:00

212 lines
7.3 KiB
C++

#pragma once
#include <algorithm>
#include <cctype>
#include <charconv>
#include <string>
#include <string_view>
#include <system_error>
#include <vector>
namespace Void::Str
{
// ── trim ──────────────────────────────────────────────────────────────────────
[[nodiscard]] inline std::string_view trim_left(std::string_view s) noexcept
{
const auto it = std::find_if(s.begin(), s.end(),
[](unsigned char c) { return !std::isspace(c); });
s.remove_prefix(static_cast<std::size_t>(it - s.begin()));
return s;
}
[[nodiscard]] inline std::string_view trim_right(std::string_view s) noexcept
{
const auto it = std::find_if(s.rbegin(), s.rend(),
[](unsigned char c) { return !std::isspace(c); });
s.remove_suffix(static_cast<std::size_t>(it - s.rbegin()));
return s;
}
[[nodiscard]] inline std::string_view trim(std::string_view s) noexcept
{
return trim_right(trim_left(s));
}
// ── case ──────────────────────────────────────────────────────────────────────
[[nodiscard]] inline std::string to_lower(std::string_view s)
{
std::string out{s};
std::transform(out.begin(), out.end(), out.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return out;
}
[[nodiscard]] inline std::string to_upper(std::string_view s)
{
std::string out{s};
std::transform(out.begin(), out.end(), out.begin(),
[](unsigned char c) { return static_cast<char>(std::toupper(c)); });
return out;
}
// ── predicate ─────────────────────────────────────────────────────────────────
[[nodiscard]] inline bool starts_with(std::string_view s, std::string_view prefix) noexcept
{
return s.size() >= prefix.size() && s.substr(0, prefix.size()) == prefix;
}
[[nodiscard]] inline bool ends_with(std::string_view s, std::string_view suffix) noexcept
{
return s.size() >= suffix.size() && s.substr(s.size() - suffix.size()) == suffix;
}
[[nodiscard]] inline bool contains(std::string_view s, std::string_view needle) noexcept
{
return s.find(needle) != std::string_view::npos;
}
[[nodiscard]] inline bool is_alpha(std::string_view s) noexcept
{
return !s.empty() && std::all_of(s.begin(), s.end(),
[](unsigned char c) { return std::isalpha(c) != 0; });
}
[[nodiscard]] inline bool is_digit(std::string_view s) noexcept
{
return !s.empty() && std::all_of(s.begin(), s.end(),
[](unsigned char c) { return std::isdigit(c) != 0; });
}
[[nodiscard]] inline bool is_alnum(std::string_view s) noexcept
{
return !s.empty() && std::all_of(s.begin(), s.end(),
[](unsigned char c) { return std::isalnum(c) != 0; });
}
// ── split / join ──────────────────────────────────────────────────────────────
[[nodiscard]] inline std::vector<std::string_view>
split(std::string_view s, char delim)
{
std::vector<std::string_view> parts;
std::size_t pos = 0;
while (true) {
const auto found = s.find(delim, pos);
parts.push_back(s.substr(pos, found - pos));
if (found == std::string_view::npos) break;
pos = found + 1;
}
return parts;
}
[[nodiscard]] inline std::vector<std::string_view>
split(std::string_view s, std::string_view delim)
{
std::vector<std::string_view> parts;
std::size_t pos = 0;
while (true) {
const auto found = s.find(delim, pos);
parts.push_back(s.substr(pos, found - pos));
if (found == std::string_view::npos) break;
pos = found + delim.size();
}
return parts;
}
[[nodiscard]] inline std::vector<std::string_view>
split_once(std::string_view s, std::string_view delim)
{
const auto pos = s.find(delim);
if (pos == std::string_view::npos) return {s};
return {s.substr(0, pos), s.substr(pos + delim.size())};
}
template <typename Range>
[[nodiscard]] inline std::string join(const Range& parts, std::string_view sep)
{
std::string out;
bool first = true;
for (const auto& p : parts) {
if (!first) out += sep;
out += p;
first = false;
}
return out;
}
// ── replace ───────────────────────────────────────────────────────────────────
[[nodiscard]] inline std::string replace_all(
std::string_view s, std::string_view from, std::string_view to)
{
std::string out;
out.reserve(s.size());
std::size_t pos = 0;
while (true) {
const auto found = s.find(from, pos);
out += s.substr(pos, found - pos);
if (found == std::string_view::npos) break;
out += to;
pos = found + from.size();
}
return out;
}
// ── repeat ────────────────────────────────────────────────────────────────────
[[nodiscard]] inline std::string repeat(std::string_view s, std::size_t n)
{
std::string out;
out.reserve(s.size() * n);
for (std::size_t i = 0; i < n; ++i) out += s;
return out;
}
// ── count_occurrences ─────────────────────────────────────────────────────────
[[nodiscard]] inline std::size_t count(std::string_view s, std::string_view needle) noexcept
{
if (needle.empty()) return 0;
std::size_t n = 0;
std::size_t pos = 0;
while ((pos = s.find(needle, pos)) != std::string_view::npos) {
++n;
pos += needle.size();
}
return n;
}
// ── pad ───────────────────────────────────────────────────────────────────────
[[nodiscard]] inline std::string pad_left(std::string_view s, std::size_t width, char fill = ' ')
{
if (s.size() >= width) return std::string{s};
return std::string(width - s.size(), fill) + std::string{s};
}
[[nodiscard]] inline std::string pad_right(std::string_view s, std::size_t width, char fill = ' ')
{
if (s.size() >= width) return std::string{s};
return std::string{s} + std::string(width - s.size(), fill);
}
// ── numeric conversion ────────────────────────────────────────────────────────
[[nodiscard]] inline bool to_int(std::string_view s, int& out) noexcept
{
const auto [ptr, ec] = std::from_chars(s.data(), s.data() + s.size(), out);
return ec == std::errc{} && ptr == s.data() + s.size();
}
[[nodiscard]] inline bool to_double(std::string_view s, double& out) noexcept
{
const auto [ptr, ec] = std::from_chars(s.data(), s.data() + s.size(), out);
return ec == std::errc{} && ptr == s.data() + s.size();
}
} // namespace Void::Str