mirror of
https://github.com/Detanup01/gbe_fork.git
synced 2024-11-23 19:25:35 +08:00
use string_view in common helpers
This commit is contained in:
parent
4860e2c079
commit
9221465e57
@ -19,19 +19,19 @@ static bool create_dir_impl(std::filesystem::path &dirpath)
|
|||||||
return std::filesystem::create_directories(dirpath);
|
return std::filesystem::create_directories(dirpath);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool common_helpers::create_dir(const std::string &filepath)
|
bool common_helpers::create_dir(const std::string_view &filepath)
|
||||||
{
|
{
|
||||||
std::filesystem::path parent = std::filesystem::path(filepath).parent_path();
|
std::filesystem::path parent(std::filesystem::path(filepath).parent_path());
|
||||||
return create_dir_impl(parent);
|
return create_dir_impl(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool common_helpers::create_dir(const std::wstring &filepath)
|
bool common_helpers::create_dir(const std::wstring_view &filepath)
|
||||||
{
|
{
|
||||||
std::filesystem::path parent = std::filesystem::path(filepath).parent_path();
|
std::filesystem::path parent(std::filesystem::path(filepath).parent_path());
|
||||||
return create_dir_impl(parent);
|
return create_dir_impl(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
void common_helpers::write(std::ofstream &file, const std::string &data)
|
void common_helpers::write(std::ofstream &file, const std::string_view &data)
|
||||||
{
|
{
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
return;
|
return;
|
||||||
@ -40,7 +40,7 @@ void common_helpers::write(std::ofstream &file, const std::string &data)
|
|||||||
file << data << std::endl;
|
file << data << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring common_helpers::str_to_w(const std::string &str)
|
std::wstring common_helpers::str_to_w(const std::string_view &str)
|
||||||
{
|
{
|
||||||
if (str.empty()) return std::wstring();
|
if (str.empty()) return std::wstring();
|
||||||
auto cvt_state = std::mbstate_t();
|
auto cvt_state = std::mbstate_t();
|
||||||
@ -51,7 +51,7 @@ std::wstring common_helpers::str_to_w(const std::string &str)
|
|||||||
return res.substr(0, conversion_bytes);
|
return res.substr(0, conversion_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string common_helpers::wstr_to_a(const std::wstring &wstr)
|
std::string common_helpers::wstr_to_a(const std::wstring_view &wstr)
|
||||||
{
|
{
|
||||||
if (wstr.empty()) return std::string();
|
if (wstr.empty()) return std::string();
|
||||||
auto cvt_state = std::mbstate_t();
|
auto cvt_state = std::mbstate_t();
|
||||||
@ -62,53 +62,45 @@ std::string common_helpers::wstr_to_a(const std::wstring &wstr)
|
|||||||
return res.substr(0, conversion_bytes);
|
return res.substr(0, conversion_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool common_helpers::starts_with_i(const std::string &target, const std::string &query)
|
bool common_helpers::starts_with_i(const std::string_view &target, const std::string_view &query)
|
||||||
{
|
|
||||||
return starts_with_i(str_to_w(target), str_to_w(query));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool common_helpers::starts_with_i(const std::wstring &target, const std::wstring &query)
|
|
||||||
{
|
{
|
||||||
if (target.size() < query.size()) {
|
if (target.size() < query.size()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto _target = std::wstring(target);
|
return str_cmp_insensitive(target.substr(0, query.size()), query);
|
||||||
auto _query = std::wstring(query);
|
|
||||||
std::transform(_target.begin(), _target.end(), _target.begin(),
|
|
||||||
[](wchar_t c) { return std::tolower(c); });
|
|
||||||
|
|
||||||
std::transform(_query.begin(), _query.end(), _query.begin(),
|
|
||||||
[](wchar_t c) { return std::tolower(c); });
|
|
||||||
|
|
||||||
return _target.compare(0, _query.length(), _query) == 0;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool common_helpers::ends_with_i(const std::string &target, const std::string &query)
|
bool common_helpers::starts_with_i(const std::wstring_view &target, const std::wstring_view &query)
|
||||||
{
|
|
||||||
return ends_with_i(str_to_w(target), str_to_w(query));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool common_helpers::ends_with_i(const std::wstring &target, const std::wstring &query)
|
|
||||||
{
|
{
|
||||||
if (target.size() < query.size()) {
|
if (target.size() < query.size()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto _target = std::wstring(target);
|
return str_cmp_insensitive(target.substr(0, query.size()), query);
|
||||||
auto _query = std::wstring(query);
|
|
||||||
std::transform(_target.begin(), _target.end(), _target.begin(),
|
|
||||||
[](wchar_t c) { return std::tolower(c); });
|
|
||||||
|
|
||||||
std::transform(_query.begin(), _query.end(), _query.begin(),
|
|
||||||
[](wchar_t c) { return std::tolower(c); });
|
|
||||||
|
|
||||||
return _target.compare(_target.length() - _query.length(), _query.length(), _query) == 0;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string common_helpers::string_strip(const std::string& str)
|
bool common_helpers::ends_with_i(const std::string_view &target, const std::string_view &query)
|
||||||
|
{
|
||||||
|
if (target.size() < query.size()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto target_offset = target.length() - query.length();
|
||||||
|
return str_cmp_insensitive(target.substr(target_offset), query);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool common_helpers::ends_with_i(const std::wstring_view &target, const std::wstring_view &query)
|
||||||
|
{
|
||||||
|
if (target.size() < query.size()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto target_offset = target.length() - query.length();
|
||||||
|
return str_cmp_insensitive(target.substr(target_offset), query);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string common_helpers::string_strip(const std::string_view &str)
|
||||||
{
|
{
|
||||||
static constexpr const char whitespaces[] = " \t\r\n";
|
static constexpr const char whitespaces[] = " \t\r\n";
|
||||||
|
|
||||||
@ -124,18 +116,17 @@ std::string common_helpers::string_strip(const std::string& str)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return str.substr(start, end - start + 1);
|
return std::string(str.substr(start, end - start + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string common_helpers::uint8_vector_to_hex_string(const std::vector<uint8_t>& v)
|
std::string common_helpers::uint8_vector_to_hex_string(const std::vector<uint8_t> &v)
|
||||||
{
|
{
|
||||||
std::string result{};
|
std::string result{};
|
||||||
result.reserve(v.size() * 2); // two digits per character
|
result.reserve(v.size() * 2); // two digits per character
|
||||||
|
|
||||||
static constexpr const char hex[] = "0123456789ABCDEF";
|
static constexpr const char hex[] = "0123456789ABCDEF";
|
||||||
|
|
||||||
for (uint8_t c : v)
|
for (uint8_t c : v) {
|
||||||
{
|
|
||||||
result.push_back(hex[c / 16]);
|
result.push_back(hex[c / 16]);
|
||||||
result.push_back(hex[c % 16]);
|
result.push_back(hex[c % 16]);
|
||||||
}
|
}
|
||||||
@ -143,7 +134,7 @@ std::string common_helpers::uint8_vector_to_hex_string(const std::vector<uint8_t
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool common_helpers::str_cmp_insensitive(const std::string &str1, const std::string &str2)
|
bool common_helpers::str_cmp_insensitive(const std::string_view &str1, const std::string_view &str2)
|
||||||
{
|
{
|
||||||
if (str1.size() != str2.size()) return false;
|
if (str1.size() != str2.size()) return false;
|
||||||
|
|
||||||
@ -152,6 +143,15 @@ bool common_helpers::str_cmp_insensitive(const std::string &str1, const std::str
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool common_helpers::str_cmp_insensitive(const std::wstring_view &str1, const std::wstring_view &str2)
|
||||||
|
{
|
||||||
|
if (str1.size() != str2.size()) return false;
|
||||||
|
|
||||||
|
return std::equal(str1.begin(), str1.end(), str2.begin(), [](const wchar_t c1, const wchar_t c2){
|
||||||
|
return std::toupper(c1) == std::toupper(c2);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
std::string common_helpers::ascii_to_lowercase(std::string data) {
|
std::string common_helpers::ascii_to_lowercase(std::string data) {
|
||||||
std::transform(data.begin(), data.end(), data.begin(),
|
std::transform(data.begin(), data.end(), data.begin(),
|
||||||
[](char c){ return std::tolower(c); });
|
[](char c){ return std::tolower(c); });
|
||||||
@ -182,28 +182,28 @@ void common_helpers::consume_bom(std::ifstream &input)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string common_helpers::to_lower(std::string str)
|
std::string common_helpers::to_lower(const std::string_view &str)
|
||||||
{
|
{
|
||||||
std::string _str(str.size(), '\0');
|
std::string _str(str.size(), '\0');
|
||||||
std::transform(str.begin(), str.end(), _str.begin(), [](char c) { return std::tolower(c); });
|
std::transform(str.begin(), str.end(), _str.begin(), [](char c) { return std::tolower(c); });
|
||||||
return _str;
|
return _str;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring common_helpers::to_lower(std::wstring wstr)
|
std::wstring common_helpers::to_lower(const std::wstring_view &wstr)
|
||||||
{
|
{
|
||||||
std::wstring _wstr(wstr.size(), '\0');
|
std::wstring _wstr(wstr.size(), '\0');
|
||||||
std::transform(wstr.begin(), wstr.end(), _wstr.begin(), [](wchar_t c) { return std::tolower(c); });
|
std::transform(wstr.begin(), wstr.end(), _wstr.begin(), [](wchar_t c) { return std::tolower(c); });
|
||||||
return _wstr;
|
return _wstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string common_helpers::to_upper(std::string str)
|
std::string common_helpers::to_upper(const std::string_view &str)
|
||||||
{
|
{
|
||||||
std::string _str(str.size(), '\0');
|
std::string _str(str.size(), '\0');
|
||||||
std::transform(str.begin(), str.end(), _str.begin(), [](char c) { return std::toupper(c); });
|
std::transform(str.begin(), str.end(), _str.begin(), [](char c) { return std::toupper(c); });
|
||||||
return _str;
|
return _str;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring common_helpers::to_upper(std::wstring wstr)
|
std::wstring common_helpers::to_upper(const std::wstring_view &wstr)
|
||||||
{
|
{
|
||||||
std::wstring _wstr(wstr.size(), '\0');
|
std::wstring _wstr(wstr.size(), '\0');
|
||||||
std::transform(wstr.begin(), wstr.end(), _wstr.begin(), [](wchar_t c) { return std::toupper(c); });
|
std::transform(wstr.begin(), wstr.end(), _wstr.begin(), [](wchar_t c) { return std::toupper(c); });
|
||||||
@ -219,9 +219,9 @@ static std::filesystem::path to_absolute_impl(const std::filesystem::path &path,
|
|||||||
return std::filesystem::absolute(base / path);
|
return std::filesystem::absolute(base / path);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string common_helpers::to_absolute(const std::string &path, const std::string &base)
|
std::string common_helpers::to_absolute(const std::string_view &path, const std::string_view &base)
|
||||||
{
|
{
|
||||||
if (path.empty()) return path;
|
if (path.empty()) return std::string(path);
|
||||||
auto path_abs = to_absolute_impl(
|
auto path_abs = to_absolute_impl(
|
||||||
std::filesystem::path(path),
|
std::filesystem::path(path),
|
||||||
base.empty() ? std::filesystem::current_path() : std::filesystem::path(base)
|
base.empty() ? std::filesystem::current_path() : std::filesystem::path(base)
|
||||||
@ -229,9 +229,9 @@ std::string common_helpers::to_absolute(const std::string &path, const std::stri
|
|||||||
return path_abs.u8string();
|
return path_abs.u8string();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring common_helpers::to_absolute(const std::wstring &path, const std::wstring &base)
|
std::wstring common_helpers::to_absolute(const std::wstring_view &path, const std::wstring_view &base)
|
||||||
{
|
{
|
||||||
if (path.empty()) return path;
|
if (path.empty()) return std::wstring(path);
|
||||||
auto path_abs = to_absolute_impl(
|
auto path_abs = to_absolute_impl(
|
||||||
std::filesystem::path(path),
|
std::filesystem::path(path),
|
||||||
base.empty() ? std::filesystem::current_path() : std::filesystem::path(base)
|
base.empty() ? std::filesystem::current_path() : std::filesystem::path(base)
|
||||||
@ -294,14 +294,14 @@ bool common_helpers::dir_exist(const std::filesystem::path &dirpath)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool common_helpers::dir_exist(const std::string &dirpath)
|
bool common_helpers::dir_exist(const std::string_view &dirpath)
|
||||||
{
|
{
|
||||||
if (dirpath.empty()) return false;
|
if (dirpath.empty()) return false;
|
||||||
std::filesystem::path path(dirpath);
|
std::filesystem::path path(dirpath);
|
||||||
return dir_exist(path);
|
return dir_exist(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool common_helpers::dir_exist(const std::wstring &dirpath)
|
bool common_helpers::dir_exist(const std::wstring_view &dirpath)
|
||||||
{
|
{
|
||||||
if (dirpath.empty()) return false;
|
if (dirpath.empty()) return false;
|
||||||
std::filesystem::path path(dirpath);
|
std::filesystem::path path(dirpath);
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@ -9,29 +10,26 @@
|
|||||||
|
|
||||||
namespace common_helpers {
|
namespace common_helpers {
|
||||||
|
|
||||||
bool create_dir(const std::string &dir);
|
bool create_dir(const std::string_view &dir);
|
||||||
|
bool create_dir(const std::wstring_view &dir);
|
||||||
|
|
||||||
bool create_dir(const std::wstring &dir);
|
void write(std::ofstream &file, const std::string_view &data);
|
||||||
|
|
||||||
void write(std::ofstream &file, const std::string &data);
|
std::wstring str_to_w(const std::string_view &str);
|
||||||
|
std::string wstr_to_a(const std::wstring_view &wstr);
|
||||||
|
|
||||||
std::wstring str_to_w(const std::string &str);
|
bool starts_with_i(const std::string_view &target, const std::string_view &query);
|
||||||
|
bool starts_with_i(const std::wstring_view &target, const std::wstring_view &query);
|
||||||
|
|
||||||
std::string wstr_to_a(const std::wstring &wstr);
|
bool ends_with_i(const std::string_view &target, const std::string_view &query);
|
||||||
|
bool ends_with_i(const std::wstring_view &target, const std::wstring_view &query);
|
||||||
|
|
||||||
bool starts_with_i(const std::string &target, const std::string &query);
|
std::string string_strip(const std::string_view &str);
|
||||||
|
|
||||||
bool starts_with_i(const std::wstring &target, const std::wstring &query);
|
std::string uint8_vector_to_hex_string(const std::vector<uint8_t> &v);
|
||||||
|
|
||||||
bool ends_with_i(const std::string &target, const std::string &query);
|
bool str_cmp_insensitive(const std::string_view &str1, const std::string_view &str2);
|
||||||
|
bool str_cmp_insensitive(const std::wstring_view &str1, const std::wstring_view &str2);
|
||||||
bool ends_with_i(const std::wstring &target, const std::wstring &query);
|
|
||||||
|
|
||||||
std::string string_strip(const std::string& str);
|
|
||||||
|
|
||||||
std::string uint8_vector_to_hex_string(const std::vector<uint8_t>& v);
|
|
||||||
|
|
||||||
bool str_cmp_insensitive(const std::string &str1, const std::string &str2);
|
|
||||||
|
|
||||||
std::string ascii_to_lowercase(std::string data);
|
std::string ascii_to_lowercase(std::string data);
|
||||||
|
|
||||||
@ -39,34 +37,25 @@ void thisThreadYieldFor(std::chrono::microseconds u);
|
|||||||
|
|
||||||
void consume_bom(std::ifstream &input);
|
void consume_bom(std::ifstream &input);
|
||||||
|
|
||||||
std::string to_lower(std::string str);
|
std::string to_lower(const std::string_view &str);
|
||||||
|
std::wstring to_lower(const std::wstring_view &wstr);
|
||||||
|
|
||||||
std::wstring to_lower(std::wstring wstr);
|
std::string to_upper(const std::string_view &str);
|
||||||
|
std::wstring to_upper(const std::wstring_view &wstr);
|
||||||
|
|
||||||
std::string to_upper(std::string str);
|
std::string to_absolute(const std::string_view &path, const std::string_view &base = std::string_view());
|
||||||
|
std::wstring to_absolute(const std::wstring_view &path, const std::wstring_view &base = std::wstring_view());
|
||||||
std::wstring to_upper(std::wstring wstr);
|
|
||||||
|
|
||||||
std::string to_absolute(const std::string &path, const std::string &base = std::string());
|
|
||||||
|
|
||||||
std::wstring to_absolute(const std::wstring &path, const std::wstring &base = std::wstring());
|
|
||||||
|
|
||||||
bool file_exist(const std::filesystem::path &filepath);
|
bool file_exist(const std::filesystem::path &filepath);
|
||||||
|
|
||||||
bool file_exist(const std::string &filepath);
|
bool file_exist(const std::string &filepath);
|
||||||
|
|
||||||
bool file_exist(const std::wstring &filepath);
|
bool file_exist(const std::wstring &filepath);
|
||||||
|
|
||||||
bool file_size(const std::filesystem::path &filepath, size_t &size);
|
bool file_size(const std::filesystem::path &filepath, size_t &size);
|
||||||
|
|
||||||
bool file_size(const std::string &filepath, size_t &size);
|
bool file_size(const std::string &filepath, size_t &size);
|
||||||
|
|
||||||
bool file_size(const std::wstring &filepath, size_t &size);
|
bool file_size(const std::wstring &filepath, size_t &size);
|
||||||
|
|
||||||
bool dir_exist(const std::filesystem::path &dirpath);
|
bool dir_exist(const std::filesystem::path &dirpath);
|
||||||
|
bool dir_exist(const std::string_view &dirpath);
|
||||||
bool dir_exist(const std::string &dirpath);
|
bool dir_exist(const std::wstring_view &dirpath);
|
||||||
|
|
||||||
bool dir_exist(const std::wstring &dirpath);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user