339 lines
10 KiB
C++
339 lines
10 KiB
C++
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <charconv>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#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();
|
|
}
|
|
|
|
// ── ASCII-explicit helpers (locale-independent, bytes >= 128 preserved) ───────
|
|
|
|
[[nodiscard]] constexpr bool is_ascii_space(char c) noexcept
|
|
{
|
|
const auto uc = static_cast<unsigned char>(c);
|
|
return uc == ' ' || (uc >= '\t' && uc <= '\r');
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool is_ascii_alpha(char c) noexcept
|
|
{
|
|
const auto uc = static_cast<unsigned char>(c);
|
|
return (uc >= 'a' && uc <= 'z') || (uc >= 'A' && uc <= 'Z');
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool is_ascii_digit(char c) noexcept
|
|
{
|
|
const auto uc = static_cast<unsigned char>(c);
|
|
return uc >= '0' && uc <= '9';
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool is_ascii_alnum(char c) noexcept
|
|
{
|
|
return is_ascii_alpha(c) || is_ascii_digit(c);
|
|
}
|
|
|
|
[[nodiscard]] constexpr char to_lower_ascii_char(char c) noexcept
|
|
{
|
|
const auto uc = static_cast<unsigned char>(c);
|
|
if (uc >= 'A' && uc <= 'Z') {
|
|
return static_cast<char>(uc + ('a' - 'A'));
|
|
}
|
|
return c;
|
|
}
|
|
|
|
[[nodiscard]] constexpr char to_upper_ascii_char(char c) noexcept
|
|
{
|
|
const auto uc = static_cast<unsigned char>(c);
|
|
if (uc >= 'a' && uc <= 'z') {
|
|
return static_cast<char>(uc - ('a' - 'A'));
|
|
}
|
|
return c;
|
|
}
|
|
|
|
[[nodiscard]] constexpr std::string_view trim_left_ascii(std::string_view s) noexcept
|
|
{
|
|
std::size_t pos = 0;
|
|
while (pos < s.size() && is_ascii_space(s[pos])) {
|
|
++pos;
|
|
}
|
|
s.remove_prefix(pos);
|
|
return s;
|
|
}
|
|
|
|
[[nodiscard]] constexpr std::string_view trim_right_ascii(std::string_view s) noexcept
|
|
{
|
|
std::size_t len = s.size();
|
|
while (len > 0 && is_ascii_space(s[len - 1])) {
|
|
--len;
|
|
}
|
|
return s.substr(0, len);
|
|
}
|
|
|
|
[[nodiscard]] constexpr std::string_view trim_ascii(std::string_view s) noexcept
|
|
{
|
|
return trim_right_ascii(trim_left_ascii(s));
|
|
}
|
|
|
|
[[nodiscard]] inline std::string to_lower_ascii(std::string_view s)
|
|
{
|
|
std::string out;
|
|
out.reserve(s.size());
|
|
for (const char c : s) {
|
|
out.push_back(to_lower_ascii_char(c));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
[[nodiscard]] inline std::string to_upper_ascii(std::string_view s)
|
|
{
|
|
std::string out;
|
|
out.reserve(s.size());
|
|
for (const char c : s) {
|
|
out.push_back(to_upper_ascii_char(c));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool is_alpha_ascii(std::string_view s) noexcept
|
|
{
|
|
if (s.empty()) return false;
|
|
for (const char c : s) {
|
|
if (!is_ascii_alpha(c)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool is_digit_ascii(std::string_view s) noexcept
|
|
{
|
|
if (s.empty()) return false;
|
|
for (const char c : s) {
|
|
if (!is_ascii_digit(c)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool is_alnum_ascii(std::string_view s) noexcept
|
|
{
|
|
if (s.empty()) return false;
|
|
for (const char c : s) {
|
|
if (!is_ascii_alnum(c)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool iequals_ascii(std::string_view a, std::string_view b) noexcept
|
|
{
|
|
if (a.size() != b.size()) return false;
|
|
for (std::size_t i = 0; i < a.size(); ++i) {
|
|
if (to_lower_ascii_char(a[i]) != to_lower_ascii_char(b[i])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace Void::Str
|