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 bool isOnlyWhitespace(const std::string& input)
48 {
49  for(const char& c: input)
50  {
51  // see http://en.cppreference.com/w/cpp/string/byte/isspace
52  // for reason for casting
53  if(!std::isspace(static_cast<unsigned char>(c)))
54  return false;
55  }
56 
57  return true;
58 }
59 
60 }
61 }
62 }
std::string simplifyWhitespace(const std::string &input)
Compress any sequence of whitespace to single spaces.
bool isOnlyWhitespace(const std::string &input)
Check if string is whitespace only (includes &#39; &#39;)


rosmon_core
Author(s): Max Schwarz
autogenerated on Wed Jul 10 2019 03:10:12