ROSE  0.9.10.44
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 
13 #if ROSE_MICROSOFT_OS
14 // This is the boost solution for lack of support for stdint.h (e.g. types such as "uint64_t")
15 #include <msvc_stdint.h>
16 #endif
17 
18 namespace Rose {
19 
24 namespace StringUtility {
25 
26 
27 
28 
30 // Character-escaping functions
32 
36 ROSE_UTIL_API std::string htmlEscape(const std::string&);
37 
43 ROSE_UTIL_API std::string cEscape(const std::string&);
44 
48 ROSE_UTIL_API std::string bourneEscape(const std::string&);
49 
50 // [Robb Matzke 2016-05-06]: I am deprecating escapeNewLineCharaters because:
51 // 1. Its name is spelled wrong: "Charater"
52 // 2. "newline" in this context is a single word and should be capitalized as "Newline" not "NewLine"
53 // 3. Its name is misleading because it also escapes double quotes.
54 // 4. It escapes newlines using "l" rather than the more customary "n".
55 // I would just remove it, but it seems to be used in some projects and the tutorial.
56 
61 ROSE_UTIL_API std::string escapeNewLineCharaters(const std::string&)
62  SAWYER_DEPRECATED("tell us if you use this"); // ROSE_DEPRECATED is not defined here; lack of sage3basic.h
63 
64 // DQ (12/8/2016): This is ued in the generation of dot files.
65 ROSE_UTIL_API std::string escapeNewlineAndDoubleQuoteCharacters(const std::string&);
66 
67 
68 
69 
71 // Splitting and joining strings
73 
88 ROSE_UTIL_API std::vector<std::string> split(const std::string &separator, const std::string &str, size_t maxparts=(size_t)(-1),
89  bool trim_white_space=false);
90 ROSE_UTIL_API std::vector<std::string> split(char separator, const std::string &str, size_t maxparts=(size_t)(-1),
91  bool trim_white_space=false);
98 ROSE_UTIL_API std::list<std::string> tokenize(const std::string&, char delim);
99 
107 template<class Iterator>
108 std::string join_range(const std::string &separator, Iterator begin, Iterator end) {
109  std::ostringstream retval;
110  for (Iterator i=begin; i!=end; ++i)
111  retval <<(i==begin ? std::string() : separator) <<*i;
112  return retval.str();
113 }
114 
115 template<class Container>
116 std::string join(const std::string &separator, const Container &container) {
117  return join_range(separator, container.begin(), container.end());
118 }
119 
120 ROSE_UTIL_API std::string join(const std::string &separator, char *strings[], size_t nstrings);
121 ROSE_UTIL_API std::string join(const std::string &separator, const char *strings[], size_t nstrings);
143 ROSE_UTIL_API std::string joinEnglish(const std::vector<std::string> &phrases,
144  const std::string &separator = ",",
145  const std::string &finalIntro = "and");
146 
147 
148 
150 // Container versus scalar functions
151 //
152 // Functions that convert containers of things to a string and vice versa.
154 
160 ROSE_UTIL_API std::string listToString(const std::list<int>&, bool separateStrings = false);
161 
168 ROSE_UTIL_API std::string listToString(const std::list<std::string>&, bool separateStrings = false);
169 ROSE_UTIL_API std::string listToString(const std::vector<std::string>&, bool separateStrings = false);
180 ROSE_UTIL_API std::list<std::string> stringToList(const std::string&);
181 
186 ROSE_UTIL_API void splitStringIntoStrings(const std::string& inputString, char separator, std::vector<std::string>& stringList);
187 
188 
189 
190 
192 // Functions for converting numbers to strings
194 
200 ROSE_UTIL_API std::string numberToString(long long);
201 ROSE_UTIL_API std::string numberToString(unsigned long long);
202 ROSE_UTIL_API std::string numberToString(long);
203 ROSE_UTIL_API std::string numberToString(unsigned long);
204 ROSE_UTIL_API std::string numberToString(int);
205 ROSE_UTIL_API std::string numberToString(unsigned int);
206 
207 #if !defined(_MSC_VER) && \
208  defined(BACKEND_CXX_IS_GNU_COMPILER) && \
209  ((BACKEND_CXX_COMPILER_MAJOR_VERSION_NUMBER == 4 && BACKEND_CXX_COMPILER_MINOR_VERSION_NUMBER > 6) || \
210  BACKEND_CXX_COMPILER_MAJOR_VERSION_NUMBER > 4) && \
211  __WORDSIZE == 64
212 ROSE_UTIL_API std::string numberToString(__int128 x);
213 ROSE_UTIL_API std::string numberToString(unsigned __int128 x);
214 #endif
215 
219 ROSE_UTIL_API std::string numberToString(const void*);
220 
224 ROSE_UTIL_API std::string numberToString(double);
225 
227 ROSE_UTIL_API std::string intToHex(uint64_t);
228 
238 ROSE_UTIL_API std::string toHex2(uint64_t value, size_t nbits,
239  bool show_unsigned_decimal=true, bool show_signed_decimal=true,
240  uint64_t decimal_threshold=256);
241 ROSE_UTIL_API std::string signedToHex2(uint64_t value, size_t nbits);
242 ROSE_UTIL_API std::string unsignedToHex2(uint64_t value, size_t nbits);
243 
244 template<typename T> std::string toHex(T value) { return toHex2((uint64_t)value, 8*sizeof(T)); }
245 template<typename T> std::string signedToHex(T value) { return signedToHex2((uint64_t)value, 8*sizeof(T)); }
246 template<typename T> std::string unsignedToHex(T value) { return unsignedToHex2((uint64_t)value, 8*sizeof(T)); }
254 ROSE_UTIL_API std::string addrToString(uint64_t value, size_t nbits = 0);
255 
256 
257 
258 
260 // Number parsing
262 
267 ROSE_UTIL_API unsigned hexadecimalToInt(char);
268 
298 template<class Container, class Stringifier>
299 std::vector<std::string> toStrings(const Container &numbers, const Stringifier &stringifier=numberToString) {
300  return toStrings_range(numbers.begin(), numbers.end(), stringifier);
301 }
302 template<class Iterator, class Stringifier>
303 std::vector<std::string> toStrings_range(Iterator begin, Iterator end, const Stringifier &stringifier=numberToString) {
304  std::vector<std::string> retval;
305  for (/*void*/; begin!=end; ++begin)
306  retval.push_back(stringifier(*begin));
307  return retval;
308 }
314 // String conversion functions
317 
321 ROSE_UTIL_API std::string convertToLowerCase(const std::string &inputString);
322 
329 ROSE_UTIL_API std::string fixLineTermination(const std::string &input);
330 
338 ROSE_UTIL_API std::string prefixLines(const std::string &lines, const std::string &prefix,
339  bool prefixAtFront=true, bool prefixAtBack=false);
340 
350 ROSE_UTIL_API std::string makeOneLine(const std::string &s, std::string replacement=" ");
351 
355 ROSE_UTIL_API std::string trim(const std::string &str, const std::string &strip=" \t\r\n",
356  bool at_beginning=true, bool at_end=true);
357 
359 ROSE_UTIL_API std::string untab(const std::string &str, size_t tabstops=8, size_t firstcol=0);
360 
370 ROSE_UTIL_API std::string removeRedundantSubstrings(const std::string&);
371 
372 // [Robb Matzke 2016-01-06]: deprecated due to being misspelled
373 ROSE_UTIL_API std::string removeRedundentSubstrings(std::string);
374 
376 ROSE_UTIL_API std::string removePseudoRedundantSubstrings(const std::string&);
377 
378 // [Robb Matzke 2016-01-06]: deprecated due to being misspelled
379 ROSE_UTIL_API std::string removePseudoRedundentSubstrings(std::string);
380 
381 
382 
383 
385 // Functions for string encoding/decoding/hashing
387 
394 ROSE_UTIL_API std::string encode_base64(const std::vector<uint8_t> &data, bool do_pad=true);
395 ROSE_UTIL_API std::string encode_base64(const uint8_t *data, size_t nbytes, bool do_padd=true);
399 ROSE_UTIL_API std::vector<uint8_t> decode_base64(const std::string &encoded);
400 
408 ROSE_UTIL_API unsigned long generate_checksum(std::string s);
409 
410 
411 
412 
414 // Predicates
416 
420 inline bool isContainedIn(const std::string &longString, const std::string &shortString) {
421  return longString.find(shortString) != std::string::npos;
422 }
423 
427 ROSE_UTIL_API bool isLineTerminated(const std::string &s);
428 
429 
430 
431 
433 // Functions related to diagnostic messages
435 
437 ROSE_UTIL_API std::string indentMultilineString(const std::string& inputString, int statementColumnNumber);
438 
440 ROSE_UTIL_API void add_to_reason_string(std::string &result, bool isset, bool do_pad,
441  const std::string &abbr, const std::string &full);
442 
443 
458 ROSE_UTIL_API std::string appendAsmComment(const std::string &s, const std::string &comment);
459 
482 template<typename T>
483 std::string plural(T n, const std::string &plural_word, const std::string &singular_word="") {
484  assert(!plural_word.empty());
485  std::string retval = numberToString(n) + " ";
486  if (1==n) {
487  if (!singular_word.empty()) {
488  retval += singular_word;
489  } else if (plural_word == "vertices") {
490  retval = "vertex";
491  } else if (plural_word.size()>3 && 0==plural_word.substr(plural_word.size()-3).compare("ies")) {
492  // string ends with "ies", as in "parties", so emit "party" instead
493  retval += plural_word.substr(0, plural_word.size()-3) + "y";
494  } else if (plural_word.size()>1 && plural_word[plural_word.size()-1]=='s') {
495  // just drop the final 's'
496  retval += plural_word.substr(0, plural_word.size()-1);
497  } else {
498  // I give up. Use the plural and risk being grammatically incorrect.
499  retval += plural_word;
500  }
501  } else {
502  retval += plural_word;
503  }
504  return retval;
505 }
506 
507 // demangledName is defined in rose_support.cpp
513 std::string demangledName(std::string);
514 
515 
516 } // namespace
517 } // namespace
518 
519 
520 #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.
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 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.
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 escapeNewLineCharaters(const std::string &) SAWYER_DEPRECATED("tell us if you use this")
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.