MinitScript  0.9.31 PRE-BETA
StringTools.cpp
Go to the documentation of this file.
2 
3 #include <algorithm>
4 #include <functional>
5 #include <regex>
6 #include <string>
7 #include <string_view>
8 
13 
14 using std::find_if;
15 using std::isspace;
16 using std::regex;
17 using std::regex_match;
18 using std::regex_replace;
19 using std::replace;
20 using std::smatch;
21 using std::string;
22 using std::string_view;
23 using std::tolower;
24 using std::toupper;
25 using std::transform;
26 
28 
32 
33 const string StringTools::replace(const string& str, const char what, const char by, int64_t beginIndex) {
34  string result = str;
35  std::replace(result.begin() + beginIndex, result.end(), what, by);
36  return result;
37 }
38 
39 const string StringTools::replace(const string& str, const string& what, const string& by, int64_t beginIndex) {
40  string result = str;
41  if (what.empty()) return result;
42  while ((beginIndex = result.find(what, beginIndex)) != std::string::npos) {
43  result.replace(beginIndex, what.length(), by);
44  beginIndex += by.length();
45  }
46  return result;
47 }
48 
49 bool StringTools::equalsIgnoreCase(const string& string1, const string& string2) {
50  string stringA = string1;
51  string stringB = string2;
52  transform(stringA.begin(), stringA.end(), stringA.begin(), (int(*)(int))toupper);
53  transform(stringB.begin(), stringB.end(), stringB.begin(), (int(*)(int))toupper);
54  return stringA == stringB;
55 }
56 
57 const string StringTools::trim(const string& str) {
58  string result = str;
59  result.erase(
60  result.begin(),
61  find_if(
62  result.begin(),
63  result.end(),
64  [](int c) {
65  return isspace(c) == 0;
66  }
67  )
68  );
69  result.erase(
70  find_if(
71  result.rbegin(),
72  result.rend(),
73  [](int c) {
74  return isspace(c) == 0;
75  }
76  ).base(),
77  result.end()
78  );
79  return result;
80 }
81 
82 const string_view StringTools::viewTrim(const string_view& str) {
83  int64_t start = 0;
84  for (int64_t i = 0; i < str.size(); i++) {
85  if (isspace(str[i]) != 0) start++; else break;
86  }
87  int64_t end = 0;
88  for (int64_t i = str.size() - 1; i >= 0; i--) {
89  if (isspace(str[i]) != 0) end++; else break;
90  }
91  return string_view(&str[start], str.size() - (start + end));
92 }
93 
94 const string StringTools::toLowerCase(const string& str) {
95  string result = str;
96  transform(result.begin(), result.end(), result.begin(), (int(*)(int))tolower);
97  return result;
98 }
99 
100 const string StringTools::toUpperCase(const string& str) {
101  string result = str;
102  transform(result.begin(), result.end(), result.begin(), (int(*)(int))toupper);
103  return result;
104 }
105 
106 bool StringTools::regexMatch(const string& str, const string& pattern, smatch* matches) {
107  if (matches == nullptr) {
108  return regex_match(str, regex(pattern, std::regex::ECMAScript));
109  } else {
110  return regex_match(str, *matches, regex(pattern, std::regex::ECMAScript));
111  }
112 }
113 
114 bool StringTools::regexSearch(const string& str, const string& pattern, smatch* matches) {
115  if (matches == nullptr) {
116  return regex_search(str, regex(pattern, std::regex::ECMAScript));
117  } else {
118  return regex_search(str, *matches, regex(pattern, std::regex::ECMAScript));
119  }
120 }
121 
122 const string StringTools::regexReplace(const string& str, const string& pattern, const string& by) {
123  return regex_replace(str, regex(pattern, std::regex::ECMAScript), by);
124 }
125 
126 const vector<string> StringTools::tokenize(const string& str, const string& delimiters, bool emptyTokens) {
127  StringTokenizer t;
128  t.tokenize(str, delimiters, emptyTokens);
129  return t.getTokens();
130 }
131 
132 int64_t StringTools::getUTF8Length(const string& str) {
133  UTF8CharacterIterator u8It(str);
134  u8It.seekCharacterPosition(4611686018427387903); // 2 ^ 62 - 1
135  return u8It.getCharacterPosition();
136 }
137 
138 const string StringTools::getUTF8CharAt(const string& str, int64_t index) {
139  // utf8 character iterator
140  UTF8CharacterIterator u8It(str);
141  u8It.seekCharacterPosition(index);
142  //
143  return u8It.hasNext() == true?::Character::toString(u8It.next()):string();
144 }
145 
146 int64_t StringTools::getUTF8BinaryIndex(const string& str, int64_t charIdx) {
147  UTF8CharacterIterator u8It(str);
148  u8It.seekCharacterPosition(charIdx);
149  return u8It.getBinaryPosition();
150 }
const vector< string > & getTokens()
void tokenize(const string &str, const string &delimiters, bool emptyTokens=false)
Tokenize.
static const string getUTF8CharAt(const string &str, int64_t index)
Get UTF8 character at given index.
static const string_view viewTrim(const string_view &str)
Trim string.
Definition: StringTools.cpp:82
static int64_t getUTF8BinaryIndex(const string &str, int64_t charIdx)
Get UTF8 binary buffer index.
static const string toLowerCase(const string &str)
Transform string to lower case.
Definition: StringTools.cpp:94
static bool equalsIgnoreCase(const string &string1, const string &string2)
Checks if strings equal ignoring case.
Definition: StringTools.cpp:49
static const string toUpperCase(const string &str)
Transform string to upper case.
static int64_t getUTF8Length(const string &str)
Get UTF8 string length.
static const string replace(const string &str, const char what, const char by, int64_t beginIndex=0)
Replace char with another char.
Definition: StringTools.cpp:33
static const string regexReplace(const string &str, const string &pattern, const string &by)
Replace regex pattern with given string.
static const string trim(const string &str)
Trim string.
Definition: StringTools.cpp:57
static bool regexSearch(const string &str, const string &pattern, smatch *matches=nullptr)
Do regex pattern search.
static bool regexMatch(const string &str, const string &pattern, smatch *matches=nullptr)
Check if pattern matches whole string.
static const vector< string > tokenize(const string &str, const string &delimiters, bool emptyTokens=false)
Tokenize.
void seekCharacterPosition(int64_t position) const
Seek character position.