| 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 |
| 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_api.h" | 16 #include "base/base_api.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/string16.h" | 19 #include "base/string16.h" |
| 20 #include "base/string_piece.h" // For implicit conversions. | 20 #include "base/string_piece.h" // For implicit conversions. |
| 21 #include "base/string_util_static.h" |
| 21 | 22 |
| 22 // TODO(brettw) remove this dependency. Previously StringPrintf lived in this | 23 // TODO(brettw) remove this dependency. Previously StringPrintf lived in this |
| 23 // file. We need to convert the callers over to using stringprintf.h instead | 24 // file. We need to convert the callers over to using stringprintf.h instead |
| 24 // and then remove this. | 25 // and then remove this. |
| 25 #include "base/stringprintf.h" | 26 #include "base/stringprintf.h" |
| 26 | 27 |
| 27 // Safe standard library wrappers for all platforms. | 28 // Safe standard library wrappers for all platforms. |
| 28 | 29 |
| 29 namespace base { | 30 namespace base { |
| 30 | 31 |
| 31 // C standard-library functions like "strncasecmp" and "snprintf" that aren't | 32 // C standard-library functions like "strncasecmp" and "snprintf" that aren't |
| 32 // cross-platform are provided as "base::strncasecmp", and their prototypes | 33 // cross-platform are provided as "base::strncasecmp", and their prototypes |
| 33 // are listed below. These functions are then implemented as inline calls | 34 // are listed below. These functions are then implemented as inline calls |
| 34 // to the platform-specific equivalents in the platform-specific headers. | 35 // to the platform-specific equivalents in the platform-specific headers. |
| 35 | 36 |
| 36 // Compares the two strings s1 and s2 without regard to case using | 37 // 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 | 38 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if |
| 38 // s2 > s1 according to a lexicographic comparison. | 39 // s2 > s1 according to a lexicographic comparison. |
| 39 BASE_API int strcasecmp(const char* s1, const char* s2); | 40 int strcasecmp(const char* s1, const char* s2); |
| 40 | 41 |
| 41 // Compares up to count characters of s1 and s2 without regard to case using | 42 // 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 | 43 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if |
| 43 // s2 > s1 according to a lexicographic comparison. | 44 // s2 > s1 according to a lexicographic comparison. |
| 44 BASE_API int strncasecmp(const char* s1, const char* s2, size_t count); | 45 int strncasecmp(const char* s1, const char* s2, size_t count); |
| 45 | 46 |
| 46 // Same as strncmp but for char16 strings. | 47 // Same as strncmp but for char16 strings. |
| 47 BASE_API int strncmp16(const char16* s1, const char16* s2, size_t count); | 48 int strncmp16(const char16* s1, const char16* s2, size_t count); |
| 48 | 49 |
| 49 // Wrapper for vsnprintf that always null-terminates and always returns the | 50 // Wrapper for vsnprintf that always null-terminates and always returns the |
| 50 // number of characters that would be in an untruncated formatted | 51 // number of characters that would be in an untruncated formatted |
| 51 // string, even when truncation occurs. | 52 // string, even when truncation occurs. |
| 52 BASE_API int vsnprintf(char* buffer, size_t size, const char* format, | 53 int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments) |
| 53 va_list arguments) | |
| 54 PRINTF_FORMAT(3, 0); | 54 PRINTF_FORMAT(3, 0); |
| 55 | 55 |
| 56 // vswprintf always null-terminates, but when truncation occurs, it will either | 56 // 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 | 57 // return -1 or the number of characters that would be in an untruncated |
| 58 // formatted string. The actual return value depends on the underlying | 58 // formatted string. The actual return value depends on the underlying |
| 59 // C library's vswprintf implementation. | 59 // C library's vswprintf implementation. |
| 60 BASE_API int vswprintf(wchar_t* buffer, size_t size, | 60 int vswprintf(wchar_t* buffer, size_t size, |
| 61 const wchar_t* format, va_list arguments) | 61 const wchar_t* format, va_list arguments) |
| 62 WPRINTF_FORMAT(3, 0); | 62 WPRINTF_FORMAT(3, 0); |
| 63 | 63 |
| 64 // Some of these implementations need to be inlined. | 64 // Some of these implementations need to be inlined. |
| 65 | 65 |
| 66 // We separate the declaration from the implementation of this inline | 66 // We separate the declaration from the implementation of this inline |
| 67 // function just so the PRINTF_FORMAT works. | 67 // function just so the PRINTF_FORMAT works. |
| 68 inline int snprintf(char* buffer, size_t size, const char* format, ...) | 68 inline int snprintf(char* buffer, size_t size, const char* format, ...) |
| 69 PRINTF_FORMAT(3, 4); | 69 PRINTF_FORMAT(3, 4); |
| 70 inline int snprintf(char* buffer, size_t size, const char* format, ...) { | 70 inline int snprintf(char* buffer, size_t size, const char* format, ...) { |
| 71 va_list arguments; | 71 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. | 165 // 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 | 166 // 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 | 167 // 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 | 168 // 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 | 169 // used as initializers, function arguments, or return values for functions |
| 170 // which return by value or outparam. | 170 // which return by value or outparam. |
| 171 BASE_API const std::string& EmptyString(); | 171 BASE_API const std::string& EmptyString(); |
| 172 BASE_API const std::wstring& EmptyWString(); | 172 BASE_API const std::wstring& EmptyWString(); |
| 173 BASE_API const string16& EmptyString16(); | 173 BASE_API const string16& EmptyString16(); |
| 174 | 174 |
| 175 extern const wchar_t kWhitespaceWide[]; | |
| 176 extern const char16 kWhitespaceUTF16[]; | |
| 177 extern const char kWhitespaceASCII[]; | |
| 178 | |
| 179 extern const char kUtf8ByteOrderMark[]; | |
| 180 | |
| 181 // Removes characters in remove_chars from anywhere in input. Returns true if | 175 // Removes characters in remove_chars from anywhere in input. Returns true if |
| 182 // any characters were removed. | 176 // any characters were removed. |
| 183 // NOTE: Safe to use the same variable for both input and output. | 177 // NOTE: Safe to use the same variable for both input and output. |
| 184 BASE_API bool RemoveChars(const std::wstring& input, | 178 BASE_API bool RemoveChars(const std::wstring& input, |
| 185 const wchar_t remove_chars[], | 179 const wchar_t remove_chars[], |
| 186 std::wstring* output); | 180 std::wstring* output); |
| 187 BASE_API bool RemoveChars(const string16& input, | 181 BASE_API bool RemoveChars(const string16& input, |
| 188 const char16 remove_chars[], | 182 const char16 remove_chars[], |
| 189 string16* output); | 183 string16* output); |
| 190 BASE_API bool RemoveChars(const std::string& input, | 184 BASE_API bool RemoveChars(const std::string& input, |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 #elif defined(WCHAR_T_IS_UTF32) | 556 #elif defined(WCHAR_T_IS_UTF32) |
| 563 typedef uint32 Unsigned; | 557 typedef uint32 Unsigned; |
| 564 #endif | 558 #endif |
| 565 }; | 559 }; |
| 566 template<> | 560 template<> |
| 567 struct ToUnsigned<short> { | 561 struct ToUnsigned<short> { |
| 568 typedef unsigned short Unsigned; | 562 typedef unsigned short Unsigned; |
| 569 }; | 563 }; |
| 570 | 564 |
| 571 #endif // BASE_STRING_UTIL_H_ | 565 #endif // BASE_STRING_UTIL_H_ |
| OLD | NEW |