Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions src/util/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,27 @@ Author: Daniel Poetzl
/// in the middle of the string is left unchanged
/// \param s: the string to strip
/// \return The stripped string
std::string strip_string(const std::string &s)
std::string strip_string(const std::string_view &s)
{
auto pred=[](char c){ return std::isspace(c); };

std::string::const_iterator left
=std::find_if_not(s.begin(), s.end(), pred);
std::string_view::const_iterator left =
std::find_if_not(s.begin(), s.end(), pred);
if(left==s.end())
return "";

std::string::size_type i=std::distance(s.begin(), left);
std::string_view::size_type i = std::distance(s.begin(), left);

std::string::const_reverse_iterator right
=std::find_if_not(s.rbegin(), s.rend(), pred);
std::string_view::const_reverse_iterator right =
std::find_if_not(s.rbegin(), s.rend(), pred);
std::string::size_type j=std::distance(right, s.rend())-1;

return s.substr(i, (j-i+1));
// copy happens here; this could return a view in the future
return std::string{s.substr(i, (j - i + 1))};
}

void split_string(
const std::string &s,
const std::string_view &s,
char delim,
std::vector<std::string> &result,
bool strip,
Expand All @@ -64,7 +65,8 @@ void split_string(
{
if(s[i]==delim)
{
std::string new_s=s.substr(start, i-start);
// this is a copy, and should become a view
std::string new_s = std::string{s.substr(start, i - start)};

if(strip)
new_s=strip_string(new_s);
Expand All @@ -76,7 +78,8 @@ void split_string(
}
}

std::string new_s=s.substr(start, n-start);
// this is a copy, and should become a view
std::string new_s = std::string{s.substr(start, n - start)};

if(strip)
new_s=strip_string(new_s);
Expand All @@ -89,7 +92,7 @@ void split_string(
}

void split_string(
const std::string &s,
const std::string_view &s,
char delim,
std::string &left,
std::string &right,
Expand All @@ -102,19 +105,19 @@ void split_string(

if(result.size() != 2)
{
throw deserialization_exceptiont{"expected string '" + s +
"' to contain two substrings "
"delimited by " +
delim + " but has " +
std::to_string(result.size())};
throw deserialization_exceptiont{
"expected string '" + std::string{s} +
"' to contain two substrings "
"delimited by " +
delim + " but has " + std::to_string(result.size())};
}

left=result[0];
right=result[1];
}

std::vector<std::string> split_string(
const std::string &s,
const std::string_view &s,
char delim,
bool strip,
bool remove_empty)
Expand All @@ -124,9 +127,8 @@ std::vector<std::string> split_string(
return result;
}

std::string trim_from_last_delimiter(
const std::string &s,
const char delim)
std::string
trim_from_last_delimiter(const std::string_view &s, const char delim)
{
std::string result;
const size_t index=s.find_last_of(delim);
Expand All @@ -135,7 +137,7 @@ std::string trim_from_last_delimiter(
return result;
}

std::string escape(const std::string &s)
std::string escape(const std::string_view &s)
{
std::string result;

Expand All @@ -150,7 +152,7 @@ std::string escape(const std::string &s)
return result;
}

std::string escape_non_alnum(const std::string &to_escape)
std::string escape_non_alnum(const std::string_view &to_escape)
{
std::ostringstream escaped;
for(auto &ch : to_escape)
Expand All @@ -172,11 +174,12 @@ std::string escape_non_alnum(const std::string &to_escape)
}
return escaped.str();
}
std::string capitalize(const std::string &str)

std::string capitalize(const std::string_view &str)
{
if(str.empty())
return str;
std::string capitalized = str;
return std::string{};
std::string capitalized = std::string{str}; // copy
capitalized[0] = toupper(capitalized[0]);
return capitalized;
}
Expand Down
18 changes: 9 additions & 9 deletions src/util/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ Author: Daniel Poetzl
#define CPROVER_UTIL_STRING_UTILS_H

#include <string>
#include <string_view>
#include <vector>

std::string strip_string(const std::string &s);
std::string strip_string(const std::string_view &);

std::string capitalize(const std::string &str);
std::string capitalize(const std::string_view &);

void split_string(
const std::string &s,
const std::string_view &s,
char delim,
std::string &left,
std::string &right,
Expand All @@ -34,14 +35,13 @@ void split_string(
/// This is applied after strip so whitespace only elements will be removed if
/// both are set to true.
std::vector<std::string> split_string(
const std::string &s,
const std::string_view &s,
char delim,
bool strip = false,
bool remove_empty = false);

std::string trim_from_last_delimiter(
const std::string &s,
const char delim);
std::string
trim_from_last_delimiter(const std::string_view &s, const char delim);

/// Prints items to an stream, separated by a constant delimiter
/// \tparam It: An iterator type
Expand Down Expand Up @@ -97,13 +97,13 @@ join_strings(Stream &&os, const It b, const It e, const Delimiter &delimiter)

/// Generic escaping of strings; this is not meant to be a particular
/// programming language.
std::string escape(const std::string &);
std::string escape(const std::string_view &);

/// Replace non-alphanumeric characters with `_xx` escapes, where xx are hex
/// digits. Underscores are replaced by `__`.
/// \param to_escape: string to escape
/// \return string with non-alphanumeric characters escaped
std::string escape_non_alnum(const std::string &to_escape);
std::string escape_non_alnum(const std::string_view &to_escape);

/// Wrap line at spaces to not extend past the right margin, and include given
/// padding with spaces to the left
Expand Down
Loading
Loading