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> |
11 #include <stdarg.h> // va_list | 11 #include <stdarg.h> // va_list |
12 | 12 |
13 #include <string> | 13 #include <string> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/base_export.h" | 16 #include "base/base_export.h" |
17 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
18 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
19 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
20 #include "base/strings/string_piece.h" // For implicit conversions. | 20 #include "base/strings/string_piece.h" // For implicit conversions. |
21 | 21 |
22 namespace base { | 22 namespace base { |
23 | 23 |
24 // C standard-library functions like "strncasecmp" and "snprintf" that aren't | 24 // C standard-library functions that aren't cross-platform are provided as |
25 // cross-platform are provided as "base::strncasecmp", and their prototypes | 25 // "base::...", and their prototypes are listed below. These functions are |
26 // are listed below. These functions are then implemented as inline calls | 26 // then implemented as inline calls to the platform-specific equivalents in the |
27 // to the platform-specific equivalents in the platform-specific headers. | 27 // platform-specific headers. |
28 | |
29 // Compares the two strings s1 and s2 without regard to case using | |
30 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if | |
31 // s2 > s1 according to a lexicographic comparison. | |
32 int strcasecmp(const char* s1, const char* s2); | |
33 | |
34 // Compares up to count characters of s1 and s2 without regard to case using | |
35 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if | |
36 // s2 > s1 according to a lexicographic comparison. | |
37 int strncasecmp(const char* s1, const char* s2, size_t count); | |
38 | 28 |
39 // Same as strncmp but for char16 strings. | 29 // Same as strncmp but for char16 strings. |
40 int strncmp16(const char16* s1, const char16* s2, size_t count); | 30 int strncmp16(const char16* s1, const char16* s2, size_t count); |
41 | 31 |
42 // Wrapper for vsnprintf that always null-terminates and always returns the | 32 // Wrapper for vsnprintf that always null-terminates and always returns the |
43 // number of characters that would be in an untruncated formatted | 33 // number of characters that would be in an untruncated formatted |
44 // string, even when truncation occurs. | 34 // string, even when truncation occurs. |
45 int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments) | 35 int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments) |
46 PRINTF_FORMAT(3, 0); | 36 PRINTF_FORMAT(3, 0); |
47 | 37 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 } | 88 } |
99 | 89 |
100 // ASCII-specific toupper. The standard library's toupper is locale sensitive, | 90 // ASCII-specific toupper. The standard library's toupper is locale sensitive, |
101 // so we don't want to use it here. | 91 // so we don't want to use it here. |
102 template <class Char> inline Char ToUpperASCII(Char c) { | 92 template <class Char> inline Char ToUpperASCII(Char c) { |
103 return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; | 93 return (c >= 'a' && c <= 'z') ? (c + ('A' - 'a')) : c; |
104 } | 94 } |
105 | 95 |
106 // Function objects to aid in comparing/searching strings. | 96 // Function objects to aid in comparing/searching strings. |
107 | 97 |
| 98 // DO NOT USE. tolower() will given incorrect results for non-ASCII characters. |
| 99 // Use the ASCII version, base::i18n::ToLower, or base::i18n::FoldCase. |
108 template<typename Char> struct CaseInsensitiveCompare { | 100 template<typename Char> struct CaseInsensitiveCompare { |
109 public: | 101 public: |
110 bool operator()(Char x, Char y) const { | 102 bool operator()(Char x, Char y) const { |
111 // TODO(darin): Do we really want to do locale sensitive comparisons here? | 103 // TODO(darin): Do we really want to do locale sensitive comparisons here? |
| 104 // ANSWER(brettw): No. |
112 // See http://crbug.com/24917 | 105 // See http://crbug.com/24917 |
113 return tolower(x) == tolower(y); | 106 return tolower(x) == tolower(y); |
114 } | 107 } |
115 }; | 108 }; |
116 | 109 |
117 template<typename Char> struct CaseInsensitiveCompareASCII { | 110 template<typename Char> struct CaseInsensitiveCompareASCII { |
118 public: | 111 public: |
119 bool operator()(Char x, Char y) const { | 112 bool operator()(Char x, Char y) const { |
120 return ToLowerASCII(x) == ToLowerASCII(y); | 113 return ToLowerASCII(x) == ToLowerASCII(y); |
121 } | 114 } |
122 }; | 115 }; |
123 | 116 |
| 117 // Like strcasecmp for case-insensitive ASCII characters only. Returns: |
| 118 // -1 (a < b) |
| 119 // 0 (a == b) |
| 120 // 1 (a > b) |
| 121 // (unlike strcasecmp which can return values greater or less than 1/-1). For |
| 122 // full Unicode support, use base::i18n::ToLower or base::i18h::FoldCase |
| 123 // and then just call the normal string operators on the result. |
| 124 BASE_EXPORT int CompareCaseInsensitiveASCII(StringPiece a, StringPiece b); |
| 125 BASE_EXPORT int CompareCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b); |
| 126 |
| 127 // Equality for ASCII case-insensitive comparisons. For full Unicode support, |
| 128 // use base::i18n::ToLower or base::i18h::FoldCase and then compare with either |
| 129 // == or !=. |
| 130 BASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece a, StringPiece b); |
| 131 BASE_EXPORT bool EqualsCaseInsensitiveASCII(StringPiece16 a, StringPiece16 b); |
| 132 |
124 // These threadsafe functions return references to globally unique empty | 133 // These threadsafe functions return references to globally unique empty |
125 // strings. | 134 // strings. |
126 // | 135 // |
127 // It is likely faster to construct a new empty string object (just a few | 136 // It is likely faster to construct a new empty string object (just a few |
128 // instructions to set the length to 0) than to get the empty string singleton | 137 // instructions to set the length to 0) than to get the empty string singleton |
129 // returned by these functions (which requires threadsafe singleton access). | 138 // returned by these functions (which requires threadsafe singleton access). |
130 // | 139 // |
131 // Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT | 140 // Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT |
132 // CONSTRUCTORS. There is only one case where you should use these: functions | 141 // CONSTRUCTORS. There is only one case where you should use these: functions |
133 // which need to return a string by reference (e.g. as a class member | 142 // which need to return a string by reference (e.g. as a class member |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 const std::vector<std::string>& subst, | 529 const std::vector<std::string>& subst, |
521 std::vector<size_t>* offsets); | 530 std::vector<size_t>* offsets); |
522 | 531 |
523 // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL. | 532 // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL. |
524 BASE_EXPORT base::string16 ReplaceStringPlaceholders( | 533 BASE_EXPORT base::string16 ReplaceStringPlaceholders( |
525 const base::string16& format_string, | 534 const base::string16& format_string, |
526 const base::string16& a, | 535 const base::string16& a, |
527 size_t* offset); | 536 size_t* offset); |
528 | 537 |
529 #endif // BASE_STRINGS_STRING_UTIL_H_ | 538 #endif // BASE_STRINGS_STRING_UTIL_H_ |
OLD | NEW |