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
@@ -3,6 +3,8 @@
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <charconv>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <system_error>
|
||||
@@ -208,4 +210,129 @@ template <typename Range>
|
||||
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
|
||||
Reference in new issue
Block a user