17bool isSpace(
char c) {
return c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n'; }
22char upper(
unsigned char c) {
return static_cast<char>(std::toupper(c)); }
28 while (!text.empty() && isSpace(text.back())) {
29 text.remove_suffix(1);
34std::string_view
trim(std::string_view text)
36 while (!text.empty() && isSpace(text.front())) {
37 text.remove_prefix(1);
44 std::string result(text);
45 std::ranges::transform(result, result.begin(), [](
unsigned char c) { return upper(c); });
51 return std::ranges::equal(a, b, [](
unsigned char x,
unsigned char y) {
return upper(x) == upper(y); });
54bool matchesLike(std::string_view text, std::string_view pattern)
56 constexpr std::size_t kNone = std::string_view::npos;
57 std::size_t t = 0, p = 0;
60 std::size_t lastPercent = kNone, resumeText = 0;
62 while (t < text.size()) {
63 if (p < pattern.size() && pattern[p] ==
'%') {
66 }
else if (p < pattern.size() && (pattern[p] ==
'_' || upper(pattern[p]) == upper(text[t]))) {
69 }
else if (lastPercent != kNone) {
76 while (p < pattern.size() && pattern[p] ==
'%') {
79 return p == pattern.size();
85 if (text.starts_with(
'+')) {
86 text.remove_prefix(1);
92 const char* end = text.data() + text.size();
93 auto [ptr, ec] = std::from_chars(text.data(), end, value);
94 if (ec != std::errc{} || ptr != end) {
The SqlError exception and small string helpers used everywhere.
std::optional< long long > parseInteger(std::string_view text)
Parses a whole string as a base-10 integer.
std::string toUpper(std::string_view text)
Converts ASCII letters to upper case.
std::string_view trimRight(std::string_view text)
Removes trailing spaces, tabs and line ends.
bool matchesLike(std::string_view text, std::string_view pattern)
Matches text against an SQL LIKE pattern, ignoring the case of ASCII letters.
bool equalsIgnoreCase(std::string_view a, std::string_view b)
Compares two strings, ignoring the case of ASCII letters.
std::string_view trim(std::string_view text)
Removes leading and trailing spaces, tabs and line ends.
char upper(unsigned char c)
Upper-cases one ASCII character.
bool isSpace(char c)
Tells whether a character is whitespace for trimming.