stringutil.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
20 #include <algorithm>
21 #include <iostream>
22 #include <cstring>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <limits.h>
26 #include <coil/stringutil.h>
27 #include <string.h>
28 #include <cctype>
29 #include <cstdio>
30 
31 namespace coil
32 {
40  std::wstring string2wstring(std::string str)
41  {
42  std::wstring wstr(str.length(), L' ');
43  std::copy(str.begin(),str.end(),wstr.begin());
44  return wstr;
45  }
46 
54  std::string wstring2string(std::wstring wstr)
55  {
56  std::string str(wstr.length(), ' ');
57  std::copy(wstr.begin(), wstr.end(), str.begin());
58  return str;
59  }
60 
68  void toUpper(std::string& str)
69  {
70  std::transform(str.begin(), str.end(), str.begin(),
71  (int (*)(int))std::toupper);
72  }
73 
81  void toLower(std::string& str)
82  {
83  std::transform(str.begin(), str.end(), str.begin(),
84  (int (*)(int))std::tolower);
85  }
86 
94  int getlinePortable(std::istream& istr, std::string& line)
95  {
96  char c;
97  std::stringstream s;
98 
99  while (istr.get(c))
100  {
101  if (c == '\n')
102  {
103  break;
104  }
105  else if (c == '\r')
106  {
107  if (istr.peek() == '\n')
108  {
109  istr.ignore();
110  }
111  break;
112  }
113  else
114  {
115  s << c;
116  }
117  }
118  line = s.str();
119  return static_cast<int>(line.size());
120  }
121 
129  bool isEscaped(const std::string& str, std::string::size_type pos)
130  {
131  --pos;
132  unsigned int i;
133  for (i = 0; (pos >= 0) && str[pos] == '\\'; --pos, ++i) ;
134  // If the number of \ is odd, delimiter is escaped.
135  return (i % 2) == 1;
136  }
137 
146  {
148  void operator()(const char c)
149  {
150  if (c == '\t') str += "\\t";
151  else if (c == '\n') str += "\\n";
152  else if (c == '\f') str += "\\f";
153  else if (c == '\r') str += "\\r";
154  else if (c == '\\') str += "\\\\";
155  // else if (c == '\"') str += "\\\"";
156  // else if (c == '\'') str += "\\\'";
157  else str.push_back(c);
158  }
159  std::string str;
160  };
161 
169  std::string escape(const std::string str)
170  {
171  return for_each(str.begin(), str.end(), escape_functor()).str;
172  }
173 
182  {
183  unescape_functor() : count(0) {};
184  void operator()(char c)
185  {
186  if (c == '\\')
187  {
188  ++count;
189  if (!(count % 2))
190  {
191  str.push_back(c);
192  }
193  }
194  else
195  {
196  if (count > 0 && (count % 2))
197  {
198  count = 0;
199  if (c == 't') str.push_back('\t');
200  else if (c == 'n') str.push_back('\n');
201  else if (c == 'f') str.push_back('\f');
202  else if (c == 'r') str.push_back('\r');
203  else if (c == '\"') str.push_back('\"');
204  else if (c == '\'') str.push_back('\'');
205  else str.push_back(c);
206  }
207  else
208  {
209  count = 0;
210  str.push_back(c);
211  }
212  }
213  }
214  std::string str;
215  int count;
216  };
217 
225  std::string unescape(const std::string str)
226  {
227  return for_each(str.begin(), str.end(), unescape_functor()).str;
228  }
229 
237  void eraseBlank(std::string& str)
238  {
239  std::string::iterator it(str.begin());
240 
241  while (it != str.end())
242  {
243  if (*it == ' ' || *it == '\t')
244  {
245  it = str.erase(it);
246  }
247  else
248  {
249  ++it;
250  }
251  }
252 
253  }
254 
262  void eraseHeadBlank(std::string& str)
263  {
264  if (str.empty()) return;
265  while (str[0] == ' ' || str[0] == '\t') str.erase(0, 1);
266  }
267 
275  void eraseTailBlank(std::string& str)
276  {
277  if (str.empty()) return;
278  while ((str[str.size() - 1] == ' ' || str[str.size() - 1] == '\t') &&
279  !isEscaped(str, str.size() - 1))
280  str.erase(str.size() - 1, 1);
281  }
282 
290  void eraseBothEndsBlank(std::string& str)
291  {
292  eraseHeadBlank(str);
293  eraseTailBlank(str);
294  }
295 
303  std::string normalize(std::string& str)
304  {
305  eraseBothEndsBlank(str);
306  toLower(str);
307  return str;
308  }
309 
317  unsigned int replaceString(std::string& str, const std::string from,
318  const std::string to)
319  {
320  std::string::size_type pos(0);
321  unsigned int cnt(0);
322 
323  while (pos != std::string::npos)
324  {
325  pos = str.find(from, pos);
326  if (pos == std::string::npos) break;
327  str.replace(pos, from.size(), to);
328  pos += to.size();
329  ++cnt;
330  }
331  return cnt;
332  }
333 
341  vstring split(const std::string& input,
342  const std::string& delimiter,
343  bool ignore_empty)
344  {
345  typedef std::string::size_type size;
346  vstring results;
347  size delim_size = delimiter.size();
348  size found_pos(0), begin_pos(0), pre_pos(0), substr_size(0);
349 
350  if (input.empty()) return results;
351 
352  // if (input.substr(0, delim_size) == delimiter)
353  // begin_pos = pre_pos = delim_size;
354 
355  while (1)
356  {
357  // REFIND:
358  found_pos = input.find(delimiter, begin_pos);
359  if (found_pos == std::string::npos)
360  {
361  std::string substr(input.substr(pre_pos));
362  eraseHeadBlank(substr);
363  eraseTailBlank(substr);
364  if (!(substr.empty() && ignore_empty)) results.push_back(substr);
365  break;
366  }
367  /*
368  if (isEscaped(input, found_pos))
369  {
370  begin_pos = found_pos + delim_size;
371  goto REFIND;
372  }
373  */
374  substr_size = found_pos - pre_pos;
375  if (substr_size >= 0)
376  {
377  std::string substr(input.substr(pre_pos, substr_size));
378  eraseHeadBlank(substr);
379  eraseTailBlank(substr);
380  if (!(substr.empty() && ignore_empty)) results.push_back(substr);
381  }
382  begin_pos = found_pos + delim_size;
383  pre_pos = found_pos + delim_size;
384  }
385  return results;
386  }
387 
395  struct Toupper
396  {
397  void operator()(char &c)
398  {
399  c = toupper(c);
400  }
401  };
402 
410  bool toBool(std::string str, std::string yes, std::string no,
411  bool default_value)
412  {
413  std::for_each(str.begin(), str.end(), Toupper());
414  std::for_each(yes.begin(), yes.end(), Toupper());
415  std::for_each(no.begin(), no.end(), Toupper());
416 
417  if (str.find(yes) != std::string::npos)
418  return true;
419  else if (str.find(no) != std::string::npos)
420  return false;
421  else
422  return default_value;
423  }
424 
432  bool includes(const vstring& list, std::string value, bool ignore_case)
433  {
434  if (ignore_case) { toLower(value); }
435 
436  for (int i(0), len(static_cast<int>(list.size())); i < len; ++i)
437  {
438  std::string str(list[i]);
439  if (ignore_case) { toLower(str); }
440  if (str == value) return true;
441  }
442  return false;
443  }
444 
452  bool includes(const std::string& list, std::string value, bool ignore_case)
453  {
454  vstring vlist(split(list, ","));
455  return includes(vlist, value, ignore_case);
456  }
457 
465  bool isAbsolutePath(const std::string& str)
466  {
467  // UNIX absolute path is begun from '/'
468  if (str[0] == '/') return true;
469  // Windows absolute path is begun from '[a-zA-Z]:\'
470  if (isalpha(str[0]) && (str[1] == ':') && str[2] == '\\') return true;
471  // Windows network file path is begun from '\\'
472  if (str[0] == '\\' && str[1] == '\\') return true;
473 
474  return false;
475  }
476 
484  bool isURL(const std::string& str)
485  {
486  typedef std::string::size_type size;
487  size pos;
488  if (str.empty()) return false;
489  pos = str.find(":");
490  if ((pos != 0) &&
491  (pos != std::string::npos) &&
492  (str[pos + 1] == '/') &&
493  (str[pos + 2] == '/'))
494  return true;
495  return false;
496  }
497 
506  {
507  void operator()(const std::string& s)
508  {
509  if (std::find(str.begin(), str.end(), s) == str.end())
510  return str.push_back(s);
511  }
513  };
514 
522  template<>
523  bool stringTo<std::string>(std::string& val, const char* str)
524  {
525  if (str == 0) { return false; }
526  val = str;
527  return true;
528  }
529 
538  {
539  return std::for_each(sv.begin(), sv.end(), unique_strvec()).str;
540  }
541 
549  std::string flatten(vstring sv)
550  {
551  if (sv.size() == 0) return "";
552 
553  std::string str;
554  for (size_t i(0), len(sv.size() - 1); i < len; ++i)
555  {
556  str += sv[i] + ", ";
557  }
558  return str + sv.back();
559  }
560 
568  char** toArgv(const vstring& args)
569  {
570  char** argv;
571  size_t argc(args.size());
572 
573  argv = new char*[argc + 1];
574 
575  for (size_t i(0); i < argc; ++i)
576  {
577  size_t sz(args[i].size());
578  argv[i] = new char[sz + 1];
579  strncpy(argv[i], args[i].c_str(), sz);
580  argv[i][sz] = '\0';
581  }
582  argv[argc] = NULL;
583  return argv;
584  }
585 
593  std::string sprintf(char const * __restrict fmt, ...)
594  {
595 #ifndef LINE_MAX
596 #define LINE_MAX 1024
597 #endif
598  char str[LINE_MAX];
599  va_list ap;
600 
601  va_start(ap, fmt);
602 #ifdef WIN32
603  _vsnprintf(str, LINE_MAX - 1, fmt, ap);
604 #else
605  vsnprintf(str, LINE_MAX - 1, fmt, ap);
606 #endif
607  va_end(ap);
608  return std::string(str);
609  }
610 
611 }; // namespace coil
std::string normalize(std::string &str)
Erase the head/tail blank and replace upper case to lower case.
Definition: stringutil.cpp:303
void operator()(const std::string &s)
Definition: stringutil.cpp:507
void toUpper(std::string &str)
Uppercase String Transformation.
Definition: stringutil.cpp:68
void operator()(char c)
Definition: stringutil.cpp:184
void operator()(char &c)
Definition: stringutil.cpp:397
void eraseBlank(std::string &str)
Erase blank characters of string.
Definition: stringutil.cpp:237
void eraseHeadBlank(std::string &str)
Erase the head blank characters of string.
Definition: stringutil.cpp:262
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
Split string by delimiter.
Definition: stringutil.cpp:341
vstring unique_sv(vstring sv)
Eliminate duplication from the given string list.
Definition: stringutil.cpp:537
void operator()(const char c)
Definition: stringutil.cpp:148
void eraseBothEndsBlank(std::string &str)
Erase the head blank and the tail blank characters of string.
Definition: stringutil.cpp:290
bool isAbsolutePath(const std::string &str)
Investigate whether the given string is absolute path or not.
Definition: stringutil.cpp:465
std::vector< std::string > vstring
Definition: stringutil.h:37
void eraseTailBlank(std::string &str)
Erase the tail blank characters of string.
Definition: stringutil.cpp:275
std::string flatten(vstring sv)
Create CSV file from the given string list.
Definition: stringutil.cpp:549
CORBA::Long find(const CorbaSequence &seq, Functor f)
Return the index of CORBA sequence element that functor matches.
char ** toArgv(const vstring &args)
Convert the given string list into the argument list.
Definition: stringutil.cpp:568
bool isURL(const std::string &str)
Investigate whether the given string is URL or not.
Definition: stringutil.cpp:484
Functor to escape string.
Definition: stringutil.cpp:145
unsigned int replaceString(std::string &str, const std::string from, const std::string to)
Replace string.
Definition: stringutil.cpp:317
void toLower(std::string &str)
Lowercase String Transformation.
Definition: stringutil.cpp:81
bool toBool(std::string str, std::string yes, std::string no, bool default_value)
Convert given string into bool value.
Definition: stringutil.cpp:410
std::string wstring2string(std::wstring wstr)
wstring to string conversion
Definition: stringutil.cpp:54
int getlinePortable(std::istream &istr, std::string &line)
Read a line from input stream.
Definition: stringutil.cpp:94
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:593
std::wstring string2wstring(std::string str)
string to wstring conversion
Definition: stringutil.cpp:40
#define LINE_MAX
bool isEscaped(const std::string &str, std::string::size_type pos)
Check whether the character is escaped or not.
Definition: stringutil.cpp:129
Functor to unescape string.
Definition: stringutil.cpp:181
bool includes(const vstring &list, std::string value, bool ignore_case)
Include if a string is included in string list.
Definition: stringutil.cpp:432
std::string unescape(const std::string str)
Unescape string.
Definition: stringutil.cpp:225
Functor for_each(CorbaSequence &seq, Functor f)
Apply the functor to all CORBA sequence elements.
Definition: CORBA_SeqUtil.h:98
std::string escape(const std::string str)
Escape string.
Definition: stringutil.cpp:169
Functor to convert to capital letters.
Definition: stringutil.cpp:395
Common Object Interface Layer.
Functor to find string in a list.
Definition: stringutil.cpp:505


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:26:00