|
|
@@ -15,11 +15,13 @@ std::string fromWstring(const std::wstring &s) { |
|
|
|
return converter.to_bytes(s); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::wstring toWstring(const std::string &s) { |
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; |
|
|
|
return converter.from_bytes(s); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string f(const char *format, ...) { |
|
|
|
va_list args; |
|
|
|
va_start(args, format); |
|
|
@@ -37,18 +39,21 @@ std::string f(const char *format, ...) { |
|
|
|
return s; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string lowercase(const std::string &s) { |
|
|
|
std::string r = s; |
|
|
|
std::transform(r.begin(), r.end(), r.begin(), [](unsigned char c) { return std::tolower(c); }); |
|
|
|
return r; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string uppercase(const std::string &s) { |
|
|
|
std::string r = s; |
|
|
|
std::transform(r.begin(), r.end(), r.begin(), [](unsigned char c) { return std::toupper(c); }); |
|
|
|
return r; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string trim(const std::string &s) { |
|
|
|
const std::string whitespace = " \n\r\t"; |
|
|
|
size_t first = s.find_first_not_of(whitespace); |
|
|
@@ -60,6 +65,7 @@ std::string trim(const std::string &s) { |
|
|
|
return s.substr(first, last - first + 1); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string ellipsize(const std::string &s, size_t len) { |
|
|
|
if (s.size() <= len) |
|
|
|
return s; |
|
|
@@ -67,6 +73,7 @@ std::string ellipsize(const std::string &s, size_t len) { |
|
|
|
return s.substr(0, len - 3) + "..."; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string ellipsizePrefix(const std::string &s, size_t len) { |
|
|
|
if (s.size() <= len) |
|
|
|
return s; |
|
|
@@ -74,14 +81,17 @@ std::string ellipsizePrefix(const std::string &s, size_t len) { |
|
|
|
return "..." + s.substr(s.size() - (len - 3)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool startsWith(const std::string &str, const std::string &prefix) { |
|
|
|
return str.substr(0, prefix.size()) == prefix; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool endsWith(const std::string &str, const std::string &suffix) { |
|
|
|
return str.substr(str.size() - suffix.size(), suffix.size()) == suffix; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string directory(const std::string &path) { |
|
|
|
char *pathDup = strdup(path.c_str()); |
|
|
|
std::string directory = dirname(pathDup); |
|
|
@@ -89,6 +99,7 @@ std::string directory(const std::string &path) { |
|
|
|
return directory; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string filename(const std::string &path) { |
|
|
|
char *pathDup = strdup(path.c_str()); |
|
|
|
std::string filename = basename(pathDup); |
|
|
@@ -96,6 +107,7 @@ std::string filename(const std::string &path) { |
|
|
|
return filename; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string filenameBase(const std::string &filename) { |
|
|
|
size_t pos = filename.rfind('.'); |
|
|
|
if (pos == std::string::npos) |
|
|
@@ -103,6 +115,7 @@ std::string filenameBase(const std::string &filename) { |
|
|
|
return std::string(filename, 0, pos); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string filenameExtension(const std::string &filename) { |
|
|
|
size_t pos = filename.rfind('.'); |
|
|
|
if (pos == std::string::npos) |
|
|
@@ -110,6 +123,24 @@ std::string filenameExtension(const std::string &filename) { |
|
|
|
return std::string(filename, pos + 1); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string absolutePath(const std::string &path) { |
|
|
|
#if defined ARCH_LIN || defined ARCH_MAC |
|
|
|
char buf[PATH_MAX]; |
|
|
|
char *absPathC = realpath(path.c_str(), buf); |
|
|
|
if (absPathC) |
|
|
|
return absPathC; |
|
|
|
#elif defined ARCH_WIN |
|
|
|
std::wstring pathW = toWstring(path); |
|
|
|
wchar_t buf[PATH_MAX]; |
|
|
|
wchar_t *absPathC = _wfullpath(buf, pathW.c_str(), PATH_MAX); |
|
|
|
if (absPathC) |
|
|
|
return fromWstring(absPathC); |
|
|
|
#endif |
|
|
|
return ""; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
float fuzzyScore(const std::string &s, const std::string &query) { |
|
|
|
size_t pos = s.find(query); |
|
|
|
if (pos == std::string::npos) |
|
|
|