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 // Safe standard library wrappers for all platforms. | |
23 | |
24 namespace base { | 22 namespace base { |
25 | 23 |
26 // C standard-library functions like "strncasecmp" and "snprintf" that aren't | 24 // C standard-library functions like "strncasecmp" and "snprintf" that aren't |
27 // cross-platform are provided as "base::strncasecmp", and their prototypes | 25 // cross-platform are provided as "base::strncasecmp", and their prototypes |
28 // are listed below. These functions are then implemented as inline calls | 26 // are listed below. These functions are then implemented as inline calls |
29 // to the platform-specific equivalents in the platform-specific headers. | 27 // to the platform-specific equivalents in the platform-specific headers. |
30 | 28 |
31 // Compares the two strings s1 and s2 without regard to case using | 29 // Compares the two strings s1 and s2 without regard to case using |
32 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if | 30 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if |
33 // s2 > s1 according to a lexicographic comparison. | 31 // s2 > s1 according to a lexicographic comparison. |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 } // namespace base | 237 } // namespace base |
240 | 238 |
241 #if defined(OS_WIN) | 239 #if defined(OS_WIN) |
242 #include "base/strings/string_util_win.h" | 240 #include "base/strings/string_util_win.h" |
243 #elif defined(OS_POSIX) | 241 #elif defined(OS_POSIX) |
244 #include "base/strings/string_util_posix.h" | 242 #include "base/strings/string_util_posix.h" |
245 #else | 243 #else |
246 #error Define string operations appropriately for your platform | 244 #error Define string operations appropriately for your platform |
247 #endif | 245 #endif |
248 | 246 |
249 // Converts to 7-bit ASCII by truncating. The result must be known to be ASCII | |
250 // beforehand. | |
251 BASE_EXPORT std::string WideToASCII(const std::wstring& wide); | |
252 BASE_EXPORT std::string UTF16ToASCII(const base::string16& utf16); | |
253 | |
254 // Returns true if the specified string matches the criteria. How can a wide | 247 // Returns true if the specified string matches the criteria. How can a wide |
255 // string be 8-bit or UTF8? It contains only characters that are < 256 (in the | 248 // string be 8-bit or UTF8? It contains only characters that are < 256 (in the |
256 // first case) or characters that use only 8-bits and whose 8-bit | 249 // first case) or characters that use only 8-bits and whose 8-bit |
257 // representation looks like a UTF-8 string (the second case). | 250 // representation looks like a UTF-8 string (the second case). |
258 // | 251 // |
259 // Note that IsStringUTF8 checks not only if the input is structurally | 252 // Note that IsStringUTF8 checks not only if the input is structurally |
260 // valid but also if it doesn't contain any non-character codepoint | 253 // valid but also if it doesn't contain any non-character codepoint |
261 // (e.g. U+FFFE). It's done on purpose because all the existing callers want | 254 // (e.g. U+FFFE). It's done on purpose because all the existing callers want |
262 // to have the maximum 'discriminating' power from other encodings. If | 255 // to have the maximum 'discriminating' power from other encodings. If |
263 // there's a use case for just checking the structural validity, we have to | 256 // there's a use case for just checking the structural validity, we have to |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 #elif defined(WCHAR_T_IS_UTF32) | 515 #elif defined(WCHAR_T_IS_UTF32) |
523 typedef uint32 Unsigned; | 516 typedef uint32 Unsigned; |
524 #endif | 517 #endif |
525 }; | 518 }; |
526 template<> | 519 template<> |
527 struct ToUnsigned<short> { | 520 struct ToUnsigned<short> { |
528 typedef unsigned short Unsigned; | 521 typedef unsigned short Unsigned; |
529 }; | 522 }; |
530 | 523 |
531 #endif // BASE_STRINGS_STRING_UTIL_H_ | 524 #endif // BASE_STRINGS_STRING_UTIL_H_ |
OLD | NEW |