openMSX
StringOp.hh
Go to the documentation of this file.
1 #ifndef STRINGOP_HH
2 #define STRINGOP_HH
3 
4 #include "stringsp.hh"
5 #include <algorithm>
6 #include <cstdint>
7 #include <iomanip>
8 #include <sstream>
9 #include <string>
10 #include <string_view>
11 #include <utility>
12 #include <vector>
13 #if defined(__APPLE__)
14 #include <CoreFoundation/CoreFoundation.h>
15 #endif
16 
17 namespace StringOp
18 {
19  [[nodiscard]] int stringToInt(const std::string& str);
20  [[nodiscard]] bool stringToInt(const std::string& str, int& result);
21  [[nodiscard]] unsigned stringToUint(const std::string& str);
22  [[nodiscard]] bool stringToUint(const std::string& str, unsigned& result);
23  [[nodiscard]] uint64_t stringToUint64(const std::string& str);
24  [[nodiscard]] bool stringToBool(std::string_view str);
25  [[nodiscard]] double stringToDouble(const std::string& str);
26  [[nodiscard]] bool stringToDouble(const std::string& str, double& result);
27 
28  [[nodiscard]] std::string toLower(std::string_view str);
29 
30  [[nodiscard]] bool startsWith(std::string_view total, std::string_view part);
31  [[nodiscard]] bool startsWith(std::string_view total, char part);
32  [[nodiscard]] bool endsWith (std::string_view total, std::string_view part);
33  [[nodiscard]] bool endsWith (std::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(std::string_view& str, std::string_view chars);
38  void trimRight(std::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 (std::string_view& str, std::string_view chars);
42  void trimLeft (std::string_view& str, char chars);
43  void trim (std::string_view& str, std::string_view chars);
44  void trim (std::string_view& str, char chars);
45 
46  std::pair<std::string_view, std::string_view> splitOnFirst(
47  std::string_view str, std::string_view chars);
48  std::pair<std::string_view, std::string_view> splitOnFirst(
49  std::string_view str, char chars);
50  std::pair<std::string_view, std::string_view> splitOnLast(
51  std::string_view str, std::string_view chars);
52  std::pair<std::string_view, std::string_view> splitOnLast(
53  std::string_view str, char chars);
54  [[nodiscard]] std::vector<std::string_view> split(std::string_view str, char chars);
55  [[nodiscard]] std::vector<unsigned> parseRange(std::string_view str,
56  unsigned min, unsigned max);
57 
58  [[nodiscard]] unsigned fast_stou(std::string_view s);
59 
60  // case insensitive less-than operator
61  struct caseless {
62  [[nodiscard]] bool operator()(std::string_view s1, std::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  [[nodiscard]] bool operator()(std::string_view s1, std::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  [[nodiscard]] std::string fromCFString(CFStringRef str);
77 #endif
78 }
79 
80 #endif
bool operator()(std::string_view s1, std::string_view s2) const
Definition: StringOp.hh:62
unsigned fast_stou(string_view s)
Definition: StringOp.cc:265
vector< string_view > split(string_view str, char chars)
Definition: StringOp.cc:193
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:122
vector< unsigned > parseRange(string_view str, unsigned min, unsigned max)
Definition: StringOp.cc:250
bool startsWith(string_view total, string_view part)
Definition: StringOp.cc:71
vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:287
void trimRight(string &str, const char *chars)
Definition: StringOp.cc:91
std::pair< string_view, string_view > splitOnFirst(string_view str, string_view chars)
Definition: StringOp.cc:155
std::pair< string_view, string_view > splitOnLast(string_view str, string_view chars)
Definition: StringOp.cc:174
bool stringToBool(string_view str)
Definition: StringOp.cc:43
bool operator()(std::string_view s1, std::string_view s2) const
Definition: StringOp.hh:69
int stringToInt(const string &str)
Definition: StringOp.cc:16
double stringToDouble(const string &str)
Definition: StringOp.cc:53
string toLower(string_view str)
Definition: StringOp.cc:64
void trim(string_view &str, string_view chars)
Definition: StringOp.cc:143
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:81