TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
stringutils.h
Go to the documentation of this file.
1 
10 #ifndef G3D_stringutils_h
11 #define G3D_stringutils_h
12 
13 #include "G3D/platform.h"
14 #include "G3D/Array.h"
15 #include <cstring>
16 
17 namespace G3D {
18 
19 extern const char* NEWLINE;
20 
29 void parseCommaSeparated(const std::string s, Array<std::string>& array, bool stripQuotes = true);
30 
34 inline size_t findSlash(const std::string& f, size_t start = 0) {
35  size_t i = f.find('/', start);
36  size_t j = f.find('\\', start);
37  if ((i != std::string::npos) && (i < j)) {
38  return i;
39  } else {
40  return j;
41  }
42 }
43 
45 inline size_t maxNotNPOS(size_t i, size_t j) {
46  if (i == std::string::npos) {
47  return j;
48  } else if (j == std::string::npos) {
49  return i;
50  } else {
51  return max(i, j);
52  }
53 }
54 
58 inline size_t findLastSlash(const std::string& f, size_t start = std::string::npos) {
59  if (start == std::string::npos) {
60  start = f.length() - 1;
61  }
62 
63  size_t i = f.rfind('/', start);
64  size_t j = f.rfind('\\', start);
65  return maxNotNPOS(i, j);
66 }
67 
68 
70 std::string replace(const std::string& s, const std::string& pattern, const std::string& replacement);
71 
73 bool isValidIdentifier(const std::string& s);
74 
78 bool beginsWith
79  (const std::string& test,
80  const std::string& pattern);
81 
85 bool endsWith
86  (const std::string& test,
87  const std::string& pattern);
88 
98 std::string wordWrap
99  (const std::string& input,
100  int numCols);
101 
105 int stringCompare(
106  const std::string& s1,
107  const std::string& s2);
108 
109 int stringPtrCompare(
110  const std::string* s1,
111  const std::string* s2);
112 
116 std::string toUpper(
117  const std::string& x);
118 
119 std::string toLower(
120  const std::string& x);
121 
126  const std::string& x,
127  char splitChar);
128 
133 std::string stringJoin(
134  const G3D::Array<std::string>& a,
135  char joinChar);
136 
137 std::string stringJoin(
138  const G3D::Array<std::string>& a,
139  const std::string& joinStr);
140 
144 std::string trimWhitespace(
145  const std::string& s);
146 
150 inline bool isWhiteSpace(const unsigned char c) {
151  return isspace(c) != 0;
152 }
153 
157 inline bool isNewline(const unsigned char c) {
158  return (c == '\n') || (c == '\r');
159 }
160 
164 inline bool isDigit(const unsigned char c) {
165  return isdigit(c) != 0;
166 }
167 
171 inline bool isLetter(const unsigned char c) {
172  return isalpha(c) != 0;
173 }
174 
175 inline bool isSlash(const unsigned char c) {
176  return (c == '\\') || (c == '/');
177 }
178 
179 inline bool isQuote(const unsigned char c) {
180  return (c == '\'') || (c == '\"');
181 }
182 
184 inline int countNewlines(const std::string& s) {
185  int c = 0;
186  for (int i = 0; i < (int)s.size(); ++i) {
187  if (s[i] == '\n') {
188  ++c;
189  }
190  }
191  return c;
192 }
193 
194 }; // namespace
195 
196 #endif
197 
bool beginsWith(const std::string &test, const std::string &pattern)
Returns true if the test string begins with the pattern string.
Definition: stringutils.cpp:81
int stringCompare(const std::string &s1, const std::string &s2)
Definition: stringutils.cpp:200
size_t findLastSlash(const std::string &f, size_t start=std::string::npos)
Definition: stringutils.h:58
std::string trimWhitespace(const std::string &s)
Definition: stringutils.cpp:288
std::string wordWrap(const std::string &input, int numCols)
Produces a new string that is the input string wrapped at a certain number of columns (where the line...
Definition: stringutils.cpp:146
Definition: AABox.h:25
std::string stringJoin(const G3D::Array< std::string > &a, char joinChar)
Definition: stringutils.cpp:252
std::string replace(const std::string &s, const std::string &pattern, const std::string &replacement)
Definition: stringutils.cpp:97
bool isNewline(const unsigned char c)
Definition: stringutils.h:157
T max(const T &x, const T &y)
Definition: g3dmath.h:320
const char * NEWLINE
Definition: stringutils.cpp:33
void parseCommaSeparated(const std::string s, Array< std::string > &array, bool stripQuotes=true)
Definition: stringutils.cpp:37
bool isDigit(const unsigned char c)
Definition: stringutils.h:164
bool isSlash(const unsigned char c)
Definition: stringutils.h:175
size_t maxNotNPOS(size_t i, size_t j)
Returns the larger string index, ignoring std::string::npos.
Definition: stringutils.h:45
G3D::Array< std::string > stringSplit(const std::string &x, char splitChar)
Definition: stringutils.cpp:230
#define input
Definition: wire_format_lite.h:242
std::string toLower(const std::string &x)
Definition: stringutils.cpp:223
std::string toUpper(const std::string &x)
Definition: stringutils.cpp:216
bool isLetter(const unsigned char c)
Definition: stringutils.h:171
bool endsWith(const std::string &test, const std::string &pattern)
Returns true if the test string ends with the pattern string.
Definition: stringutils.cpp:127
int countNewlines(const std::string &s)
Definition: stringutils.h:184
int stringPtrCompare(const std::string *s1, const std::string *s2)
Definition: stringutils.cpp:208
size_t findSlash(const std::string &f, size_t start=0)
Definition: stringutils.h:34
bool isWhiteSpace(const unsigned char c)
Definition: stringutils.h:150
bool isQuote(const unsigned char c)
Definition: stringutils.h:179
G3D::int16 x
Definition: Vector2int16.h:37
bool isValidIdentifier(const std::string &s)
Definition: stringutils.cpp:115