18 #ifndef ION_BASE_STRINGUTILS_H_
19 #define ION_BASE_STRINGUTILS_H_
27 #include "base/integral_types.h"
51 const std::string&
str,
const std::string& delimiters);
55 const std::string&
str,
const std::string& delimiters,
65 const std::string&
str,
const std::string& delimiters);
70 std::ostringstream out;
76 inline bool StartsWith(
const std::string& target,
const std::string& start) {
77 return start.length() && start.compare(target.substr(0, start.size())) == 0;
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;
89 inline std::string
JoinStrings(
const std::vector<std::string>& strings,
90 const std::string& glue) {
92 if (
const size_t count = strings.size()) {
94 for (
size_t i = 1; i < count; ++i)
95 joined += glue + strings[i];
102 inline bool RemovePrefix(
const std::string& prefix, std::string* target) {
104 *target = target->substr(prefix.length(),
105 target->length() - prefix.length());
113 inline bool RemoveSuffix(
const std::string& suffix, std::string* target) {
115 *target = target->substr(0, target->length() - suffix.length());
123 const std::string& from,
124 const std::string& to) {
125 if (search.empty() || from.empty())
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);
138 const size_t pos = target.find_first_not_of(
" \f\n\r\t\v");
140 if (pos != std::string::npos)
141 trimmed = target.substr(pos, std::string::npos);
147 const size_t pos = target.find_last_not_of(
" \f\n\r\t\v");
149 if (pos != std::string::npos)
150 trimmed = target.substr(0, pos + 1U);
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);
179 const std::string& str2);
183 const std::string& start);
187 const std::string& end);
191 const std::string& substr);
214 template <
char expected>
216 char c = expected + 1;
217 if (in >> c && c != expected) {
220 in.setstate(std::ios_base::failbit);
229 const std::string& expected) {
231 const size_t length = expected.length();
232 for (
size_t i = 0; i <
length; ++i) {
235 if (!in.good() || c != expected[i]) {
237 for (
size_t j = 0; j <= i; ++j)
239 in.setstate(std::ios_base::failbit);
254 const char* data =
reinterpret_cast<const char*
>(&
value);
255 s->insert(s->end(), data, data +
sizeof(
T));
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.
bool StartsWith(const std::string &target, const std::string &start)
Returns whether target begins with start.
ION_API bool EndsWithCaseInsensitive(const std::string &target, const std::string &end)
Returns whether target ends with end (case-insensitive).
std::string TrimStartAndEndWhitespace(const std::string &target)
Removes any whitespace characters at the beginning and end of the string.
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...
bool RemovePrefix(const std::string &prefix, std::string *target)
Removes prefix from the beginning of target if target starts with it.
SharedPtr< Allocator > AllocatorPtr
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).
bool RemoveSuffix(const std::string &suffix, std::string *target)
Removes suffix from the end of target if target ends with it.
std::string ION_API EscapeNewlines(const std::string &str)
Returns a string with all newlines replaced by "\\n".
std::string ION_API EscapeString(const std::string &str)
Returns an escaped version of the passed string.
void AppendBytes(std::string *s, const T &value)
Convenience method to append bytes to string.
int32 ION_API StringToInt32(const std::string &str)
Extracts and returns an integral value from str.
ION_API std::string UrlDecodeString(const std::string &str)
Returns a decoded version of a URL-encoded string.
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.
std::istream & GetExpectedChar(std::istream &in)
Reads a single character from the stream and returns the stream.
std::string QuoteString(const std::string &val)
Returns a quoted and escaped version of the passed string.
std::istream & GetExpectedString(std::istream &in, const std::string &expected)
Attempts to read a string from the stream and returns the stream.
std::string ION_API MimeBase64EncodeString(const std::string &str)
Returns a mime base-64 encoded version of the passed string.
bool EndsWith(const std::string &target, const std::string &end)
Returns whether target ends with end.
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.
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...
std::string TrimEndWhitespace(const std::string &target)
Removes any whitespace characters at the end of the string.
ION_API bool StartsWithCaseInsensitive(const std::string &target, const std::string &start)
Returns whether target begins with start (case-insensitive).
ION_API std::string UrlEncodeString(const std::string &str)
Returns a URL-encoded version of a string.
std::string TrimStartWhitespace(const std::string &target)
Removes any whitespace characters at the beginning of the string.
ION_API int FindCaseInsensitive(const std::string &target, const std::string &substr)
Case-insensitive version of std::string find.
ION_API std::string WebSafeBase64Decode(const std::string &str)
Decodes a Base64 encoded string.
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.