Sierra Toolkit  Version of the Day
StringUtil.cpp
1 
10 #include <iostream>
11 #include <iomanip>
12 #include <sstream>
13 #include <stdexcept>
14 #include <string>
15 #include <cstring>
16 #include <vector>
17 #include <typeinfo>
18 
19 #include <float.h>
20 #include <time.h>
21 
22 #include <stk_util/diag/StringUtil.hpp>
23 
24 // Set for unit testing
25 
26 #define SIERRA_STRING_UNIT_TEST 0
27 
28 //----------------------------------------------------------------------
29 
30 namespace sierra {
31 
32 std::istream &
34  std::istream & is,
35  sierra::String & s,
36  char eol)
37 {
38  std::string std_string;
39 
40  getline(is, std_string, eol);
41  s = std_string.c_str();
42  return is;
43 }
44 
45 
46 int
48  const char * c1,
49  const char * c2)
50 {
51  for ( ; ; c1++, c2++) {
52  if ( std::tolower(*c1) != std::tolower(*c2) )
53  return ( std::tolower(*c1) - std::tolower(*c2) ) ;
54  if (*c1 == '\0')
55  return 0 ;
56  }
57 }
58 
59 
60 int
61 case_strncmp(
62  const char * c1,
63  const char * c2,
64  size_t n)
65 {
66 
67  if (n == 0)
68  return 0;
69 
70  do {
71  if (*c1 != *c2++)
72  return std::tolower(*c1) - std::tolower(*(c2 - 1));
73  if (*c1++ == 0)
74  break;
75  } while (--n != 0);
76  return 0;
77 }
78 
79 
80 const char *
81 case_strstr(
82  const char * s,
83  const char * find)
84 {
85  if (!*find)
86  return s;
87 
88  const char *cp = s;
89  while (*cp) {
90  const char *t1 = cp;
91  const char *t2 = find;
92 
93  for ( ; std::tolower(*t1) && std::tolower(*t2) && !(std::tolower(*t1) - std::tolower(*t2)); ++t1, ++t2)
94  ;
95 
96  if (!*t2)
97  return cp;
98 
99  cp++;
100  }
101 
102  return NULL;
103 }
104 
105 
106 std::string
108  const std::string & s)
109 {
110  std::string t(s);
111 
112  bool all_upper = true;
113  bool all_lower = true;
114 
115  bool next_upper = true;
116  for (std::string::iterator c = t.begin(); c != t.end(); ++c) {
117  all_upper &= (*c == std::toupper(*c));
118  all_lower &= (*c == std::tolower(*c));
119  if (next_upper)
120  *c = std::toupper(*c);
121  else
122  *c = std::tolower(*c);
123  next_upper = !isalpha(*c);
124  }
125 
126  if (all_upper || all_lower)
127  return t;
128  else
129  return s;
130 }
131 
132 
133 template <typename T>
134 std::string
136  const T & t)
137 {
138  std::ostringstream os;
139  os << t;
140  return os.str();
141 }
142 
143 template std::string to_string<double>(const double &);
144 template std::string to_string<float>(const float &);
145 template std::string to_string<int>(const int &);
146 template std::string to_string<unsigned>(const unsigned &);
147 template std::string to_string<long>(const long &);
148 template std::string to_string<unsigned long>(const unsigned long &);
149 
150 std::string
152  const double & r,
153  int precision)
154 {
155  std::ostringstream os;
156  os << std::setprecision(precision) << r;
157  return std::string(os.str());
158 }
159 
160 
161 std::string
163  const float & r,
164  int precision)
165 {
166  std::ostringstream os;
167  os << std::setprecision(precision) << r;
168  return std::string(os.str());
169 }
170 
171 
172 std::string
174  double t,
175  const char * format)
176 {
177  time_t time = (time_t) t;
178  char s[128];
179 
180  ::strftime(s, sizeof(s), format, ::localtime(&time));
181 
182  return std::string(s);
183 }
184 
185 
186 std::ostream &
188  std::ostream & os) const
189 {
190  if (m_n == 0)
191  os << m_plural << " no " << m_noun << "s";
192  else if (m_n == 1)
193  os << m_singular << " 1 " << m_noun;
194  else
195  os << m_plural << " " << m_n << " " << m_noun << "s";
196 
197  return os;
198 }
199 
200 
201 object_phrase::operator std::string() const
202 {
203  std::ostringstream strout;
204  strout << *this;
205 
206  return strout.str();
207 }
208 
209 
210 namespace {
211 
212 std::string::const_iterator
213 find_next_char(
214  std::string::const_iterator p,
215  std::string::const_iterator end,
216  char c)
217 {
218  while (p != end && *p != c)
219  p++;
220  return p;
221 }
222 
223 std::string::const_iterator
224 find_next_not_char(
225  std::string::const_iterator p,
226  std::string::const_iterator end,
227  char c)
228 {
229  while (p != end && *p == c)
230  p++;
231  return p;
232 }
233 
234 inline std::string::const_iterator find_next_space(std::string::const_iterator p, std::string::const_iterator end) {
235  return find_next_char(p, end, ' ');
236 }
237 
238 inline std::string::const_iterator find_next_endl(std::string::const_iterator p, std::string::const_iterator end) {
239  return find_next_char(p, end, '\n');
240 }
241 
242 inline std::string::const_iterator find_next_nonspace(std::string::const_iterator p, std::string::const_iterator end) {
243  return find_next_not_char(p, end, ' ');
244 }
245 
246 } // namespace <null>
247 
248 std::string
250  const std::string & s,
251  unsigned int line_length,
252  const std::string & prefix,
253  const std::string & prefix_first_line)
254 {
255  std::string t;
256  const std::string *u = &prefix_first_line;
257 
258  std::string::const_iterator p0, p1, p2, p3;
259  p0 = p1 = p2 = s.begin();
260 
261  while (p2 != s.end() ) {
262 
263  // skip preceeding whitespace
264  p1 = find_next_nonspace(p0, s.end());
265  p3 = find_next_endl(p0, s.end());
266  p2 = p1 = find_next_space(p1, s.end());
267  do {
268  p1 = find_next_nonspace(p1, s.end());
269  p1 = find_next_space(p1, s.end());
270  if (p3 < p1) {
271  p2 = p3;
272  break;
273  }
274  if ((unsigned int) (p1 - p0) > (line_length - u->size()))
275  break;
276  p2 = p1;
277  } while (p2 != s.end());
278 
279  t.append(*u).append(p0, p2).append("\n");
280 
281  if (p2 == p3)
282  u = &prefix_first_line;
283  else
284  u = &prefix;
285 
286  p0 = p2 + 1;
287  }
288 
289  return t;
290 }
291 
292 
293 template <class T>
294 T convert_cast(const String &s)
295 {
296  /* %TRACE% */ /* %TRACE% */
297  std::istringstream is(s.c_str());
298  T t = 0;
299 
300  is >> t;
301 
302  if (!is) {
303  std::ostringstream msg;
304  msg << "Unable to convert \"" << s << "\" to type " << typeid(T).name();
305  throw std::runtime_error(msg.str().c_str());
306  }
307 
308 
309  return t;
310 }
311 
312 template double convert_cast<double>(const String &);
313 template float convert_cast<float>(const String &);
314 template int convert_cast<int>(const String &);
315 template unsigned convert_cast<unsigned>(const String &);
316 template long convert_cast<long>(const String &);
317 template unsigned long convert_cast<unsigned long>(const String &);
318 
319 
320 // void
321 // get_function_spec_parts(
322 // const std::string & spec,
323 // std::string & namespace_name,
324 // std::string & class_name,
325 // std::string & function_name,
326 // std::vector<std::string> & arglist)
327 // {
328 // namespace_name.erase(namespace_name.begin(), namespace_name.end());
329 // class_name.erase(class_name.begin(), class_name.end());
330 // function_name.erase(function_name.begin(), function_name.end());
331 // arglist.erase(arglist.begin(), arglist.end());
332 
333 // std::string::const_iterator it_paren = find_next_open_paren(spec.begin(), spec.end());
334 // std::string::const_iterator it_func_name = find_prev_double_colon(spec.begin(), it_paren);
335 // function_name = std::string(it_func_name, it_paren);
336 // if (it_func_name != spec.begin()) {
337 // it_func_name -= 2;
338 // std::string::const_iterator it_class_name = find_prev_double_colon(spec.begin(), it_func_name);
339 // class_name = std::string(it_class_name, it_func_name);
340 // if (it_class_name != spec.begin()) {
341 // it_class_name -= 2;
342 // namespace_name = std::string(spec.begin(), it_class_name);
343 // }
344 // }
345 // }
346 
347 } // namespace sierra
std::string title(const std::string &s)
Function title returns a first letter of each word capitalized of the string.
Definition: StringUtil.cpp:107
Definition: Env.cpp:53
std::ostream & print(std::ostream &os) const
Member function print writes the object phrase to the output stream.
Definition: StringUtil.cpp:187
std::istream & getline(std::istream &is, sierra::String &s, char eol)
Function getline returns a string from the input stream which has been terminated by the newline char...
Definition: StringUtil.cpp:33
_setprecision setprecision(int precision)
Function setprecision sets the numeric precision as a manipulator.
Definition: WriterManip.hpp:70
T convert_cast(const String &s)
Class specialization hash_nocase for std::string.
Definition: StringUtil.cpp:294
std::string format_time(double t, const char *format)
Function format_time encodes the time using the format specified. The format is described in stdftime...
Definition: StringUtil.cpp:173
std::string word_wrap(const std::string &s, unsigned int line_length, const std::string &prefix, const std::string &prefix_first_line)
Function word_wrap reformats a string into multiple lines, none longer that line_length, the first line prefixed with prefix_first_line and the remaining lines prefixed with prefix.
Definition: StringUtil.cpp:249
Part * find(const PartVector &parts, const std::string &name)
Find a part by name in a collection of parts.
Definition: Part.cpp:22
int case_strcmp(const char *c1, const char *c2)
Function case_strcmp compares two null terminated strings case insenstively. It returns zero if they ...
Definition: StringUtil.cpp:47
std::string to_string(const T &t)
Definition: StringUtil.cpp:135