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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 } | 116 } |
117 }; | 117 }; |
118 | 118 |
119 template<typename Char> struct CaseInsensitiveCompareASCII { | 119 template<typename Char> struct CaseInsensitiveCompareASCII { |
120 public: | 120 public: |
121 bool operator()(Char x, Char y) const { | 121 bool operator()(Char x, Char y) const { |
122 return ToLowerASCII(x) == ToLowerASCII(y); | 122 return ToLowerASCII(x) == ToLowerASCII(y); |
123 } | 123 } |
124 }; | 124 }; |
125 | 125 |
| 126 // These threadsafe functions return references to globally unique empty |
| 127 // strings. |
| 128 // |
| 129 // It is likely faster to construct a new empty string object (just a few |
| 130 // instructions to set the length to 0) than to get the empty string singleton |
| 131 // returned by these functions (which requires threadsafe singleton access). |
| 132 // |
| 133 // Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT |
| 134 // CONSTRUCTORS. There is only one case where you should use these: functions |
| 135 // which need to return a string by reference (e.g. as a class member |
| 136 // accessor), and don't have an empty string to use (e.g. in an error case). |
| 137 // These should not be used as initializers, function arguments, or return |
| 138 // values for functions which return by value or outparam. |
| 139 BASE_EXPORT const std::string& EmptyString(); |
| 140 BASE_EXPORT const string16& EmptyString16(); |
| 141 |
| 142 // Contains the set of characters representing whitespace in the corresponding |
| 143 // encoding. Null-terminated. |
| 144 BASE_EXPORT extern const wchar_t kWhitespaceWide[]; |
| 145 BASE_EXPORT extern const char16 kWhitespaceUTF16[]; |
| 146 BASE_EXPORT extern const char kWhitespaceASCII[]; |
| 147 |
| 148 // Null-terminated string representing the UTF-8 byte order mark. |
| 149 BASE_EXPORT extern const char kUtf8ByteOrderMark[]; |
| 150 |
126 } // namespace base | 151 } // namespace base |
127 | 152 |
128 #if defined(OS_WIN) | 153 #if defined(OS_WIN) |
129 #include "base/strings/string_util_win.h" | 154 #include "base/strings/string_util_win.h" |
130 #elif defined(OS_POSIX) | 155 #elif defined(OS_POSIX) |
131 #include "base/strings/string_util_posix.h" | 156 #include "base/strings/string_util_posix.h" |
132 #else | 157 #else |
133 #error Define string operations appropriately for your platform | 158 #error Define string operations appropriately for your platform |
134 #endif | 159 #endif |
135 | 160 |
136 // These threadsafe functions return references to globally unique empty | |
137 // strings. | |
138 // | |
139 // DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT CONSTRUCTORS. | |
140 // There is only one case where you should use these: functions which need to | |
141 // return a string by reference (e.g. as a class member accessor), and don't | |
142 // have an empty string to use (e.g. in an error case). These should not be | |
143 // used as initializers, function arguments, or return values for functions | |
144 // which return by value or outparam. | |
145 BASE_EXPORT const std::string& EmptyString(); | |
146 BASE_EXPORT const string16& EmptyString16(); | |
147 | |
148 BASE_EXPORT extern const wchar_t kWhitespaceWide[]; | |
149 BASE_EXPORT extern const char16 kWhitespaceUTF16[]; | |
150 BASE_EXPORT extern const char kWhitespaceASCII[]; | |
151 | |
152 BASE_EXPORT extern const char kUtf8ByteOrderMark[]; | |
153 | |
154 // Removes characters in |remove_chars| from anywhere in |input|. Returns true | 161 // Removes characters in |remove_chars| from anywhere in |input|. Returns true |
155 // if any characters were removed. |remove_chars| must be null-terminated. | 162 // if any characters were removed. |remove_chars| must be null-terminated. |
156 // NOTE: Safe to use the same variable for both |input| and |output|. | 163 // NOTE: Safe to use the same variable for both |input| and |output|. |
157 BASE_EXPORT bool RemoveChars(const string16& input, | 164 BASE_EXPORT bool RemoveChars(const string16& input, |
158 const char16 remove_chars[], | 165 const char16 remove_chars[], |
159 string16* output); | 166 string16* output); |
160 BASE_EXPORT bool RemoveChars(const std::string& input, | 167 BASE_EXPORT bool RemoveChars(const std::string& input, |
161 const char remove_chars[], | 168 const char remove_chars[], |
162 std::string* output); | 169 std::string* output); |
163 | 170 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 return c - '0'; | 370 return c - '0'; |
364 if (c >= 'A' && c <= 'F') | 371 if (c >= 'A' && c <= 'F') |
365 return c - 'A' + 10; | 372 return c - 'A' + 10; |
366 if (c >= 'a' && c <= 'f') | 373 if (c >= 'a' && c <= 'f') |
367 return c - 'a' + 10; | 374 return c - 'a' + 10; |
368 return 0; | 375 return 0; |
369 } | 376 } |
370 | 377 |
371 // Returns true if it's a whitespace character. | 378 // Returns true if it's a whitespace character. |
372 inline bool IsWhitespace(wchar_t c) { | 379 inline bool IsWhitespace(wchar_t c) { |
373 return wcschr(kWhitespaceWide, c) != NULL; | 380 return wcschr(base::kWhitespaceWide, c) != NULL; |
374 } | 381 } |
375 | 382 |
376 // Return a byte string in human-readable format with a unit suffix. Not | 383 // Return a byte string in human-readable format with a unit suffix. Not |
377 // appropriate for use in any UI; use of FormatBytes and friends in ui/base is | 384 // appropriate for use in any UI; use of FormatBytes and friends in ui/base is |
378 // highly recommended instead. TODO(avi): Figure out how to get callers to use | 385 // highly recommended instead. TODO(avi): Figure out how to get callers to use |
379 // FormatBytes instead; remove this. | 386 // FormatBytes instead; remove this. |
380 BASE_EXPORT string16 FormatBytesUnlocalized(int64 bytes); | 387 BASE_EXPORT string16 FormatBytesUnlocalized(int64 bytes); |
381 | 388 |
382 // Starting at |start_offset| (usually 0), replace the first instance of | 389 // Starting at |start_offset| (usually 0), replace the first instance of |
383 // |find_this| with |replace_with|. | 390 // |find_this| with |replace_with|. |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 #elif defined(WCHAR_T_IS_UTF32) | 524 #elif defined(WCHAR_T_IS_UTF32) |
518 typedef uint32 Unsigned; | 525 typedef uint32 Unsigned; |
519 #endif | 526 #endif |
520 }; | 527 }; |
521 template<> | 528 template<> |
522 struct ToUnsigned<short> { | 529 struct ToUnsigned<short> { |
523 typedef unsigned short Unsigned; | 530 typedef unsigned short Unsigned; |
524 }; | 531 }; |
525 | 532 |
526 #endif // BASE_STRINGS_STRING_UTIL_H_ | 533 #endif // BASE_STRINGS_STRING_UTIL_H_ |
OLD | NEW |