inline void kbe_split(const std::basic_string<T>& s, T c, std::vector< std::basic_string<T> > &v) { if(s.size() == 0) return; typename std::basic_string< T >::size_type i = 0; typename std::basic_string< T >::size_type j = s.find(c); while(j != std::basic_string<T>::npos) { std::basic_string<T> buf = s.substr(i, j - i); if(buf.size() > 0) v.push_back(buf); i = ++j; j = s.find(c, j); } if(j == std::basic_string<T>::npos) { std::basic_string<T> buf = s.substr(i, s.length() - i); if(buf.size() > 0) v.push_back(buf); } }
std::basic_string<C> perl_s (std::basic_string<C> const& src, std::basic_string<C> const& e) { typedef std::basic_string<C> string; typedef typename string::size_type size; if (e.empty ()) return src; C delimiter (e[0]); size first = e.find (delimiter); size middle = e.find (delimiter, first + 1); size last = e.find (delimiter, middle + 1); string pattern (e, first + 1, middle - first - 1); string format (e, middle + 1, last - middle - 1); //std::cout << pattern << " " << format << std::endl; boost::basic_regex<C> expr (pattern); return regex_merge ( src, expr, format, boost::match_default | boost::format_all ); }
ResultList split(const std::basic_string<CharT> &s, const std::basic_string<CharT> &delimiter, bool keepEmptyParts = true) { ResultList slist; // If delimiter.empty(): // pos = s.find(delimiter, start); // pos will be 0. if (delimiter.empty()) { slist.push_back(s); return slist; } typename std::basic_string<CharT>::size_type start = 0; typename std::basic_string<CharT>::size_type pos; std::basic_string<CharT> part; if (delimiter.length() == 1) { CharT ch = delimiter[0]; // Hope that: // find(std::basic_string<CharT>, CharT ch) // will be faster than: // find(std::basic_string<CharT>, std::basic_string<CharT>) while ((pos = s.find(ch, start)) != s.npos) { // Use strchr/wcschr instead? part = s.substr(start, pos - start); if (!part.empty() || keepEmptyParts) slist.push_back(part); start = pos + delimiter.length(); } } else { while ((pos = s.find(delimiter, start)) != s.npos) { // Use strstr/wcsstr instead? part = s.substr(start, pos - start); if (!part.empty() || keepEmptyParts) slist.push_back(part); start = pos + delimiter.length(); } } if (start != s.length() || keepEmptyParts) slist.push_back(s.substr(start)); return slist; }
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::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; }
void read_cmdline(int argc, typename Ptree::char_type *argv[], const std::basic_string<typename Ptree::char_type> &metachars, Ptree &pt) { typedef typename Ptree::char_type Ch; typedef std::basic_string<Ch> Str; Ptree local; // For all arguments for (int i = 0; i < argc; ++i) { Str text = detail::trim<Ch>(argv[i]); if (!text.empty()) if (metachars.find(text[0]) != Str::npos) { if (text.size() == 1) { Ptree &child = local.put(text, Str()); Str key; if (child.size() < 10) key.push_back(typename Ptree::char_type('0' + child.size())); child.push_back(std::make_pair(key, Ptree(child.data()))); } else if (text.size() == 2) { Ptree &child = local.put(text.substr(1, 1), Str()); Str key; if (child.size() < 10) key.push_back(typename Ptree::char_type('0' + child.size())); child.push_back(std::make_pair(key, Ptree(child.data()))); } else { Ptree &child = local.put(text.substr(1, 1), detail::trim<Ch>(text.substr(2, Str::npos))); Str key; if (child.size() < 10) key.push_back(typename Ptree::char_type('0' + child.size())); child.push_back(std::make_pair(key, Ptree(child.data()))); } } else { Ptree &child = local.put(Str(), detail::trim<Ch>(text)); Str key; if (child.size() < 10) key.push_back(typename Ptree::char_type('0' + child.size())); child.push_back(std::make_pair(key, Ptree(child.data()))); } } // Swap local and pt pt.swap(local); }
template<typename char_t> std::basic_string<char_t> strip(const std::basic_string<char_t>& what, const std::basic_string<char_t> of_what) { std::basic_string<char_t> result; for ( size_t i=0; i<what.length(); i++ ) { if ( of_what.find(what[i]) == std::basic_string<char_t>::npos ) { result.push_back(what[i]); } } return result; }
//@todo test template<typename TARGET, typename charT, typename traits> std::list<TARGET> stringToList( const std::basic_string<charT, traits> &source, charT separator ) { std::list<TARGET> ret; for ( size_t next = source.find_first_not_of( separator ); next != std::string::npos; next = source.find_first_not_of( separator, next ) ) { const size_t start = next; next = source.find( separator, start ); ret.push_back( boost::lexical_cast<TARGET>( source.substr( start, next - start ) ) ); } return ret; }
std::basic_string<CharT> to_str_gcc_workaround(std::basic_string<CharT> str) { std::locale loc; std::numpunct<CharT> const& np = BOOST_USE_FACET(std::numpunct<CharT>, loc); std::ctype<CharT> const& ct = BOOST_USE_FACET(std::ctype<CharT>, loc); if(np.grouping().empty()) return str; CharT prefix[3] = { ct.widen('-'), np.thousands_sep(), CharT() }; if(str.find(prefix) != 0) return str; prefix[1] = CharT(); str.replace(0, 2, prefix); return str; }
std::vector<std::basic_string<TCHAR>> Split (const std::basic_string<TCHAR> &inString, const std::basic_string<TCHAR> &separator) { std::vector<std::basic_string<TCHAR>> returnVector; std::basic_string<TCHAR>::size_type start = 0; std::basic_string<TCHAR>::size_type end = 0; while ((end=inString.find (separator, start)) != std::string::npos) { returnVector.push_back (inString.substr (start, end-start)); start = end+separator.size(); } returnVector.push_back (inString.substr (start)); return returnVector; }
template<typename char_t> bool ends_with(const std::basic_string<char_t>& what, const std::basic_string<char_t>& with) { return !what.empty() && (what.find(with) == what.length()-with.length()); }
CountT GetCount(const std::basic_string<CharT> & gram) const { std::map<std::basic_string<CharT>, CountT>::iterator iter = gram.find(gram); if (iter == gram.end()) return 0; else return iter->second; }
size_t StringReplaceAllT(const std::basic_string<CharType> &find, const std::basic_string<CharType> &replace, std::basic_string<CharType> &output) { size_t find_length = find.size(); size_t replace_length = replace.size(); size_t offset = 0, endpos; size_t target = 0, found_pos; size_t replaced = 0; CharType *data_ptr; if (find.empty() || output.empty()) return 0; /* * to avoid extra memory reallocating, * we use two passes to finish the task in the case that replace.size() is greater find.size() */ if (find_length < replace_length) { /* the first pass, count all available 'find' to be replaced */ for (;;) { offset = output.find(find, offset); if (offset == std::basic_string<CharType>::npos) break; replaced++; offset += find_length; } if (replaced == 0) return 0; size_t newsize = output.size() + replaced * (replace_length - find_length); /* we apply for more memory to hold the content to be replaced */ endpos = newsize; offset = newsize - output.size(); output.resize(newsize); data_ptr = &output[0]; memmove((void*)(data_ptr + offset), (void*)data_ptr, (output.size() - offset) * sizeof(CharType)); } else { endpos = output.size(); offset = 0; data_ptr = const_cast<CharType *>(&output[0]); } /* the second pass, the replacement */ while (offset < endpos) { found_pos = output.find(find, offset); if (found_pos != std::basic_string<CharType>::npos) { /* move the content between two targets */ if (target != found_pos) memmove((void*)(data_ptr + target), (void*)(data_ptr + offset), (found_pos - offset) * sizeof(CharType)); target += found_pos - offset; /* replace */ memcpy(data_ptr + target, replace.data(), replace_length * sizeof(CharType)); target += replace_length; offset = find_length + found_pos; replaced++; } else { /* ending work */ if (target != offset) memcpy((void*)(data_ptr + target), (void*)(data_ptr + offset), (endpos - offset) * sizeof(CharType)); break; } } if (replace_length < find_length) output.resize(output.size() - replaced * (find_length - replace_length)); return replaced; }
INT ReplaceHtmlEntities(std::basic_string<TCHAR>& htmlText) { INT replaces = 0; size_t startPos = htmlText.find(L"&", 0); while (startPos != std::wstring::npos) { size_t nextEndPos = htmlText.find(L";", startPos + 1); if (nextEndPos == std::wstring::npos) { //=== Last &... wasn't an entity & we ve found EOF break; } BOOL bEntityIsValid = TRUE; size_t nextStartPos = htmlText.find(L"&", startPos + 1); if (nextStartPos != std::wstring::npos) { if (nextStartPos < nextEndPos) { //=== We ve found &..&..; pattern which is illegal bEntityIsValid = FALSE; } } if (bEntityIsValid) bEntityIsValid = ((nextEndPos - startPos) <= 8); if (bEntityIsValid) { wchar_t repVal = 0; LPCTSTR entityStart = &htmlText[startPos+1]; if (*entityStart/*htmlText[startPos+1]*/ == '#') { //=== It is a int entity TCHAR intbf[10]; INT copyChars = (INT) min(nextEndPos - startPos - 2, 9); _tcsncpy(intbf, &htmlText[startPos+2], copyChars); intbf[copyChars] = 0; INT val = _wtoi(intbf); if (val > 0 && val < 0xFFFE) repVal = wchar_t(val); } else { //=== It is a string entity //=== Check the ISO88591 entities for (int i = 0; i < sizeof(ISO88591_Entities)/sizeof(LPCTSTR); i++) { if (_tcsncmp(ISO88591_Entities[i], entityStart, _tcslen(ISO88591_Entities[i])) == 0) { repVal = (wchar_t) (i + 160); break; } } //=== Check the "other" mapped entities if (repVal == 0) { for (int i = 0; i < sizeof(strentity2wchar_t)/sizeof(HtmlStringEntityMap); i++) { if (_tcsncmp(strentity2wchar_t[i].html, entityStart, _tcslen(strentity2wchar_t[i].html)) == 0) { repVal = strentity2wchar_t[i].res; break; } } } } if (repVal != 0) { htmlText.erase(startPos + 1, nextEndPos - startPos); htmlText[startPos] = repVal; replaces++; } nextStartPos = htmlText.find(L"&", startPos + 1); } startPos = nextStartPos; } //for (int i = 0; i < sizeof(html2text)/sizeof(HtmlEntityMap); i++) // replaces += replace(htmlText, html2text[i].html, html2text[i].text); return replaces; }
std::basic_string<char_type> convert ( std::basic_string<char_type> & s ) { return s.substr( 0, s.find( '\r' ) ); }