StringManipulator.cxx
Go to the documentation of this file.
1 // StringManipulator.h
2 
4 #include <iostream>
5 #include <sstream>
6 #include <cctype>
7 #include <cstdlib>
8 
9 using std::string;
10 using std::cout;
11 using std::endl;
12 using std::ostringstream;
13 
15 using StringVV = std::vector<StringVector>;
16 
17 //**********************************************************************
18 
19 const StringVector& StringManipulator::split(string seps, bool fullSplit) {
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 }
48 
49 //**********************************************************************
50 
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 }
106 
107 //**********************************************************************
108 
109 string StringManipulator::
110 floatToString(float val, int prec, bool trunc, string sdot, string smin) {
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 }
145 
146 //**********************************************************************
147 
149  if ( str().empty() ) return false;
150  for ( char c : str() ) if ( ! std::isdigit(c) ) return false;
151  return true;
152 }
153 
154 //**********************************************************************
155 
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 }
162 
163 //**********************************************************************
164 
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 }
171 
172 //**********************************************************************
173 
175  if ( str().size() == 0 ) return false;
176  char* pch = nullptr;
177  strtof(c_str(), &pch);
178  return pch[0] == '\0';
179 }
180 
181 //**********************************************************************
182 
183 int StringManipulator::toInt(int badval) const {
184  if ( ! isInt() ) return badval;
185  return std::strtol(c_str(), nullptr, 10);
186 }
187 
188 //**********************************************************************
189 
190 unsigned int StringManipulator::toUnsignedInt(unsigned int badval) const {
191  if ( ! isUnsignedInt() ) return badval;
192  return std::strtoul(c_str(), nullptr, 10);
193 }
194 
195 //**********************************************************************
196 
197 float StringManipulator::toFloat(float badval) const {
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 }
203 
204 //**********************************************************************
const StringVector & patternSplit(std::string spat)
static std::string floatToString(float val, int prec, bool trunc, std::string sdot="", std::string smin="")
const std::vector< std::string > & split(std::string seps, bool fullSplit=false)
std::string string
Definition: nybbler.cc:12
Index logLevel() const
const std::string & str() const
std::string::size_type size() const
unsigned int toUnsignedInt(unsigned int valbad=0) const
const char * c_str() const
int toInt(int valbad=0) const
std::vector< string > StringVector
Definition: fcldump.cxx:29
std::vector< StringVector > StringVV
StringManipulator(std::string &strin, bool copy)
std::vector< std::string > StringVector
bool isUnsignedInt() const
union ptb::content::word::word word
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:97
QTextStream & endl(QTextStream &s)
float toFloat(float valbad=0) const