std::vector<std::basic_string<T_Char>> split( const std::basic_string<T_Char>& src, const std::basic_string<T_Char>& delimit) { std::vector<std::basic_string<T_Char>> array; typedef typename std::basic_string<T_Char>::size_type size_type; typedef typename std::basic_string<T_Char>::const_iterator const_iterator; std::basic_string<T_Char> tmp; if (src.empty()) return array; if (delimit.empty()) { array.reserve(array.size() + src.size()); for (const_iterator it = src.begin(); it != src.end(); ++it) array.push_back(std::basic_string<T_Char>(1, *it)); return array; } size_type src_pos = 0; while (src.begin() + src_pos != src.end()) { size_type fnd_pos = src.find(delimit, src_pos); if (fnd_pos == std::basic_string<T_Char>::npos) { array.push_back(std::basic_string<T_Char>(src, src_pos)); break; } array.push_back( std::basic_string<T_Char>(src, src_pos, fnd_pos - src_pos)); src_pos = fnd_pos + delimit.length(); } return array; }
std::size_t regex_split(OutputIterator out, std::basic_string<charT, Traits1, Alloc1>& s, const basic_regex<charT, Traits2>& e, match_flag_type flags, std::size_t max_split) { typedef typename std::basic_string<charT, Traits1, Alloc1>::const_iterator ci_t; //typedef typename match_results<ci_t>::allocator_type match_allocator; ci_t last = s.begin(); std::size_t init_size = max_split; re_detail::split_pred<OutputIterator, charT, Traits1, Alloc1> pred(&last, &out, &max_split); ci_t i, j; i = s.begin(); j = s.end(); regex_grep(pred, i, j, e, flags); // // if there is still input left, do a final push as long as max_split // is not exhausted, and we're not splitting sub-expressions rather // than whitespace: if(max_split && (last != s.end()) && (e.mark_count() == 0)) { *out = std::basic_string<charT, Traits1, Alloc1>((ci_t)last, (ci_t)s.end()); ++out; last = s.end(); --max_split; } // // delete from the string everything that has been processed so far: s.erase(0, last - s.begin()); // // return the number of new records pushed: return init_size - max_split; }
bool compare(const std::basic_string<CharT> &lhs, const std::basic_string<CharT> &rhs) { return std::equal(lhs.begin(), lhs.end(), rhs.begin(), [](const CharT l, const CharT r)->bool { return ::toupper(l) == ::toupper(r); }); }
bool is_palindrome(std::basic_string<Char, Traits, Allocater> str) { typedef typename std::basic_string<Char, Traits, Allocater> string; typename string::iterator end {std::remove_if(str.begin(), str.end(), non_letter<Char>)}; string reversed{str.begin(), end}; std::reverse(reversed.begin(), reversed.end()); return not reversed.empty() and std::equal(str.begin(), end, reversed.begin(), is_same_character<Char>); }
void test(const std::basic_string<CharT>& x, const std::basic_string<CharT>& y, bool doCStrTests = true) { typedef std::basic_string<CharT> string; typedef std::sub_match<typename string::const_iterator> sub_match; sub_match sm1; sm1.first = x.begin(); sm1.second = x.end(); sm1.matched = true; sub_match sm2; sm2.first = y.begin(); sm2.second = y.end(); sm2.matched = true; assert((sm1 == sm2) == (x == y)); assert((sm1 != sm2) == (x != y)); assert((sm1 < sm2) == (x < y)); assert((sm1 > sm2) == (x > y)); assert((sm1 <= sm2) == (x <= y)); assert((sm1 >= sm2) == (x >= y)); assert((x == sm2) == (x == y)); assert((x != sm2) == (x != y)); assert((x < sm2) == (x < y)); assert((x > sm2) == (x > y)); assert((x <= sm2) == (x <= y)); assert((x >= sm2) == (x >= y)); assert((sm1 == y) == (x == y)); assert((sm1 != y) == (x != y)); assert((sm1 < y) == (x < y)); assert((sm1 > y) == (x > y)); assert((sm1 <= y) == (x <= y)); assert((sm1 >= y) == (x >= y)); if (doCStrTests) { assert((x.c_str() == sm2) == (x == y)); assert((x.c_str() != sm2) == (x != y)); assert((x.c_str() < sm2) == (x < y)); assert((x.c_str() > sm2) == (x > y)); assert((x.c_str() <= sm2) == (x <= y)); assert((x.c_str() >= sm2) == (x >= y)); assert((sm1 == y.c_str()) == (x == y)); assert((sm1 != y.c_str()) == (x != y)); assert((sm1 < y.c_str()) == (x < y)); assert((sm1 > y.c_str()) == (x > y)); assert((sm1 <= y.c_str()) == (x <= y)); assert((sm1 >= y.c_str()) == (x >= y)); } assert((x[0] == sm2) == (string(1, x[0]) == y)); assert((x[0] != sm2) == (string(1, x[0]) != y)); assert((x[0] < sm2) == (string(1, x[0]) < y)); assert((x[0] > sm2) == (string(1, x[0]) > y)); assert((x[0] <= sm2) == (string(1, x[0]) <= y)); assert((x[0] >= sm2) == (string(1, x[0]) >= y)); assert((sm1 == y[0]) == (x == string(1, y[0]))); assert((sm1 != y[0]) == (x != string(1, y[0]))); assert((sm1 < y[0]) == (x < string(1, y[0]))); assert((sm1 > y[0]) == (x > string(1, y[0]))); assert((sm1 <= y[0]) == (x <= string(1, y[0]))); assert((sm1 >= y[0]) == (x >= string(1, y[0]))); }
std::basic_string < Elem, Traits > cell_encode(std::basic_string < Elem, Traits > Str, Elem Sep_, Elem Esc){ if(Str.find(Sep_) < Str.size() || Str.find(Esc) < Str.size()){ for(auto itr = Str.begin(); itr != Str.end(); ++itr){ if(*itr == Esc){ itr = Str.insert(++itr, Esc); } } Str.insert(Str.begin(), Esc); Str.push_back(Esc); } return std::move(Str); }
std::basic_string<CharType> escape_argument(const std::basic_string<CharType>& arg) { std::basic_string<CharType> result(1, argument_helper<CharType>::QUOTE_CHARACTER); for (auto it = arg.begin();; ++it) { unsigned int escapes_count = 0; while ((it != arg.end()) && (*it == argument_helper<CharType>::ESCAPE_CHARACTER)) { ++it; ++escapes_count; } if (it == arg.end()) { result.append(escapes_count * 2, argument_helper<CharType>::ESCAPE_CHARACTER); break; } else if (*it == argument_helper<CharType>::QUOTE_CHARACTER) { result.append(escapes_count * 2 + 1, argument_helper<CharType>::ESCAPE_CHARACTER); result.push_back(*it); } else { result.append(escapes_count, argument_helper<CharType>::ESCAPE_CHARACTER); result.push_back(*it); } } result.push_back(argument_helper<CharType>::QUOTE_CHARACTER); return result; }
inline unsigned int regex_grep(bool (*foo)(const match_results<std::basic_string<wchar_t>::const_iterator>&), const std::basic_string<wchar_t>& s, const wregex& e, match_flag_type flags = match_default) { return regex_grep(foo, s.begin(), s.end(), e, flags); }
std::basic_string<Ch> decode_char_entities(const std::basic_string<Ch> &s) { typedef typename std::basic_string<Ch> Str; Str r; typename Str::const_iterator end = s.end(); for (typename Str::const_iterator it = s.begin(); it != end; ++it) { if (*it == Ch('&')) { typename Str::const_iterator semicolon = std::find(it + 1, end, Ch(';')); if (semicolon == end) throw xml_parser_error("invalid character entity", "", 0); Str ent(it + 1, semicolon); if (ent == detail::widen<Ch>("lt")) r += Ch('<'); else if (ent == detail::widen<Ch>("gt")) r += Ch('>'); else if (ent == detail::widen<Ch>("amp")) r += Ch('&'); else throw xml_parser_error("invalid character entity", "", 0); it = semicolon; } else r += *it; } return r; }
inline bool regex_match(const std::basic_string<wchar_t>& s, match_results<std::basic_string<wchar_t>::const_iterator>& m, const basic_regex<wchar_t, c_regex_traits<wchar_t> >& e, match_flag_type flags = match_default) { return regex_match(s.begin(), s.end(), m, e, flags); }
inline bool regex_search(const std::basic_string<wchar_t>& s, wsmatch& m, const wregex& e, match_flag_type flags = match_default) { return regex_search(s.begin(), s.end(), m, e, flags); }
void IO::TrimRight(std::basic_string<charType> & str, const char* chars2remove) { if (!str.empty()) { //trim the characters in chars2remove from the right std::string::size_type pos = 0; if (chars2remove != NULL) { pos = str.find_last_not_of(chars2remove); if (pos != std::string::npos) str.erase(pos+1); else str.erase( str.begin() , str.end() ); // make empty } else { //trim space pos = std::string::npos; for (int i = str.size()-1; i >= 0; --i) { if (!isspace(str[i])) { pos = i; break; } } if (pos != std::string::npos) { if (pos+1 != str.size()) str.resize(pos+1); } else { str.clear(); } } } }
inline bool regex_match(const std::basic_string<wchar_t>& s, const basic_regex<wchar_t, w32_regex_traits<wchar_t> >& e, match_flag_type flags = match_default) { match_results<std::basic_string<wchar_t>::const_iterator> m; return regex_match(s.begin(), s.end(), m, e, flags | regex_constants::match_any); }
inline bool regex_search(const std::basic_string<charT, ST, SA>& s, match_results<typename std::basic_string<charT, ST, SA>::const_iterator, Allocator>& m, const basic_regex<charT, traits>& e, match_flag_type flags = match_default) { return regex_search(s.begin(), s.end(), m, e, flags); }
std::basic_string<Ch> encode_char_entities(const std::basic_string<Ch> &s) { // Don't do anything for empty strings. if(s.empty()) return s; typedef typename std::basic_string<Ch> Str; Str r; // To properly round-trip spaces and not uglify the XML beyond // recognition, we have to encode them IF the text contains only spaces. Str sp(1, Ch(' ')); if(s.find_first_not_of(sp) == Str::npos) { // The first will suffice. r = detail::widen<Ch>(" "); r += Str(s.size() - 1, Ch(' ')); } else { typename Str::const_iterator end = s.end(); for (typename Str::const_iterator it = s.begin(); it != end; ++it) { switch (*it) { case Ch('<'): r += detail::widen<Ch>("<"); break; case Ch('>'): r += detail::widen<Ch>(">"); break; case Ch('&'): r += detail::widen<Ch>("&"); break; case Ch('"'): r += detail::widen<Ch>("""); break; case Ch('\''): r += detail::widen<Ch>("'"); break; default: r += *it; break; } } } return r; }
inline bool regex_search(const std::basic_string<wchar_t>& s, const wregex& e, match_flag_type flags = match_default) { wsmatch m; return regex_search(s.begin(), s.end(), m, e, flags | regex_constants::match_any); }
std::basic_string<Ch> trim(const std::basic_string<Ch> &s, const std::locale &loc = std::locale()) { typename std::basic_string<Ch>::const_iterator first = s.begin(); typename std::basic_string<Ch>::const_iterator end = s.end(); while (first != end && std::isspace(*first, loc)) ++first; if (first == end) return std::basic_string<Ch>(); typename std::basic_string<Ch>::const_iterator last = end; do --last; while (std::isspace(*last, loc)); if (first != s.begin() || last + 1 != end) return std::basic_string<Ch>(first, last + 1); else return s; }
//! Converts escape sequences to the corresponding characters void char_constants< char >::translate_escape_sequences(std::basic_string< char_type >& str) { using namespace std; // to make sure we can use C functions unqualified std::basic_string< char_type >::iterator it = str.begin(); while (it != str.end()) { it = std::find(it, str.end(), '\\'); if (std::distance(it, str.end()) >= 2) { it = str.erase(it); switch (*it) { case 'n': *it = '\n'; break; case 'r': *it = '\r'; break; case 'a': *it = '\a'; break; case '\\': ++it; break; case 't': *it = '\t'; break; case 'b': *it = '\b'; break; case 'x': { std::basic_string< char_type >::iterator b = it; if (std::distance(++b, str.end()) >= 2) { char_type c1 = *b++, c2 = *b++; if (isxdigit(c1) && isxdigit(c2)) { *it++ = char_type((to_number(c1) << 4) | to_number(c2)); it = str.erase(it, b); } } break; } default: { if (*it >= '0' && *it <= '7') { std::basic_string< char_type >::iterator b = it; int c = (*b++) - '0'; if (*b >= '0' && *b <= '7') c = c * 8 + (*b++) - '0'; if (*b >= '0' && *b <= '7') c = c * 8 + (*b++) - '0'; *it++ = char_type(c); it = str.erase(it, b); } break; } } } } }
void print_string(std::basic_string<char> const& str, std::ostream& out) { for (std::string::const_iterator cur = str.begin(); cur != str.end(); ++cur) { print_char(*cur, out); } }
inline bool regex_match(const std::basic_string<charT, ST, SA>& s, const basic_regex<charT, traits>& e, match_flag_type flags = match_default) { typedef typename std::basic_string<charT, ST, SA>::const_iterator iterator; match_results<iterator> m; return regex_match(s.begin(), s.end(), m, e, flags | regex_constants::match_any); }
inline std::basic_string<OutputT> convert_string_type(const std::basic_string<InputT>& inp_str) { typedef std::basic_string<OutputT> output_type; output_type result; result.insert(result.begin(), inp_str.begin(), inp_str.end()); return result; }
std::basic_string<To> ConvertUTF(const std::basic_string<From>& string) { typedef typename detail::WideImpl<From, sizeof(wchar_t)>::type TypeFrom; typedef typename detail::WideImpl<To, sizeof(wchar_t)>::type TypeTo; TypeTo result; detail::Convert().Impl(TypeFrom(string.begin(), string.end()), result); return std::basic_string<To>(result.begin(), result.end()); }
//! Serialization for basic_string types, if binary data is supported template<class Archive, class CharT, class Traits, class Alloc> inline typename std::enable_if<traits::is_input_serializable<BinaryData<CharT>, Archive>(), void>::type load(Archive & ar, std::basic_string<CharT, Traits, Alloc> & str) { size_t size; ar( make_size_tag( size ) ); str.resize(size); ar( binary_data( &(*str.begin()), size * sizeof(CharT) ) ); }
inline bool regex_search ( std::basic_string<Char, Traits, Alloc> const &str , match_results<typename std::basic_string<Char, Traits, Alloc>::const_iterator> &what , basic_regex<typename std::basic_string<Char, Traits, Alloc>::const_iterator> const &re , regex_constants::match_flag_type flags = regex_constants::match_default ) { return regex_search(str.begin(), str.end(), what, re, flags); }
void print_if_failed(char const* func, bool result , std::basic_string<Char> const& generated, T const& expected) { if (!result) std::cerr << "in " << func << ": result is false" << std::endl; else if (generated != expected) std::cerr << "in " << func << ": generated \"" << std::string(generated.begin(), generated.end()) << "\"" << std::endl; }
std::basic_string<charT> regex_replace(const std::basic_string<charT>& s, const basic_regex<charT, traits>& e, Formatter fmt, match_flag_type flags = match_default) { std::basic_string<charT> result; re_detail::string_out_iterator<std::basic_string<charT> > i(result); regex_replace(i, s.begin(), s.end(), e, fmt, flags); return result; }
template< class CharT > bool IsPrintable( const std::basic_string< CharT >& str ) { const std::ctype< CharT > *pct = 0, &ct = tss::GetFacet( std::locale(), pct ); for( std::basic_string< CharT >::const_iterator at = str.begin(); at != str.end(); at++ ) { if( ! ct.is( std::ctype_base::print, *at ) ) // if not printable return false; } return true; }
int countUnique(const std::basic_string<T>& s) { using std::basic_string; basic_string<T> chars; for (typename basic_string<T>::const_iterator p = s.begin( ); p != s.end( ); ++p) { if (chars.find(*p) == basic_string<T>::npos) chars += *p; } return(chars.length( )); }
std::basic_string <CharType, TraitsType, AllocType> lowercase( const std::basic_string <CharType, TraitsType, AllocType> & s ) { std::basic_string <CharType, TraitsType, AllocType> result( s.length(), '\0' ); std::transform( s.begin(), s.end(), result.begin(), std::ptr_fun <int, int> ( std::tolower ) ); return result; }
inline std::basic_string<Char> regex_replace ( std::basic_string<Char> const &str , basic_regex<typename std::basic_string<Char>::const_iterator> const &re , std::basic_string<Char> const &fmt , regex_constants::match_flag_type flags = regex_constants::match_default ) { std::basic_string<Char> result; result.reserve(fmt.length() * 2); regex_replace(std::back_inserter(result), str.begin(), str.end(), re, fmt, flags); return result; }