src/string.cpp

changeset 134
eca2fc0acaa2
parent 133
dbbdb870c835
equal deleted inserted replaced
133:dbbdb870c835 134:eca2fc0acaa2
69 { 69 {
70 String newstr (_string); 70 String newstr (_string);
71 71
72 for (char& c : newstr) 72 for (char& c : newstr)
73 { 73 {
74 if (c >= 'a' && c <= 'z') 74 if (c >= 'a' and c <= 'z')
75 c -= 'a' - 'A'; 75 c -= 'a' - 'A';
76 } 76 }
77 77
78 return newstr; 78 return newstr;
79 } 79 }
84 { 84 {
85 String newstr (_string); 85 String newstr (_string);
86 86
87 for (char& c : newstr) 87 for (char& c : newstr)
88 { 88 {
89 if (c >= 'A' && c <= 'Z') 89 if (c >= 'A' and c <= 'Z')
90 c += 'a' - 'A'; 90 c += 'a' - 'A';
91 } 91 }
92 92
93 return newstr; 93 return newstr;
94 } 94 }
212 212
213 // ============================================================================= 213 // =============================================================================
214 // 214 //
215 int String::lastIndexOf (const char* c, int a) const 215 int String::lastIndexOf (const char* c, int a) const
216 { 216 {
217 if (a == -1 || a >= length()) 217 if (a == -1 or a >= length())
218 a = length() - 1; 218 a = length() - 1;
219 219
220 for (; a > 0; a--) 220 for (; a > 0; a--)
221 if (_string[a] == c[0] && strncmp (_string.c_str() + a, c, strlen (c)) == 0) 221 if (_string[a] == c[0] and strncmp (_string.c_str() + a, c, strlen (c)) == 0)
222 return a; 222 return a;
223 223
224 return -1; 224 return -1;
225 } 225 }
226 226
242 errno = 0; 242 errno = 0;
243 char* endptr; 243 char* endptr;
244 long i = strtol (_string.c_str(), &endptr, base); 244 long i = strtol (_string.c_str(), &endptr, base);
245 245
246 if (ok) 246 if (ok)
247 *ok = (errno == 0 && *endptr == '\0'); 247 *ok = (errno == 0 and *endptr == '\0');
248 248
249 return i; 249 return i;
250 } 250 }
251 251
252 // ============================================================================= 252 // =============================================================================
256 errno = 0; 256 errno = 0;
257 char* endptr; 257 char* endptr;
258 float i = strtof (_string.c_str(), &endptr); 258 float i = strtof (_string.c_str(), &endptr);
259 259
260 if (ok) 260 if (ok)
261 *ok = (errno == 0 && *endptr == '\0'); 261 *ok = (errno == 0 and *endptr == '\0');
262 262
263 return i; 263 return i;
264 } 264 }
265 265
266 // ============================================================================= 266 // =============================================================================
270 errno = 0; 270 errno = 0;
271 char* endptr; 271 char* endptr;
272 double i = strtod (_string.c_str(), &endptr); 272 double i = strtod (_string.c_str(), &endptr);
273 273
274 if (ok) 274 if (ok)
275 *ok = (errno == 0 && *endptr == '\0'); 275 *ok = (errno == 0 and *endptr == '\0');
276 276
277 return i; 277 return i;
278 } 278 }
279 279
280 // ============================================================================= 280 // =============================================================================
302 bool gotDot = false; 302 bool gotDot = false;
303 303
304 for (const char & c : _string) 304 for (const char & c : _string)
305 { 305 {
306 // Allow leading hyphen for negatives 306 // Allow leading hyphen for negatives
307 if (&c == &_string[0] && c == '-') 307 if (&c == &_string[0] and c == '-')
308 continue; 308 continue;
309 309
310 // Check for decimal point 310 // Check for decimal point
311 if (!gotDot && c == '.') 311 if (!gotDot and c == '.')
312 { 312 {
313 gotDot = true; 313 gotDot = true;
314 continue; 314 continue;
315 } 315 }
316 316
317 if (c >= '0' && c <= '9') 317 if (c >= '0' and c <= '9')
318 continue; // Digit 318 continue; // Digit
319 319
320 // If the above cases didn't catch this character, it was 320 // If the above cases didn't catch this character, it was
321 // illegal and this is therefore not a number. 321 // illegal and this is therefore not a number.
322 return false; 322 return false;
411 // the string any further. 411 // the string any further.
412 if (end == '\0') 412 if (end == '\0')
413 return true; 413 return true;
414 414
415 // Skip to the end character 415 // Skip to the end character
416 while (*sptr != end && *sptr != '\0') 416 while (*sptr != end and *sptr != '\0')
417 sptr++; 417 sptr++;
418 418
419 // String ended while the mask still had stuff 419 // String ended while the mask still had stuff
420 if (*sptr == '\0') 420 if (*sptr == '\0')
421 return false; 421 return false;

mercurial