OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This file defines utility functions for working with strings. | 5 // This file defines utility functions for working with strings. |
6 | 6 |
7 #ifndef BASE_STRINGS_STRING_UTIL_H_ | 7 #ifndef BASE_STRINGS_STRING_UTIL_H_ |
8 #define BASE_STRINGS_STRING_UTIL_H_ | 8 #define BASE_STRINGS_STRING_UTIL_H_ |
9 | 9 |
10 #include <ctype.h> | 10 #include <ctype.h> |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 *i = ToLowerASCII(*i); | 265 *i = ToLowerASCII(*i); |
266 } | 266 } |
267 | 267 |
268 template <class str> inline str StringToLowerASCII(const str& s) { | 268 template <class str> inline str StringToLowerASCII(const str& s) { |
269 // for std::string and std::wstring | 269 // for std::string and std::wstring |
270 str output(s); | 270 str output(s); |
271 StringToLowerASCII(&output); | 271 StringToLowerASCII(&output); |
272 return output; | 272 return output; |
273 } | 273 } |
274 | 274 |
275 } // namespace base | |
276 | |
277 #if defined(OS_WIN) | |
278 #include "base/strings/string_util_win.h" | |
279 #elif defined(OS_POSIX) | |
280 #include "base/strings/string_util_posix.h" | |
281 #else | |
282 #error Define string operations appropriately for your platform | |
283 #endif | |
284 | |
285 // Converts the elements of the given string. This version uses a pointer to | 275 // Converts the elements of the given string. This version uses a pointer to |
286 // clearly differentiate it from the non-pointer variant. | 276 // clearly differentiate it from the non-pointer variant. |
287 template <class str> inline void StringToUpperASCII(str* s) { | 277 template <class str> inline void StringToUpperASCII(str* s) { |
288 for (typename str::iterator i = s->begin(); i != s->end(); ++i) | 278 for (typename str::iterator i = s->begin(); i != s->end(); ++i) |
289 *i = base::ToUpperASCII(*i); | 279 *i = ToUpperASCII(*i); |
290 } | 280 } |
291 | 281 |
292 template <class str> inline str StringToUpperASCII(const str& s) { | 282 template <class str> inline str StringToUpperASCII(const str& s) { |
293 // for std::string and std::wstring | 283 // for std::string and std::wstring |
294 str output(s); | 284 str output(s); |
295 StringToUpperASCII(&output); | 285 StringToUpperASCII(&output); |
296 return output; | 286 return output; |
297 } | 287 } |
298 | 288 // |
299 // Compare the lower-case form of the given string against the given ASCII | 289 // Compare the lower-case form of the given string against the given ASCII |
300 // string. This is useful for doing checking if an input string matches some | 290 // string. This is useful for doing checking if an input string matches some |
301 // token, and it is optimized to avoid intermediate string copies. This API is | 291 // token, and it is optimized to avoid intermediate string copies. This API is |
302 // borrowed from the equivalent APIs in Mozilla. | 292 // borrowed from the equivalent APIs in Mozilla. |
303 BASE_EXPORT bool LowerCaseEqualsASCII(const std::string& a, const char* b); | 293 BASE_EXPORT bool LowerCaseEqualsASCII(const std::string& a, const char* b); |
304 BASE_EXPORT bool LowerCaseEqualsASCII(const base::string16& a, const char* b); | 294 BASE_EXPORT bool LowerCaseEqualsASCII(const string16& a, const char* b); |
305 | 295 |
306 // Same thing, but with string iterators instead. | 296 // Same thing, but with string iterators instead. |
307 BASE_EXPORT bool LowerCaseEqualsASCII(std::string::const_iterator a_begin, | 297 BASE_EXPORT bool LowerCaseEqualsASCII(std::string::const_iterator a_begin, |
308 std::string::const_iterator a_end, | 298 std::string::const_iterator a_end, |
309 const char* b); | 299 const char* b); |
310 BASE_EXPORT bool LowerCaseEqualsASCII(base::string16::const_iterator a_begin, | 300 BASE_EXPORT bool LowerCaseEqualsASCII(string16::const_iterator a_begin, |
311 base::string16::const_iterator a_end, | 301 string16::const_iterator a_end, |
312 const char* b); | 302 const char* b); |
313 BASE_EXPORT bool LowerCaseEqualsASCII(const char* a_begin, | 303 BASE_EXPORT bool LowerCaseEqualsASCII(const char* a_begin, |
314 const char* a_end, | 304 const char* a_end, |
315 const char* b); | 305 const char* b); |
316 BASE_EXPORT bool LowerCaseEqualsASCII(const base::char16* a_begin, | 306 BASE_EXPORT bool LowerCaseEqualsASCII(const char* a_begin, |
317 const base::char16* a_end, | 307 const char* a_end, |
| 308 const char* b_begin, |
| 309 const char* b_end); |
| 310 BASE_EXPORT bool LowerCaseEqualsASCII(const char16* a_begin, |
| 311 const char16* a_end, |
318 const char* b); | 312 const char* b); |
319 | 313 |
320 // Performs a case-sensitive string compare. The behavior is undefined if both | 314 // Performs a case-sensitive string compare. The behavior is undefined if both |
321 // strings are not ASCII. | 315 // strings are not ASCII. |
322 BASE_EXPORT bool EqualsASCII(const base::string16& a, const base::StringPiece& b
); | 316 BASE_EXPORT bool EqualsASCII(const string16& a, const StringPiece& b); |
| 317 |
| 318 } // namespace base |
| 319 |
| 320 #if defined(OS_WIN) |
| 321 #include "base/strings/string_util_win.h" |
| 322 #elif defined(OS_POSIX) |
| 323 #include "base/strings/string_util_posix.h" |
| 324 #else |
| 325 #error Define string operations appropriately for your platform |
| 326 #endif |
323 | 327 |
324 // Returns true if str starts with search, or false otherwise. | 328 // Returns true if str starts with search, or false otherwise. |
325 BASE_EXPORT bool StartsWithASCII(const std::string& str, | 329 BASE_EXPORT bool StartsWithASCII(const std::string& str, |
326 const std::string& search, | 330 const std::string& search, |
327 bool case_sensitive); | 331 bool case_sensitive); |
328 BASE_EXPORT bool StartsWith(const base::string16& str, | 332 BASE_EXPORT bool StartsWith(const base::string16& str, |
329 const base::string16& search, | 333 const base::string16& search, |
330 bool case_sensitive); | 334 bool case_sensitive); |
331 | 335 |
332 // Returns true if str ends with search, or false otherwise. | 336 // Returns true if str ends with search, or false otherwise. |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 // Returns true if the string passed in matches the pattern. The pattern | 497 // Returns true if the string passed in matches the pattern. The pattern |
494 // string can contain wildcards like * and ? | 498 // string can contain wildcards like * and ? |
495 // The backslash character (\) is an escape character for * and ? | 499 // The backslash character (\) is an escape character for * and ? |
496 // We limit the patterns to having a max of 16 * or ? characters. | 500 // We limit the patterns to having a max of 16 * or ? characters. |
497 // ? matches 0 or 1 character, while * matches 0 or more characters. | 501 // ? matches 0 or 1 character, while * matches 0 or more characters. |
498 BASE_EXPORT bool MatchPattern(const base::StringPiece& string, | 502 BASE_EXPORT bool MatchPattern(const base::StringPiece& string, |
499 const base::StringPiece& pattern); | 503 const base::StringPiece& pattern); |
500 BASE_EXPORT bool MatchPattern(const base::string16& string, | 504 BASE_EXPORT bool MatchPattern(const base::string16& string, |
501 const base::string16& pattern); | 505 const base::string16& pattern); |
502 | 506 |
503 // Hack to convert any char-like type to its unsigned counterpart. | |
504 // For example, it will convert char, signed char and unsigned char to unsigned | |
505 // char. | |
506 template<typename T> | |
507 struct ToUnsigned { | |
508 typedef T Unsigned; | |
509 }; | |
510 | |
511 template<> | |
512 struct ToUnsigned<char> { | |
513 typedef unsigned char Unsigned; | |
514 }; | |
515 template<> | |
516 struct ToUnsigned<signed char> { | |
517 typedef unsigned char Unsigned; | |
518 }; | |
519 template<> | |
520 struct ToUnsigned<wchar_t> { | |
521 #if defined(WCHAR_T_IS_UTF16) | |
522 typedef unsigned short Unsigned; | |
523 #elif defined(WCHAR_T_IS_UTF32) | |
524 typedef uint32 Unsigned; | |
525 #endif | |
526 }; | |
527 template<> | |
528 struct ToUnsigned<short> { | |
529 typedef unsigned short Unsigned; | |
530 }; | |
531 | |
532 #endif // BASE_STRINGS_STRING_UTIL_H_ | 507 #endif // BASE_STRINGS_STRING_UTIL_H_ |
OLD | NEW |