38#ifndef DRAIN_STRING_H_
39#define DRAIN_STRING_H_
57 void convertToString(
const T & value, std::string &s){
58 std::stringstream sstr;
63 void convertFromString(
const std::string &s, T & value){
64 std::stringstream sstr(s);
71template <
typename T=std::
string>
92 void set(
const std::string & s=
""){
93 std::string::assign(s);
97 void set(
const StringWrapper & e){
98 std::string::assign(e);
102 void set(
const char *s){
103 std::string::assign(s);
114 template <
typename T2>
138 bool startsWith(
const std::string &s,
const std::string & substring);
142 bool endsWith(
const std::string &s,
const std::string & substring);
149 std::string &
upperCase(std::string & s,
size_t n = std::numeric_limits<size_t>::max());
162 std::string &
lowerCase(std::string & s,
size_t n = std::numeric_limits<size_t>::max());
177 void replace(
const std::string &src,
char search,
char repl, std::ostream & ostr);
184 void replace(
const std::string &src,
char from,
char repl, std::string &dst);
190 template <
typename S,
typename R>
192 void replace(
const std::string &src,
const S & search,
const R & repl, std::ostream & ostr){
194 const size_t length = getLength(search);
195 std::string::size_type i = 0;
196 std::string::size_type pos;
199 pos = src.find(search, i);
200 if (pos == std::string::npos){
201 ostr << src.substr(i);
206 ostr << src.substr(i, pos-i) << repl;
218 template <
typename T1,
typename T2>
220 void replace(
const std::string &src,
const T1 &search,
const T2 & repl, std::string & dst){
221 std::stringstream result;
222 replace(src, search, repl, result);
247 template <
typename T>
249 void replace(
const std::string & src,
const std::map<char,T> & m, std::ostream & ostr){
250 typename std::map<char,T>::const_iterator it;
262 template <
typename T>
264 void replace(
const std::string & src,
const std::map<char,T> & m, std::string & dst){
265 std::stringstream result;
275 void replace(
const std::string & src,
const std::map<char,char> & m, std::string & dst);
282 template <
class K,
class V>
284 void replace(
const std::string & src,
const std::map<K,V> & m, std::string & dst){
285 replaceWithMap(src, m, dst);
288 template <
class K,
class V>
290 void replace(
const std::string & src,
const std::initializer_list<std::pair<K,V> > & m, std::string & dst){
291 replaceWithMap(src, m, dst);
302 size_t getLength(
char c){
307 size_t getLength(
const std::string & s){
313 void replaceWithMap(
const std::string & src,
const M & m, std::string & dst){
323 for (
const auto & entry: m){
325 replace(dst, entry.first, entry.second, dst);
336 std::string
trim(
const std::string &s,
const std::string &trimChars=
" \t\n\r");
347 std::string
trimSymmetric(
const std::string &s,
const std::string &leading=
"'\"",
const std::string & trailing=
"");
363 bool trimScan(
const std::string &s,
size_t & pos1,
size_t & pos2,
const std::string &trimChars=
" \t\n");
379 template <
class T,
class C>
381 void split(
const std::string & s, T & sequence,
const C &separators,
const std::string & trimChars=
" \t\n");
408 template <
class T1,
class T2,
class C>
410 bool split2(
const std::string & s, T1 & first, T2 & second,
const C &separators,
const std::string & trimChars=
" \t\n");
420 size_t extractPrefixLength(
const std::string & src1,
const std::string & src2,
size_t step = 1);
432 size_t extractPrefix(
const std::string & src1,
const std::string & src2,
433 std::string & prefix, std::string & dst1, std::string & dst2,
size_t step = 1);
443 size_t extractPrefix(std::string & s1, std::string & s2, std::string & prefix,
size_t step = 1){
454 std::ostream &
join(
const T & container, std::ostream & ostr,
char separator = 0){
456 for (
typename T::const_iterator it = container.begin(); it != container.end(); ++it){
471 std::string
join(T & container,
char separator = 0){
472 std::stringstream sstr;
478 template <
unsigned int S>
480 void read(std::istream &istr, std::string & s){
482 while (istr.read(buffer, S)){
485 s.append(buffer, istr.gcount());
500 void convert(
const std::string &s, T & dst);
511 T
convert(
const std::string &s);
516 std::string &
import(
const T & src, std::string & target);
536 const T &
lazyConvert(
const std::string &s, T & tmp);
542 void appendString(T & sequence,
const std::string & str){
543 typename T::value_type tmp;
549 void appendSubstring(T & sequence,
const std::string & str, std::string::size_type pos, std::string::size_type n){
552 appendString(sequence, str.substr(pos, n));
555 appendString(sequence,
"");
563template <
class T,
class C>
564void StringTools::split(
const std::string & str, T & sequence,
const C & separators,
const std::string & trimChars){
568 const bool TRIM = !trimChars.empty();
569 const std::string::size_type n = str.size();
571 std::string::size_type pos1 = 0;
572 std::string::size_type pos2 = n;
578 appendSubstring(sequence, str, pos1, pos2-pos1);
581 appendString(sequence, str);
588 std::string::size_type pos = pos1;
593 pos = str.find_first_of(separators, pos);
594 if (pos == std::string::npos){
598 appendSubstring(sequence, str, pos1, pos2-pos1);
605 appendSubstring(sequence, str, pos1, pos2-pos1);
606 pos = str.find_first_not_of(trimChars, pos+1);
620template <
class T1,
class T2,
class S>
621bool StringTools::split2(
const std::string & s, T1 & first, T2 & second,
const S & separators,
const std::string & trimChars){
623 std::size_t i = s.find_first_of(separators);
625 if (i != std::string::npos){
627 std::string srcFirst(s, 0, i);
629 std::string srcSecond(s, std::min(s.size(), i));
631 if (!trimChars.empty()){
638 if (!srcSecond.empty()){
639 if (!trimChars.empty()){
667 if (trimChars.empty()){
699 std::stringstream sstr(str);
724std::string & StringTools::import(
const std::string & src, std::string & dst){
730std::string & StringTools::import(
const T & x, std::string & dst){
731 std::stringstream sstr;
733 dst.assign(sstr.str());
Definition StringTools.h:53
Definition StringTools.h:72
StringWrapper(const T &x)
All the other constructors, including default constructor.
Definition StringTools.h:86
void set(const T2 &x)
Set the value from an other, user-defined dictionary.
Definition StringTools.h:116
Definition DataSelector.cpp:1277