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 <sstream>
9 #include <iomanip>
10 #include <algorithm>
11 #include <cstdint>
12 #if defined(__APPLE__)
13 #include <CoreFoundation/CoreFoundation.h>
14 #endif
15 
16 namespace StringOp
17 {
18  int stringToInt(const std::string& str);
19  bool stringToInt(const std::string& str, int& result);
20  unsigned stringToUint(const std::string& str);
21  bool stringToUint(const std::string& str, unsigned& result);
22  uint64_t stringToUint64(const std::string& str);
23  bool stringToBool(string_view str);
24  double stringToDouble(const std::string& str);
25  bool stringToDouble(const std::string& str, double& result);
26 
27  std::string toLower(string_view str);
28 
29  bool startsWith(string_view total, string_view part);
30  bool startsWith(string_view total, char part);
31  bool endsWith (string_view total, string_view part);
32  bool endsWith (string_view total, char part);
33 
34  void trimRight(std::string& str, const char* chars);
35  void trimRight(std::string& str, char chars);
36  void trimRight(string_view& str, string_view chars);
37  void trimRight(string_view& str, char chars);
38  void trimLeft (std::string& str, const char* chars);
39  void trimLeft (std::string& str, char chars);
40  void trimLeft (string_view& str, string_view chars);
41  void trimLeft (string_view& str, char chars);
42  void trim (string_view& str, string_view chars);
43  void trim (string_view& str, char chars);
44 
45  void splitOnFirst(string_view str, string_view chars,
46  string_view& first, string_view& last);
47  void splitOnFirst(string_view str, char chars,
48  string_view& first, string_view& last);
49  void splitOnLast (string_view str, string_view chars,
50  string_view& first, string_view& last);
51  void splitOnLast (string_view str, char chars,
52  string_view& first, string_view& last);
53  std::vector<string_view> split(string_view str, char chars);
54  std::vector<unsigned> parseRange(string_view str,
55  unsigned min, unsigned max);
56 
57  // case insensitive less-than operator
58  struct caseless {
59  bool operator()(string_view s1, string_view s2) const {
60  auto m = std::min(s1.size(), s2.size());
61  int r = strncasecmp(s1.data(), s2.data(), m);
62  return (r != 0) ? (r < 0) : (s1.size() < s2.size());
63  }
64  };
65  struct casecmp {
66  bool operator()(string_view s1, string_view s2) const {
67  if (s1.size() != s2.size()) return false;
68  return strncasecmp(s1.data(), s2.data(), s1.size()) == 0;
69  }
70  };
71 
72 #if defined(__APPLE__)
73  std::string fromCFString(CFStringRef str);
74 #endif
75 }
76 
77 #endif
const char * data() const
Definition: string_view.hh:57
vector< unsigned > parseRange(string_view str, unsigned min, unsigned max)
Definition: StringOp.cc:255
void splitOnLast(string_view str, string_view chars, string_view &first, string_view &last)
Definition: StringOp.cc:174
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:118
bool startsWith(string_view total, string_view part)
Definition: StringOp.cc:69
vector< string_view > split(string_view str, char chars)
Definition: StringOp.cc:197
vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:287
bool operator()(string_view s1, string_view s2) const
Definition: StringOp.hh:66
void trimRight(string &str, const char *chars)
Definition: StringOp.cc:87
bool operator()(string_view s1, string_view s2) const
Definition: StringOp.hh:59
string toLower(string_view str)
Definition: StringOp.cc:62
bool stringToBool(string_view str)
Definition: StringOp.cc:41
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
int stringToInt(const string &str)
Definition: StringOp.cc:14
double stringToDouble(const string &str)
Definition: StringOp.cc:51
void splitOnFirst(string_view str, string_view chars, string_view &first, string_view &last)
Definition: StringOp.cc:151
size_type size() const
Definition: string_view.hh:44
void trim(string_view &str, string_view chars)
Definition: StringOp.cc:139
unsigned stringToUint(const string &str)
Definition: StringOp.cc:25
uint64_t stringToUint64(const string &str)
Definition: StringOp.cc:36
bool endsWith(string_view total, string_view part)
Definition: StringOp.cc:78