CBMC
string_utils.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Poetzl
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_UTIL_STRING_UTILS_H
11 #define CPROVER_UTIL_STRING_UTILS_H
12 
13 #include <iosfwd>
14 #include <string>
15 #include <vector>
16 
17 std::string strip_string(const std::string &s);
18 
19 std::string capitalize(const std::string &str);
20 
21 void split_string(
22  const std::string &s,
23  char delim,
24  std::string &left,
25  std::string &right,
26  bool strip = false);
27 
37 std::vector<std::string> split_string(
38  const std::string &s,
39  char delim,
40  bool strip = false,
41  bool remove_empty = false);
42 
43 std::string trim_from_last_delimiter(
44  const std::string &s,
45  const char delim);
46 
57 template <
58  typename Stream,
59  typename It,
60  typename Delimiter,
61  typename TransformFunc>
62 Stream &join_strings(
63  Stream &&os,
64  const It b,
65  const It e,
66  const Delimiter &delimiter,
67  TransformFunc &&transform_func)
68 {
69  if(b==e)
70  {
71  return os;
72  }
73  os << transform_func(*b);
74  for(auto it=std::next(b); it!=e; ++it)
75  {
76  os << delimiter << transform_func(*it);
77  }
78  return os;
79 }
80 
89 template <typename Stream, typename It, typename Delimiter>
90 Stream &
91 join_strings(Stream &&os, const It b, const It e, const Delimiter &delimiter)
92 {
93  using value_type = decltype(*b);
94  // Call auxiliary function with identity function
95  return join_strings(
96  os, b, e, delimiter, [](const value_type &x) { return x; });
97 }
98 
101 std::string escape(const std::string &);
102 
107 std::string escape_non_alnum(const std::string &to_escape);
108 
123 std::string wrap_line(
124  const std::string &line,
125  const std::size_t left_margin = 0,
126  const std::size_t width = 80);
127 
143 std::string wrap_line(
144  const std::string::const_iterator left,
145  const std::string::const_iterator right,
146  const std::size_t left_margin = 0,
147  const std::size_t width = 80);
148 
149 #endif
wrap_line
std::string wrap_line(const std::string &line, const std::size_t left_margin=0, const std::size_t width=80)
Wrap line at spaces to not extend past the right margin, and include given padding with spaces to the...
Definition: string_utils.cpp:184
escape
std::string escape(const std::string &)
Generic escaping of strings; this is not meant to be a particular programming language.
Definition: string_utils.cpp:138
capitalize
std::string capitalize(const std::string &str)
Definition: string_utils.cpp:175
join_strings
Stream & join_strings(Stream &&os, const It b, const It e, const Delimiter &delimiter, TransformFunc &&transform_func)
Prints items to an stream, separated by a constant delimiter.
Definition: string_utils.h:62
split_string
void split_string(const std::string &s, char delim, std::string &left, std::string &right, bool strip=false)
Definition: string_utils.cpp:91
trim_from_last_delimiter
std::string trim_from_last_delimiter(const std::string &s, const char delim)
Definition: string_utils.cpp:127
escape_non_alnum
std::string escape_non_alnum(const std::string &to_escape)
Replace non-alphanumeric characters with _xx escapes, where xx are hex digits.
Definition: string_utils.cpp:153
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