Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
StringManipulator Class Reference

#include <StringManipulator.h>

Public Types

using Index = unsigned int
 
using StringVector = std::vector< std::string >
 

Public Member Functions

 StringManipulator (std::string &strin, bool copy)
 
 StringManipulator (const std::string &sin)
 
 StringManipulator (const char *chin)
 
void setLogLevel (Index logLevel)
 
Index logLevel () const
 
const std::stringstr () const
 
const char * c_str () const
 
std::string::size_type size () const
 
template<typename T >
int replace (std::string substr, const T &xsub)
 
template<typename T >
int replaceFixedWidth (std::string substr, const T &xsub, Index width)
 
const std::vector< std::string > & split (std::string seps, bool fullSplit=false)
 
const StringVectorpatternSplit (std::string spat)
 
const StringVectorsplits () const
 
bool isDigits () const
 
bool isUnsignedInt () const
 
bool isInt () const
 
bool isFloat () const
 
int toInt (int valbad=0) const
 
unsigned int toUnsignedInt (unsigned int valbad=0) const
 
float toFloat (float valbad=0) const
 

Static Public Member Functions

template<typename T >
static char getFill (typename std::enable_if< std::is_integral< T >::value, const T & >::type val)
 
template<typename T >
static char getFill (typename std::enable_if<!std::is_integral< T >::value, const T & >::type val)
 
static std::string floatToString (float val, int prec, bool trunc, std::string sdot="", std::string smin="")
 

Private Attributes

int m_LogLevel = 0
 
std::stringm_str
 
std::string m_strCopy
 
StringVector m_splits
 

Detailed Description

Definition at line 15 of file StringManipulator.h.

Member Typedef Documentation

using StringManipulator::Index = unsigned int

Definition at line 19 of file StringManipulator.h.

Definition at line 20 of file StringManipulator.h.

Constructor & Destructor Documentation

StringManipulator::StringManipulator ( std::string strin,
bool  copy 
)
inline

Definition at line 49 of file StringManipulator.h.

50  : m_str(copy ? m_strCopy : strin), m_strCopy(strin) { }
T copy(T const &v)
StringManipulator::StringManipulator ( const std::string sin)
inlineexplicit

Definition at line 53 of file StringManipulator.h.

53 : m_str(m_strCopy), m_strCopy(sin) { }
StringManipulator::StringManipulator ( const char *  chin)
inlineexplicit

Definition at line 56 of file StringManipulator.h.

56 : m_str(m_strCopy), m_strCopy(chin) { }

Member Function Documentation

const char* StringManipulator::c_str ( ) const
inline

Definition at line 66 of file StringManipulator.h.

66 { return m_str.c_str(); }
string StringManipulator::floatToString ( float  val,
int  prec,
bool  trunc,
std::string  sdot = "",
std::string  smin = "" 
)
static

Definition at line 110 of file StringManipulator.cxx.

110  {
111  ostringstream ssout;
112  ssout.setf(std::ios::fixed);
113  ssout.precision(prec);
114  ssout << val;
115  string sout = ssout.str();
116  if ( trunc && sout.size() ) {
117  string::size_type ipos = sout.size() - 1;
118  while ( ipos > 0 ) {
119  if ( sout[ipos] == '.' ) {
120  --ipos;
121  break;
122  } else if ( sout[ipos] == '0' ) --ipos;
123  else break;
124  }
125  sout = sout.substr(0, ipos + 1);
126  if ( sdot.size() && sdot != "." ) {
127  for ( ipos=0; ipos<sout.size(); ++ipos ) {
128  if ( sout[ipos] == '.' ) {
129  sout.replace(ipos, 1, sdot);
130  ipos += sdot.size() - 1;
131  }
132  }
133  }
134  if ( smin.size() && smin != "." ) {
135  for ( ipos=0; ipos<sout.size(); ++ipos ) {
136  if ( sout[ipos] == '-' ) {
137  sout.replace(ipos, 1, smin);
138  ipos += smin.size() - 1;
139  }
140  }
141  }
142  }
143  return sout;
144 }
template<typename T >
static char StringManipulator::getFill ( typename std::enable_if< std::is_integral< T >::value, const T & >::type  val)
inlinestatic

Definition at line 27 of file StringManipulator.h.

