104 lines
2.7 KiB
C++
104 lines
2.7 KiB
C++
/*
|
|
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 <algorithm>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "usenetsearch/Except.h"
|
|
|
|
namespace usenetsearch {
|
|
|
|
std::ostream& operator<<(std::ofstream& out, const std::string& str);
|
|
std::ifstream& operator>>(std::ifstream& in, std::string& str);
|
|
std::ostream& operator<<(std::ofstream& out, const std::wstring& str);
|
|
std::ifstream& operator>>(std::ifstream& in, std::wstring& str);
|
|
|
|
struct StringException: public UsenetSearchException
|
|
{
|
|
StringException(int errorCode, const std::string& message):
|
|
UsenetSearchException(errorCode, message){}
|
|
|
|
virtual ~StringException() = default;
|
|
};
|
|
|
|
std::string StringHash(const std::string& input);
|
|
|
|
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;
|
|
int tokens = 1;
|
|
while (((pos = s.find(delim)) != T::npos)
|
|
and ((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 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);
|
|
|
|
} // namespace usenetsearch
|