15 #include "string_utils.h"
16 #include "textEncoder.h"
24 cmp_nocase(
const string &s,
const string &s2) {
25 string::const_iterator p = s.
begin();
26 string::const_iterator p2 = s2.begin();
28 while (p != s.end() && p2 != s2.end()) {
29 if (toupper(*p) != toupper(*p2)) {
30 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
36 return (s2.size() == s.size()) ? 0 :
37 (s.size() < s2.size()) ? -1 : 1;
42 return (ch ==
'_') ?
'-' : toupper(ch);
47 cmp_nocase_uh(
const string &s,
const string &s2) {
48 string::const_iterator p = s.begin();
49 string::const_iterator p2 = s2.begin();
51 while (p != s.end() && p2 != s2.end()) {
52 if (toupper_uh(*p) != toupper_uh(*p2)) {
53 return (toupper_uh(*p) < toupper_uh(*p2)) ? -1 : 1;
59 return (s2.size() == s.size()) ? 0 :
60 (s.size() < s2.size()) ? -1 : 1;
71 downcase(
const string &s) {
73 result.reserve(s.size());
74 string::const_iterator p;
75 for (p = s.begin(); p != s.end(); ++p) {
76 result += tolower(*p);
87 upcase(
const string &s) {
89 result.reserve(s.size());
90 string::const_iterator p;
91 for (p = s.begin(); p != s.end(); ++p) {
92 result += toupper(*p);
109 extract_words(
const string &str, vector_string &words) {
113 while (pos < str.length() && isspace((
unsigned int)str[pos])) {
116 while (pos < str.length()) {
117 size_t word_start = pos;
118 while (pos < str.length() && !isspace((
unsigned int)str[pos])) {
121 words.push_back(str.substr(word_start, pos - word_start));
124 while (pos < str.length() && isspace((
unsigned int)str[pos])) {
150 while (pos < str.length()) {
151 size_t word_start = pos;
155 words.push_back(str.substr(word_start, pos - word_start));
179 tokenize(
const string &str, vector_string &words,
const string &delimiters,
180 bool discard_repeated_delimiters) {
182 while (p < str.length()) {
183 size_t q = str.find_first_of(delimiters, p);
184 if (q == string::npos) {
185 if (q - p || !discard_repeated_delimiters){
186 words.push_back(str.substr(p));
190 if (q - p || !discard_repeated_delimiters){
191 words.push_back(str.substr(p, q - p));
195 words.push_back(
string());
211 tokenize(
const wstring &str,
pvector<wstring> &words,
const wstring &delimiters,
212 bool discard_repeated_delimiters) {
214 while (p < str.length()) {
215 size_t q = str.find_first_of(delimiters, p);
216 if (q == string::npos) {
217 if (q - p || !discard_repeated_delimiters){
218 words.push_back(str.substr(p));
222 if (q - p || !discard_repeated_delimiters){
223 words.push_back(str.substr(p, q - p));
227 words.push_back(wstring());
236 trim_left(
const string &str) {
238 while (begin < str.size() && isspace((
unsigned int)str[begin])) {
242 return str.substr(begin);
251 trim_left(
const wstring &str) {
257 return str.substr(begin);
266 trim_right(
const string &str) {
268 size_t end = str.size();
269 while (end > begin && isspace((
unsigned int)str[end - 1])) {
273 return str.substr(begin, end - begin);
282 trim_right(
const wstring &str) {
284 size_t end = str.size();
289 return str.substr(begin, end - begin);
299 trim(
const string &str) {
301 while (begin < str.size() && isspace((
unsigned int)str[begin])) {
305 size_t end = str.size();
306 while (end > begin && isspace((
unsigned int)str[end - 1])) {
310 return str.substr(begin, end - begin);
320 trim(
const wstring &str) {
326 size_t end = str.size();
331 return str.substr(begin, end - begin);
347 string_to_int(
const string &str,
string &tail) {
348 const char *nptr = str.c_str();
350 int result = strtol(nptr, &endptr, 10);
362 string_to_int(
const string &str,
int &result) {
364 result = string_to_int(str, tail);
381 string_to_double(
const string &str,
string &tail) {
382 const char *nptr = str.c_str();
384 double result = pstrtod(nptr, &endptr);
397 string_to_double(
const string &str,
double &result) {
399 result = string_to_double(str, tail);
408 string_to_float(
const string &str,
float &result) {
410 result = (float)string_to_double(str, tail);
419 string_to_stdfloat(
const string &str, PN_stdfloat &result) {
421 result = (PN_stdfloat)string_to_double(str, tail);
static bool unicode_isspace(int character)
Returns true if the indicated character is a whitespace letter, false otherwise.
This is our own Panda specialization on the default STL vector.
iterator begin()
Returns an iterator that may be used to traverse the elements of the matrix, STL-style.