1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#pragma once
#include <string>
#include <algorithm>
namespace string_library
{
template <typename CharT>
using tstring = std::basic_string<CharT, std::char_traits<CharT>,
std::allocator<CharT>>;
template <typename CharT>
using tstringstream = std::basic_stringstream<CharT, std::char_traits<CharT>,
std::allocator<CharT>>;
template<typename CharT>
inline tstring<CharT> to_upper(tstring<CharT> text)
{
std::transform(std::begin(text), std::end(text), std::begin(text), toupper);
return text;
}
template<typename CharT>
inline tstring<CharT> to_lower(tstring<CharT> text)
{
std::transform(std::begin(text), std::end(text), std::begin(text), tolower);
return text;
}
template<typename CharT>
inline tstring<CharT> reverse(tstring<CharT> text)
{
std::reverse(std::begin(text), std::end(text));
return text;
}
template<typename CharT>
inline tstring<CharT> trim(tstring<CharT> const & text)
{
auto first{ text.find_first_not_of(' ') };
auto last{ text.find_last_not_of(' ') };
return text.substr(first, (last - first + 1));
}
template<typename CharT>
inline tstring<CharT> trimleft(tstring<CharT> const & text)
{
auto first{ text.find_first_not_of(' ') };
return text.substr(first, text.size() - first);
}
template<typename CharT>
inline tstring<CharT> trimright(tstring<CharT> const & text)
{
auto last{ text.find_last_not_of(' ') };
return text.substr(0, last + 1);
}
template<typename CharT>
inline tstring<CharT> trim(tstring<CharT> const & text, tstring<CharT> const & chars)
{
auto first{ text.find_first_not_of(chars) };
auto last{ text.find_last_not_of(chars) };
return text.substr(first, (last - first + 1));
}
template<typename CharT>
inline tstring<CharT> trimleft(tstring<CharT> const & text, tstring<CharT> const & chars)
{
auto first{ text.find_first_not_of(chars) };
return text.substr(first, text.size() - first);
}
template<typename CharT>
inline tstring<CharT> trimright(tstring<CharT> const & text, tstring<CharT> const & chars)
{
auto last{ text.find_last_not_of(chars) };
return text.substr(0, last + 1);
}
template<typename CharT>
inline tstring<CharT> remove(tstring<CharT> text, CharT const ch)
{
auto start = std::remove_if(std::begin(text), std::end(text), [=](CharT const c) {return c == ch; });
text.erase(start, std::end(text));
return text;
}
template<typename CharT>
inline std::vector<tstring<CharT>> split(tstring<CharT> text, CharT const delimiter)
{
auto sstr = tstringstream<CharT>{ text };
auto tokens = std::vector<tstring<CharT>>{};
auto token = tstring<CharT>{};
while (std::getline(sstr, token, delimiter))
{
if (!token.empty()) tokens.push_back(token);
}
return tokens;
}
}
|