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 #ifndef CHROME_INSTALLER_MINI_INSTALLER_MINI_STRING_H_ | 5 #ifndef CHROME_INSTALLER_MINI_INSTALLER_MINI_STRING_H_ |
6 #define CHROME_INSTALLER_MINI_INSTALLER_MINI_STRING_H_ | 6 #define CHROME_INSTALLER_MINI_INSTALLER_MINI_STRING_H_ |
7 | 7 |
| 8 #ifndef COMPILE_ASSERT |
| 9 // COMPILE_ASSERT macro borrowed from basictypes.h |
| 10 template <bool> |
| 11 struct CompileAssert {}; |
| 12 #define COMPILE_ASSERT(expr, msg) \ |
| 13 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] |
| 14 #endif |
8 | 15 |
9 namespace mini_installer { | 16 namespace mini_installer { |
10 | 17 |
11 // NOTE: Do not assume that these string functions support UTF encoding. | 18 // NOTE: Do not assume that these string functions support UTF encoding. |
12 // This is fine for the purposes of the mini_installer, but you have | 19 // This is fine for the purposes of the mini_installer, but you have |
13 // been warned! | 20 // been warned! |
14 | 21 |
15 // Formats a sequence of |bytes| as hex. The |str| buffer must have room for | 22 // Formats a sequence of |bytes| as hex. The |str| buffer must have room for |
16 // at least 2*|size| + 1. | 23 // at least 2*|size| + 1. |
17 bool HexEncode(const void* bytes, size_t size, wchar_t* str, size_t str_size); | 24 bool HexEncode(const void* bytes, size_t size, wchar_t* str, size_t str_size); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 // terminator. | 65 // terminator. |
59 wchar_t* GetNameFromPathExt(wchar_t* path, size_t size); | 66 wchar_t* GetNameFromPathExt(wchar_t* path, size_t size); |
60 | 67 |
61 // A string class that manages a fixed size buffer on the stack. | 68 // A string class that manages a fixed size buffer on the stack. |
62 // The methods in the class are based on the above string methods and the | 69 // The methods in the class are based on the above string methods and the |
63 // class additionally is careful about proper buffer termination. | 70 // class additionally is careful about proper buffer termination. |
64 template <size_t kCapacity> | 71 template <size_t kCapacity> |
65 class StackString { | 72 class StackString { |
66 public: | 73 public: |
67 StackString() { | 74 StackString() { |
68 static_assert(kCapacity != 0, "invalid buffer size"); | 75 COMPILE_ASSERT(kCapacity != 0, invalid_buffer_size); |
69 buffer_[kCapacity] = L'\0'; // We always reserve 1 more than asked for. | 76 buffer_[kCapacity] = L'\0'; // We always reserve 1 more than asked for. |
70 clear(); | 77 clear(); |
71 } | 78 } |
72 | 79 |
73 // We do not expose a constructor that accepts a string pointer on purpose. | 80 // We do not expose a constructor that accepts a string pointer on purpose. |
74 // We expect the caller to call assign() and handle failures. | 81 // We expect the caller to call assign() and handle failures. |
75 | 82 |
76 // Returns the number of reserved characters in this buffer, _including_ | 83 // Returns the number of reserved characters in this buffer, _including_ |
77 // the reserved char for the terminator. | 84 // the reserved char for the terminator. |
78 size_t capacity() const { | 85 size_t capacity() const { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 wchar_t buffer_[kCapacity + 1]; | 137 wchar_t buffer_[kCapacity + 1]; |
131 | 138 |
132 private: | 139 private: |
133 StackString(const StackString&); | 140 StackString(const StackString&); |
134 StackString& operator=(const StackString&); | 141 StackString& operator=(const StackString&); |
135 }; | 142 }; |
136 | 143 |
137 } // namespace mini_installer | 144 } // namespace mini_installer |
138 | 145 |
139 #endif // CHROME_INSTALLER_MINI_INSTALLER_MINI_STRING_H_ | 146 #endif // CHROME_INSTALLER_MINI_INSTALLER_MINI_STRING_H_ |
OLD | NEW |