25 std::string::const_iterator left
26 =std::find_if_not(s.begin(), s.end(), pred);
32 std::string::const_reverse_iterator right
33 =std::find_if_not(s.rbegin(), s.rend(), pred);
36 return s.substr(i, (j-i+1));
42 std::vector<std::string> &result,
58 INVARIANT(n > 0,
"Empty string case should already be handled");
67 std::string new_s=s.substr(start, i-start);
72 if(!remove_empty || !new_s.empty())
73 result.push_back(new_s);
79 std::string new_s=s.substr(start, n-start);
84 if(!remove_empty || !new_s.empty())
85 result.push_back(new_s);
87 if(!remove_empty && result.empty())
101 std::vector<std::string> result =
split_string(s, delim, strip);
103 if(result.size() != 2)
106 "' to contain two substrings "
108 delim +
" but has " +
117 const std::string &s,
122 std::vector<std::string> result;
128 const std::string &s,
132 const size_t index=s.find_last_of(delim);
133 if(index!=std::string::npos)
134 result=s.substr(0, index);
142 for(std::size_t i=0; i<s.size(); i++)
144 if(s[i]==
'\\' || s[i]==
'"')
155 std::ostringstream escaped;
156 for(
auto &ch : to_escape)
165 const int uch{
static_cast<unsigned char>(ch)};
171 escaped <<
'_' << std::hex << std::setfill(
'0') << std::setw(2) << uch;
173 return escaped.str();
179 std::string capitalized = str;
180 capitalized[0] =
toupper(capitalized[0]);
185 const std::string &line,
186 const std::size_t left_margin,
187 const std::size_t width)
189 return wrap_line(line.cbegin(), line.cend(), left_margin, width);
193 std::string::const_iterator left,
194 std::string::const_iterator right,
195 const std::size_t left_margin,
196 const std::size_t width)
200 const std::size_t column_width = width - left_margin;
201 const std::string margin(left_margin,
' ');
203 auto distance = std::distance(left, right);
208 if(
static_cast<std::size_t
>(distance) <= column_width)
210 result.append(margin);
211 result.append(left, right);
216 auto it_line_begin = left;
221 auto it = it_line_begin + column_width;
223 auto rit_r = std::reverse_iterator<decltype(it)>(it) - 1;
224 auto rit_l = rit_r + column_width;
226 auto rit_space = std::find(rit_r, rit_l,
' ');
228 if(rit_space != rit_l)
230 auto it_space = rit_space.base() - 1;
233 result.append(margin);
234 result.append(it_line_begin, it_space);
237 it_line_begin = it_space + 1;
243 result.append(left, right);
247 }
while(
static_cast<std::size_t
>(std::distance(it_line_begin, right)) >
250 result.append(margin);
251 result.append(it_line_begin, right);
Thrown when failing to deserialize a value from some low level format, like JSON or raw bytes.
#define CHECK_RETURN(CONDITION)
#define PRECONDITION(CONDITION)
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
std::string trim_from_last_delimiter(const std::string &s, const char delim)
void split_string(const std::string &s, char delim, std::vector< std::string > &result, bool strip, bool remove_empty)
std::string strip_string(const std::string &s)
Remove all whitespace characters from either end of a string.
std::string escape(const std::string &s)
Generic escaping of strings; this is not meant to be a particular programming language.
std::string escape_non_alnum(const std::string &to_escape)
Replace non-alphanumeric characters with _xx escapes, where xx are hex digits.
std::string wrap_line(const std::string &line, const std::size_t left_margin, const std::size_t width)
Wrap line at spaces to not extend past the right margin, and include given padding with spaces to the...
std::string capitalize(const std::string &str)