45 lines
869 B
C++
45 lines
869 B
C++
#pragma once
|
|
|
|
#include <cctype>
|
|
#include <functional>
|
|
#include <string_view>
|
|
|
|
static inline bool isSpace(char ch) {
|
|
return std::isspace(ch);
|
|
}
|
|
|
|
static std::string_view ltrim(std::string_view str,
|
|
std::function<bool(char)> filter = isSpace) {
|
|
auto it = str.begin();
|
|
|
|
while (it != str.end()) {
|
|
if (!filter(*it)) {
|
|
break;
|
|
}
|
|
|
|
++it;
|
|
}
|
|
|
|
return {it, str.end()};
|
|
}
|
|
|
|
static std::string_view rtrim(std::string_view str,
|
|
std::function<bool(char)> filter = isSpace) {
|
|
auto it = str.rbegin();
|
|
|
|
while (it != str.rend()) {
|
|
if (!filter(*it)) {
|
|
break;
|
|
}
|
|
|
|
++it;
|
|
}
|
|
|
|
return {str.begin(), it.base()};
|
|
}
|
|
|
|
static std::string_view trim(std::string_view str,
|
|
std::function<bool(char)> filter = isSpace) {
|
|
return ltrim(rtrim(str, filter), filter);
|
|
}
|