ROSE  0.9.9.139
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 
45 
46 // [Robb Matzke 2016-05-06]: I am deprecating escapeNewLineCharaters because:
47 // 1. Its name is spelled wrong: "Charater"
48 // 2. "newline" in this context is a single word and should be capitalized as "Newline" not "NewLine"
49 // 3. Its name is misleading because it also escapes double quotes.
50 // 4. It escapes newlines using "l" rather than the more customary "n".
51 // I would just remove it, but it seems to be used in some projects and the tutorial.
52 
57 ROSE_UTIL_API std::string escapeNewLineCharaters(const std::string&)
58  SAWYER_DEPRECATED("tell us if you use this"); // ROSE_DEPRECATED is not defined here; lack of sage3basic.h
59 
60 // DQ (12/8/2016): This is ued in the generation of dot files.
61 ROSE_UTIL_API std::string escapeNewlineAndDoubleQuoteCharacters(const std::string&);
62 
63 
64 
65 
67 // Splitting and joining strings
69 
84 ROSE_UTIL_API std::vector<std::string> split(const std::string &separator, const std::string &str, size_t maxparts=(size_t)(-1),
85  bool trim_white_space=false);
86 ROSE_UTIL_API std::vector<std::string> split(char separator, const std::string &str, size_t maxparts=(size_t)(-1),
87  bool trim_white_space=false);
94 ROSE_UTIL_API std::list<std::string> tokenize(const std::string&, char delim);
95 
103 template<class Iterator>
104 std::string join_range(const std::string &separator, Iterator begin, Iterator end) {
105  std::ostringstream retval;
106  for (Iterator i=begin; i!=end; ++i)
107  retval <<(i==begin ? std::string() : separator) <<*i;
108  return retval.str();
109 }
110 
111 template<class Container>
112 std::string join(const std::string &separator, const Container &container) {
113  return join_range(separator, container.begin(), container.end());
114 }
115 
116 ROSE_UTIL_API std::string join(const std::string &separator, char *strings[], size_t nstrings);
117 ROSE_UTIL_API std::string join(const std::string &separator, const char *strings[], size_t nstrings);
123 // Container versus scalar functions
125 //
126 // Functions that convert containers of things to a string and vice versa.
128 
134 ROSE_UTIL_API std::string listToString(const std::list<int>&, bool separateStrings = false);
135 
142 ROSE_UTIL_API std::string listToString(const std::list<std::string>&, bool separateStrings = false);
143 ROSE_UTIL_API std::string listToString(const std::vector<std::string>&, bool separateStrings = false);
154 ROSE_UTIL_API std::list<std::string> stringToList(const std::string&);
155 
160 ROSE_UTIL_API void splitStringIntoStrings(const std::string& inputString, char separator, std::vector<std::string>& stringList);
161 
162 
163 
164 
166 // Functions for converting numbers to strings
168 
174 ROSE_UTIL_API std::string numberToString(long long);
175 ROSE_UTIL_API std::string numberToString(unsigned long long);
176 ROSE_UTIL_API std::string numberToString(long);
177 ROSE_UTIL_API std::string numberToString(unsigned long);
178 ROSE_UTIL_API std::string numberToString(int);
179 ROSE_UTIL_API std::string numberToString(unsigned int);
180 
181 #if !defined(_MSC_VER) && \
182  defined(BACKEND_CXX_IS_GNU_COMPILER) && \
183  ((BACKEND_CXX_COMPILER_MAJOR_VERSION_NUMBER == 4 && BACKEND_CXX_COMPILER_MINOR_VERSION_NUMBER > 6) || \
184  BACKEND_CXX_COMPILER_MAJOR_VERSION_NUMBER > 4) && \
185  __WORDSIZE == 64
186 ROSE_UTIL_API std::string numberToString(__int128 x);
187 ROSE_UTIL_API std::string numberToString(unsigned __int128 x);
188 #endif
189 
193 ROSE_UTIL_API std::string numberToString(const void*);
194 
198 ROSE_UTIL_API std::string numberToString(double);
199 
201 ROSE_UTIL_API std::string intToHex(uint64_t);
202 
212 ROSE_UTIL_API std::string toHex2(uint64_t value, size_t nbits,
213  bool show_unsigned_decimal=true, bool show_signed_decimal=true,
214  uint64_t decimal_threshold=256);
215 ROSE_UTIL_API std::string signedToHex2(uint64_t value, size_t nbits);
216 ROSE_UTIL_API std::string unsignedToHex2(uint64_t value, size_t nbits);
217 
218 template<typename T> std::string toHex(T value) { return toHex2((uint64_t)value, 8*sizeof(T)); }
219 template<typename T> std::string signedToHex(T value) { return signedToHex2((uint64_t)value, 8*sizeof(T)); }
220 template<typename T> std::string unsignedToHex(T value) { return unsignedToHex2((uint64_t)value, 8*sizeof(T)); }
228 ROSE_UTIL_API std::string addrToString(uint64_t value, size_t nbits = 0);
229 
230 
231 
232 
234 // Number parsing
236 
241 ROSE_UTIL_API unsigned hexadecimalToInt(char);
242 
272 template<class Container, class Stringifier>
273 std::vector<std::string> toStrings(const Container &numbers, const Stringifier &stringifier=numberToString) {
274  return toStrings_range(numbers.begin(), numbers.end(), stringifier);
275 }
276 template<class Iterator, class Stringifier>
277 std::vector<std::string> toStrings_range(Iterator begin, Iterator end, const Stringifier &stringifier=numberToString) {
278  std::vector<std::string> retval;
279  for (/*void*/; begin!=end; ++begin)
280  retval.push_back(stringifier(*begin));
281  return retval;
282 }
288 // String conversion functions
291 
295 ROSE_UTIL_API std::string convertToLowerCase(const std::string &inputString);
296 
303 ROSE_UTIL_API std::string fixLineTermination(const std::string &input);
304 
312 ROSE_UTIL_API std::string prefixLines(const std::string &lines, const std::string &prefix,
313  bool prefixAtFront=true, bool prefixAtBack=false);
314 
324 ROSE_UTIL_API std::string makeOneLine(const std::string &s, std::string replacement=" ");
325 
329 ROSE_UTIL_API std::string trim(const std::string &str, const std::string &strip=" \t\r\n",
330  bool at_beginning=true, bool at_end=true);
331 
333 ROSE_UTIL_API std::string untab(const std::string &str, size_t tabstops=8, size_t firstcol=0);
334 
344 ROSE_UTIL_API std::string removeRedundantSubstrings(const std::string&);
345 
346 // [Robb Matzke 2016-01-06]: deprecated due to being misspelled
347 ROSE_UTIL_API std::string removeRedundentSubstrings(std::string);
348 
350 ROSE_UTIL_API std::string removePseudoRedundantSubstrings(const std::string&);
351 
352 // [Robb Matzke 2016-01-06]: deprecated due to being misspelled
353 ROSE_UTIL_API std::string removePseudoRedundentSubstrings(std::string);
354 
355 
356 
357 
359 // Functions for string encoding/decoding/hashing
361 
368 ROSE_UTIL_API std::string encode_base64(const std::vector<uint8_t> &data, bool do_pad=true);
369 ROSE_UTIL_API std::string encode_base64(const uint8_t *data, size_t nbytes, bool do_padd=true);
373 ROSE_UTIL_API std::vector<uint8_t> decode_base64(const std::string &encoded);
374 
382 ROSE_UTIL_API unsigned long generate_checksum(std::string s);
383 
384 
385 
386 
388 // Predicates
390 
394 inline bool isContainedIn(const std::string &longString, const std::string &shortString) {
395  return longString.find(shortString) != std::string::npos;
396 }
397 
401 ROSE_UTIL_API bool isLineTerminated(const std::string &s);
402 
403 
404 
405 
407 // Functions related to diagnostic messages
409 
411 ROSE_UTIL_API std::string indentMultilineString(const std::string& inputString, int statementColumnNumber);
412 
414 ROSE_UTIL_API void add_to_reason_string(std::string &result, bool isset, bool do_pad,
415  const std::string &abbr, const std::string &full);
416 
417 
432 ROSE_UTIL_API std::string appendAsmComment(const std::string &s, const std::string &comment);
433 
456 template<typename T>
457 std::string plural(T n, const std::string &plural_word, const std::string &singular_word="") {
458  assert(!plural_word.empty());
459  std::string retval = numberToString(n) + " ";
460  if (1==n) {
461  if (!singular_word.empty()) {
462  retval += singular_word;
463  } else if (plural_word.size()>3 && 0==plural_word.substr(plural_word.size()-3).compare("ies")) {
464  // string ends with "ies", as in "parties", so emit "party" instead
465  retval += plural_word.substr(0, plural_word.size()-3) + "y";
466  } else if (plural_word.size()>1 && plural_word[plural_word.size()-1]=='s') {
467  // just drop the final 's'
468  retval += plural_word.substr(0, plural_word.size()-1);
469  } else {
470  // I give up. Use the plural and risk being grammatically incorrect.
471  retval += plural_word;
472  }
473  } else {
474  retval += plural_word;
475  }
476  return retval;
477 }
478 
479 // demangledName is defined in rose_support.cpp
485 std::string demangledName(std::string);
486 
487 
488 } // namespace
489 } // namespace
490 
491 
492 #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 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.
bool isContainedIn(const std::string &longString, const std::string &shortString)
Determines whether one string contains another.