| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 | 9 |
| 10 #include <stdarg.h> // va_list | 10 #include <stdarg.h> // va_list |
| 11 | 11 |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 16 #include "base/compiler_specific.h" |
| 16 #include "base/string16.h" | 17 #include "base/string16.h" |
| 17 #include "base/string_piece.h" // For implicit conversions. | 18 #include "base/string_piece.h" // For implicit conversions. |
| 18 | 19 |
| 19 // TODO(brettw) this dependency should be removed and callers that need | 20 // TODO(brettw) this dependency should be removed and callers that need |
| 20 // these functions should include this file directly. | 21 // these functions should include this file directly. |
| 21 #include "base/utf_string_conversions.h" | 22 #include "base/utf_string_conversions.h" |
| 22 | 23 |
| 23 // Safe standard library wrappers for all platforms. | 24 // Safe standard library wrappers for all platforms. |
| 24 | 25 |
| 25 namespace base { | 26 namespace base { |
| 26 | 27 |
| 27 // C standard-library functions like "strncasecmp" and "snprintf" that aren't | 28 // C standard-library functions like "strncasecmp" and "snprintf" that aren't |
| 28 // cross-platform are provided as "base::strncasecmp", and their prototypes | 29 // cross-platform are provided as "base::strncasecmp", and their prototypes |
| 29 // are listed below. These functions are then implemented as inline calls | 30 // are listed below. These functions are then implemented as inline calls |
| 30 // to the platform-specific equivalents in the platform-specific headers. | 31 // to the platform-specific equivalents in the platform-specific headers. |
| 31 | 32 |
| 32 // Compare the two strings s1 and s2 without regard to case using | 33 // Compare the two strings s1 and s2 without regard to case using |
| 33 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if | 34 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if |
| 34 // s2 > s1 according to a lexicographic comparison. | 35 // s2 > s1 according to a lexicographic comparison. |
| 35 int strcasecmp(const char* s1, const char* s2); | 36 int strcasecmp(const char* s1, const char* s2); |
| 36 | 37 |
| 37 // Compare up to count characters of s1 and s2 without regard to case using | 38 // Compare up to count characters of s1 and s2 without regard to case using |
| 38 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if | 39 // the current locale; returns 0 if they are equal, 1 if s1 > s2, and -1 if |
| 39 // s2 > s1 according to a lexicographic comparison. | 40 // s2 > s1 according to a lexicographic comparison. |
| 40 int strncasecmp(const char* s1, const char* s2, size_t count); | 41 int strncasecmp(const char* s1, const char* s2, size_t count); |
| 41 | 42 |
| 42 // Wrapper for vsnprintf that always null-terminates and always returns the | 43 // Wrapper for vsnprintf that always null-terminates and always returns the |
| 43 // number of characters that would be in an untruncated formatted | 44 // number of characters that would be in an untruncated formatted |
| 44 // string, even when truncation occurs. | 45 // string, even when truncation occurs. |
| 45 int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments); | 46 int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments) |
| 47 PRINTF_FORMAT(3, 0); |
| 46 | 48 |
| 47 // vswprintf always null-terminates, but when truncation occurs, it will either | 49 // vswprintf always null-terminates, but when truncation occurs, it will either |
| 48 // return -1 or the number of characters that would be in an untruncated | 50 // return -1 or the number of characters that would be in an untruncated |
| 49 // formatted string. The actual return value depends on the underlying | 51 // formatted string. The actual return value depends on the underlying |
| 50 // C library's vswprintf implementation. | 52 // C library's vswprintf implementation. |
| 51 int vswprintf(wchar_t* buffer, size_t size, | 53 int vswprintf(wchar_t* buffer, size_t size, |
| 52 const wchar_t* format, va_list arguments); | 54 const wchar_t* format, va_list arguments) WPRINTF_FORMAT(3, 0); |
| 53 | 55 |
| 54 // Some of these implementations need to be inlined. | 56 // Some of these implementations need to be inlined. |
| 55 | 57 |
| 58 // We separate the declaration from the implementation of this inline |
| 59 // function just so the PRINTF_FORMAT works. |
| 60 inline int snprintf(char* buffer, size_t size, const char* format, ...) |
| 61 PRINTF_FORMAT(3, 4); |
| 56 inline int snprintf(char* buffer, size_t size, const char* format, ...) { | 62 inline int snprintf(char* buffer, size_t size, const char* format, ...) { |
| 57 va_list arguments; | 63 va_list arguments; |
| 58 va_start(arguments, format); | 64 va_start(arguments, format); |
| 59 int result = vsnprintf(buffer, size, format, arguments); | 65 int result = vsnprintf(buffer, size, format, arguments); |
| 60 va_end(arguments); | 66 va_end(arguments); |
| 61 return result; | 67 return result; |
| 62 } | 68 } |
| 63 | 69 |
| 70 // We separate the declaration from the implementation of this inline |
| 71 // function just so the WPRINTF_FORMAT works. |
| 72 inline int swprintf(wchar_t* buffer, size_t size, const wchar_t* format, ...) |
| 73 WPRINTF_FORMAT(3, 4); |
| 64 inline int swprintf(wchar_t* buffer, size_t size, const wchar_t* format, ...) { | 74 inline int swprintf(wchar_t* buffer, size_t size, const wchar_t* format, ...) { |
| 65 va_list arguments; | 75 va_list arguments; |
| 66 va_start(arguments, format); | 76 va_start(arguments, format); |
| 67 int result = vswprintf(buffer, size, format, arguments); | 77 int result = vswprintf(buffer, size, format, arguments); |
| 68 va_end(arguments); | 78 va_end(arguments); |
| 69 return result; | 79 return result; |
| 70 } | 80 } |
| 71 | 81 |
| 72 // BSD-style safe and consistent string copy functions. | 82 // BSD-style safe and consistent string copy functions. |
| 73 // Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|. | 83 // Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|. |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 int StringToInt(const std::string& value); | 441 int StringToInt(const std::string& value); |
| 432 int StringToInt(const string16& value); | 442 int StringToInt(const string16& value); |
| 433 int64 StringToInt64(const std::string& value); | 443 int64 StringToInt64(const std::string& value); |
| 434 int64 StringToInt64(const string16& value); | 444 int64 StringToInt64(const string16& value); |
| 435 int HexStringToInt(const std::string& value); | 445 int HexStringToInt(const std::string& value); |
| 436 int HexStringToInt(const string16& value); | 446 int HexStringToInt(const string16& value); |
| 437 double StringToDouble(const std::string& value); | 447 double StringToDouble(const std::string& value); |
| 438 double StringToDouble(const string16& value); | 448 double StringToDouble(const string16& value); |
| 439 | 449 |
| 440 // Return a C++ string given printf-like input. | 450 // Return a C++ string given printf-like input. |
| 441 std::string StringPrintf(const char* format, ...); | 451 std::string StringPrintf(const char* format, ...) PRINTF_FORMAT(1, 2); |
| 442 std::wstring StringPrintf(const wchar_t* format, ...); | 452 std::wstring StringPrintf(const wchar_t* format, ...) WPRINTF_FORMAT(1, 2); |
| 443 | 453 |
| 444 // Store result into a supplied string and return it | 454 // Store result into a supplied string and return it |
| 445 const std::string& SStringPrintf(std::string* dst, const char* format, ...); | 455 const std::string& SStringPrintf(std::string* dst, const char* format, ...) |
| 456 PRINTF_FORMAT(2, 3); |
| 446 const std::wstring& SStringPrintf(std::wstring* dst, | 457 const std::wstring& SStringPrintf(std::wstring* dst, |
| 447 const wchar_t* format, ...); | 458 const wchar_t* format, ...) |
| 459 WPRINTF_FORMAT(2, 3); |
| 448 | 460 |
| 449 // Append result to a supplied string | 461 // Append result to a supplied string |
| 450 void StringAppendF(std::string* dst, const char* format, ...); | 462 void StringAppendF(std::string* dst, const char* format, ...) |
| 451 void StringAppendF(std::wstring* dst, const wchar_t* format, ...); | 463 PRINTF_FORMAT(2, 3); |
| 464 void StringAppendF(std::wstring* dst, const wchar_t* format, ...) |
| 465 WPRINTF_FORMAT(2, 3); |
| 452 | 466 |
| 453 // Lower-level routine that takes a va_list and appends to a specified | 467 // Lower-level routine that takes a va_list and appends to a specified |
| 454 // string. All other routines are just convenience wrappers around it. | 468 // string. All other routines are just convenience wrappers around it. |
| 455 void StringAppendV(std::string* dst, const char* format, va_list ap); | 469 void StringAppendV(std::string* dst, const char* format, va_list ap) |
| 456 void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap); | 470 PRINTF_FORMAT(2, 0); |
| 471 void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap) |
| 472 WPRINTF_FORMAT(2, 0); |
| 457 | 473 |
| 458 // This is mpcomplete's pattern for saving a string copy when dealing with | 474 // This is mpcomplete's pattern for saving a string copy when dealing with |
| 459 // a function that writes results into a wchar_t[] and wanting the result to | 475 // a function that writes results into a wchar_t[] and wanting the result to |
| 460 // end up in a std::wstring. It ensures that the std::wstring's internal | 476 // end up in a std::wstring. It ensures that the std::wstring's internal |
| 461 // buffer has enough room to store the characters to be written into it, and | 477 // buffer has enough room to store the characters to be written into it, and |
| 462 // sets its .length() attribute to the right value. | 478 // sets its .length() attribute to the right value. |
| 463 // | 479 // |
| 464 // The reserve() call allocates the memory required to hold the string | 480 // The reserve() call allocates the memory required to hold the string |
| 465 // plus a terminating null. This is done because resize() isn't | 481 // plus a terminating null. This is done because resize() isn't |
| 466 // guaranteed to reserve space for the null. The resize() call is | 482 // guaranteed to reserve space for the null. The resize() call is |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 #elif defined(WCHAR_T_IS_UTF32) | 626 #elif defined(WCHAR_T_IS_UTF32) |
| 611 typedef uint32 Unsigned; | 627 typedef uint32 Unsigned; |
| 612 #endif | 628 #endif |
| 613 }; | 629 }; |
| 614 template<> | 630 template<> |
| 615 struct ToUnsigned<short> { | 631 struct ToUnsigned<short> { |
| 616 typedef unsigned short Unsigned; | 632 typedef unsigned short Unsigned; |
| 617 }; | 633 }; |
| 618 | 634 |
| 619 #endif // BASE_STRING_UTIL_H_ | 635 #endif // BASE_STRING_UTIL_H_ |
| OLD | NEW |