2019-01-10 19:41:51 +00:00
|
|
|
#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>
|
2019-07-02 21:28:28 +00:00
|
|
|
#include <set>
|
2018-10-03 11:00:30 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2019-07-02 09:06:29 +00:00
|
|
|
bool
|
2020-02-22 19:21:08 +00:00
|
|
|
CaselessLessThan::operator()(string_view lhs, string_view rhs) const
|
2019-06-28 10:10:01 +00:00
|
|
|
{
|
2020-02-22 19:21:08 +00:00
|
|
|
const size_t s = std::min(lhs.size(), rhs.size());
|
|
|
|
for(size_t i = 0; i < s; ++i)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2019-07-06 17:03:40 +00:00
|
|
|
auto l = std::tolower(lhs[i]);
|
|
|
|
auto r = std::tolower(rhs[i]);
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2019-07-06 17:03:40 +00:00
|
|
|
if(l < r)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(l > r)
|
|
|
|
{
|
|
|
|
return false;
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-22 19:21:08 +00:00
|
|
|
|
|
|
|
return lhs.size() < rhs.size();
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsFalseValue(string_view str)
|
|
|
|
{
|
2020-02-22 19:21:08 +00:00
|
|
|
static const std::set< string_view, CaselessLessThan > vals{"no", "false",
|
|
|
|
"0", "off"};
|
2019-07-02 21:28:28 +00:00
|
|
|
|
|
|
|
return vals.count(str) > 0;
|
2018-10-03 11:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-07-02 21:28:28 +00:00
|
|
|
IsTrueValue(string_view str)
|
2018-10-03 11:00:30 +00:00
|
|
|
{
|
2020-02-22 19:21:08 +00:00
|
|
|
static const std::set< string_view, CaselessLessThan > vals{"yes", "true",
|
|
|
|
"1", "on"};
|
2019-07-02 21:28:28 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return strncmp(s1, s2, sz1) == 0;
|
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
return false;
|
2018-10-03 11:00:30 +00:00
|
|
|
}
|
2020-02-22 19:55:53 +00:00
|
|
|
|
|
|
|
constexpr static char whitespace[] = " \t\n\r\f\v";
|
2020-02-23 02:23:19 +00:00
|
|
|
|
|
|
|
string_view
|
2020-02-23 03:08:27 +00:00
|
|
|
TrimWhitespace(string_view str)
|
2020-02-22 19:55:53 +00:00
|
|
|
{
|
|
|
|
size_t begin = str.find_first_not_of(whitespace);
|
2020-02-23 02:23:19 +00:00
|
|
|
if(begin == string_view::npos)
|
2020-02-22 19:55:53 +00:00
|
|
|
{
|
2020-02-23 02:23:19 +00:00
|
|
|
str.remove_prefix(str.size());
|
|
|
|
return str;
|
2020-02-22 19:55:53 +00:00
|
|
|
}
|
2020-02-23 02:23:19 +00:00
|
|
|
str.remove_prefix(begin);
|
|
|
|
|
|
|
|
size_t end = str.find_last_not_of(whitespace);
|
|
|
|
if(end != string_view::npos)
|
|
|
|
str.remove_suffix(str.size() - end - 1);
|
|
|
|
|
|
|
|
return str;
|
2020-02-22 19:55:53 +00:00
|
|
|
}
|
2018-10-03 11:00:30 +00:00
|
|
|
} // namespace llarp
|