lokinet/llarp/util/str.cpp

112 lines
2.2 KiB
C++
Raw Normal View History

#include <util/str.hpp>
2018-10-03 11:00:30 +00:00
2018-12-12 02:17:40 +00:00
#include <algorithm>
2019-07-16 23:27:09 +00:00
#include <cctype>
2018-10-03 11:00:30 +00:00
#include <cstring>
#include <string>
#include <set>
2018-10-03 11:00:30 +00:00
namespace llarp
{
2019-07-02 09:06:29 +00:00
bool
CaselessLessThan::operator()(std::string_view lhs, std::string_view rhs) const
{
const size_t s = std::min(lhs.size(), rhs.size());
for (size_t i = 0; i < s; ++i)
{
2019-07-06 17:03:40 +00:00
auto l = std::tolower(lhs[i]);
auto r = std::tolower(rhs[i]);
if (l < r)
2019-07-06 17:03:40 +00:00
{
return true;
}
if (l > r)
2019-07-06 17:03:40 +00:00
{
return false;
}
}
return lhs.size() < rhs.size();
}
bool
IsFalseValue(std::string_view str)
{
static const std::set<std::string_view, CaselessLessThan> vals{"no", "false", "0", "off"};
return vals.count(str) > 0;
2018-10-03 11:00:30 +00:00
}
bool
IsTrueValue(std::string_view str)
2018-10-03 11:00:30 +00:00
{
static const std::set<std::string_view, CaselessLessThan> vals{"yes", "true", "1", "on"};
return vals.count(str) > 0;
2018-10-03 11:00:30 +00:00
}
bool
StrEq(const char* s1, const char* s2)
{
size_t sz1 = strlen(s1);
size_t sz2 = strlen(s2);
if (sz1 == sz2)
2018-10-03 11:00:30 +00:00
{
return strncmp(s1, s2, sz1) == 0;
}
2019-07-06 17:03:40 +00:00
return false;
2018-10-03 11:00:30 +00:00
}
constexpr static char whitespace[] = " \t\n\r\f\v";
std::string_view
TrimWhitespace(std::string_view str)
{
size_t begin = str.find_first_not_of(whitespace);
if (begin == std::string_view::npos)
{
str.remove_prefix(str.size());
return str;
}
str.remove_prefix(begin);
size_t end = str.find_last_not_of(whitespace);
if (end != std::string_view::npos)
str.remove_suffix(str.size() - end - 1);
return str;
}
std::vector<std::string_view>
2020-05-21 14:26:51 +00:00
split(const std::string_view str, char delimiter)
{
std::vector<std::string_view> splits;
2020-05-21 14:26:51 +00:00
const auto str_size = str.size();
size_t last = 0;
size_t next = 0;
2020-05-21 14:26:51 +00:00
while (last < str_size and next < std::string_view::npos)
{
next = str.find_first_of(delimiter, last);
if (next > last)
{
splits.push_back(str.substr(last, next - last));
last = next;
// advance to next non-delimiter
2020-05-21 14:26:51 +00:00
while (str[last] == delimiter and last < str_size)
last++;
}
else
{
break;
}
}
return splits;
}
2018-10-03 11:00:30 +00:00
} // namespace llarp