openMSX
StringOp.hh
Go to the documentation of this file.
1 #ifndef STRINGOP_HH
2 #define STRINGOP_HH
3 
4 #include "string_ref.hh"
5 #include "stringsp.hh"
6 #include <string>
7 #include <vector>
8 #include <set>
9 #include <sstream>
10 #include <iomanip>
11 #include <algorithm>
12 #include <cstdint>
13 #if defined(__APPLE__)
14 #include <CoreFoundation/CoreFoundation.h>
15 #endif
16 
17 namespace StringOp
18 {
19  class Builder
20  {
21  public:
22  Builder();
23  ~Builder();
24 
25  // Overloaded for the most common types, to avoid having to use
26  // the templatized version below (expect for being implicitly
27  // inlined, the template is just fine).
28  Builder& operator<<(const std::string& t);
30  Builder& operator<<(const char* t);
31  Builder& operator<<(unsigned char t);
32  Builder& operator<<(unsigned short t);
33  Builder& operator<<(unsigned t);
34  Builder& operator<<(unsigned long t);
35  Builder& operator<<(unsigned long long t);
36  Builder& operator<<(char t);
37  Builder& operator<<(short t);
38  Builder& operator<<(int t);
39  Builder& operator<<(long t);
40  Builder& operator<<(long long t);
41  Builder& operator<<(float t);
42  Builder& operator<<(double t);
43 
44  // Templatized version is commented out. There's no problem in
45  // enabling it, but the code works ATM without, and having it
46  // disabled helps to catch future missing overloads in the
47  // list above.
48  /*template <typename T> Builder& operator<<(const T& t) {
49  buf += toString(t); return *this;
50  }*/
51 
52  //operator std::string() const;
53  operator std::string() const { return buf; }
54  operator string_ref() const { return buf; }
55 
56  private:
57  std::string buf;
58  };
59 
60  // Generic toString implementation, works for all 'streamable' types.
61  template <typename T> std::string toString(const T& t)
62  {
63  std::ostringstream s;
64  s << t;
65  return s.str();
66  }
67  // Overloads for specific types. These are much faster than the generic
68  // version. Having them non-inline also reduces size of executable.
69  std::string toString(long long a);
70  std::string toString(unsigned long long a);
71  std::string toString(long a);
72  std::string toString(unsigned long a);
73  std::string toString(int a);
74  std::string toString(unsigned a);
75  std::string toString(short a);
76  std::string toString(unsigned short a);
77  std::string toString(char a);
78  std::string toString(signed char a);
79  std::string toString(unsigned char a);
80  std::string toString(bool a);
81  // These are simple enough to inline.
82  inline std::string toString(const char* s) { return s; }
83  inline const std::string& toString(const std::string& s) { return s; }
84 
85  // Print the lower 'width' number of digits of 'a' in hex format
86  // (without leading '0x' and with a-f in lower case).
87  std::string toHexString(unsigned a, unsigned width);
88 
89  int stringToInt(const std::string& str);
90  bool stringToInt(const std::string& str, int& result);
91  unsigned stringToUint(const std::string& str);
92  bool stringToUint(const std::string& str, unsigned& result);
93  uint64_t stringToUint64(const std::string& str);
94  bool stringToBool(string_ref str);
95  double stringToDouble(const std::string& str);
96  bool stringToDouble(const std::string& str, double& result);
97 
98  std::string toLower(string_ref str);
99 
100  bool startsWith(string_ref total, string_ref part);
101  bool startsWith(string_ref total, char part);
102  bool endsWith (string_ref total, string_ref part);
103  bool endsWith (string_ref total, char part);
104 
105  void trimRight(std::string& str, const char* chars);
106  void trimRight(std::string& str, char chars);
107  void trimRight(string_ref& str, string_ref chars);
108  void trimRight(string_ref& str, char chars);
109  void trimLeft (std::string& str, const char* chars);
110  void trimLeft (string_ref& str, string_ref chars);
111 
112  void splitOnFirst(string_ref str, string_ref chars,
113  string_ref& first, string_ref& last);
114  void splitOnFirst(string_ref str, char chars,
115  string_ref& first, string_ref& last);
116  void splitOnLast (string_ref str, string_ref chars,
117  string_ref& first, string_ref& last);
118  void splitOnLast (string_ref str, char chars,
119  string_ref& first, string_ref& last);
120  std::vector<std::string> split(string_ref str, string_ref chars);
121  std::string join(const std::vector<std::string>& elems,
122  const std::string& separator);
123  std::set<unsigned> parseRange(string_ref str,
124  unsigned min, unsigned max);
125 
126  // case insensitive less then operator
127  struct caseless {
128  bool operator()(const std::string& s1, const std::string& s2) const {
129  return strcasecmp(s1.c_str(), s2.c_str()) < 0;
130  }
131  bool operator()(string_ref s1, string_ref s2) const {
132  auto m = std::min(s1.size(), s2.size());
133  int r = strncasecmp(s1.data(), s2.data(), m);
134  return (r != 0) ? (r < 0) : (s1.size() < s2.size());
135  }
136  };
137 
138 #if defined(__APPLE__)
139  std::string fromCFString(CFStringRef str);
140 #endif
141 }
142 
143 #endif