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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 // Use %ld, %lo, and %lu instead. | 86 // Use %ld, %lo, and %lu instead. |
87 // | 87 // |
88 // Note that there is no portable conversion specifier for char data when | 88 // Note that there is no portable conversion specifier for char data when |
89 // working with wprintf. | 89 // working with wprintf. |
90 // | 90 // |
91 // This function is intended to be called from base::vswprintf. | 91 // This function is intended to be called from base::vswprintf. |
92 BASE_EXPORT bool IsWprintfFormatPortable(const wchar_t* format); | 92 BASE_EXPORT bool IsWprintfFormatPortable(const wchar_t* format); |
93 | 93 |
94 // ASCII-specific tolower. The standard library's tolower is locale sensitive, | 94 // ASCII-specific tolower. The standard library's tolower is locale sensitive, |
95 // so we don't want to use it here. | 95 // so we don't want to use it here. |
96 template <class Char> inline Char ToLowerASCII(Char c) { | 96 inline char ToLowerASCII(char c) { |
| 97 return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; |
| 98 } |
| 99 inline char16 ToLowerASCII(char16 c) { |
97 return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; | 100 return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; |
98 } | 101 } |
99 | 102 |
100 // ASCII-specific toupper. The standard library's toupper is locale sensitive, | 103 // ASCII-specific toupper. The standard library's toupper is locale sensitive, |
101 // so we don't want to use it here. | 104 // so we don't want to use it here. |
102 template <class Char> inline Char ToUpperASCII(Char c) { | 105 inline char ToUpperASCII(char c) { |
103 return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; | 106 return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; |
104 } | 107 } |
| 108 inline char16 ToUpperASCII(char16 c) { |
| 109 return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; |
| 110 } |
| 111 |
| 112 // Converts the given string to it's ASCII-lowercase equivalent. |
| 113 BASE_EXPORT std::string ToLowerASCII(StringPiece str); |
| 114 BASE_EXPORT string16 ToLowerASCII(StringPiece16 str); |
| 115 |
| 116 // Converts the given string to it's ASCII-uppercase equivalent. |
| 117 BASE_EXPORT std::string ToUpperASCII(StringPiece str); |
| 118 BASE_EXPORT string16 ToUpperASCII(StringPiece16 str); |
105 | 119 |
106 // Functor for case-insensitive ASCII comparisons for STL algorithms like | 120 // Functor for case-insensitive ASCII comparisons for STL algorithms like |
107 // std::search. | 121 // std::search. |
108 // | 122 // |
109 // Note that a full Unicode version of this functor is not possible to write | 123 // Note that a full Unicode version of this functor is not possible to write |
110 // because case mappings might change the number of characters, depend on | 124 // because case mappings might change the number of characters, depend on |
111 // context (combining accents), and require handling UTF-16. If you need | 125 // context (combining accents), and require handling UTF-16. If you need |
112 // proper Unicode support, use base::i18n::ToLower/FoldCase and then just | 126 // proper Unicode support, use base::i18n::ToLower/FoldCase and then just |
113 // use a normal operator== on the result. | 127 // use a normal operator== on the result. |
114 template<typename Char> struct CaseInsensitiveCompareASCII { | 128 template<typename Char> struct CaseInsensitiveCompareASCII { |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 BASE_EXPORT bool IsStringASCII(const StringPiece16& str); | 298 BASE_EXPORT bool IsStringASCII(const StringPiece16& str); |
285 // A convenience adaptor for WebStrings, as they don't convert into | 299 // A convenience adaptor for WebStrings, as they don't convert into |
286 // StringPieces directly. | 300 // StringPieces directly. |
287 BASE_EXPORT bool IsStringASCII(const string16& str); | 301 BASE_EXPORT bool IsStringASCII(const string16& str); |
288 #if defined(WCHAR_T_IS_UTF32) | 302 #if defined(WCHAR_T_IS_UTF32) |
289 BASE_EXPORT bool IsStringASCII(const std::wstring& str); | 303 BASE_EXPORT bool IsStringASCII(const std::wstring& str); |
290 #endif | 304 #endif |
291 | 305 |
292 // Converts the elements of the given string. This version uses a pointer to | 306 // Converts the elements of the given string. This version uses a pointer to |
293 // clearly differentiate it from the non-pointer variant. | 307 // clearly differentiate it from the non-pointer variant. |
| 308 // TODO(brettw) remove this. Callers should use base::ToLowerASCII above. |
294 template <class str> inline void StringToLowerASCII(str* s) { | 309 template <class str> inline void StringToLowerASCII(str* s) { |
295 for (typename str::iterator i = s->begin(); i != s->end(); ++i) | 310 for (typename str::iterator i = s->begin(); i != s->end(); ++i) |
296 *i = ToLowerASCII(*i); | 311 *i = ToLowerASCII(*i); |
297 } | 312 } |
298 | 313 |
| 314 // TODO(brettw) remove this. Callers should use base::ToLowerASCII above. |
299 template <class str> inline str StringToLowerASCII(const str& s) { | 315 template <class str> inline str StringToLowerASCII(const str& s) { |
300 // for std::string and std::wstring | 316 // for std::string and std::wstring |
301 str output(s); | 317 str output(s); |
302 StringToLowerASCII(&output); | 318 StringToLowerASCII(&output); |
303 return output; | 319 return output; |
304 } | 320 } |
305 | 321 |
306 // Converts the elements of the given string. This version uses a pointer to | |
307 // clearly differentiate it from the non-pointer variant. | |
308 template <class str> inline void StringToUpperASCII(str* s) { | |
309 for (typename str::iterator i = s->begin(); i != s->end(); ++i) | |
310 *i = ToUpperASCII(*i); | |
311 } | |
312 | |
313 template <class str> inline str StringToUpperASCII(const str& s) { | |
314 // for std::string and std::wstring | |
315 str output(s); | |
316 StringToUpperASCII(&output); | |
317 return output; | |
318 } | |
319 | |
320 // Compare the lower-case form of the given string against the given | 322 // Compare the lower-case form of the given string against the given |
321 // previously-lower-cased ASCII string (typically a constant). | 323 // previously-lower-cased ASCII string (typically a constant). |
322 BASE_EXPORT bool LowerCaseEqualsASCII(StringPiece str, | 324 BASE_EXPORT bool LowerCaseEqualsASCII(StringPiece str, |
323 StringPiece lowecase_ascii); | 325 StringPiece lowecase_ascii); |
324 BASE_EXPORT bool LowerCaseEqualsASCII(StringPiece16 str, | 326 BASE_EXPORT bool LowerCaseEqualsASCII(StringPiece16 str, |
325 StringPiece lowecase_ascii); | 327 StringPiece lowecase_ascii); |
326 | 328 |
327 // Performs a case-sensitive string compare of the given 16-bit string against | 329 // Performs a case-sensitive string compare of the given 16-bit string against |
328 // the given 8-bit ASCII string (typically a constant). The behavior is | 330 // the given 8-bit ASCII string (typically a constant). The behavior is |
329 // undefined if the |ascii| string is not ASCII. | 331 // undefined if the |ascii| string is not ASCII. |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 | 482 |
481 #if defined(OS_WIN) | 483 #if defined(OS_WIN) |
482 #include "base/strings/string_util_win.h" | 484 #include "base/strings/string_util_win.h" |
483 #elif defined(OS_POSIX) | 485 #elif defined(OS_POSIX) |
484 #include "base/strings/string_util_posix.h" | 486 #include "base/strings/string_util_posix.h" |
485 #else | 487 #else |
486 #error Define string operations appropriately for your platform | 488 #error Define string operations appropriately for your platform |
487 #endif | 489 #endif |
488 | 490 |
489 #endif // BASE_STRINGS_STRING_UTIL_H_ | 491 #endif // BASE_STRINGS_STRING_UTIL_H_ |
OLD | NEW |