OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 9 #pragma once |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 namespace base { | 29 namespace base { |
30 | 30 |
31 // C standard-library functions like "strncasecmp" and "snprintf" that aren't | 31 // C standard-library functions like "strncasecmp" and "snprintf" that aren't |
32 // cross-platform are provided as "base::strncasecmp", and their prototypes | 32 // cross-platform are provided as "base::strncasecmp", and their prototypes |
33 // are listed below. These functions are then implemented as inline calls | 33 // are listed below. These functions are then implemented as inline calls |
34 // to the platform-specific equivalents in the platform-specific headers. | 34 // to the platform-specific equivalents in the platform-specific headers. |
35 | 35 |
36 // Compares the two strings s1 and s2 without regard to case using | 36 // Compares the two strings s1 and s2 without regard to case using |
37 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if | 37 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if |
38 // s2 > s1 according to a lexicographic comparison. | 38 // s2 > s1 according to a lexicographic comparison. |
39 BASE_API int strcasecmp(const char* s1, const char* s2); | 39 int strcasecmp(const char* s1, const char* s2); |
40 | 40 |
41 // Compares up to count characters of s1 and s2 without regard to case using | 41 // Compares up to count characters of s1 and s2 without regard to case using |
42 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if | 42 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if |
43 // s2 > s1 according to a lexicographic comparison. | 43 // s2 > s1 according to a lexicographic comparison. |
44 BASE_API int strncasecmp(const char* s1, const char* s2, size_t count); | 44 int strncasecmp(const char* s1, const char* s2, size_t count); |
45 | 45 |
46 // Same as strncmp but for char16 strings. | 46 // Same as strncmp but for char16 strings. |
47 BASE_API int strncmp16(const char16* s1, const char16* s2, size_t count); | 47 int strncmp16(const char16* s1, const char16* s2, size_t count); |
48 | 48 |
49 // Wrapper for vsnprintf that always null-terminates and always returns the | 49 // Wrapper for vsnprintf that always null-terminates and always returns the |
50 // number of characters that would be in an untruncated formatted | 50 // number of characters that would be in an untruncated formatted |
51 // string, even when truncation occurs. | 51 // string, even when truncation occurs. |
52 BASE_API int vsnprintf(char* buffer, size_t size, const char* format, | 52 int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments) |
53 va_list arguments) | |
54 PRINTF_FORMAT(3, 0); | 53 PRINTF_FORMAT(3, 0); |
55 | 54 |
56 // vswprintf always null-terminates, but when truncation occurs, it will either | 55 // vswprintf always null-terminates, but when truncation occurs, it will either |
57 // return -1 or the number of characters that would be in an untruncated | 56 // return -1 or the number of characters that would be in an untruncated |
58 // formatted string. The actual return value depends on the underlying | 57 // formatted string. The actual return value depends on the underlying |
59 // C library's vswprintf implementation. | 58 // C library's vswprintf implementation. |
60 BASE_API int vswprintf(wchar_t* buffer, size_t size, | 59 int vswprintf(wchar_t* buffer, size_t size, |
61 const wchar_t* format, va_list arguments) | 60 const wchar_t* format, va_list arguments) |
62 WPRINTF_FORMAT(3, 0); | 61 WPRINTF_FORMAT(3, 0); |
63 | 62 |
64 // Some of these implementations need to be inlined. | 63 // Some of these implementations need to be inlined. |
65 | 64 |
66 // We separate the declaration from the implementation of this inline | 65 // We separate the declaration from the implementation of this inline |
67 // function just so the PRINTF_FORMAT works. | 66 // function just so the PRINTF_FORMAT works. |
68 inline int snprintf(char* buffer, size_t size, const char* format, ...) | 67 inline int snprintf(char* buffer, size_t size, const char* format, ...) |
69 PRINTF_FORMAT(3, 4); | 68 PRINTF_FORMAT(3, 4); |
70 inline int snprintf(char* buffer, size_t size, const char* format, ...) { | 69 inline int snprintf(char* buffer, size_t size, const char* format, ...) { |
71 va_list arguments; | 70 va_list arguments; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 // DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT CONSTRUCTORS. | 164 // DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT CONSTRUCTORS. |
166 // There is only one case where you should use these: functions which need to | 165 // There is only one case where you should use these: functions which need to |
167 // return a string by reference (e.g. as a class member accessor), and don't | 166 // return a string by reference (e.g. as a class member accessor), and don't |
168 // have an empty string to use (e.g. in an error case). These should not be | 167 // have an empty string to use (e.g. in an error case). These should not be |
169 // used as initializers, function arguments, or return values for functions | 168 // used as initializers, function arguments, or return values for functions |
170 // which return by value or outparam. | 169 // which return by value or outparam. |
171 BASE_API const std::string& EmptyString(); | 170 BASE_API const std::string& EmptyString(); |
172 BASE_API const std::wstring& EmptyWString(); | 171 BASE_API const std::wstring& EmptyWString(); |
173 BASE_API const string16& EmptyString16(); | 172 BASE_API const string16& EmptyString16(); |
174 | 173 |
175 extern const wchar_t kWhitespaceWide[]; | 174 BASE_API extern const wchar_t kWhitespaceWide[]; |
176 extern const char16 kWhitespaceUTF16[]; | 175 BASE_API extern const char16 kWhitespaceUTF16[]; |
177 extern const char kWhitespaceASCII[]; | 176 BASE_API extern const char kWhitespaceASCII[]; |
178 | 177 |
179 extern const char kUtf8ByteOrderMark[]; | 178 BASE_API extern const char kUtf8ByteOrderMark[]; |
180 | 179 |
181 // Removes characters in remove_chars from anywhere in input. Returns true if | 180 // Removes characters in remove_chars from anywhere in input. Returns true if |
182 // any characters were removed. | 181 // any characters were removed. |
183 // NOTE: Safe to use the same variable for both input and output. | 182 // NOTE: Safe to use the same variable for both input and output. |
184 BASE_API bool RemoveChars(const std::wstring& input, | 183 BASE_API bool RemoveChars(const std::wstring& input, |
185 const wchar_t remove_chars[], | 184 const wchar_t remove_chars[], |
186 std::wstring* output); | 185 std::wstring* output); |
187 BASE_API bool RemoveChars(const string16& input, | 186 BASE_API bool RemoveChars(const string16& input, |
188 const char16 remove_chars[], | 187 const char16 remove_chars[], |
189 string16* output); | 188 string16* output); |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 #elif defined(WCHAR_T_IS_UTF32) | 561 #elif defined(WCHAR_T_IS_UTF32) |
563 typedef uint32 Unsigned; | 562 typedef uint32 Unsigned; |
564 #endif | 563 #endif |
565 }; | 564 }; |
566 template<> | 565 template<> |
567 struct ToUnsigned<short> { | 566 struct ToUnsigned<short> { |
568 typedef unsigned short Unsigned; | 567 typedef unsigned short Unsigned; |
569 }; | 568 }; |
570 | 569 |
571 #endif // BASE_STRING_UTIL_H_ | 570 #endif // BASE_STRING_UTIL_H_ |
OLD | NEW |