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 
11 #include <cassert>
12 #include <cctype>
13 #include <algorithm>
14 
15 std::string strip_string(const std::string &s)
16 {
17  auto pred=[](char c){ return std::isspace(c); };
18 
19  std::string::const_iterator left
20  =std::find_if_not(s.begin(), s.end(), pred);
21  if(left==s.end())
22  return "";
23 
24  std::string::size_type i=std::distance(s.begin(), left);
25 
26  std::string::const_reverse_iterator right
27  =std::find_if_not(s.rbegin(), s.rend(), pred);
28  std::string::size_type j=std::distance(right, s.rend())-1;
29 
30  return s.substr(i, (j-i+1));
31 }
32 
34  const std::string &s,
35  char delim,
36  std::vector<std::string> &result,
37  bool strip,
38  bool remove_empty)
39 {
40  assert(result.empty());
41  assert(!std::isspace(delim));
42 
43  if(s.empty())
44  {
45  result.push_back("");
46  return;
47  }
48 
49  std::string::size_type n=s.length();
50  assert(n>0);
51 
52  std::string::size_type start=0;
54 
55  for(i=0; i<n; i++)
56  {
57  if(s[i]==delim)
58  {
59  std::string new_s=s.substr(start, i-start);
60 
61  if(strip)
62  new_s=strip_string(new_s);
63 
64  if(!remove_empty || !new_s.empty())
65  result.push_back(new_s);
66 
67  start=i+1;
68  }
69  }
70 
71  std::string new_s=s.substr(start, n-start);
72 
73  if(strip)
74  new_s=strip_string(new_s);
75 
76  if(!remove_empty || !new_s.empty())
77  result.push_back(new_s);
78 
79  if(result.empty())
80  result.push_back("");
81 }
82 
84  const std::string &s,
85  char delim,
86  std::string &left,
87  std::string &right,
88  bool strip)
89 {
90  assert(!std::isspace(delim));
91 
92  std::vector<std::string> result;
93 
94  split_string(s, delim, result, strip);
95  if(result.size()!=2)
96  throw "split string did not generate exactly 2 parts";
97 
98  left=result[0];
99  right=result[1];
100 }
std::string strip_string(const std::string &s)
unsignedbv_typet size_type()
Definition: c_types.cpp:57
void split_string(const std::string &s, char delim, std::vector< std::string > &result, bool strip, bool remove_empty)