1 | // Copyright 2005, Google Inc. |
2 | // All rights reserved. |
3 | // |
4 | // Redistribution and use in source and binary forms, with or without |
5 | // modification, are permitted provided that the following conditions are |
6 | // met: |
7 | // |
8 | // * Redistributions of source code must retain the above copyright |
9 | // notice, this list of conditions and the following disclaimer. |
10 | // * Redistributions in binary form must reproduce the above |
11 | // copyright notice, this list of conditions and the following disclaimer |
12 | // in the documentation and/or other materials provided with the |
13 | // distribution. |
14 | // * Neither the name of Google Inc. nor the names of its |
15 | // contributors may be used to endorse or promote products derived from |
16 | // this software without specific prior written permission. |
17 | // |
18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | // |
30 | // The Google C++ Testing and Mocking Framework (Google Test) |
31 | // |
32 | // This header file declares the String class and functions used internally by |
33 | // Google Test. They are subject to change without notice. They should not used |
34 | // by code external to Google Test. |
35 | // |
36 | // This header file is #included by gtest-internal.h. |
37 | // It should not be #included by other files. |
38 | |
39 | // GOOGLETEST_CM0001 DO NOT DELETE |
40 | |
41 | #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ |
42 | #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ |
43 | |
44 | #ifdef __BORLANDC__ |
45 | // string.h is not guaranteed to provide strcpy on C++ Builder. |
46 | # include <mem.h> |
47 | #endif |
48 | |
49 | #include <string.h> |
50 | #include <cstdint> |
51 | #include <string> |
52 | |
53 | #include "gtest/internal/gtest-port.h" |
54 | |
55 | namespace testing { |
56 | namespace internal { |
57 | |
58 | // String - an abstract class holding static string utilities. |
59 | class GTEST_API_ String { |
60 | public: |
61 | // Static utility methods |
62 | |
63 | // Clones a 0-terminated C string, allocating memory using new. The |
64 | // caller is responsible for deleting the return value using |
65 | // delete[]. Returns the cloned string, or NULL if the input is |
66 | // NULL. |
67 | // |
68 | // This is different from strdup() in string.h, which allocates |
69 | // memory using malloc(). |
70 | static const char* CloneCString(const char* c_str); |
71 | |
72 | #if GTEST_OS_WINDOWS_MOBILE |
73 | // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be |
74 | // able to pass strings to Win32 APIs on CE we need to convert them |
75 | // to 'Unicode', UTF-16. |
76 | |
77 | // Creates a UTF-16 wide string from the given ANSI string, allocating |
78 | // memory using new. The caller is responsible for deleting the return |
79 | // value using delete[]. Returns the wide string, or NULL if the |
80 | // input is NULL. |
81 | // |
82 | // The wide string is created using the ANSI codepage (CP_ACP) to |
83 | // match the behaviour of the ANSI versions of Win32 calls and the |
84 | // C runtime. |
85 | static LPCWSTR AnsiToUtf16(const char* c_str); |
86 | |
87 | // Creates an ANSI string from the given wide string, allocating |
88 | // memory using new. The caller is responsible for deleting the return |
89 | // value using delete[]. Returns the ANSI string, or NULL if the |
90 | // input is NULL. |
91 | // |
92 | // The returned string is created using the ANSI codepage (CP_ACP) to |
93 | // match the behaviour of the ANSI versions of Win32 calls and the |
94 | // C runtime. |
95 | static const char* Utf16ToAnsi(LPCWSTR utf16_str); |
96 | #endif |
97 | |
98 | // Compares two C strings. Returns true if and only if they have the same |
99 | // content. |
100 | // |
101 | // Unlike strcmp(), this function can handle NULL argument(s). A |
102 | // NULL C string is considered different to any non-NULL C string, |
103 | // including the empty string. |
104 | static bool CStringEquals(const char* lhs, const char* rhs); |
105 | |
106 | // Converts a wide C string to a String using the UTF-8 encoding. |
107 | // NULL will be converted to "(null)". If an error occurred during |
108 | // the conversion, "(failed to convert from wide string)" is |
109 | // returned. |
110 | static std::string ShowWideCString(const wchar_t* wide_c_str); |
111 | |
112 | // Compares two wide C strings. Returns true if and only if they have the |
113 | // same content. |
114 | // |
115 | // Unlike wcscmp(), this function can handle NULL argument(s). A |
116 | // NULL C string is considered different to any non-NULL C string, |
117 | // including the empty string. |
118 | static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs); |
119 | |
120 | // Compares two C strings, ignoring case. Returns true if and only if |
121 | // they have the same content. |
122 | // |
123 | // Unlike strcasecmp(), this function can handle NULL argument(s). |
124 | // A NULL C string is considered different to any non-NULL C string, |
125 | // including the empty string. |
126 | static bool CaseInsensitiveCStringEquals(const char* lhs, |
127 | const char* rhs); |
128 | |
129 | // Compares two wide C strings, ignoring case. Returns true if and only if |
130 | // they have the same content. |
131 | // |
132 | // Unlike wcscasecmp(), this function can handle NULL argument(s). |
133 | // A NULL C string is considered different to any non-NULL wide C string, |
134 | // including the empty string. |
135 | // NB: The implementations on different platforms slightly differ. |
136 | // On windows, this method uses _wcsicmp which compares according to LC_CTYPE |
137 | // environment variable. On GNU platform this method uses wcscasecmp |
138 | // which compares according to LC_CTYPE category of the current locale. |
139 | // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the |
140 | // current locale. |
141 | static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs, |
142 | const wchar_t* rhs); |
143 | |
144 | // Returns true if and only if the given string ends with the given suffix, |
145 | // ignoring case. Any string is considered to end with an empty suffix. |
146 | static bool EndsWithCaseInsensitive( |
147 | const std::string& str, const std::string& suffix); |
148 | |
149 | // Formats an int value as "%02d". |
150 | static std::string FormatIntWidth2(int value); // "%02d" for width == 2 |
151 | |
152 | // Formats an int value to given width with leading zeros. |
153 | static std::string FormatIntWidthN(int value, int width); |
154 | |
155 | // Formats an int value as "%X". |
156 | static std::string FormatHexInt(int value); |
157 | |
158 | // Formats an int value as "%X". |
159 | static std::string FormatHexUInt32(uint32_t value); |
160 | |
161 | // Formats a byte as "%02X". |
162 | static std::string FormatByte(unsigned char value); |
163 | |
164 | private: |
165 | String(); // Not meant to be instantiated. |
166 | }; // class String |
167 | |
168 | // Gets the content of the stringstream's buffer as an std::string. Each '\0' |
169 | // character in the buffer is replaced with "\\0". |
170 | GTEST_API_ std::string StringStreamToString(::std::stringstream* stream); |
171 | |
172 | } // namespace internal |
173 | } // namespace testing |
174 | |
175 | #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ |
176 | |