2026-08-11 17:40:36 +03:00
|
|
|
#include "std_string.hpp"
|
|
|
|
|
|
2026-08-15 16:55:46 +03:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <locale>
|
|
|
|
|
#include <ranges>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2026-08-11 17:40:36 +03:00
|
|
|
namespace arrays {
|
|
|
|
|
|
2026-08-15 16:55:46 +03:00
|
|
|
auto to_uppercase(const std::string &text) -> std::string {
|
|
|
|
|
// Returns copy of the string without mutating the original string
|
|
|
|
|
auto s = std::string{text};
|
|
|
|
|
auto c_to_upper = [](const unsigned char c) { return std::toupper(c); };
|
|
|
|
|
std::ranges::transform(s, s.begin(), c_to_upper);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto trim(const std::string& text) -> std::string {
|
|
|
|
|
// Returns a trimmed copy of the string
|
|
|
|
|
auto s = std::string{text};
|
|
|
|
|
auto is_not_space = [](const unsigned char c) { return !std::isspace(c); };
|
|
|
|
|
const auto first = std::ranges::find_if(s, is_not_space);
|
|
|
|
|
const auto last = std::ranges::find_if(s.rbegin(), s.rend(), is_not_space).base();
|
|
|
|
|
s.erase(last, s.end());
|
|
|
|
|
s.erase(s.begin(), first);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto replace_all(const std::string &text, const char from, const char to) -> std::string {
|
|
|
|
|
// This one also copies the string
|
|
|
|
|
auto s = std::string{text};
|
|
|
|
|
std::ranges::replace(s, from, to);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto starts_with(const std::string& text, const std::string& prefix) -> bool {
|
|
|
|
|
// Since C++20 you can use std::string::starts_with, but anyways.
|
|
|
|
|
if (text.size() < prefix.size()) return false;
|
|
|
|
|
// uz -> unsigned size_t
|
|
|
|
|
for (auto idx = 0uz; idx < prefix.size(); ++idx) {
|
|
|
|
|
if (text[idx] != prefix[idx]) return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-08-11 17:40:36 +03:00
|
|
|
|
|
|
|
|
} // namespace arrays
|