UsenetSearch/include/usenetsearch/StringUtils.h

74 lines
2.1 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 <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