1 #ifndef ROSE_StringUtility_H
2 #define ROSE_StringUtility_H
4 #include "commandline_processing.h"
7 #include <boost/algorithm/string/erase.hpp>
8 #include <boost/algorithm/string/predicate.hpp>
9 #include <boost/algorithm/string/replace.hpp>
12 #include <rose_constants.h>
17 #include <Sawyer/IntervalSet.h>
21 #include <msvc_stdint.h>
30 namespace StringUtility {
42 ROSE_UTIL_API std::string
htmlEscape(
const std::string&);
56 ROSE_UTIL_API std::string
cEscape(
const std::string&,
char context =
'"');
57 ROSE_UTIL_API std::string
cEscape(
char,
char context =
'\'');
68 ROSE_UTIL_API std::string
cUnescape(
const std::string&);
73 ROSE_UTIL_API std::string
bourneEscape(
const std::string&);
89 ROSE_UTIL_API std::string escapeNewlineAndDoubleQuoteCharacters(
const std::string&);
112 ROSE_UTIL_API std::vector<std::string>
split(
const std::string &separator,
const std::string &str,
size_t maxparts =
UNLIMITED,
113 bool trim_white_space =
false);
114 ROSE_UTIL_API std::vector<std::string>
split(
char separator,
const std::string &str,
size_t maxparts =
UNLIMITED,
115 bool trim_white_space =
false);
122 ROSE_UTIL_API std::list<std::string>
tokenize(
const std::string&,
char delim);
131 template<
class Iterator>
132 std::string
join_range(
const std::string &separator, Iterator begin, Iterator end) {
133 std::ostringstream retval;
134 for (Iterator i=begin; i!=end; ++i)
135 retval <<(i==begin ? std::string() : separator) <<*i;
139 template<
class Container>
140 std::string
join(
const std::string &separator,
const Container &container) {
141 return join_range(separator, container.begin(), container.end());
144 template<
class Container>
145 std::string
join(
char separator,
const Container &container) {
146 return join_range(std::string(1, separator), container.begin(), container.end());
149 ROSE_UTIL_API std::string
join(
const std::string &separator,
char *strings[],
size_t nstrings);
150 ROSE_UTIL_API std::string
join(
const std::string &separator,
const char *strings[],
size_t nstrings);
151 ROSE_UTIL_API std::string
join(
char separator,
char *strings[],
size_t nstrings);
152 ROSE_UTIL_API std::string
join(
char separator,
const char *strings[],
size_t nstrings);
175 ROSE_UTIL_API std::string
joinEnglish(
const std::vector<std::string> &phrases,
176 const std::string &separator =
",",
177 const std::string &finalIntro =
"and");
192 ROSE_UTIL_API std::string
listToString(
const std::list<int>&,
bool separateStrings =
false);
200 ROSE_UTIL_API std::string
listToString(
const std::list<std::string>&,
bool separateStrings =
false);
201 ROSE_UTIL_API std::string
listToString(
const std::vector<std::string>&,
bool separateStrings =
false);
212 ROSE_UTIL_API std::list<std::string>
stringToList(
const std::string&);
218 ROSE_UTIL_API
void splitStringIntoStrings(
const std::string& inputString,
char separator, std::vector<std::string>& stringList);
239 #if !defined(_MSC_VER) && \
240 defined(BACKEND_CXX_IS_GNU_COMPILER) && \
241 ((BACKEND_CXX_COMPILER_MAJOR_VERSION_NUMBER == 4 && BACKEND_CXX_COMPILER_MINOR_VERSION_NUMBER > 6) || \
242 BACKEND_CXX_COMPILER_MAJOR_VERSION_NUMBER > 4) && \
259 ROSE_UTIL_API std::string
intToHex(uint64_t);
270 ROSE_UTIL_API std::string
toHex2(uint64_t value,
size_t nbits,
271 bool show_unsigned_decimal=
true,
bool show_signed_decimal=
true,
272 uint64_t decimal_threshold=256);
273 ROSE_UTIL_API std::string
signedToHex2(uint64_t value,
size_t nbits);
274 ROSE_UTIL_API std::string
unsignedToHex2(uint64_t value,
size_t nbits);
276 template<
typename T> std::string
toHex(T value) {
return toHex2((uint64_t)value, 8*
sizeof(T)); }
286 ROSE_UTIL_API std::string
addrToString(uint64_t value,
size_t nbits = 0);
303 template<
typename Un
signed>
304 std::string
toBinary(Unsigned value,
size_t nBits = 0,
size_t groupSize = 4,
const std::string groupSeparator =
"_") {
308 for (
size_t i = nBits; i > 0; --i) {
310 if (groupSize > 0 && i > 1 && (i-1) % groupSize == 0)
311 retval += groupSeparator;
355 template<
class Container,
class Stringifier>
359 template<
class Iterator,
class Stringifier>
361 std::vector<std::string> retval;
362 for (; begin!=end; ++begin)
363 retval.push_back(stringifier(*begin));
395 ROSE_UTIL_API std::string
prefixLines(
const std::string &lines,
const std::string &prefix,
396 bool prefixAtFront=
true,
bool prefixAtBack=
false);
407 ROSE_UTIL_API std::string
makeOneLine(
const std::string &s, std::string replacement=
" ");
412 ROSE_UTIL_API std::string
trim(
const std::string &str,
const std::string &strip=
" \t\r\n",
413 bool at_beginning=
true,
bool at_end=
true);
416 ROSE_UTIL_API std::string
untab(
const std::string &str,
size_t tabstops=8,
size_t firstcol=0);
445 ROSE_UTIL_API std::string
encode_base64(
const std::vector<uint8_t> &data,
bool do_pad=
true);
446 ROSE_UTIL_API std::string
encode_base64(
const uint8_t *data,
size_t nbytes,
bool do_padd=
true);
450 ROSE_UTIL_API std::vector<uint8_t>
decode_base64(
const std::string &encoded);
471 inline bool isContainedIn(
const std::string &longString,
const std::string &shortString) {
472 return longString.find(shortString) != std::string::npos;
488 ROSE_UTIL_API std::string
indentMultilineString(
const std::string& inputString,
int statementColumnNumber);
492 const std::string &abbr,
const std::string &full);
509 ROSE_UTIL_API std::string
appendAsmComment(
const std::string &s,
const std::string &comment);
534 std::string
plural(T n,
const std::string &plural_phrase,
const std::string &singular_phrase=
"") {
535 assert(!plural_phrase.empty());
538 if (!singular_phrase.empty()) {
539 retval += singular_phrase;
540 }
else if (boost::ends_with(plural_phrase,
"vertices")) {
541 retval += boost::replace_tail_copy(plural_phrase, 8,
"vertex");
542 }
else if (boost::ends_with(plural_phrase,
"indices")) {
543 retval += boost::replace_tail_copy(plural_phrase, 7,
"index");
544 }
else if (boost::ends_with(plural_phrase,
"ies") && plural_phrase.size() > 3) {
546 retval += boost::replace_tail_copy(plural_phrase, 3,
"y");
547 }
else if (boost::ends_with(plural_phrase,
"sses") || boost::ends_with(plural_phrase,
"indexes")) {
549 retval += boost::erase_tail_copy(plural_phrase, 2);
550 }
else if (boost::ends_with(plural_phrase,
"s") && plural_phrase.size() > 1) {
552 retval += boost::erase_tail_copy(plural_phrase, 1);
555 retval += plural_phrase;
558 retval += plural_phrase;
ROSE_UTIL_API std::string intToHex(uint64_t)
Convert an integer to a hexadecimal string.
std::string demangledName(std::string)
Compute demangled version of mangled name.
ROSE_UTIL_API std::string signedToHex2(uint64_t value, size_t nbits)
Convert a number to a hexadecimal and decimal string.
ROSE_UTIL_API std::string cUnescape(const std::string &)
Unescapes C++ string literals.
ROSE_UTIL_API std::vector< std::string > split(const std::string &separator, const std::string &str, size_t maxparts=UNLIMITED, bool trim_white_space=false)
Splits strings into parts.
ROSE_UTIL_API std::string numberToString(long long)
Convert an integer to a string.
std::string toBinary(Unsigned value, size_t nBits=0, size_t groupSize=4, const std::string groupSeparator="_")
Convert a number to a binary string.
const size_t UNLIMITED(-1)
Effictively unlimited size.
std::string toHex(T value)
Convert a number to a hexadecimal and decimal string.
std::string plural(T n, const std::string &plural_phrase, const std::string &singular_phrase="")
Helpful way to print singular or plural words.
ROSE_UTIL_API std::string removeRedundantSubstrings(const std::string &)
Remove redundant and blank lines.
size_t nBits(Unsigned x=Unsigned(0))
Number of bits in a type or value.
std::vector< std::string > toStrings_range(Iterator begin, Iterator end, const Stringifier &stringifier=numberToString)
Converts a bunch of numbers to strings.
ROSE_UTIL_API std::string encode_base64(const std::vector< uint8_t > &data, bool do_pad=true)
Convert binary data to base-64.
Main namespace for the ROSE library.
ROSE_UTIL_API std::string cEscape(const std::string &, char context= '"')
Escapes characters that are special to C/C++.
ROSE_UTIL_API std::string prefixLines(const std::string &lines, const std::string &prefix, bool prefixAtFront=true, bool prefixAtBack=false)
Insert a prefix string before every line.
A container holding a set of values.
ROSE_UTIL_API std::list< std::string > stringToList(const std::string &)
Split a string into substrings at line feeds.
ROSE_UTIL_API std::vector< uint8_t > decode_base64(const std::string &encoded)
Convert base-64 to binary.
std::string join_range(const std::string &separator, Iterator begin, Iterator end)
Join individual items to form a single string.
ROSE_UTIL_API bool isLineTerminated(const std::string &s)
Returns true if the string ends with line termination.
ROSE_UTIL_API std::list< std::string > tokenize(const std::string &, char delim)
Split a string into a list based on a separator character.
ROSE_UTIL_API void splitStringIntoStrings(const std::string &inputString, char separator, std::vector< std::string > &stringList)
Splits string into substring based on a separator character.
ROSE_UTIL_API std::string unsignedToHex2(uint64_t value, size_t nbits)
Convert a number to a hexadecimal and decimal string.
ROSE_UTIL_API std::string bourneEscape(const std::string &)
Escapes characters that are special to the Bourne shell.
ROSE_UTIL_API std::string appendAsmComment(const std::string &s, const std::string &comment)
Append an assembly comment to a string.
ROSE_UTIL_API std::string untab(const std::string &str, size_t tabstops=8, size_t firstcol=0)
Expand horizontal tab characters.
ROSE_UTIL_API std::string convertToLowerCase(const std::string &inputString)
Convert to lower case.
ROSE_UTIL_API unsigned hexadecimalToInt(char)
Convert an ASCII hexadecimal character to an integer.
std::vector< std::string > toStrings(const Container &numbers, const Stringifier &stringifier=numberToString)
Converts a bunch of numbers to strings.
ROSE_UTIL_API std::string htmlEscape(const std::string &)
Escapes HTML special characters.
ROSE_UTIL_API std::string addrToString(uint64_t value, size_t nbits=0)
Convert a virtual address to a string.
ROSE_UTIL_API std::string makeOneLine(const std::string &s, std::string replacement=" ")
Converts a multi-line string to a single line.
ROSE_UTIL_API void add_to_reason_string(std::string &result, bool isset, bool do_pad, const std::string &abbr, const std::string &full)
Append an abbreviation or full name to a string.
ROSE_UTIL_API std::string trim(const std::string &str, const std::string &strip=" \t\r\n", bool at_beginning=true, bool at_end=true)
Trims white space from the beginning and end of a string.
ROSE_UTIL_API unsigned long generate_checksum(std::string s)
Compute a checkshum.
ROSE_UTIL_API std::string listToString(const std::list< int > &, bool separateStrings=false)
Generate a string from a list of integers.
ROSE_UTIL_API std::string fixLineTermination(const std::string &input)
Normalizes line termination.
std::string unsignedToHex(T value)
Convert a number to a hexadecimal and decimal string.
std::string join(const std::string &separator, const Container &container)
Join individual items to form a single string.
bool bit(Unsigned src, size_t i)
Extract a single bit.
Range of values delimited by endpoints.
std::string signedToHex(T value)
Convert a number to a hexadecimal and decimal string.
Shortens names of int64_t stringifiers.
ROSE_UTIL_API std::string removeAnsiEscapes(const std::string &)
Remove ANSI escape characters.
ROSE_UTIL_API std::string escapeNewLineCharaters(const std::string &)
Escapes line feeds and double quotes.
ROSE_UTIL_API std::string indentMultilineString(const std::string &inputString, int statementColumnNumber)
Formatting support for generated code strings.
ROSE_UTIL_API std::string toHex2(uint64_t value, size_t nbits, bool show_unsigned_decimal=true, bool show_signed_decimal=true, uint64_t decimal_threshold=256)
Convert a number to a hexadecimal and decimal string.
ROSE_UTIL_API std::string joinEnglish(const std::vector< std::string > &phrases, const std::string &separator=",", const std::string &finalIntro="and")
Join strings as if they were English prose.
bool isContainedIn(const std::string &longString, const std::string &shortString)
Determines whether one string contains another.