UsenetSearch/include/usenetsearch/StringUtils.h

145 lines
3.6 KiB
C++

/*
Copyright© 2021 John Sennesael
UsenetSearch is Free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UsenetSearch is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "usenetsearch/Except.h"
#include <algorithm>
#include <fstream>
#include <functional>
#include <string>
#include <vector>
namespace usenetsearch {
struct StringException: public UsenetSearchException
{
StringException(int errorCode, const std::string& message):
UsenetSearchException(errorCode, message){}
virtual ~StringException() = default;
};
std::string CharToHex(const char c);
std::string HashBytesToString(const std::array<std::uint8_t, 16>& input);
std::string StringFromWideString(const std::wstring& input);
std::string StringHash(const std::string& input);
std::array<std::uint8_t, 16> StringHashBytes(const std::string& input);
template<typename T>
std::string StringJoin(const std::vector<T>& list, const T& delim)
{
T result;
if (list.empty()) return result;
if (list.size() == 1) return list[0];
for (const auto& token: list)
{
result += token + delim;
}
return result.substr(0, result.size() - delim.size());
}
template<typename T>
std::vector<T> StringSplit(
const T& str,
const T& delim,
int max_elem = -1
)
{
size_t pos=0;
T s = str;
std::vector<T> result;
if (str.empty()) return result;
int tokens = 1;
while (((pos = s.find(delim)) != T::npos)
&& ((max_elem < 0) or (tokens != max_elem)))
{
result.push_back(s.substr(0, pos));
s.erase(0, pos + delim.length());
tokens++;
}
if (max_elem != 0) result.push_back(s);
return result;
}
template<typename T>
T StringLeftTrim(const T& str)
{
T s = str;
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
template<typename T>
T StringRemove(const T& subject, const T& toErase)
{
T s(subject);
size_t pos = T::npos;
while ((pos = s.find(toErase) )!= T::npos)
{
s.erase(pos, toErase.length());
}
return s;
}
template<typename T>
T StringRightTrim(const T& str)
{
T s = str;
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(),
s.end());
return s;
}
bool StringStartsWith(const std::string& needle, const std::string& haystack);
bool StringStartsWith(const std::wstring& needle, const std::wstring& haystack);
template<typename T>
T StringTrim(const T& str)
{
return StringLeftTrim(StringRightTrim(str));
}
template<typename T>
T StringToLower(const T& str)
{
T copy = str;
std::transform(copy.begin(),copy.end(),copy.begin(),::tolower);
return copy;
}
bool StringToBoolean(const std::string& str);
bool StringToBoolean(const std::wstring& str);
void StringTreeOperation(
const std::string& searchString,
const std::string& splitBy,
size_t maxDepth,
std::function<void(const std::string& subToken, const std::string& str)> Fn
);
std::wstring WideStringFromString(const std::string& input);
} // namespace usenetsearch