string_utils.cpp
Go to the documentation of this file.
1 // String utilities
2 // Author: Max Schwarz <max.schwarz@ais.uni-bonn.de>
3 
4 #include "string_utils.h"
5 
6 namespace rosmon
7 {
8 namespace launch
9 {
10 namespace string_utils
11 {
12 
13 std::string simplifyWhitespace(const std::string& input)
14 {
15  std::string output;
16  output.reserve(input.size());
17 
18  // Skip initial space
19  size_t i = 0;
20  for(; i < input.size(); ++i)
21  {
22  if(!std::isspace(static_cast<unsigned char>(input[i])))
23  break;
24  }
25 
26  bool in_space = false;
27 
28  for(; i < input.size(); ++i)
29  {
30  char c = input[i];
31 
32  if(std::isspace(static_cast<unsigned char>(c)))
33  in_space = true;
34  else
35  {
36  if(in_space)
37  output.push_back(' ');
38 
39  output.push_back(c);
40  in_space = false;
41  }
42  }
43 
44  return output;
45 }
46 
47 std::string strip(const std::string& input)
48 {
49  constexpr const char whitespace[] = " \t\f\v\r\n";
50 
51  std::string ret = input;
52  ret.erase(0, ret.find_first_not_of(whitespace, 0, sizeof(whitespace)));
53  ret.erase(ret.find_last_not_of(whitespace, std::string::npos, sizeof(whitespace))+1);
54 
55  return ret;
56 }
57 
58 std::string convertWhitespace(const std::string& input)
59 {
60  std::string ret;
61  ret.reserve(input.size());
62 
63  for(auto c : input)
64  {
65  if(std::isspace(static_cast<unsigned char>(c)))
66  ret.push_back(' ');
67  else
68  ret.push_back(c);
69  }
70 
71  return ret;
72 }
73 
74 bool isOnlyWhitespace(const std::string& input)
75 {
76  for(const char& c: input)
77  {
78  // see http://en.cppreference.com/w/cpp/string/byte/isspace
79  // for reason for casting
80  if(!std::isspace(static_cast<unsigned char>(c)))
81  return false;
82  }
83 
84  return true;
85 }
86 
87 }
88 }
89 }
std::string simplifyWhitespace(const std::string &input)
Compress any sequence of whitespace to single spaces.
std::string convertWhitespace(const std::string &input)
Convert any whitespace to space characters.
std::string strip(const std::string &input)
bool isOnlyWhitespace(const std::string &input)
Check if string is whitespace only (includes &#39; &#39;)


rosmon_core
Author(s): Max Schwarz
autogenerated on Sat Jan 9 2021 03:35:43