diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index a49ec18..ac764bd 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -1,2 +1,2 @@ -add_library(linuxdeploy_util STATIC magicwrapper.cpp magicwrapper.h util.h) +add_library(linuxdeploy_util STATIC magicwrapper.cpp magicwrapper.h util.h misc.h) target_include_directories(linuxdeploy_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/util/misc.h b/src/util/misc.h new file mode 100644 index 0000000..9b0f537 --- /dev/null +++ b/src/util/misc.h @@ -0,0 +1,67 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace linuxdeploy { + namespace util { + namespace misc { + static inline bool ltrim(std::string& s, char to_trim = ' ') { + // TODO: find more efficient way to check whether elements have been removed + size_t initialLength = s.length(); + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [to_trim](int ch) { + return ch != to_trim; + })); + return s.length() < initialLength; + } + + static inline bool rtrim(std::string& s, char to_trim = ' ') { + // TODO: find more efficient way to check whether elements have been removed + auto initialLength = s.length(); + s.erase(std::find_if(s.rbegin(), s.rend(), [to_trim](int ch) { + return ch != to_trim; + }).base(), s.end()); + return s.length() < initialLength; + } + + static inline bool trim(std::string& s, char to_trim = ' ') { + // returns true if either modifies s + auto ltrim_result = ltrim(s, to_trim); + return rtrim(s, to_trim) && ltrim_result; + } + + static std::vector split(const std::string& s, char delim = ' ') { + std::vector result; + + std::stringstream ss(s); + std::string item; + + while (std::getline(ss, item, delim)) { + result.push_back(item); + } + + return result; + } + + static std::vector splitLines(const std::string& s) { + return split(s, '\n'); + } + + static inline std::string strLower(std::string s) { + std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); }); + return s; + } + + static bool stringStartsWith(const std::string& string, const std::string& prefix) { + // sanity check + if (string.size() < prefix.size()) + return false; + + return strncmp(string.c_str(), prefix.c_str(), prefix.size()) == 0; + } + } + } +} diff --git a/src/util/util.h b/src/util/util.h index fde0c4f..787fbae 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -1,67 +1,10 @@ -#pragma once - -#include -#include -#include -#include -#include +// local includes +#include "magicwrapper.h" +#include "misc.h" +// import functions from misc module for convenience namespace linuxdeploy { - namespace core { - namespace util { - static inline bool ltrim(std::string& s, char to_trim = ' ') { - // TODO: find more efficient way to check whether elements have been removed - size_t initialLength = s.length(); - s.erase(s.begin(), std::find_if(s.begin(), s.end(), [to_trim](int ch) { - return ch != to_trim; - })); - return s.length() < initialLength; - } - - static inline bool rtrim(std::string& s, char to_trim = ' ') { - // TODO: find more efficient way to check whether elements have been removed - auto initialLength = s.length(); - s.erase(std::find_if(s.rbegin(), s.rend(), [to_trim](int ch) { - return ch != to_trim; - }).base(), s.end()); - return s.length() < initialLength; - } - - static inline bool trim(std::string& s, char to_trim = ' ') { - // returns true if either modifies s - auto ltrim_result = ltrim(s, to_trim); - return rtrim(s, to_trim) && ltrim_result; - } - - static std::vector split(const std::string& s, char delim = ' ') { - std::vector result; - - std::stringstream ss(s); - std::string item; - - while (std::getline(ss, item, delim)) { - result.push_back(item); - } - - return result; - } - - static std::vector splitLines(const std::string& s) { - return split(s, '\n'); - } - - static inline std::string strLower(std::string s) { - std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); }); - return s; - } - - static bool stringStartsWith(const std::string& string, const std::string& prefix) { - // sanity check - if (string.size() < prefix.size()) - return false; - - return strncmp(string.c_str(), prefix.c_str(), prefix.size()) == 0; - } - } + namespace util { + using namespace misc; } }