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::string join(const std::vector<string_view>& elems,
55  char separator);
56  std::vector<unsigned> parseRange(string_view str,
57  unsigned min, unsigned max);
58 
59  // case insensitive less-than operator
60  struct caseless {
61  bool operator()(string_view s1, string_view s2) const {
62  auto m = std::min(s1.size(), s2.size());
63  int r = strncasecmp(s1.data(), s2.data(), m);
64  return (r != 0) ? (r < 0) : (s1.size() < s2.size());
65  }
66  };
67  struct casecmp {
68  bool operator()(string_view s1, string_view s2) const {
69  if (s1.size() != s2.size()) return false;
70  return strncasecmp(s1.data(), s2.data(), s1.size()) == 0;
71  }
72  };
73 
74 #if defined(__APPLE__)
75  std::string fromCFString(CFStringRef str);
76 #endif
77 }
78 
79 #endif
const char * data() const
Definition: string_view.hh:65
vector< unsigned > parseRange(string_view str, unsigned min, unsigned max)
Definition: StringOp.cc:268
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:68
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:61
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
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