mirror of
https://github.com/audacity/linuxdeploy.git
synced 2026-04-13 15:19:49 -05:00
Refactor util lib
This commit is contained in:
@@ -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})
|
||||
|
||||
67
src/util/misc.h
Normal file
67
src/util/misc.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string> split(const std::string& s, char delim = ' ') {
|
||||
std::vector<std::string> result;
|
||||
|
||||
std::stringstream ss(s);
|
||||
std::string item;
|
||||
|
||||
while (std::getline(ss, item, delim)) {
|
||||
result.push_back(item);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static std::vector<std::string> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,67 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
// 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<std::string> split(const std::string& s, char delim = ' ') {
|
||||
std::vector<std::string> result;
|
||||
|
||||
std::stringstream ss(s);
|
||||
std::string item;
|
||||
|
||||
while (std::getline(ss, item, delim)) {
|
||||
result.push_back(item);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static std::vector<std::string> 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user