Skip to content

Commit

Permalink
clang format
Browse files Browse the repository at this point in the history
  • Loading branch information
briaguya-ai committed May 1, 2024
1 parent 02375de commit 5c77245
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 292 deletions.
203 changes: 86 additions & 117 deletions src/utils/StringHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#define vsprintf_s vsprintf
#endif

std::vector<std::string> StringHelper::Split(std::string s, const std::string& delimiter)
{
std::vector<std::string> StringHelper::Split(std::string s, const std::string& delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
std::vector<std::string> res;
Expand All @@ -25,14 +24,12 @@ std::vector<std::string> StringHelper::Split(std::string s, const std::string& d
return res;
}

std::vector<std::string_view> StringHelper::Split(std::string_view s, const std::string& delimiter)
{
std::vector<std::string_view> StringHelper::Split(std::string_view s, const std::string& delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string_view token;
std::vector<std::string_view> res;

while ((pos_end = s.find(delimiter, pos_start)) != std::string_view::npos)
{
while ((pos_end = s.find(delimiter, pos_start)) != std::string_view::npos) {
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
Expand All @@ -42,150 +39,122 @@ std::vector<std::string_view> StringHelper::Split(std::string_view s, const std:
return res;
}

std::string StringHelper::Strip(std::string s, const std::string& delimiter)
{
size_t pos = 0;
std::string token;
std::string StringHelper::Strip(std::string s, const std::string& delimiter) {
size_t pos = 0;
std::string token;

while ((pos = s.find(delimiter)) != std::string::npos)
{
token = s.substr(0, pos);
s.erase(pos, pos + delimiter.length());
}
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
s.erase(pos, pos + delimiter.length());
}

return s;
return s;
}

std::string StringHelper::Replace(std::string str, const std::string& from,
const std::string& to)
{
size_t start_pos = str.find(from);
std::string StringHelper::Replace(std::string str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);

while (start_pos != std::string::npos)
{
str.replace(start_pos, from.length(), to);
start_pos = str.find(from);
}
while (start_pos != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos = str.find(from);
}

return str;
return str;
}

void StringHelper::ReplaceOriginal(std::string& str, const std::string& from, const std::string& to)
{
size_t start_pos = str.find(from);
void StringHelper::ReplaceOriginal(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);

while (start_pos != std::string::npos)
{
str.replace(start_pos, from.length(), to);
start_pos = str.find(from);
}
while (start_pos != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos = str.find(from);
}
}

bool StringHelper::StartsWith(const std::string& s, const std::string& input)
{
bool StringHelper::StartsWith(const std::string& s, const std::string& input) {
#if __cplusplus >= 202002L
return s.starts_with(input.c_str());
return s.starts_with(input.c_str());
#else
return s.rfind(input, 0) == 0;
return s.rfind(input, 0) == 0;
#endif
}

bool StringHelper::Contains(const std::string& s, const std::string& input)
{
return s.find(input) != std::string::npos;
bool StringHelper::Contains(const std::string& s, const std::string& input) {
return s.find(input) != std::string::npos;
}

bool StringHelper::EndsWith(const std::string& s, const std::string& input)
{
size_t inputLen = strlen(input.c_str());
return s.rfind(input) == (s.size() - inputLen);
bool StringHelper::EndsWith(const std::string& s, const std::string& input) {
size_t inputLen = strlen(input.c_str());
return s.rfind(input) == (s.size() - inputLen);
}

std::string StringHelper::Sprintf(const char* format, ...)
{
char buffer[32768];
// char buffer[2048];
std::string output;
va_list va;
std::string StringHelper::Sprintf(const char* format, ...) {
char buffer[32768];
// char buffer[2048];
std::string output;
va_list va;

va_start(va, format);
vsprintf_s(buffer, format, va);
va_end(va);
va_start(va, format);
vsprintf_s(buffer, format, va);
va_end(va);

output = buffer;
return output;
output = buffer;
return output;
}

std::string StringHelper::Implode(std::vector<std::string>& elements,
const char* const separator)
{
return "";
std::string StringHelper::Implode(std::vector<std::string>& elements, const char* const separator) {
return "";

// return std::accumulate(std::begin(elements), std::end(elements), std::string(),
//[separator](std::string& ss, std::string& s) {
// return ss.empty() ? s : ss + separator + s;
//});
// return std::accumulate(std::begin(elements), std::end(elements), std::string(),
//[separator](std::string& ss, std::string& s) {
// return ss.empty() ? s : ss + separator + s;
//});
}

int64_t StringHelper::StrToL(const std::string& str, int32_t base)
{
return std::strtoull(str.c_str(), nullptr, base);
int64_t StringHelper::StrToL(const std::string& str, int32_t base) {
return std::strtoull(str.c_str(), nullptr, base);
}

std::string StringHelper::BoolStr(bool b)
{
return b ? "true" : "false";
std::string StringHelper::BoolStr(bool b) {
return b ? "true" : "false";
}

bool StringHelper::HasOnlyDigits(const std::string& str)
{
return std::all_of(str.begin(), str.end(), ::isdigit);
bool StringHelper::HasOnlyDigits(const std::string& str) {
return std::all_of(str.begin(), str.end(), ::isdigit);
}

// Validate a hex string based on the c89 standard
// https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Integer-Constants
bool StringHelper::IsValidHex(std::string_view str)
{
if (str.length() < 3)
{
return false;
}
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
{
return std::all_of(str.begin() + 2, str.end(), ::isxdigit);
}
return false;
}


bool StringHelper::IsValidHex(const std::string& str)
{
return IsValidHex(std::string_view(str.c_str()));
}

bool StringHelper::IsValidOffset(std::string_view str)
{
if (str.length() == 1)
{
// 0 is a valid offset
return isdigit(str[0]);
}
return IsValidHex(str);
}

bool StringHelper::IsValidOffset(const std::string& str)
{
if (str.length() == 1)
{
// 0 is a valid offset
return isdigit(str[0]);
}
return IsValidHex(str);
}


bool StringHelper::IEquals(const std::string& a, const std::string& b)
{
return std::equal(a.begin(), a.end(), b.begin(), b.end(),
[](char a, char b) { return tolower(a) == tolower(b); });
bool StringHelper::IsValidHex(std::string_view str) {
if (str.length() < 3) {
return false;
}
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
return std::all_of(str.begin() + 2, str.end(), ::isxdigit);
}
return false;
}

bool StringHelper::IsValidHex(const std::string& str) {
return IsValidHex(std::string_view(str.c_str()));
}

bool StringHelper::IsValidOffset(std::string_view str) {
if (str.length() == 1) {
// 0 is a valid offset
return isdigit(str[0]);
}
return IsValidHex(str);
}

bool StringHelper::IsValidOffset(const std::string& str) {
if (str.length() == 1) {
// 0 is a valid offset
return isdigit(str[0]);
}
return IsValidHex(str);
}

bool StringHelper::IEquals(const std::string& a, const std::string& b) {
return std::equal(a.begin(), a.end(), b.begin(), b.end(), [](char a, char b) { return tolower(a) == tolower(b); });
}
41 changes: 20 additions & 21 deletions src/utils/StringHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,24 @@
#include <vector>
#include <cstdint>

class StringHelper
{
public:
static std::vector<std::string> Split(std::string s, const std::string& delimiter);
static std::vector<std::string_view> Split(std::string_view s, const std::string& delimiter);
static std::string Strip(std::string s, const std::string& delimiter);
static std::string Replace(std::string str, const std::string& from, const std::string& to);
static void ReplaceOriginal(std::string& str, const std::string& from, const std::string& to);
static bool StartsWith(const std::string& s, const std::string& input);
static bool Contains(const std::string& s, const std::string& input);
static bool EndsWith(const std::string& s, const std::string& input);
static std::string Sprintf(const char* format, ...);
static std::string Implode(std::vector<std::string>& elements, const char* const separator);
static int64_t StrToL(const std::string& str, int32_t base = 10);
static std::string BoolStr(bool b);
static bool HasOnlyDigits(const std::string& str);
static bool IsValidHex(std::string_view str);
static bool IsValidHex(const std::string& str);
static bool IsValidOffset(std::string_view str);
static bool IsValidOffset(const std::string& str);
static bool IEquals(const std::string& a, const std::string& b);
class StringHelper {
public:
static std::vector<std::string> Split(std::string s, const std::string& delimiter);
static std::vector<std::string_view> Split(std::string_view s, const std::string& delimiter);
static std::string Strip(std::string s, const std::string& delimiter);
static std::string Replace(std::string str, const std::string& from, const std::string& to);
static void ReplaceOriginal(std::string& str, const std::string& from, const std::string& to);
static bool StartsWith(const std::string& s, const std::string& input);
static bool Contains(const std::string& s, const std::string& input);
static bool EndsWith(const std::string& s, const std::string& input);
static std::string Sprintf(const char* format, ...);
static std::string Implode(std::vector<std::string>& elements, const char* const separator);
static int64_t StrToL(const std::string& str, int32_t base = 10);
static std::string BoolStr(bool b);
static bool HasOnlyDigits(const std::string& str);
static bool IsValidHex(std::string_view str);
static bool IsValidHex(const std::string& str);
static bool IsValidOffset(std::string_view str);
static bool IsValidOffset(const std::string& str);
static bool IEquals(const std::string& a, const std::string& b);
};
76 changes: 35 additions & 41 deletions src/utils/filesystemtools/Directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,39 @@ namespace fs = std::experimental::filesystem;
#undef GetCurrentDirectory
#undef CreateDirectory

class Directory
{
public:
#ifndef PATH_HACK
static std::string GetCurrentDirectory() { return fs::current_path().string(); }
#endif

static bool Exists(const fs::path& path) { return fs::exists(path); }

// Stupid hack because of Windows.h
static void MakeDirectory(const std::string& path)
{
CreateDirectory(path);
}

static void CreateDirectory(const std::string& path)
{
try
{
fs::create_directories(path);
}
catch (...)
{
}
}

static std::vector<std::string> ListFiles(const std::string& dir)
{
std::vector<std::string> lst;

if (Exists(dir))
{
for (auto& p : fs::recursive_directory_iterator(dir))
{
if (!p.is_directory())
lst.push_back(p.path().generic_string());
}
}

return lst;
}
class Directory {
public:
#ifndef PATH_HACK
static std::string GetCurrentDirectory() {
return fs::current_path().string();
}
#endif

static bool Exists(const fs::path& path) {
return fs::exists(path);
}

// Stupid hack because of Windows.h
static void MakeDirectory(const std::string& path) {
CreateDirectory(path);
}

static void CreateDirectory(const std::string& path) {
try {
fs::create_directories(path);
} catch (...) {}
}

static std::vector<std::string> ListFiles(const std::string& dir) {
std::vector<std::string> lst;

if (Exists(dir)) {
for (auto& p : fs::recursive_directory_iterator(dir)) {
if (!p.is_directory())
lst.push_back(p.path().generic_string());
}
}

return lst;
}
};
Loading

0 comments on commit 5c77245

Please sign in to comment.