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 std::string() const && { return std::move(buf); }
55  operator string_ref() const { return buf ; }
56 
57  private:
58  std::string buf;
59  };
60 
61  // Generic toString implementation, works for all 'streamable' types.
62  template <typename T> std::string toString(const T& t)
63  {
64  std::ostringstream s;
65  s << t;
66  return s.str();
67  }
68  // Overloads for specific types. These are much faster than the generic
69  // version. Having them non-inline also reduces size of executable.
70  std::string toString(long long a);
71  std::string toString(unsigned long long a);
72  std::string toString(long a);
73  std::string toString(unsigned long a);
74  std::string toString(int a);
75  std::string toString(unsigned a);
76  std::string toString(short a);
77  std::string toString(unsigned short a);
78  std::string toString(char a);
79  std::string toString(signed char a);
80  std::string toString(unsigned char a);
81  std::string toString(bool a);
82  // These are simple enough to inline.
83  inline std::string toString(const char* s) { return s; }
84  inline const std::string& toString(const std::string& s) { return s; }
85 
86  // Print the lower 'width' number of digits of 'a' in hex format
87  // (without leading '0x' and with a-f in lower case).
88  std::string toHexString(unsigned a, unsigned width);
89 
90  int stringToInt(const std::string& str);
91  bool stringToInt(const std::string& str, int& result);
92  unsigned stringToUint(const std::string& str);
93  bool stringToUint(const std::string& str, unsigned& result);
94  uint64_t stringToUint64(const std::string& str);
95  bool stringToBool(string_ref str);
96  double stringToDouble(const std::string& str);
97  bool stringToDouble(const std::string& str, double& result);
98 
99  std::string toLower(string_ref str);
100 
101  bool startsWith(string_ref total, string_ref part);
102  bool startsWith(string_ref total, char part);
103  bool endsWith (string_ref total, string_ref part);
104  bool endsWith (string_ref total, char part);
105 
106  void trimRight(std::string& str, const char* chars);
107  void trimRight(std::string& str, char chars);
108  void trimRight(string_ref& str, string_ref chars);
109  void trimRight(string_ref& str, char chars);
110  void trimLeft (std::string& str, const char* chars);
111  void trimLeft (std::string& str, char chars);
112  void trimLeft (string_ref& str, string_ref chars);
113  void trimLeft (string_ref& str, char chars);
114  void trim (string_ref& str, string_ref chars);
115  void trim (string_ref& str, char chars);
116 
117  void splitOnFirst(string_ref str, string_ref chars,
118  string_ref& first, string_ref& last);
119  void splitOnFirst(string_ref str, char chars,
120  string_ref& first, string_ref& last);
121  void splitOnLast (string_ref str, string_ref chars,
122  string_ref& first, string_ref& last);
123  void splitOnLast (string_ref str, char chars,
124  string_ref& first, string_ref& last);
125  std::vector<string_ref> split(string_ref str, char chars);
126  std::string join(const std::vector<string_ref>& elems,
127  char separator);
128  std::set<unsigned> parseRange(string_ref str,
129  unsigned min, unsigned max);
130 
131  // case insensitive less-than operator
132  struct caseless {
133  bool operator()(string_ref s1, string_ref s2) const {
134  auto m = std::min(s1.size(), s2.size());
135  int r = strncasecmp(s1.data(), s2.data(), m);
136  return (r != 0) ? (r < 0) : (s1.size() < s2.size());
137  }
138  };
139  struct casecmp {
140  bool operator()(string_ref s1, string_ref s2) const {
141  if (s1.size() != s2.size()) return false;
142  return strncasecmp(s1.data(), s2.data(), s1.size()) == 0;
143  }
144  };
145 
146 #if defined(__APPLE__)
147  std::string fromCFString(CFStringRef str);
148 #endif
149 }
150 
151 #endif
string toHexString(unsigned x, unsigned width)
Definition: StringOp.cc:167
bool stringToBool(string_ref str)
Definition: StringOp.cc:208
string toLower(string_ref str)
Definition: StringOp.cc:229
vecN< N, T > min(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:268
void splitOnFirst(string_ref str, string_ref chars, string_ref &first, string_ref &last)
Definition: StringOp.cc:318
void splitOnLast(string_ref str, string_ref chars, string_ref &first, string_ref &last)
Definition: StringOp.cc:341
bool operator()(string_ref s1, string_ref s2) const
Definition: StringOp.hh:140
string toString(long long a)
Definition: StringOp.cc:150
Builder & operator<<(const std::string &t)
Definition: StringOp.cc:22
bool operator()(string_ref s1, string_ref s2) const
Definition: StringOp.hh:133
void trimLeft(string &str, const char *chars)
Definition: StringOp.cc:285
string join(const vector< string_ref > &elems, char separator)
Definition: StringOp.cc:376
set< unsigned > parseRange(string_ref str, unsigned min, unsigned max)
Definition: StringOp.cc:433
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:277
void trimRight(string &str, const char *chars)
Definition: StringOp.cc:254
const char * data() const
Definition: string_ref.hh:68
bool startsWith(string_ref total, string_ref part)
Definition: StringOp.cc:236
vector< string_ref > split(string_ref str, char chars)
Definition: StringOp.cc:364
int stringToInt(const string &str)
Definition: StringOp.cc:181
double stringToDouble(const string &str)
Definition: StringOp.cc:218
size_type size() const
Definition: string_ref.hh:55
void trim(string_ref &str, string_ref chars)
Definition: StringOp.cc:306
unsigned stringToUint(const string &str)
Definition: StringOp.cc:192
uint64_t stringToUint64(const string &str)
Definition: StringOp.cc:203
bool endsWith(string_ref total, string_ref part)
Definition: StringOp.cc:245