cprover
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 "deprecate.h"
14 
15 #include <iosfwd>
16 #include <string>
17 #include <vector>
18 
19 std::string strip_string(const std::string &s);
20 
21 std::string capitalize(const std::string &str);
22 
23 void split_string(
24  const std::string &s,
25  char delim,
26  std::string &left,
27  std::string &right,
28  bool strip = false);
29 
39 std::vector<std::string> split_string(
40  const std::string &s,
41  char delim,
42  bool strip = false,
43  bool remove_empty = false);
44 
45 std::string trim_from_last_delimiter(
46  const std::string &s,
47  const char delim);
48 
59 template <
60  typename Stream,
61  typename It,
62  typename Delimiter,
63  typename TransformFunc>
64 Stream &join_strings(
65  Stream &&os,
66  const It b,
67  const It e,
68  const Delimiter &delimiter,
69  TransformFunc &&transform_func)
70 {
71  if(b==e)
72  {
73  return os;
74  }
75  os << transform_func(*b);
76  for(auto it=std::next(b); it!=e; ++it)
77  {
78  os << delimiter << transform_func(*it);
79  }
80  return os;
81 }
82 
91 template <typename Stream, typename It, typename Delimiter>
92 Stream &
93 join_strings(Stream &&os, const It b, const It e, const Delimiter &delimiter)
94 {
95  using value_type = decltype(*b);
96  // Call auxiliary function with identity function
97  return join_strings(
98  os, b, e, delimiter, [](const value_type &x) { return x; });
99 }
100 
103 std::string escape(const std::string &);
104 
109 std::string escape_non_alnum(const std::string &to_escape);
110 
125 std::string wrap_line(
126  const std::string &line,
127  const std::size_t left_margin = 0,
128  const std::size_t width = 80);
129 
145 std::string wrap_line(
146  const std::string::const_iterator left,
147  const std::string::const_iterator right,
148  const std::size_t left_margin = 0,
149  const std::size_t width = 80);
150 
151 #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:185
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:139
deprecate.h
capitalize
std::string capitalize(const std::string &str)
Definition: string_utils.cpp:176
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:64
split_string
void split_string(const std::string &s, char delim, std::string &left, std::string &right, bool strip=false)
Definition: string_utils.cpp:92
trim_from_last_delimiter
std::string trim_from_last_delimiter(const std::string &s, const char delim)
Definition: string_utils.cpp:128
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:154
strip_string
std::string strip_string(const std::string &s)
Remove all whitespace characters from either end of a string.
Definition: string_utils.cpp:22