ROSE  0.11.2.0
StringUtility.h
1 #ifndef ROSE_StringUtility_H
2 #define ROSE_StringUtility_H
3 
4 #include "commandline_processing.h"
5 
6 #include <BitOps.h>
7 #include <boost/algorithm/string/erase.hpp>
8 #include <boost/algorithm/string/predicate.hpp>
9 #include <boost/algorithm/string/replace.hpp>
10 #include <limits.h>
11 #include <map>
12 #include <rose_constants.h>
13 #include <sstream>
14 #include <stdint.h>
15 #include <string>
16 #include <vector>
17 #include <Sawyer/IntervalSet.h>
18 
19 #if ROSE_MICROSOFT_OS
20 // This is the boost solution for lack of support for stdint.h (e.g. types such as "uint64_t")
21 #include <msvc_stdint.h>
22 #endif
23 
24 namespace Rose {
25 
30 namespace StringUtility {
31 
32 
33 
34 
36 // Character-escaping functions
38 
42 ROSE_UTIL_API std::string htmlEscape(const std::string&);
43 
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&);
69 
73 ROSE_UTIL_API std::string bourneEscape(const std::string&);
74 
75 // [Robb Matzke 2016-05-06]: I am deprecating escapeNewLineCharaters because:
76 // 1. Its name is spelled wrong: "Charater"
77 // 2. "newline" in this context is a single word and should be capitalized as "Newline" not "NewLine"
78 // 3. Its name is misleading because it also escapes double quotes.
79 // 4. It escapes newlines using "l" rather than the more customary "n".
80 // I would just remove it, but it seems to be used in some projects and the tutorial.
81 
86 ROSE_UTIL_API std::string escapeNewLineCharaters(const std::string&);
87 
88 // DQ (12/8/2016): This is ued in the generation of dot files.
89 ROSE_UTIL_API std::string escapeNewlineAndDoubleQuoteCharacters(const std::string&);
90 
91 
92 
93 
95 // Splitting and joining strings
97 
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);
123 
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;
136  return retval.str();
137 }
138 
139 template<class Container>
140 std::string join(const std::string &separator, const Container &container) {
141  return join_range(separator, container.begin(), container.end());
142 }
143 
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());
147 }
148 
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);
153 
175 ROSE_UTIL_API std::string joinEnglish(const std::vector<std::string> &phrases,
176  const std::string &separator = ",",
177  const std::string &finalIntro = "and");
178 
179 
180 
182 // Container versus scalar functions
183 //
184 // Functions that convert containers of things to a string and vice versa.
186 
192 ROSE_UTIL_API std::string listToString(const std::list<int>&, bool separateStrings = false);
193 
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&);
213 
218 ROSE_UTIL_API void splitStringIntoStrings(const std::string& inputString, char separator, std::vector<std::string>& stringList);
219 
220 
221 
222 
224 // Functions for converting numbers to strings
226 
232 ROSE_UTIL_API std::string numberToString(long long);
233 ROSE_UTIL_API std::string numberToString(unsigned long long);
234 ROSE_UTIL_API std::string numberToString(long);
235 ROSE_UTIL_API std::string numberToString(unsigned long);
236 ROSE_UTIL_API std::string numberToString(int);
237 ROSE_UTIL_API std::string numberToString(unsigned int);
238 
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) && \
243  __WORDSIZE == 64
244 ROSE_UTIL_API std::string numberToString(__int128 x);
245 ROSE_UTIL_API std::string numberToString(unsigned __int128 x);
246 #endif
247 
251 ROSE_UTIL_API std::string numberToString(const void*);
252 
256 ROSE_UTIL_API std::string numberToString(double);
257 
259 ROSE_UTIL_API std::string intToHex(uint64_t);
260 
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);
275 
276 template<typename T> std::string toHex(T value) { return toHex2((uint64_t)value, 8*sizeof(T)); }
277 template<typename T> std::string signedToHex(T value) { return signedToHex2((uint64_t)value, 8*sizeof(T)); }
278 template<typename T> std::string unsignedToHex(T value) { return unsignedToHex2((uint64_t)value, 8*sizeof(T)); }
286 ROSE_UTIL_API std::string addrToString(uint64_t value, size_t nbits = 0);
287 
292 ROSE_UTIL_API std::string addrToString(const Sawyer::Container::Interval<uint64_t> &interval, size_t nbits = 0);
293 
299  size_t nbits = 0);
300 
301 
303 template<typename Unsigned>
304 std::string toBinary(Unsigned value, size_t nBits = 0, size_t groupSize = 4, const std::string groupSeparator = "_") {
305  if (0 == nBits)
306  nBits = BitOps::nBits(value);
307  std::string retval;
308  for (size_t i = nBits; i > 0; --i) {
309  retval += BitOps::bit(value, i-1) ? '1' : '0';
310  if (groupSize > 0 && i > 1 && (i-1) % groupSize == 0)
311  retval += groupSeparator;
312  }
313  return retval;
314 }
315 
317 // Number parsing
319 
324 ROSE_UTIL_API unsigned hexadecimalToInt(char);
325 
355 template<class Container, class Stringifier>
356 std::vector<std::string> toStrings(const Container &numbers, const Stringifier &stringifier=numberToString) {
357  return toStrings_range(numbers.begin(), numbers.end(), stringifier);
358 }
359 template<class Iterator, class Stringifier>
360 std::vector<std::string> toStrings_range(Iterator begin, Iterator end, const Stringifier &stringifier=numberToString) {
361  std::vector<std::string> retval;
362  for (/*void*/; begin!=end; ++begin)
363  retval.push_back(stringifier(*begin));
364  return retval;
365 }
371 // String conversion functions
374 
378 ROSE_UTIL_API std::string convertToLowerCase(const std::string &inputString);
379 
386 ROSE_UTIL_API std::string fixLineTermination(const std::string &input);
387 
395 ROSE_UTIL_API std::string prefixLines(const std::string &lines, const std::string &prefix,
396  bool prefixAtFront=true, bool prefixAtBack=false);
397 
407 ROSE_UTIL_API std::string makeOneLine(const std::string &s, std::string replacement=" ");
408 
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);
414 
416 ROSE_UTIL_API std::string untab(const std::string &str, size_t tabstops=8, size_t firstcol=0);
417 
427 ROSE_UTIL_API std::string removeRedundantSubstrings(const std::string&);
428 
429 
430 
432 // Functions for string encoding/decoding/hashing
434 
441 ROSE_UTIL_API std::string encode_base64(const std::vector<uint8_t> &data, bool do_pad=true);
442 ROSE_UTIL_API std::string encode_base64(const uint8_t *data, size_t nbytes, bool do_padd=true);
446 ROSE_UTIL_API std::vector<uint8_t> decode_base64(const std::string &encoded);
447 
455 ROSE_UTIL_API unsigned long generate_checksum(std::string s);
456 
457 
458 
459 
461 // Predicates
463 
467 inline bool isContainedIn(const std::string &longString, const std::string &shortString) {
468  return longString.find(shortString) != std::string::npos;
469 }
470 
474 ROSE_UTIL_API bool isLineTerminated(const std::string &s);
475 
476 
477 
478 
480 // Functions related to diagnostic messages
482 
484 ROSE_UTIL_API std::string indentMultilineString(const std::string& inputString, int statementColumnNumber);
485 
487 ROSE_UTIL_API void add_to_reason_string(std::string &result, bool isset, bool do_pad,
488  const std::string &abbr, const std::string &full);
489 
490 
505 ROSE_UTIL_API std::string appendAsmComment(const std::string &s, const std::string &comment);
506 
529 template<typename T>
530 std::string plural(T n, const std::string &plural_phrase, const std::string &singular_phrase="") {
531  assert(!plural_phrase.empty());
532  std::string retval = numberToString(n) + " ";
533  if (1==n) {
534  if (!singular_phrase.empty()) {
535  retval += singular_phrase;
536  } else if (boost::ends_with(plural_phrase, "vertices")) {
537  retval += boost::replace_tail_copy(plural_phrase, 8, "vertex");
538  } else if (boost::ends_with(plural_phrase, "indices")) {
539  retval += boost::replace_tail_copy(plural_phrase, 7, "index");
540  } else if (boost::ends_with(plural_phrase, "ies") && plural_phrase.size() > 3) {
541  // string ends with "ies", as in "parties", so emit "party" instead
542  retval += boost::replace_tail_copy(plural_phrase, 3, "y");
543  } else if (boost::ends_with(plural_phrase, "sses") || boost::ends_with(plural_phrase, "indexes")) {
544  // Sometimes we need to drop an "es" rather than just the "s"
545  retval += boost::erase_tail_copy(plural_phrase, 2);
546  } else if (boost::ends_with(plural_phrase, "s") && plural_phrase.size() > 1) {
547  // strings ends with "s", as in "runners", so drop the final "s" to get "runner"
548  retval += boost::erase_tail_copy(plural_phrase, 1);
549  } else {
550  // I give up. Use the plural and risk being grammatically incorrect.
551  retval += plural_phrase;
552  }
553  } else {
554  retval += plural_phrase;
555  }
556  return retval;
557 }
558 
559 // demangledName is defined in rose_support.cpp
565 std::string demangledName(std::string);
566 
567 
568 } // namespace
569 } // namespace
570 
571 
572 #endif
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.
Definition: BitOps.h:17
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.
Definition: IntervalSet.h:55
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.
Definition: BitOps.h:172
Range of values delimited by endpoints.
Definition: Interval.h:33
std::string signedToHex(T value)
Convert a number to a hexadecimal and decimal string.
Shortens names of int64_t stringifiers.
Definition: stringify.h:20
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.