cprover
string_utils.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Poetzl
6 
7 \*******************************************************************/
8 
9 #include "string_utils.h"
10 #include "invariant.h"
11 
12 #include <cassert>
13 #include <cctype>
14 #include <algorithm>
15 
20 std::string strip_string(const std::string &s)
21 {
22  auto pred=[](char c){ return std::isspace(c); };
23 
24  std::string::const_iterator left
25  =std::find_if_not(s.begin(), s.end(), pred);
26  if(left==s.end())
27  return "";
28 
29  std::string::size_type i=std::distance(s.begin(), left);
30 
31  std::string::const_reverse_iterator right
32  =std::find_if_not(s.rbegin(), s.rend(), pred);
33  std::string::size_type j=std::distance(right, s.rend())-1;
34 
35  return s.substr(i, (j-i+1));
36 }
37 
49  const std::string &s,
50  char delim,
51  std::vector<std::string> &result,
52  bool strip,
53  bool remove_empty)
54 {
55  PRECONDITION(result.empty());
56  // delim can't be a space character if using strip
57  PRECONDITION(!std::isspace(delim) || !strip);
58 
59  if(s.empty())
60  {
61  result.push_back("");
62  return;
63  }
64 
65  std::string::size_type n=s.length();
66  INVARIANT(n > 0, "Empty string case should already be handled");
67 
68  std::string::size_type start=0;
70 
71  for(i=0; i<n; i++)
72  {
73  if(s[i]==delim)
74  {
75  std::string new_s=s.substr(start, i-start);
76 
77  if(strip)
78  new_s=strip_string(new_s);
79 
80  if(!remove_empty || !new_s.empty())
81  result.push_back(new_s);
82 
83  start=i+1;
84  }
85  }
86 
87  std::string new_s=s.substr(start, n-start);
88 
89  if(strip)
90  new_s=strip_string(new_s);
91 
92  if(!remove_empty || !new_s.empty())
93  result.push_back(new_s);
94 
95  if(result.empty())
96  result.push_back("");
97 }
98 
100  const std::string &s,
101  char delim,
102  std::string &left,
103  std::string &right,
104  bool strip)
105 {
106  // delim can't be a space character if using strip
107  PRECONDITION(!std::isspace(delim) || !strip);
108 
109  std::vector<std::string> result;
110 
111  split_string(s, delim, result, strip);
112  if(result.size()!=2)
113  throw "split string did not generate exactly 2 parts";
114 
115  left=result[0];
116  right=result[1];
117 }
118 
119 std::vector<std::string> split_string(const std::string &s, char delim)
120 {
121  std::vector<std::string> result;
122  split_string(s, delim, result);
123  return result;
124 }
125 
127  const std::string &s,
128  const char delim)
129 {
130  std::string result="";
131  const size_t index=s.find_last_of(delim);
132  if(index!=std::string::npos)
133  result=s.substr(0, index);
134  return result;
135 }
136 
137 std::string escape(const std::string &s)
138 {
139  std::string result;
140 
141  for(std::size_t i=0; i<s.size(); i++)
142  {
143  if(s[i]=='\\' || s[i]=='"')
144  result+='\\';
145 
146  result+=s[i];
147  }
148 
149  return result;
150 }
151 
161  std::string &str,
162  const std::string &from,
163  const std::string &to)
164 {
165  size_t start_pos = 0;
166  while((start_pos = str.find(from, start_pos)) != std::string::npos)
167  {
168  str.replace(start_pos, from.length(), to);
169  start_pos += to.length();
170  }
171 }
std::string strip_string(const std::string &s)
Remove all whitespace characters from either end of a string.
unsignedbv_typet size_type()
Definition: c_types.cpp:58
#define INVARIANT(CONDITION, REASON)
Definition: invariant.h:204
void replace_all(std::string &str, const std::string &from, const std::string &to)
Replace all occurrences of a string inside a string.
#define PRECONDITION(CONDITION)
Definition: invariant.h:242
std::string escape(const std::string &s)
Generic escaping of strings; this is not meant to be a particular programming language.
void split_string(const std::string &s, char delim, std::vector< std::string > &result, bool strip, bool remove_empty)
Given a string s, split into a sequence of substrings when separated by specified delimiter...
std::string trim_from_last_delimiter(const std::string &s, const char delim)