Select Git revision
-
Franck Dary authoredFranck Dary authored
util.cpp 6.32 KiB
#include "util.hpp"
#include "utf8.hpp"
#include <ctime>
#include <algorithm>
#include "upper2lower"
int util::printedLength(std::string_view s)
{
return splitAsUtf8(s).size();
}
std::string_view util::getFilenameFromPath(std::string_view s)
{
int indexOfSlash = s.find_last_of('/');
return {s.data()+indexOfSlash+1, s.size()-1-indexOfSlash};
}
bool util::isSeparator(utf8char c)
{
return c == ' ' || isIllegal(c);
}
bool util::isIllegal(utf8char c)
{
return c == '\n' || c == '\t';
}
bool util::isNumber(const std::string & s)
{
static std::map<utf8char, bool> digits{{"0",true},{"1",true},{"2",true},{"3",true},{"4",true},{"5",true},{"6",true},{"7",true},{"8",true},{"9",true},};
utf8string asUtf8 = splitAsUtf8(s);
bool hasDigit = false;
for (auto & c : asUtf8)
if (digits.count(c))
hasDigit = true;
else if (lower2upper.count(c) or upper2lower.count(c))
return false;
return hasDigit;
}
bool util::isUrl(const std::string & s)
{
return s.size() >= 4 and s[0] == 'h' and s[1] == 't' and s[2] == 't' and s[3] == 'p';
}
std::vector<std::string> util::split(std::string_view remaining, char delimiter)
{
std::vector<std::string> result;
for (auto firstDelimiterIndex = remaining.find_first_of(delimiter); firstDelimiterIndex != std::string_view::npos; firstDelimiterIndex = remaining.find_first_of(delimiter))
{
if (remaining[0] != delimiter)
result.emplace_back(remaining.data(), firstDelimiterIndex);
remaining = std::string_view(remaining.data()+firstDelimiterIndex+1, remaining.size()-1-firstDelimiterIndex);
}
if (remaining.size() > 0)
result.emplace_back(remaining);
return result;
}
util::utf8string util::splitAsUtf8(std::string_view s)
{
utf8string result;
const char * beginPtr = s.data();
const char * currentPtr = beginPtr;
const char * endPtr = s.data()+s.size();