lokinet/llarp/util/str.cpp

71 lines
1.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
CaselessCmp::operator()(string_view lhs, string_view rhs) const
{
if(lhs.size() < rhs.size())
{
return true;
}
2019-07-06 17:03:40 +00:00
if(lhs.size() > rhs.size())
{
return false;
}
2019-07-06 17:03:40 +00:00
for(size_t i = 0; i < lhs.size(); ++i)
{
2019-07-06 17:03:40 +00:00
auto l = std::tolower(lhs[i]);
auto r = std::tolower(rhs[i]);
2019-07-06 17:03:40 +00:00
if(l < r)
{
return true;
}
if(l > r)
{
return false;
}
}
2019-07-06 17:03:40 +00:00
return false;
}
bool
IsFalseValue(string_view str)
{
static const std::set< string_view, CaselessCmp > vals{"no", "false", "0",
"off"};
return vals.count(str) > 0;
2018-10-03 11:00:30 +00:00
}
bool
IsTrueValue(string_view str)
2018-10-03 11:00:30 +00:00
{
static const std::set< string_view, CaselessCmp > 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)
{
return strncmp(s1, s2, sz1) == 0;
}
2019-07-06 17:03:40 +00:00
return false;
2018-10-03 11:00:30 +00:00
}
} // namespace llarp