aboutsummaryrefslogtreecommitdiff
path: root/dep/g3dlite/include/G3D/stringutils.h
blob: 01ac674f3368066e0057e70e8c4a7002af316d78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
 @file stringutils.h
 
 @maintainer Morgan McGuire, http://graphics.cs.williams.edu
 
 @author  2000-09-09
 @edited  2010-03-05
 */

#ifndef G3D_stringutils_h
#define G3D_stringutils_h

#include "G3D/platform.h"
#include "G3D/Array.h"
#include <cstring>

namespace G3D {

extern const char* NEWLINE;

/** Separates a comma-separated line, properly escaping commas within
    double quotes (") and super quotes ("""). This matches Microsoft Excel's 
    CSV output.

    \param stripQuotes If true, strips leading and trailing " and """

    \sa G3D::stringSplit, G3D::TextInput, G3D::readWholeFile
*/
void parseCommaSeparated(const std::string s, Array<std::string>& array, bool stripQuotes = true);

/** Finds the index of the first '\\' or '/' character, starting at index \a start. 
  \sa G3D::findLastSlash, G3D::isSlash
*/
inline size_t findSlash(const std::string& f, size_t start = 0) {
    size_t i = f.find('/', start);
    size_t j = f.find('\\', start);
    if ((i != std::string::npos) && (i < j)) {
        return i;
    } else {
        return j;
    }
}

/** \brief Returns the larger string index, ignoring std::string::npos. */
inline size_t maxNotNPOS(size_t i, size_t j) {
    if (i == std::string::npos) {
        return j;
    } else if (j == std::string::npos) {
        return i;
    } else {
        return max(i, j);
    }
}

/** Finds the index of the first '\\' or '/' character, starting at index \a start (if \a start is -1, starts at the end of the string).
  \sa G3D::findSlash, G3D::isSlash
  */
inline size_t findLastSlash(const std::string& f, size_t start = std::string::npos) {
    if (start == std::string::npos) {
        start = f.length() - 1;
    }

    size_t i = f.rfind('/', start);
    size_t j = f.rfind('\\', start);
    return maxNotNPOS(i, j);
}


/** Returns a string which is \a s, with all instances of \a pattern replaced */
std::string replace(const std::string& s, const std::string& pattern, const std::string& replacement);

/** Returns true if \a s is a valid C++ identifier */
bool isValidIdentifier(const std::string& s);

/**
 \brief Returns true if the test string begins with the pattern string.
 */
bool beginsWith
   (const std::string&          test,
    const std::string&          pattern);

/**
 \brief Returns true if the test string ends with the pattern string.
 */
bool endsWith
   (const std::string&          test,
    const std::string&          pattern);

/**
 \brief Produces a new string that is the input string
 wrapped at a certain number of columns (where
 the line is broken at the latest space before the
 column limit.)  Platform specific NEWLINEs
 are inserted to wrap.

 \sa G3D::GFont::wordWrapCut, G3D::TextOutput::Settings::WordWrapMode
 */
std::string wordWrap
   (const std::string&          input,
    int                         numCols);

/**
 A comparison function for passing to Array::sort.
 */
int stringCompare(
    const std::string&          s1,
    const std::string&          s2);

int stringPtrCompare(
    const std::string*          s1,
    const std::string*          s2);

/**
 Returns a new string that is an uppercase version of x.
 */
std::string toUpper(
    const std::string&          x);

std::string toLower(
    const std::string&          x);

/**
 Splits x at each occurance of splitChar.
 */
G3D::Array<std::string> stringSplit(
    const std::string&          x,
    char                        splitChar);

/**
 joinChar is not inserted at the beginning or end, just in between
 elements.
 */
std::string stringJoin(
    const G3D::Array<std::string>&   a,
    char                        joinChar);

std::string stringJoin(
    const G3D::Array<std::string>&   a,
    const std::string&               joinStr);

/**
 Strips whitespace from both ends of the string.
 */
std::string trimWhitespace(
    const std::string&              s);

/** These standard C functions are renamed for clarity/naming
   conventions and to return bool, not int.
   */
inline bool isWhiteSpace(const unsigned char c) {
    return isspace(c) != 0;
}

/** These standard C functions are renamed for clarity/naming
   conventions and to return bool, not int.
   */
inline bool isNewline(const unsigned char c) {
    return (c == '\n') || (c == '\r');
}

/** These standard C functions are renamed for clarity/naming
   conventions and to return bool, not int.
   */
inline bool isDigit(const unsigned char c) {
    return isdigit(c) != 0;
}

/** These standard C functions are renamed for clarity/naming
   conventions and to return bool, not int.
   */
inline bool isLetter(const unsigned char c) {
    return isalpha(c) != 0;
}

inline bool isSlash(const unsigned char c) {
    return (c == '\\') || (c == '/');
}

inline bool isQuote(const unsigned char c) {
    return (c == '\'') || (c == '\"');
}

/** Number of new lines in the given string */
inline int countNewlines(const std::string& s) {
    int c = 0;
    for (int i = 0; i < (int)s.size(); ++i) {
        if (s[i] == '\n') {
            ++c;
        }
    }
    return c;
}

}; // namespace

#endif