27  {
28  return val < 0 ? '-' : '0';
29  }
template<typename T >
static char StringManipulator::getFill ( typename std::enable_if<!std::is_integral< T >::value, const T & >::type  val)
inlinestatic

Definition at line 33 of file StringManipulator.h.

33  {
34  return '_';
35  }
bool StringManipulator::isDigits ( ) const

Definition at line 148 of file StringManipulator.cxx.

148  {
149  if ( str().empty() ) return false;
150  for ( char c : str() ) if ( ! std::isdigit(c) ) return false;
151  return true;
152 }
const std::string & str() const
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:97
bool StringManipulator::isFloat ( ) const

Definition at line 174 of file StringManipulator.cxx.

174  {
175  if ( str().size() == 0 ) return false;
176  char* pch = nullptr;
177  strtof(c_str(), &pch);
178  return pch[0] == '\0';
179 }
const std::string & str() const
std::string::size_type size() const
const char * c_str() const
bool StringManipulator::isInt ( ) const

Definition at line 156 of file StringManipulator.cxx.

156  {
157  if ( str().empty() ) return false;
158  char c = str()[0];
159  string schk = (c == '+' || c == '-') ? str().substr(1) : str();
160  return StringManipulator(schk).isDigits();
161 }
const std::string & str() const
StringManipulator(std::string &strin, bool copy)
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:97
bool StringManipulator::isUnsignedInt ( ) const

Definition at line 165 of file StringManipulator.cxx.

165  {
166  if ( str().empty() ) return false;
167  char c = str()[0];
168  string schk = c == '+' ? str().substr(1) : str();
169  return StringManipulator(schk).isDigits();
170 }
const std::string & str() const
StringManipulator(std::string &strin, bool copy)
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:97
Index StringManipulator::logLevel ( ) const
inline

Definition at line 60 of file StringManipulator.h.

60 { return m_LogLevel; }
const StringVector & StringManipulator::patternSplit ( std::string  spat)

Definition at line 51 of file StringManipulator.cxx.

51  {
52  const string myname = "StringManipulator::patternSplit: ";
53  string word;
54  bool inPat = false;
55  m_splits.clear();
56  if ( spat.size() < 2 ) return m_splits;
57  char chStart = spat[0];
58  char chEnd = spat[spat.size() - 1 ];
59  string seps = spat.substr(1, spat.size() - 2 );
60  // Build the sequence of words and word vectors used to create the
61  // full vector of strings.
62  StringVV wordvv;
63  for ( char ch : m_str ) {
64  // In gap between patterns.
65  if ( ! inPat ) {
66  if ( ch == chStart ) {
67  wordvv.push_back(StringVector(1, word));
68  word = "";
69  inPat = true;
70  } else {
71  word += ch;
72  }
73  // In a pattern.
74  } else {
75  if ( ch == chEnd ) {
76  StringManipulator sman(word,false);
77  wordvv.push_back(sman.split(seps));
78  word = "";
79  inPat = false;
80  } else {
81  word += ch;
82  }
83  }
84  }
85  if ( word.size() ) wordvv.push_back(StringVector(1, word));
86  if ( logLevel() >= 1 ) {
87  cout << myname << "Word sequence count: " << wordvv.size() << endl;
88  }
89  // Construct the vector of strings from the wors sequence.
90  StringVector& strs1(m_splits);
91  strs1.push_back("");
92  for ( const StringVector& wordvec : wordvv ) {
93  StringVector strs2;
94  for ( string str1 : strs1 ) {
95  for ( string word : wordvec ) {
96  strs2.push_back(str1 + word);
97  }
98  }
99  strs1 = strs2;
100  }
101  if ( logLevel() >= 1 ) {
102  cout << myname << "Split count: " << m_splits.size() << endl;
103  }
104  return m_splits;
105 }
Index logLevel() const
std::vector< string > StringVector
Definition: fcldump.cxx:29
std::vector< StringVector > StringVV
std::vector< std::string > StringVector
union ptb::content::word::word word
QTextStream & endl(QTextStream &s)
template<typename T >
int StringManipulator::replace ( std::string  substr,
const T &  xsub 
)

Definition at line 136 of file StringManipulator.h.

