OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 // vswprintf always null-terminates, but when truncation occurs, it will either | 48 // vswprintf always null-terminates, but when truncation occurs, it will either |
49 // return -1 or the number of characters that would be in an untruncated | 49 // return -1 or the number of characters that would be in an untruncated |
50 // formatted string. The actual return value depends on the underlying | 50 // formatted string. The actual return value depends on the underlying |
51 // C library's vswprintf implementation. | 51 // C library's vswprintf implementation. |
52 int vswprintf(wchar_t* buffer, size_t size, | 52 int vswprintf(wchar_t* buffer, size_t size, |
53 const wchar_t* format, va_list arguments) WPRINTF_FORMAT(3, 0); | 53 const wchar_t* format, va_list arguments) WPRINTF_FORMAT(3, 0); |
54 | 54 |
55 // Some of these implementations need to be inlined. | 55 // Some of these implementations need to be inlined. |
56 | 56 |
57 // CLANG NOTE | |
58 // Qualified calls with base:: is to work around | |
59 // http://llvm.org/bugs/show_bug.cgi?id=6762 | |
60 | |
61 // We separate the declaration from the implementation of this inline | 57 // We separate the declaration from the implementation of this inline |
62 // function just so the PRINTF_FORMAT works. | 58 // function just so the PRINTF_FORMAT works. |
63 inline int snprintf(char* buffer, size_t size, const char* format, ...) | 59 inline int snprintf(char* buffer, size_t size, const char* format, ...) |
64 PRINTF_FORMAT(3, 4); | 60 PRINTF_FORMAT(3, 4); |
65 inline int snprintf(char* buffer, size_t size, const char* format, ...) { | 61 inline int snprintf(char* buffer, size_t size, const char* format, ...) { |
66 va_list arguments; | 62 va_list arguments; |
67 va_start(arguments, format); | 63 va_start(arguments, format); |
68 int result = base::vsnprintf(buffer, size, format, arguments); | 64 int result = vsnprintf(buffer, size, format, arguments); |
69 va_end(arguments); | 65 va_end(arguments); |
70 return result; | 66 return result; |
71 } | 67 } |
72 | 68 |
73 // We separate the declaration from the implementation of this inline | 69 // We separate the declaration from the implementation of this inline |
74 // function just so the WPRINTF_FORMAT works. | 70 // function just so the WPRINTF_FORMAT works. |
75 inline int swprintf(wchar_t* buffer, size_t size, const wchar_t* format, ...) | 71 inline int swprintf(wchar_t* buffer, size_t size, const wchar_t* format, ...) |
76 WPRINTF_FORMAT(3, 4); | 72 WPRINTF_FORMAT(3, 4); |
77 inline int swprintf(wchar_t* buffer, size_t size, const wchar_t* format, ...) { | 73 inline int swprintf(wchar_t* buffer, size_t size, const wchar_t* format, ...) { |
78 va_list arguments; | 74 va_list arguments; |
79 va_start(arguments, format); | 75 va_start(arguments, format); |
80 int result = base::vswprintf(buffer, size, format, arguments); | 76 int result = vswprintf(buffer, size, format, arguments); |
81 va_end(arguments); | 77 va_end(arguments); |
82 return result; | 78 return result; |
83 } | 79 } |
84 | 80 |
85 // BSD-style safe and consistent string copy functions. | 81 // BSD-style safe and consistent string copy functions. |
86 // Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|. | 82 // Copies |src| to |dst|, where |dst_size| is the total allocated size of |dst|. |
87 // Copies at most |dst_size|-1 characters, and always NULL terminates |dst|, as | 83 // Copies at most |dst_size|-1 characters, and always NULL terminates |dst|, as |
88 // long as |dst_size| is not 0. Returns the length of |src| in characters. | 84 // long as |dst_size| is not 0. Returns the length of |src| in characters. |
89 // If the return value is >= dst_size, then the output was truncated. | 85 // If the return value is >= dst_size, then the output was truncated. |
90 // NOTE: All sizes are in number of characters, NOT in bytes. | 86 // NOTE: All sizes are in number of characters, NOT in bytes. |
(...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
692 #elif defined(WCHAR_T_IS_UTF32) | 688 #elif defined(WCHAR_T_IS_UTF32) |
693 typedef uint32 Unsigned; | 689 typedef uint32 Unsigned; |
694 #endif | 690 #endif |
695 }; | 691 }; |
696 template<> | 692 template<> |
697 struct ToUnsigned<short> { | 693 struct ToUnsigned<short> { |
698 typedef unsigned short Unsigned; | 694 typedef unsigned short Unsigned; |
699 }; | 695 }; |
700 | 696 |
701 #endif // BASE_STRING_UTIL_H_ | 697 #endif // BASE_STRING_UTIL_H_ |
OLD | NEW |