| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_STRING_UTIL_H_ | 7 #ifndef BASE_STRING_UTIL_H_ |
| 8 #define BASE_STRING_UTIL_H_ | 8 #define BASE_STRING_UTIL_H_ |
| 9 | 9 |
| 10 #include <stdarg.h> // va_list | 10 #include <stdarg.h> // va_list |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 StringToUpperASCII(&output); | 244 StringToUpperASCII(&output); |
| 245 return output; | 245 return output; |
| 246 } | 246 } |
| 247 | 247 |
| 248 // Compare the lower-case form of the given string against the given ASCII | 248 // Compare the lower-case form of the given string against the given ASCII |
| 249 // string. This is useful for doing checking if an input string matches some | 249 // string. This is useful for doing checking if an input string matches some |
| 250 // token, and it is optimized to avoid intermediate string copies. This API is | 250 // token, and it is optimized to avoid intermediate string copies. This API is |
| 251 // borrowed from the equivalent APIs in Mozilla. | 251 // borrowed from the equivalent APIs in Mozilla. |
| 252 bool LowerCaseEqualsASCII(const std::string& a, const char* b); | 252 bool LowerCaseEqualsASCII(const std::string& a, const char* b); |
| 253 bool LowerCaseEqualsASCII(const std::wstring& a, const char* b); | 253 bool LowerCaseEqualsASCII(const std::wstring& a, const char* b); |
| 254 bool LowerCaseEqualsASCII(const string16& a, const char* b); |
| 254 | 255 |
| 255 // Same thing, but with string iterators instead. | 256 // Same thing, but with string iterators instead. |
| 256 bool LowerCaseEqualsASCII(std::string::const_iterator a_begin, | 257 bool LowerCaseEqualsASCII(std::string::const_iterator a_begin, |
| 257 std::string::const_iterator a_end, | 258 std::string::const_iterator a_end, |
| 258 const char* b); | 259 const char* b); |
| 259 bool LowerCaseEqualsASCII(std::wstring::const_iterator a_begin, | 260 bool LowerCaseEqualsASCII(std::wstring::const_iterator a_begin, |
| 260 std::wstring::const_iterator a_end, | 261 std::wstring::const_iterator a_end, |
| 261 const char* b); | 262 const char* b); |
| 263 bool LowerCaseEqualsASCII(string16::const_iterator a_begin, |
| 264 string16::const_iterator a_end, |
| 265 const char* b); |
| 262 bool LowerCaseEqualsASCII(const char* a_begin, | 266 bool LowerCaseEqualsASCII(const char* a_begin, |
| 263 const char* a_end, | 267 const char* a_end, |
| 264 const char* b); | 268 const char* b); |
| 265 bool LowerCaseEqualsASCII(const wchar_t* a_begin, | 269 bool LowerCaseEqualsASCII(const wchar_t* a_begin, |
| 266 const wchar_t* a_end, | 270 const wchar_t* a_end, |
| 267 const char* b); | 271 const char* b); |
| 272 bool LowerCaseEqualsASCII(const char16* a_begin, |
| 273 const char16* a_end, |
| 274 const char* b); |
| 268 | 275 |
| 269 // Performs a case-sensitive string compare. The behavior is undefined if both | 276 // Performs a case-sensitive string compare. The behavior is undefined if both |
| 270 // strings are not ASCII. | 277 // strings are not ASCII. |
| 271 bool EqualsASCII(const string16& a, const base::StringPiece& b); | 278 bool EqualsASCII(const string16& a, const base::StringPiece& b); |
| 272 | 279 |
| 273 // Returns true if str starts with search, or false otherwise. | 280 // Returns true if str starts with search, or false otherwise. |
| 274 bool StartsWithASCII(const std::string& str, | 281 bool StartsWithASCII(const std::string& str, |
| 275 const std::string& search, | 282 const std::string& search, |
| 276 bool case_sensitive); | 283 bool case_sensitive); |
| 277 bool StartsWith(const std::wstring& str, | 284 bool StartsWith(const std::wstring& str, |
| 278 const std::wstring& search, | 285 const std::wstring& search, |
| 279 bool case_sensitive); | 286 bool case_sensitive); |
| 287 bool StartsWith(const string16& str, |
| 288 const string16& search, |
| 289 bool case_sensitive); |
| 280 | 290 |
| 281 // Returns true if str ends with search, or false otherwise. | 291 // Returns true if str ends with search, or false otherwise. |
| 282 bool EndsWith(const std::wstring& str, | 292 bool EndsWith(const std::wstring& str, |
| 283 const std::wstring& search, | 293 const std::wstring& search, |
| 284 bool case_sensitive); | 294 bool case_sensitive); |
| 295 bool EndsWith(const string16& str, |
| 296 const string16& search, |
| 297 bool case_sensitive); |
| 285 | 298 |
| 286 | 299 |
| 287 // Determines the type of ASCII character, independent of locale (the C | 300 // Determines the type of ASCII character, independent of locale (the C |
| 288 // library versions will change based on locale). | 301 // library versions will change based on locale). |
| 289 template <typename Char> | 302 template <typename Char> |
| 290 inline bool IsAsciiWhitespace(Char c) { | 303 inline bool IsAsciiWhitespace(Char c) { |
| 291 return c == ' ' || c == '\r' || c == '\n' || c == '\t'; | 304 return c == ' ' || c == '\r' || c == '\n' || c == '\t'; |
| 292 } | 305 } |
| 293 template <typename Char> | 306 template <typename Char> |
| 294 inline bool IsAsciiAlpha(Char c) { | 307 inline bool IsAsciiAlpha(Char c) { |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 return &((*str)[0]); | 473 return &((*str)[0]); |
| 461 } | 474 } |
| 462 | 475 |
| 463 //----------------------------------------------------------------------------- | 476 //----------------------------------------------------------------------------- |
| 464 | 477 |
| 465 // Function objects to aid in comparing/searching strings. | 478 // Function objects to aid in comparing/searching strings. |
| 466 | 479 |
| 467 template<typename Char> struct CaseInsensitiveCompare { | 480 template<typename Char> struct CaseInsensitiveCompare { |
| 468 public: | 481 public: |
| 469 bool operator()(Char x, Char y) const { | 482 bool operator()(Char x, Char y) const { |
| 483 // TODO(darin): Do we really want to do locale sensitive comparisons here? |
| 484 // See http://crbug.com/24917 |
| 470 return tolower(x) == tolower(y); | 485 return tolower(x) == tolower(y); |
| 471 } | 486 } |
| 472 }; | 487 }; |
| 473 | 488 |
| 474 template<typename Char> struct CaseInsensitiveCompareASCII { | 489 template<typename Char> struct CaseInsensitiveCompareASCII { |
| 475 public: | 490 public: |
| 476 bool operator()(Char x, Char y) const { | 491 bool operator()(Char x, Char y) const { |
| 477 return ToLowerASCII(x) == ToLowerASCII(y); | 492 return ToLowerASCII(x) == ToLowerASCII(y); |
| 478 } | 493 } |
| 479 }; | 494 }; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 #elif defined(WCHAR_T_IS_UTF32) | 596 #elif defined(WCHAR_T_IS_UTF32) |
| 582 typedef uint32 Unsigned; | 597 typedef uint32 Unsigned; |
| 583 #endif | 598 #endif |
| 584 }; | 599 }; |
| 585 template<> | 600 template<> |
| 586 struct ToUnsigned<short> { | 601 struct ToUnsigned<short> { |
| 587 typedef unsigned short Unsigned; | 602 typedef unsigned short Unsigned; |
| 588 }; | 603 }; |
| 589 | 604 |
| 590 #endif // BASE_STRING_UTIL_H_ | 605 #endif // BASE_STRING_UTIL_H_ |
| OLD | NEW |