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 
32  2019,
33  11,
34  14,
35  "use split_string(s, delim, strip, remove_empty) instead"))
36 void split_string(
37  const std::string &s,
38  char delim,
39  std::vector<std::string> &result,
40  bool strip = false,
41  bool remove_empty = false);
42 
43 void split_string(
44  const std::string &s,
45  char delim,
46  std::string &left,
47  std::string &right,
48  bool strip=false);
49 
50 std::vector<std::string> split_string(
51  const std::string &s,
52  char delim,
53  bool strip = false,
54  bool remove_empty = false);
55 
56 std::string trim_from_last_delimiter(
57  const std::string &s,
58  const char delim);
59 
70 template <
71  typename Stream,
72  typename It,
73  typename Delimiter,
74  typename TransformFunc>
75 Stream &join_strings(
76  Stream &&os,
77  const It b,
78  const It e,
79  const Delimiter &delimiter,
80  TransformFunc &&transform_func)
81 {
82  if(b==e)
83  {
84  return os;
85  }
86  os << transform_func(*b);
87  for(auto it=std::next(b); it!=e; ++it)
88  {
89  os << delimiter << transform_func(*it);
90  }
91  return os;
92 }
93 
102 template <typename Stream, typename It, typename Delimiter>
103 Stream &
104 join_strings(Stream &&os, const It b, const It e, const Delimiter &delimiter)
105 {
106  using value_type = decltype(*b);
107  // Call auxiliary function with identity function
108  return join_strings(
109  os, b, e, delimiter, [](const value_type &x) { return x; });
110 }
111 
114 std::string escape(const std::string &);
115 
120 std::string escape_non_alnum(const std::string &to_escape);
121 
122 #endif
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:140
deprecate.h
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:75
SINCE
#define SINCE(year, month, day, msg)
Definition: deprecate.h:26
split_string
void split_string(const std::string &s, char delim, std::vector< std::string > &result, bool strip=false, bool remove_empty=false)
Given a string s, split into a sequence of substrings when separated by specified delimiter.
Definition: string_utils.cpp:40
DEPRECATED
#define DEPRECATED(msg)
Definition: deprecate.h:23
trim_from_last_delimiter
std::string trim_from_last_delimiter(const std::string &s, const char delim)
Definition: string_utils.cpp:129
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:155
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