136  {
137  std::string ssubin;
138  bool havesub = false;
139  std::string::size_type ipos = m_str.find(ssubout);
140  int count = 0;
141  while ( ipos != std::string::npos ) {
142  if ( ! havesub ) {
143  std::ostringstream sssubin;
144  sssubin << xsubin;
145  ssubin = sssubin.str();
146  }
147  std::string::size_type lout = ssubout.size();
148  m_str.replace(ipos, lout, ssubin);
149  ipos = m_str.find(ssubout, ipos+lout);
150  ++count;
151  }
152  return count;
153 }
std::string string
Definition: nybbler.cc:12
template<typename T >
int StringManipulator::replaceFixedWidth ( std::string  substr,
const T &  xsub,
Index  width 
)

Definition at line 159 of file StringManipulator.h.

159  {
160  std::string ssubin;
161  char cfill = getFill<T>(xsubin);
162  bool havesub = false;
163  std::string::size_type ipos = m_str.find(ssubout);
164  int count = 0;
165  while ( ipos != std::string::npos ) {
166  if ( ! havesub ) {
167  std::ostringstream sssubin;
168  sssubin.fill(cfill);
169  sssubin.width(width);
170  sssubin << xsubin;
171  ssubin = sssubin.str();
172  }
173  std::string::size_type lout = ssubout.size();
174  m_str.replace(ipos, lout, ssubin);
175  ipos = m_str.find(ssubout, ipos+lout);
176  ++count;
177  }
178  return count;
179 }
std::string string
Definition: nybbler.cc:12
void StringManipulator::setLogLevel ( Index  logLevel)
inline

Definition at line 59 of file StringManipulator.h.

std::string::size_type StringManipulator::size ( ) const
inline

Definition at line 69 of file StringManipulator.h.

69 { return str().size(); }
const std::string & str() const
const StringVector & StringManipulator::split ( std::string  seps,
bool  fullSplit = false 
)

Definition at line 19 of file StringManipulator.cxx.

19  {
20  m_splits.clear();
21  if ( seps.size() == 0 ) {
22  m_splits.push_back(m_str);
23  return m_splits;
24  }
25  // Replace all separators with the first,
26  string str = m_str;
27  char csep = seps[0];
28  for ( char ch : seps.substr(1) ) {
29  for ( char& rch : str ) if ( rch == ch ) rch = csep;
30  }
31  // Split.
32  string word;
33  bool isSep = false;
34  for ( char ch : m_str ) {
35  isSep = ch == csep;
36  if ( isSep ) {
37  if ( fullSplit || word.size() ) {
38  m_splits.push_back(word);
39  word = "";
40  }
41  } else {
42  word += ch;
43  }
44  }
45  if ( word.size() || (fullSplit && isSep) ) m_splits.push_back(word);
46  return m_splits;
47 }
const std::string & str() const
union ptb::content::word::word word
const StringVector& StringManipulator::splits ( ) const
inline

Definition at line 98 of file StringManipulator.h.

98 { return m_splits; }
const std::string& StringManipulator::str ( ) const
inline

Definition at line 63 of file StringManipulator.h.

63 { return m_str; }
float StringManipulator::toFloat ( float  valbad = 0) const

Definition at line 197 of file StringManipulator.cxx.

197  {
198  if ( str().size() == 0 ) return badval;
199  char* pch = nullptr;
200  float val = strtof(c_str(), &pch);
201  return pch[0] == '\0' ? val : badval;
202 }
const std::string & str() const
std::string::size_type size() const
const char * c_str() const
int StringManipulator::toInt ( int  valbad = 0) const

Definition at line 183 of file StringManipulator.cxx.

183  {
184  if ( ! isInt() ) return badval;
185  return std::strtol(c_str(), nullptr, 10);
186 }
const char * c_str() const
unsigned int StringManipulator::toUnsignedInt ( unsigned int  valbad = 0) const

Definition at line 190 of file StringManipulator.cxx.

190  {
191  if ( ! isUnsignedInt() ) return badval;
192  return std::strtoul(c_str(), nullptr, 10);
193 }
const char * c_str() const
bool isUnsignedInt() const

Member Data Documentation

int StringManipulator::m_LogLevel = 0
private

Definition at line 126 of file StringManipulator.h.

StringVector StringManipulator::m_splits
private

Definition at line 129 of file StringManipulator.h.

std::string& StringManipulator::m_str
private

Definition at line 127 of file StringManipulator.h.

std::string StringManipulator::m_strCopy
private

Definition at line 128 of file StringManipulator.h.


The documentation for this class was generated from the following files: