ROSE  0.9.11.56
StringUtility.h
1 #ifndef ROSE_StringUtility_H
2 #define ROSE_StringUtility_H
3 
4 #include "commandline_processing.h"
5 
6 #include <boost/algorithm/string/erase.hpp>
7 #include <boost/algorithm/string/predicate.hpp>
8 #include <boost/algorithm/string/replace.hpp>
9 #include <limits.h>
10 #include <map>
11 #include <sstream>
12 #include <stdint.h>
13 #include <string>
14 #include <vector>
15 #include <Sawyer/IntervalSet.h>
16 
17 #if ROSE_MICROSOFT_OS
18 // This is the boost solution for lack of support for stdint.h (e.g. types such as "uint64_t")
19 #include <msvc_stdint.h>
20 #endif
21 
22 namespace Rose {
23 
28 namespace StringUtility {
29 
30 
31 
32 
34 // Character-escaping functions
36 
40 ROSE_UTIL_API std::string htmlEscape(const std::string&);
41 
47 ROSE_UTIL_API std::string cEscape(const std::string&);
48 
52 ROSE_UTIL_API std::string bourneEscape(const std::string&);
53 
54 // [Robb Matzke 2016-05-06]: I am deprecating escapeNewLineCharaters because:
55 // 1. Its name is spelled wrong: "Charater"
56 // 2. "newline" in this context is a single word and should be capitalized as "Newline" not "NewLine"
57 // 3. Its name is misleading because it also escapes double quotes.
58 // 4. It escapes newlines using "l" rather than the more customary "n".
59 // I would just remove it, but it seems to be used in some projects and the tutorial.
60 
65 ROSE_UTIL_API std::string escapeNewLineCharaters(const std::string&);
66 
67 // DQ (12/8/2016): This is ued in the generation of dot files.
68 ROSE_UTIL_API std::string escapeNewlineAndDoubleQuoteCharacters(const std::string&);
69 
70 
71 
72 
74 // Splitting and joining strings
76 
91 ROSE_UTIL_API std::vector<std::string> split(const std::string &separator, const std::string &str, size_t maxparts=(size_t)(-1),
92  bool trim_white_space=false);
93 ROSE_UTIL_API std::vector<std::string> split(char separator, const std::string &str, size_t maxparts=(size_t)(-1),
94  bool trim_white_space=false);
101 ROSE_UTIL_API std::list<std::string> tokenize(const std::string&, char delim);
102 
110 template<class Iterator>
111 std::string join_range(const std::string &separator, Iterator begin, Iterator end) {
112  std::ostringstream retval;
113  for (Iterator i=begin; i!=end; ++i)
114  retval <<(i==begin ? std::string() : separator) <<*i;
115  return retval.str();
116 }
117 
118 template<class Container>
119 std::string join(const std::string &separator, const Container &container) {
120  return join_range(separator, container.begin(), container.end());
121 }
122 
123 ROSE_UTIL_API std::string join(const std::string &separator, char *strings[], size_t nstrings);
124 ROSE_UTIL_API std::string join(const std::string &separator, const char *strings[], size_t nstrings);
146 ROSE_UTIL_API std::string joinEnglish(const std::vector<std::string> &phrases,
147  const std::string &separator = ",",
148  const std::string &finalIntro = "and");
149 
150 
151 
153 // Container versus scalar functions
154 //
155 // Functions that convert containers of things to a string and vice versa.
157 
163 ROSE_UTIL_API std::string listToString(const std::list<int>&, bool separateStrings = false);
164 
171 ROSE_UTIL_API std::string listToString(const std::list<std::string>&, bool separateStrings = false);
172 ROSE_UTIL_API std::string listToString(const std::vector<std::string>&, bool separateStrings = false);
183 ROSE_UTIL_API std::list<std::string> stringToList(const std::string&);
184 
189 ROSE_UTIL_API void splitStringIntoStrings(const std::string& inputString, char separator, std::vector<std::string>& stringList);
190 
191 
192 
193 
195 // Functions for converting numbers to strings
197 
203 ROSE_UTIL_API std::string numberToString(long long);
204 ROSE_UTIL_API std::string numberToString(unsigned long long);
205 ROSE_UTIL_API std::string numberToString(long);
206 ROSE_UTIL_API std::string numberToString(unsigned long);
207 ROSE_UTIL_API std::string numberToString(int);
208 ROSE_UTIL_API std::string numberToString(unsigned int);
209 
210 #if !defined(_MSC_VER) && \
211  defined(BACKEND_CXX_IS_GNU_COMPILER) && \
212  ((BACKEND_CXX_COMPILER_MAJOR_VERSION_NUMBER == 4 && BACKEND_CXX_COMPILER_MINOR_VERSION_NUMBER > 6) || \
213  BACKEND_CXX_COMPILER_MAJOR_VERSION_NUMBER > 4) && \
214  __WORDSIZE == 64
215 ROSE_UTIL_API std::string numberToString(__int128 x);
216 ROSE_UTIL_API std::string numberToString(unsigned __int128 x);
217 #endif
218 
222 ROSE_UTIL_API std::string numberToString(const void*);
223 
227 ROSE_UTIL_API std::string numberToString(double);
228 
230 ROSE_UTIL_API std::string intToHex(uint64_t);
231 
241 ROSE_UTIL_API std::string toHex2(uint64_t value, size_t nbits,
242  bool show_unsigned_decimal=true, bool show_signed_decimal=true,
243  uint64_t decimal_threshold=256);
244 ROSE_UTIL_API std::string signedToHex2(uint64_t value, size_t nbits);
245 ROSE_UTIL_API std::string unsignedToHex2(uint64_t value, size_t nbits);
246 
247 template<typename T> std::string toHex(T value) { return toHex2((uint64_t)value, 8*sizeof(T)); }
248 template<typename T> std::string signedToHex(T value) { return signedToHex2((uint64_t)value, 8*sizeof(T)); }
249 template<typename T> std::string unsignedToHex(T value) { return unsignedToHex2((uint64_t)value, 8*sizeof(T)); }
257 ROSE_UTIL_API std::string addrToString(uint64_t value, size_t nbits = 0);
258 
263 ROSE_UTIL_API std::string addrToString(const Sawyer::Container::Interval<uint64_t> &interval, size_t nbits = 0);
264 
270  size_t nbits = 0);
271 
272 
273 
274 
276 // Number parsing
278 
283 ROSE_UTIL_API unsigned hexadecimalToInt(char);
284 
314 template<class Container, class Stringifier>
315 std::vector<std::string> toStrings(const Container &numbers, const Stringifier &stringifier=numberToString) {
316  return toStrings_range(numbers.begin(), numbers.end(), stringifier);
317 }
318 template<class Iterator, class Stringifier>
319 std::vector<std::string> toStrings_range(Iterator begin, Iterator end, const Stringifier &stringifier=numberToString) {
320  std::vector<std::string> retval;
321  for (/*void*/; begin!=end; ++begin)
322  retval.push_back(stringifier(*begin));
323  return retval;
324 }
330 // String conversion functions
333 
337 ROSE_UTIL_API std::string convertToLowerCase(const std::string &inputString);
338 
345 ROSE_UTIL_API std::string fixLineTermination(const std::string &input);
346 
354 ROSE_UTIL_API std::string prefixLines(const std::string &lines, const std::string &prefix,
355  bool prefixAtFront=true, bool prefixAtBack=false);
356 
366 ROSE_UTIL_API std::string makeOneLine(const std::string &s, std::string replacement=" ");
367 
371 ROSE_UTIL_API std::string trim(const std::string &str, const std::string &strip=" \t\r\n",
372  bool at_beginning=true, bool at_end=true);
373 
375 ROSE_UTIL_API std::string untab(const std::string &str, size_t tabstops=8, size_t firstcol=0);
376 
386 ROSE_UTIL_API std::string removeRedundantSubstrings(const std::string&);
387 
388 // [Robb Matzke 2016-01-06]: deprecated due to being misspelled
389 ROSE_UTIL_API std::string removeRedundentSubstrings(std::string);
390 
392 ROSE_UTIL_API std::string removePseudoRedundantSubstrings(const std::string&);
393 
394 // [Robb Matzke 2016-01-06]: deprecated due to being misspelled
395 ROSE_UTIL_API std::string removePseudoRedundentSubstrings(std::string);
396 
397 
398 
399 
401 // Functions for string encoding/decoding/hashing
403 
410 ROSE_UTIL_API std::string encode_base64(const std::vector<uint8_t> &data, bool do_pad=true);
411 ROSE_UTIL_API std::string encode_base64(const uint8_t *data, size_t nbytes, bool do_padd=true);
415 ROSE_UTIL_API std::vector<uint8_t> decode_base64(const std::string &encoded);
416 
424 ROSE_UTIL_API unsigned long generate_checksum(std::string s);
425 
426 
427 
428 
430 // Predicates
432 
436 inline bool isContainedIn(const std::string &longString, const std::string &shortString) {
437  return longString.find(shortString) != std::string::npos;
438 }
439 
443 ROSE_UTIL_API bool isLineTerminated(const std::string &s);
444 
445 
446 
447 
449 // Functions related to diagnostic messages
451 
453 ROSE_UTIL_API std::string indentMultilineString(const std::string& inputString, int statementColumnNumber);
454 
456 ROSE_UTIL_API void add_to_reason_string(std::string &result, bool isset, bool do_pad,
457  const std::string &abbr, const std::string &full);
458 
459 
474 ROSE_UTIL_API std::string appendAsmComment(const std::string &s, const std::string &comment);
475 
498 template<typename T>
499 std::string plural(T n, const std::string &plural_word, const std::string &singular_word="") {
500  assert(!plural_word.empty());
501  std::string retval = numberToString(n) + " ";
502  if (1==n) {
503  if (!singular_word.empty()) {
504  retval += singular_word;
505  } else if (boost::ends_with(plural_word, "vertices")) {
506  retval += boost::replace_tail_copy(plural_word, 8, "vertex");
507  } else if (boost::ends_with(plural_word, "ies") && plural_word.size() > 3) {
508  // string ends with "ies", as in "parties", so emit "party" instead
509  retval += boost::replace_tail_copy(plural_word, 3, "y");
510  } else if (boost::ends_with(plural_word, "s") && plural_word.size() > 1) {
511  // strings ends with "s", as in "runners", so drop the final "s" to get "runner"
512  retval += boost::erase_tail_copy(plural_word, 1);
513  } else {
514  // I give up. Use the plural and risk being grammatically incorrect.
515  retval += plural_word;
516  }
517  } else {
518  retval += plural_word;
519  }
520  return retval;
521 }
522 
523 // demangledName is defined in rose_support.cpp
529 std::string demangledName(std::string);
530 
531 
532 } // namespace
533 } // namespace
534 
535 
536 #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.
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 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 int64_t stringifiers.
Definition: stringify.h:20
ROSE_UTIL_API std::string escapeNewLineCharaters(const std::string &)
Escapes line feeds and double quotes.
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.