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 "exception_utils.h"
11 #include "invariant.h"
12 
13 #include <cassert>
14 #include <cctype>
15 #include <algorithm>
16 
21 std::string strip_string(const std::string &s)
22 {
23  auto pred=[](char c){ return std::isspace(c); };
24 
25  std::string::const_iterator left
26  =std::find_if_not(s.begin(), s.end(), pred);
27  if(left==s.end())
28  return "";
29 
30  std::string::size_type i=std::distance(s.begin(), left);
31 
32  std::string::const_reverse_iterator right
33  =std::find_if_not(s.rbegin(), s.rend(), pred);
34  std::string::size_type j=std::distance(right, s.rend())-1;
35 
36  return s.substr(i, (j-i+1));
37 }
38 
50  const std::string &s,
51  char delim,
52  std::vector<std::string> &result,
53  bool strip,
54  bool remove_empty)
55 {
56  PRECONDITION(result.empty());
57  // delim can't be a space character if using strip
58  PRECONDITION(!std::isspace(delim) || !strip);
59 
60  if(s.empty())
61  {
62  result.push_back("");
63  return;
64  }
65 
66  std::string::size_type n=s.length();
67  INVARIANT(n > 0, "Empty string case should already be handled");
68 
69  std::string::size_type start=0;
71 
72  for(i=0; i<n; i++)
73  {
74  if(s[i]==delim)
75  {
76  std::string new_s=s.substr(start, i-start);
77 
78  if(strip)
79  new_s=strip_string(new_s);
80 
81  if(!remove_empty || !new_s.empty())
82  result.push_back(new_s);
83 
84  start=i+1;
85  }
86  }
87 
88  std::string new_s=s.substr(start, n-start);
89 
90  if(strip)
91  new_s=strip_string(new_s);
92 
93  if(!remove_empty || !new_s.empty())
94  result.push_back(new_s);
95 
96  if(result.empty())
97  result.push_back("");
98 }
99 
101  const std::string &s,
102  char delim,
103  std::string &left,
104  std::string &right,
105  bool strip)
106 {
107  // delim can't be a space character if using strip
108  PRECONDITION(!std::isspace(delim) || !strip);
109 
110  std::vector<std::string> result;
111 
112  split_string(s, delim, result, strip);
113  if(result.size() != 2)
114  {
115  throw deserialization_exceptiont{"expected string `" + s +
116  "' to contain two substrings "
117  "delimited by " +
118  delim + " but has " +
119  std::to_string(result.size())};
120  }
121 
122  left=result[0];
123  right=result[1];
124 }
125 
126 std::vector<std::string> split_string(
127  const std::string &s,
128  char delim,
129  bool strip,
130  bool remove_empty)
131 {
132  std::vector<std::string> result;
133  split_string(s, delim, result, strip, remove_empty);
134  return result;
135 }
136 
138  const std::string &s,
139  const char delim)
140 {
141  std::string result="";
142  const size_t index=s.find_last_of(delim);
143  if(index!=std::string::npos)
144  result=s.substr(0, index);
145  return result;
146 }
147 
148 std::string escape(const std::string &s)
149 {
150  std::string result;
151 
152  for(std::size_t i=0; i<s.size(); i++)
153  {
154  if(s[i]=='\\' || s[i]=='"')
155  result+='\\';
156 
157  result+=s[i];
158  }
159 
160  return result;
161 }
162 
172  std::string &str,
173  const std::string &from,
174  const std::string &to)
175 {
176  size_t start_pos = 0;
177  while((start_pos = str.find(from, start_pos)) != std::string::npos)
178  {
179  str.replace(start_pos, from.length(), to);
180  start_pos += to.length();
181  }
182 }
exception_utils.h
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:438
string_utils.h
deserialization_exceptiont
Thrown when failing to deserialize a value from some low level format, like JSON or raw bytes.
Definition: exception_utils.h:72
INVARIANT
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:400
invariant.h
to_string
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Definition: string_constraint.cpp:53
trim_from_last_delimiter
std::string trim_from_last_delimiter(const std::string &s, const char delim)
Definition: string_utils.cpp:137
split_string
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.
Definition: string_utils.cpp:49
strip_string
std::string strip_string(const std::string &s)
Remove all whitespace characters from either end of a string.
Definition: string_utils.cpp:21
replace_all
void replace_all(std::string &str, const std::string &from, const std::string &to)
Replace all occurrences of a string inside a string.
Definition: string_utils.cpp:171
size_type
unsignedbv_typet size_type()
Definition: c_types.cpp:58
escape
std::string escape(const std::string &s)
Generic escaping of strings; this is not meant to be a particular programming language.
Definition: string_utils.cpp:148