openMSX
StringOp.hh
Go to the documentation of this file.
1 #ifndef STRINGOP_HH
2 #define STRINGOP_HH
3 
4 #include "string_view.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  int stringToInt(const std::string& str);
20  bool stringToInt(const std::string& str, int& result);
21  unsigned stringToUint(const std::string& str);
22  bool stringToUint(const std::string& str, unsigned& result);
23  uint64_t stringToUint64(const std::string& str);
24  bool stringToBool(string_view str);
25  double stringToDouble(const std::string& str);
26  bool stringToDouble(const std::string& str, double& result);
27 
28  std::string toLower(string_view str);
29 
30  bool startsWith(string_view total, string_view part);
31  bool startsWith(string_view total, char part);
32  bool endsWith (string_view total, string_view part);
33  bool endsWith (string_view total, char part);
34 
35  void trimRight(std::string& str, const char* chars);
36  void trimRight(std::string& str, char chars);
37  void trimRight(string_view& str, string_view chars);
38  void trimRight(string_view& str, char chars);
39  void trimLeft (std::string& str, const char* chars);
40  void trimLeft (std::string& str, char chars);
41  void trimLeft (string_view& str, string_view chars);
42  void trimLeft (string_view& str, char chars);
43  void trim (string_view& str, string_view chars);
44  void trim (string_view& str, char chars);
45 
46  void splitOnFirst(string_view str, string_view chars,
47  string_view& first, string_view& last);
48  void splitOnFirst(string_view str, char chars,
49  string_view& first, string_view& last);
50  void splitOnLast (string_view str, string_view chars,
51  string_view& first, string_view& last);
52  void splitOnLast (string_view str, char chars,
53  string_view& first, string_view& last);
54  std::vector<string_view> split(string_view str, char chars);
55  std::string join(const std::vector<string_view>& elems,
56  char separator);
57  std::set<unsigned> parseRange(string_view str,
58  unsigned min, unsigned max);
59 
60  // case insensitive less-than operator
61  struct caseless {
62  bool operator()(string_view s1, string_view s2) const {
63  auto m = std::min(s1.size(), s2.size());
64  int r = strncasecmp(s1.data(), s2.data(), m);
65  return (r != 0) ? (r < 0) : (s1.size() < s2.size());
66  }
67  };
68  struct casecmp {
69  bool operator()(string_view s1, string_view s2) const {
70  if (s1.size() != s2.size()) return false;
71  return strncasecmp(s1.data(), s2.data(), s1.size()) == 0;
72  }
73  };
74 
75 #if defined(__APPLE__)
76  std::string fromCFString(CFStringRef str);
77 #endif
78 }
79 
80 #endif
const char * data() const
Definition: string_view.hh:65
void splitOnLast(string_view str, string_view chars, string_view &first, string_view &last)
Definition: StringOp.cc:176
vecN< N, T > min(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:269
void trimLeft(string &str, const char *chars)
Definition: StringOp.cc:120
bool startsWith(string_view total, string_view part)
Definition: StringOp.cc:71
vector< string_view > split(string_view str, char chars)
Definition: StringOp.cc:199
vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:278
bool operator()(string_view s1, string_view s2) const
Definition: StringOp.hh:69
void trimRight(string &str, const char *chars)
Definition: StringOp.cc:89
string join(const vector< string_view > &elems, char separator)
Definition: StringOp.cc:211
bool operator()(string_view s1, string_view s2) const
Definition: StringOp.hh:62
string toLower(string_view str)
Definition: StringOp.cc:64
bool stringToBool(string_view str)
Definition: StringOp.cc:43
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:15
int stringToInt(const string &str)
Definition: StringOp.cc:16
double stringToDouble(const string &str)
Definition: StringOp.cc:53
void splitOnFirst(string_view str, string_view chars, string_view &first, string_view &last)
Definition: StringOp.cc:153
size_type size() const
Definition: string_view.hh:52
void trim(string_view &str, string_view chars)
Definition: StringOp.cc:141
set< unsigned > parseRange(string_view str, unsigned min, unsigned max)
Definition: StringOp.cc:266
unsigned stringToUint(const string &str)
Definition: StringOp.cc:27
uint64_t stringToUint64(const string &str)
Definition: StringOp.cc:38
bool endsWith(string_view total, string_view part)
Definition: StringOp.cc:80