Files
Library/include/Void/Str/Str.hpp
T
faumarayandClaude Sonnet 4.6 58ff7b38a0 Add 20+ new modules with tests: Pipeline, Matrix, Tokenizer, ScopeGuard, Bitset, Optional, Counted, Curry, TypeMap, Bench, DisjointSet, Histogram, StopWatch, and more
Also fixes Diff/Memo/PriorityQueue from previous session: LineDiff is now
an alias for Edit<string> so count_op/apply work on line diffs; Memoized
uses std::apply instead of type-based get to handle duplicate arg types.

874/877 tests pass (3 are platform-gated plugin tests).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 23:49:09 +03:00

198 lines
7.0 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, 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