UsenetSearch/include/usenetsearch/StringUtils.h

59 lines
1.4 KiB
C++

#pragma once
#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;
};
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;
}
std::string StringLeftTrim(const std::string& str);
std::string StringRightTrim(const std::string& str);
bool StringStartsWith(const std::string& needle, const std::string& haystack);
std::string StringTrim(const std::string& str);
std::string StringToLower(const std::string& str);
bool StringToBoolean(const std::string& str);
} // namespace usenetsearch