ROSE  0.9.10.89
StringUtility.h
1 #ifndef ROSE_StringUtility_H
2 #define ROSE_StringUtility_H
3 
4 #include "commandline_processing.h"
5 
6 #include <limits.h>
7 #include <map>
8 #include <sstream>
9 #include <stdint.h>
10 #include <string>
11 #include <vector>
12 #include <Sawyer/IntervalSet.h>
13 
14 #if ROSE_MICROSOFT_OS
15 // This is the boost solution for lack of support for stdint.h (e.g. types such as "uint64_t")
16 #include <msvc_stdint.h>
17 #endif
18 
19 namespace Rose {
20 
25 namespace StringUtility {
26 
27 
28 
29 
31 // Character-escaping functions
33 
37 ROSE_UTIL_API std::string htmlEscape(const std::string&);
38 
44 ROSE_UTIL_API std::string cEscape(const std::string&);
45 
49 ROSE_UTIL_API std::string bourneEscape(const std::string&);
50 
51 // [Robb Matzke 2016-05-06]: I am deprecating escapeNewLineCharaters because:
52 // 1. Its name is spelled wrong: "Charater"
53 // 2. "newline" in this context is a single word and should be capitalized as "Newline" not "NewLine"
54 // 3. Its name is misleading because it also escapes double quotes.
55 // 4. It escapes newlines using "l" rather than the more customary "n".
56 // I would just remove it, but it seems to be used in some projects and the tutorial.
57 
62 ROSE_UTIL_API std::string escapeNewLineCharaters(const std::string&)
63  SAWYER_DEPRECATED("tell us if you use this"); // ROSE_DEPRECATED is not defined here; lack of sage3basic.h
64 
65 // DQ (12/8/2016): This is ued in the generation of dot files.
66 ROSE_UTIL_API std::string escapeNewlineAndDoubleQuoteCharacters(const std::string&);
67 
68 
69 
70 
72 // Splitting and joining strings
74 
89 ROSE_UTIL_API std::vector<std::string> split(const std::string &separator, const std::string &str, size_t maxparts=(size_t)(-1),
90  bool trim_white_space=false);
91 ROSE_UTIL_API std::vector<std::string> split(char separator, const std::string &str, size_t maxparts=(size_t)(-1),
92  bool trim_white_space=false);
99 ROSE_UTIL_API std::list<std::string> tokenize(const std::string&, char delim);
100 
108 template<class Iterator>
109 std::string join_range(const std::string &separator, Iterator begin, Iterator end) {
110  std::ostringstream retval;
111  for (Iterator i=begin; i!=end; ++i)
112  retval <<(i==begin ? std::string() : separator) <<*i;
113  return retval.str();
114 }
115 
116 template<class Container>
117 std::string join(const std::string &separator, const Container &container) {
118  return join_range(separator, container.begin(), container.end());
119 }
120 
121 ROSE_UTIL_API std::string join(const std::string &separator, char *strings[], size_t nstrings);
122 ROSE_UTIL_API std::string join(const std::string &separator, const char *strings[], size_t nstrings);
144 ROSE_UTIL_API std::string joinEnglish(const std::vector<std::string> &phrases,
145  const std::string &separator = ",",
146  const std::string &finalIntro = "and");
147 
148 
149 
151 // Container versus scalar functions
152 //
153 // Functions that convert containers of things to a string and vice versa.
155 
161 ROSE_UTIL_API std::string listToString(const std::list<int>&, bool separateStrings = false);
162 
169 ROSE_UTIL_API std::string listToString(const std::list<std::string>&, bool separateStrings = false);
170 ROSE_UTIL_API std::string listToString(const std::vector<std::string>&, bool separateStrings = false);
181 ROSE_UTIL_API std::list<std::string> stringToList(const std::string&);
182 
187 ROSE_UTIL_API void splitStringIntoStrings(const std::string& inputString, char separator, std::vector<std::string>& stringList);
188 
189 
190 
191 
193 // Functions for converting numbers to strings
195 
201 ROSE_UTIL_API std::string numberToString(long long);
202 ROSE_UTIL_API std::string numberToString(unsigned long long);
203 ROSE_UTIL_API std::string numberToString(long);
204 ROSE_UTIL_API std::string numberToString(unsigned long);
205 ROSE_UTIL_API std::string numberToString(int);
206 ROSE_UTIL_API std::string numberToString(unsigned int);
207 
208 #if !defined(_MSC_VER) && \
209  defined(BACKEND_CXX_IS_GNU_COMPILER) && \
210  ((BACKEND_CXX_COMPILER_MAJOR_VERSION_NUMBER == 4 && BACKEND_CXX_COMPILER_MINOR_VERSION_NUMBER > 6) || \
211  BACKEND_CXX_COMPILER_MAJOR_VERSION_NUMBER > 4) && \
212  __WORDSIZE == 64
213 ROSE_UTIL_API std::string numberToString(__int128 x);
214 ROSE_UTIL_API std::string numberToString(unsigned __int128 x);
215 #endif
216 
220 ROSE_UTIL_API std::string numberToString(const void*);
221 
225 ROSE_UTIL_API std::string numberToString(double);
226 
228 ROSE_UTIL_API std::string intToHex(uint64_t);
229 
239 ROSE_UTIL_API std::string toHex2(uint64_t value, size_t nbits,
240  bool show_unsigned_decimal=true, bool show_signed_decimal=true,
241  uint64_t decimal_threshold=256);
242 ROSE_UTIL_API std::string signedToHex2(uint64_t value, size_t nbits);
243 ROSE_UTIL_API std::string unsignedToHex2(uint64_t value, size_t nbits);
244 
245 template<typename T> std::string toHex(T value) { return toHex2((uint64_t)value, 8*sizeof(T)); }
246 template<typename T> std::string signedToHex(T value) { return signedToHex2((uint64_t)value, 8*sizeof(T)); }
247 template<typename T> std::string unsignedToHex(T value) { return unsignedToHex2((uint64_t)value, 8*sizeof(T)); }
255 ROSE_UTIL_API std::string addrToString(uint64_t value, size_t nbits = 0);
256 
261 ROSE_UTIL_API std::string addrToString(const Sawyer::Container::Interval<uint64_t> &interval, size_t nbits = 0);
262 
268  size_t nbits = 0);
269 
270 
271 
272 
274 // Number parsing
276 
281 ROSE_UTIL_API unsigned hexadecimalToInt(char);
282 
312 template<class Container, class Stringifier>
313 std::vector<std::string> toStrings(const Container &numbers, const Stringifier &stringifier=numberToString) {
314  return toStrings_range(numbers.begin(), numbers.end(), stringifier);
315 }
316 template<class Iterator, class Stringifier>
317 std::vector<std::string> toStrings_range(Iterator begin, Iterator end, const Stringifier &stringifier=numberToString) {
318  std::vector<std::string> retval;
319  for (/*void*/; begin!=end; ++begin)
320  retval.push_back(stringifier(*begin));
321  return retval;
322 }
328 // String conversion functions
331 
335 ROSE_UTIL_API std::string convertToLowerCase(const std::string &inputString);
336 
343 ROSE_UTIL_API std::string fixLineTermination(const std::string &input);
344 
352 ROSE_UTIL_API std::string prefixLines(const std::string &lines, const std::string &prefix,
353  bool prefixAtFront=true, bool prefixAtBack=false);
354 
364 ROSE_UTIL_API std::string makeOneLine(const std::string &s, std::string replacement=" ");
365 
369 ROSE_UTIL_API std::string trim(const std::string &str, const std::string &strip=" \t\r\n",
370  bool at_beginning=true, bool at_end=true);
371 
373 ROSE_UTIL_API std::string untab(const std::string &str, size_t tabstops=8, size_t firstcol=0);
374 
384 ROSE_UTIL_API std::string removeRedundantSubstrings(const std::string&);
385 
386 // [Robb Matzke 2016-01-06]: deprecated due to being misspelled
387 ROSE_UTIL_API std::string removeRedundentSubstrings(std::string);
388 
390 ROSE_UTIL_API std::string removePseudoRedundantSubstrings(const std::string&);
391 
392 // [Robb Matzke 2016-01-06]: deprecated due to being misspelled
393 ROSE_UTIL_API std::string removePseudoRedundentSubstrings(std::string);
394 
395 
396 
397 
399 // Functions for string encoding/decoding/hashing
401 
408 ROSE_UTIL_API std::string encode_base64(const std::vector<uint8_t> &data, bool do_pad=true);
409 ROSE_UTIL_API std::string encode_base64(const uint8_t *data, size_t nbytes, bool do_padd=true);
413 ROSE_UTIL_API std::vector<uint8_t> decode_base64(const std::string &encoded);
414 
422 ROSE_UTIL_API unsigned long generate_checksum(std::string s);
423 
424 
425 
426 
428 // Predicates
430 
434 inline bool isContainedIn(const std::string &longString, const std::string &shortString) {
435  return longString.find(shortString) != std::string::npos;
436 }
437 
441 ROSE_UTIL_API bool isLineTerminated(const std::string &s);
442 
443 
444 
445 
447 // Functions related to diagnostic messages
449 
451 ROSE_UTIL_API std::string indentMultilineString(const std::string& inputString, int statementColumnNumber);
452 
454 ROSE_UTIL_API void add_to_reason_string(std::string &result, bool isset, bool do_pad,
455  const std::string &abbr, const std::string &full);
456 
457 
472 ROSE_UTIL_API std::string appendAsmComment(const std::string &s, const std::string &comment);
473 
496 template<typename T>
497 std::string plural(T n, const std::string &plural_word, const std::string &singular_word="") {
498  assert(!plural_word.empty());
499  std::string retval = numberToString(n) + " ";
500  if (1==n) {
501  if (!singular_word.empty()) {
502  retval += singular_word;
503  } else if (plural_word == "vertices") {
504  retval = "vertex";
505  } else if (plural_word.size()>3 && 0==plural_word.substr(plural_word.size()-3).compare("ies")) {
506  // string ends with "ies", as in "parties", so emit "party" instead
507  retval += plural_word.substr(0, plural_word.size()-3) + "y";
508  } else if (plural_word.size()>1 && plural_word[plural_word.size()-1]=='s') {
509  // just drop the final 's'
510  retval += plural_word.substr(0, plural_word.size()-1);
511  } else {
512  // I give up. Use the plural and risk being grammatically incorrect.
513  retval += plural_word;
514  }
515  } else {
516  retval += plural_word;
517  }
518  return retval;
519 }
520 
521 // demangledName is defined in rose_support.cpp
527 std::string demangledName(std::string);
528 
529 
530 } // namespace
531 } // namespace
532 
533 
534 #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::vector< std::string > split(const std::string &separator, const std::string &str, size_t maxparts=(size_t)(-1), bool trim_white_space=false)
Splits strings into parts.
ROSE_UTIL_API std::string numberToString(long long)
Convert an integer to a string.
ROSE_UTIL_API std::string removePseudoRedundantSubstrings(const std::string &)
Remove redundant lines containing special substrings of form string#.
std::string toHex(T value)
Convert a number to a hexadecimal and decimal string.
ROSE_UTIL_API std::string removeRedundantSubstrings(const std::string &)
Remove redundant and blank lines.
std::vector< std::string > toStrings_range(Iterator begin, Iterator end, const Stringifier &stringifier=numberToString)
Converts a bunch of numbers to strings.
STL namespace.
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 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::string cEscape(const std::string &)
Escapes characters that are special to C/C++.
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 std::string escapeNewLineCharaters(const std::string &) __attribute__((deprecated))
Escapes line feeds and double quotes.
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.
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 long stringifiers.
Definition: stringify.h:19
std::string plural(T n, const std::string &plural_word, const std::string &singular_word="")
Helpful way to print singular or plural words.
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.