Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stringutils.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_STRINGUTILS_H_
19 #define ION_BASE_STRINGUTILS_H_
20 
22 
23 #include <istream> // NOLINT
24 #include <string>
25 #include <vector>
26 
27 #include "base/integral_types.h"
29 
30 namespace ion {
31 namespace base {
32 
35 ION_API std::string EscapeString(const std::string& str);
36 
38 ION_API std::string EscapeNewlines(const std::string& str);
39 
42 ION_API std::string MimeBase64EncodeString(const std::string& str);
43 
50 ION_API std::vector<std::string> SplitString(
51  const std::string& str, const std::string& delimiters);
52 
54 ION_API AllocVector<std::string> SplitString(
55  const std::string& str, const std::string& delimiters,
56  const AllocatorPtr& alloc);
57 
64 ION_API std::vector<std::string> SplitStringWithoutSkipping(
65  const std::string& str, const std::string& delimiters);
66 
69 inline std::string QuoteString(const std::string& val) {
70  std::ostringstream out;
71  out << '"' << EscapeString(val) << '"';
72  return out.str();
73 }
74 
76 inline bool StartsWith(const std::string& target, const std::string& start) {
77  return start.length() && start.compare(target.substr(0, start.size())) == 0;
78 }
79 
81 inline bool EndsWith(const std::string& target, const std::string& end) {
82  return end.length() && end.length() <= target.length() &&
83  target.compare(target.length() - end.length(), end.length(), end) == 0;
84 }
85 
89 inline std::string JoinStrings(const std::vector<std::string>& strings,
90  const std::string& glue) {
91  std::string joined;
92  if (const size_t count = strings.size()) {
93  joined = strings[0];
94  for (size_t i = 1; i < count; ++i)
95  joined += glue + strings[i];
96  }
97  return joined;
98 }
99 
102 inline bool RemovePrefix(const std::string& prefix, std::string* target) {
103  if (StartsWith(*target, prefix)) {
104  *target = target->substr(prefix.length(),
105  target->length() - prefix.length());
106  return true;
107  }
108  return false;
109 }
110 
113 inline bool RemoveSuffix(const std::string& suffix, std::string* target) {
114  if (EndsWith(*target, suffix)) {
115  *target = target->substr(0, target->length() - suffix.length());
116  return true;
117  }
118  return false;
119 }
120 
122 inline std::string ReplaceString(const std::string& search,
123  const std::string& from,
124  const std::string& to) {
125  if (search.empty() || from.empty())
126  return search;
127 
128  std::string replaced = search;
129  const size_t to_size = to.size();
130  for (size_t start_pos = replaced.find(from); start_pos != std::string::npos;
131  start_pos = replaced.find(from, start_pos + to_size))
132  replaced.replace(start_pos, from.length(), to);
133  return replaced;
134 }
135 
137 inline std::string TrimStartWhitespace(const std::string& target) {
138  const size_t pos = target.find_first_not_of(" \f\n\r\t\v");
139  std::string trimmed;
140  if (pos != std::string::npos)
141  trimmed = target.substr(pos, std::string::npos);
142  return trimmed;
143 }
144 
146 inline std::string TrimEndWhitespace(const std::string& target) {
147  const size_t pos = target.find_last_not_of(" \f\n\r\t\v");
148  std::string trimmed;
149  if (pos != std::string::npos)
150  trimmed = target.substr(0, pos + 1U);
151  return trimmed;
152 }
153 
155 inline std::string TrimStartAndEndWhitespace(const std::string& target) {
156  return TrimEndWhitespace(TrimStartWhitespace(target));
157 }
158 
160 ION_API std::string UrlDecodeString(const std::string& str);
161 
163 ION_API std::string UrlEncodeString(const std::string& str);
164 
172 ION_API bool AreMultiLineStringsEqual(
173  const std::string& s0, const std::string& s1,
174  size_t* first_different_index, std::string* line0, std::string* line1,
175  std::string* context0, std::string* context1);
176 
178 ION_API int CompareCaseInsensitive(const std::string& str1,
179  const std::string& str2);
180 
182 ION_API bool StartsWithCaseInsensitive(const std::string& target,
183  const std::string& start);
184 
186 ION_API bool EndsWithCaseInsensitive(const std::string& target,
187  const std::string& end);
188 
190 ION_API int FindCaseInsensitive(const std::string& target,
191  const std::string& substr);
192 
202 ION_API std::string WebSafeBase64Decode(const std::string& str);
203 
207 ION_API std::string WebSafeBase64Encode(const std::string& input);
208 
214 template <char expected>
215 inline std::istream& GetExpectedChar(std::istream& in) { // NOLINT
216  char c = expected + 1;
217  if (in >> c && c != expected) {
219  in.unget();
220  in.setstate(std::ios_base::failbit);
221  }
222  return in;
223 }
224 
228 inline std::istream& GetExpectedString(std::istream& in, // NOLINT
229  const std::string& expected) {
230  if (!in.fail()) {
231  const size_t length = expected.length();
232  for (size_t i = 0; i < length; ++i) {
233  char c;
234  in >> c;
235  if (!in.good() || c != expected[i]) {
237  for (size_t j = 0; j <= i; ++j)
238  in.unget();
239  in.setstate(std::ios_base::failbit);
240  break;
241  }
242  }
243  }
244  return in;
245 }
246 
249 ION_API int32 StringToInt32(const std::string& str);
250 
252 template<typename T>
253 void AppendBytes(std::string* s, const T& value) {
254  const char* data = reinterpret_cast<const char*>(&value);
255  s->insert(s->end(), data, data + sizeof(T));
256 }
257 
258 } // namespace base
259 } // namespace ion
260 
261 #endif // ION_BASE_STRINGUTILS_H_
ION_API int CompareCaseInsensitive(const std::string &str1, const std::string &str2)
Case-insensitive comparison of str1 and str2.
Definition: stringutils.cc:335
bool StartsWith(const std::string &target, const std::string &start)
Returns whether target begins with start.
Definition: stringutils.h:76
const std::string & str
double value
ION_API bool EndsWithCaseInsensitive(const std::string &target, const std::string &end)
Returns whether target ends with end (case-insensitive).
Definition: stringutils.cc:353
std::string TrimStartAndEndWhitespace(const std::string &target)
Removes any whitespace characters at the beginning and end of the string.
Definition: stringutils.h:155
std::vector< std::string > ION_API SplitString(const std::string &str, const std::string &delimiters)
Splits a string into a vector of substrings, given a set of delimiter characters (expressed as a stri...
Definition: stringutils.cc:187
uint32 length
bool RemovePrefix(const std::string &prefix, std::string *target)
Removes prefix from the beginning of target if target starts with it.
Definition: stringutils.h:102
SharedPtr< Allocator > AllocatorPtr
Definition: allocator.h:51
ION_API std::string WebSafeBase64Encode(const std::string &input)
Encodes a byte array using RFC 4648 base64url ('-' and '_' for 62 and 63, respectively, and no padding).
Definition: stringutils.cc:411
bool RemoveSuffix(const std::string &suffix, std::string *target)
Removes suffix from the end of target if target ends with it.
Definition: stringutils.h:113
std::string ION_API EscapeNewlines(const std::string &str)
Returns a string with all newlines replaced by "\\n".
Definition: stringutils.cc:152
std::string ION_API EscapeString(const std::string &str)
Returns an escaped version of the passed string.
Definition: stringutils.cc:105
void AppendBytes(std::string *s, const T &value)
Convenience method to append bytes to string.
Definition: stringutils.h:253
int32 ION_API StringToInt32(const std::string &str)
Extracts and returns an integral value from str.
Definition: stringutils.cc:328
ION_API std::string UrlDecodeString(const std::string &str)
Returns a decoded version of a URL-encoded string.
Definition: stringutils.cc:222
std::string JoinStrings(const std::vector< std::string > &strings, const std::string &glue)
Joins the strings in the passed vector together with the passed glue.
Definition: stringutils.h:89
std::istream & GetExpectedChar(std::istream &in)
Reads a single character from the stream and returns the stream.
Definition: stringutils.h:215
std::string QuoteString(const std::string &val)
Returns a quoted and escaped version of the passed string.
Definition: stringutils.h:69
std::istream & GetExpectedString(std::istream &in, const std::string &expected)
Attempts to read a string from the stream and returns the stream.
Definition: stringutils.h:228
std::string ION_API MimeBase64EncodeString(const std::string &str)
Returns a mime base-64 encoded version of the passed string.
Definition: stringutils.cc:57
bool EndsWith(const std::string &target, const std::string &end)
Returns whether target ends with end.
Definition: stringutils.h:81
bool ION_API AreMultiLineStringsEqual(const std::string &s0, const std::string &s1, size_t *first_different_index, std::string *line0, std::string *line1, std::string *context0, std::string *context1)
This function can be useful for comparing multi-line strings in tests.
Definition: stringutils.cc:267
std::vector< std::string > ION_API SplitStringWithoutSkipping(const std::string &str, const std::string &delimiters)
Splits a string into a vector of substrings, given a set of delimiter characters (expressed as a stri...
Definition: stringutils.cc:202
std::string TrimEndWhitespace(const std::string &target)
Removes any whitespace characters at the end of the string.
Definition: stringutils.h:146
ION_API bool StartsWithCaseInsensitive(const std::string &target, const std::string &start)
Returns whether target begins with start (case-insensitive).
Definition: stringutils.cc:347
ION_API std::string UrlEncodeString(const std::string &str)
Returns a URL-encoded version of a string.
Definition: stringutils.cc:246
std::string TrimStartWhitespace(const std::string &target)
Removes any whitespace characters at the beginning of the string.
Definition: stringutils.h:137
ION_API int FindCaseInsensitive(const std::string &target, const std::string &substr)
Case-insensitive version of std::string find.
Definition: stringutils.cc:359
ION_API std::string WebSafeBase64Decode(const std::string &str)
Decodes a Base64 encoded string.
Definition: stringutils.cc:375
std::string ReplaceString(const std::string &search, const std::string &from, const std::string &to)
Returns a string with all instances of from replaced with to.
Definition: stringutils.h:122