blob: 612b6cefef9ab4f60ad4be4120f0a4b5b7aba177 [file] [log] [blame]
shiqiand2014562008-07-03 22:38:12 +00001// 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// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
31//
32// The Google C++ Testing Framework (Google Test)
33//
34// This header file declares the String class and functions used internally by
35// Google Test. They are subject to change without notice. They should not used
36// by code external to Google Test.
37//
38// This header file is #included by testing/base/internal/gtest-internal.h.
39// It should not be #included by other files.
40
41#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
42#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
43
44#include <string.h>
shiqiand2014562008-07-03 22:38:12 +000045#include <gtest/internal/gtest-port.h>
shiqiand2014562008-07-03 22:38:12 +000046
47namespace testing {
48namespace internal {
49
50// String - a UTF-8 string class.
51//
52// We cannot use std::string as Microsoft's STL implementation in
53// Visual C++ 7.1 has problems when exception is disabled. There is a
54// hack to work around this, but we've seen cases where the hack fails
55// to work.
56//
57// Also, String is different from std::string in that it can represent
58// both NULL and the empty string, while std::string cannot represent
59// NULL.
60//
61// NULL and the empty string are considered different. NULL is less
62// than anything (including the empty string) except itself.
63//
64// This class only provides minimum functionality necessary for
65// implementing Google Test. We do not intend to implement a full-fledged
66// string class here.
67//
68// Since the purpose of this class is to provide a substitute for
69// std::string on platforms where it cannot be used, we define a copy
70// constructor and assignment operators such that we don't need
71// conditional compilation in a lot of places.
72//
73// In order to make the representation efficient, the d'tor of String
74// is not virtual. Therefore DO NOT INHERIT FROM String.
75class String {
76 public:
77 // Static utility methods
78
79 // Returns the input if it's not NULL, otherwise returns "(null)".
80 // This function serves two purposes:
81 //
82 // 1. ShowCString(NULL) has type 'const char *', instead of the
83 // type of NULL (which is int).
84 //
85 // 2. In MSVC, streaming a null char pointer to StrStream generates
86 // an access violation, so we need to convert NULL to "(null)"
87 // before streaming it.
88 static inline const char* ShowCString(const char* c_str) {
89 return c_str ? c_str : "(null)";
90 }
91
92 // Returns the input enclosed in double quotes if it's not NULL;
93 // otherwise returns "(null)". For example, "\"Hello\"" is returned
94 // for input "Hello".
95 //
96 // This is useful for printing a C string in the syntax of a literal.
97 //
98 // Known issue: escape sequences are not handled yet.
99 static String ShowCStringQuoted(const char* c_str);
100
101 // Clones a 0-terminated C string, allocating memory using new. The
102 // caller is responsible for deleting the return value using
103 // delete[]. Returns the cloned string, or NULL if the input is
104 // NULL.
105 //
106 // This is different from strdup() in string.h, which allocates
107 // memory using malloc().
108 static const char* CloneCString(const char* c_str);
109
shiqianbf9b4b42008-07-31 18:34:08 +0000110#ifdef _WIN32_WCE
111 // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be
112 // able to pass strings to Win32 APIs on CE we need to convert them
113 // to 'Unicode', UTF-16.
114
115 // Creates a UTF-16 wide string from the given ANSI string, allocating
116 // memory using new. The caller is responsible for deleting the return
117 // value using delete[]. Returns the wide string, or NULL if the
118 // input is NULL.
119 //
120 // The wide string is created using the ANSI codepage (CP_ACP) to
121 // match the behaviour of the ANSI versions of Win32 calls and the
122 // C runtime.
123 static LPCWSTR AnsiToUtf16(const char* c_str);
124
125 // Creates an ANSI string from the given wide string, allocating
126 // memory using new. The caller is responsible for deleting the return
127 // value using delete[]. Returns the ANSI string, or NULL if the
128 // input is NULL.
129 //
130 // The returned string is created using the ANSI codepage (CP_ACP) to
131 // match the behaviour of the ANSI versions of Win32 calls and the
132 // C runtime.
133 static const char* Utf16ToAnsi(LPCWSTR utf16_str);
134#endif
135
shiqiand2014562008-07-03 22:38:12 +0000136 // Compares two C strings. Returns true iff they have the same content.
137 //
138 // Unlike strcmp(), this function can handle NULL argument(s). A
139 // NULL C string is considered different to any non-NULL C string,
140 // including the empty string.
141 static bool CStringEquals(const char* lhs, const char* rhs);
142
143 // Converts a wide C string to a String using the UTF-8 encoding.
144 // NULL will be converted to "(null)". If an error occurred during
145 // the conversion, "(failed to convert from wide string)" is
146 // returned.
147 static String ShowWideCString(const wchar_t* wide_c_str);
148
149 // Similar to ShowWideCString(), except that this function encloses
150 // the converted string in double quotes.
151 static String ShowWideCStringQuoted(const wchar_t* wide_c_str);
152
153 // Compares two wide C strings. Returns true iff they have the same
154 // content.
155 //
156 // Unlike wcscmp(), this function can handle NULL argument(s). A
157 // NULL C string is considered different to any non-NULL C string,
158 // including the empty string.
159 static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs);
160
161 // Compares two C strings, ignoring case. Returns true iff they
162 // have the same content.
163 //
164 // Unlike strcasecmp(), this function can handle NULL argument(s).
165 // A NULL C string is considered different to any non-NULL C string,
166 // including the empty string.
167 static bool CaseInsensitiveCStringEquals(const char* lhs,
168 const char* rhs);
169
170 // Formats a list of arguments to a String, using the same format
171 // spec string as for printf.
172 //
173 // We do not use the StringPrintf class as it is not universally
174 // available.
175 //
176 // The result is limited to 4096 characters (including the tailing
177 // 0). If 4096 characters are not enough to format the input,
178 // "<buffer exceeded>" is returned.
179 static String Format(const char* format, ...);
180
181 // C'tors
182
183 // The default c'tor constructs a NULL string.
184 String() : c_str_(NULL) {}
185
186 // Constructs a String by cloning a 0-terminated C string.
187 String(const char* c_str) : c_str_(NULL) { // NOLINT
188 *this = c_str;
189 }
190
191 // Constructs a String by copying a given number of chars from a
192 // buffer. E.g. String("hello", 3) will create the string "hel".
193 String(const char* buffer, size_t len);
194
195 // The copy c'tor creates a new copy of the string. The two
196 // String objects do not share content.
197 String(const String& str) : c_str_(NULL) {
198 *this = str;
199 }
200
201 // D'tor. String is intended to be a final class, so the d'tor
202 // doesn't need to be virtual.
203 ~String() { delete[] c_str_; }
204
205 // Returns true iff this is an empty string (i.e. "").
206 bool empty() const {
207 return (c_str_ != NULL) && (*c_str_ == '\0');
208 }
209
210 // Compares this with another String.
211 // Returns < 0 if this is less than rhs, 0 if this is equal to rhs, or > 0
212 // if this is greater than rhs.
213 int Compare(const String& rhs) const;
214
215 // Returns true iff this String equals the given C string. A NULL
216 // string and a non-NULL string are considered not equal.
217 bool operator==(const char* c_str) const {
218 return CStringEquals(c_str_, c_str);
219 }
220
221 // Returns true iff this String doesn't equal the given C string. A NULL
222 // string and a non-NULL string are considered not equal.
223 bool operator!=(const char* c_str) const {
224 return !CStringEquals(c_str_, c_str);
225 }
226
227 // Returns true iff this String ends with the given suffix. *Any*
228 // String is considered to end with a NULL or empty suffix.
229 bool EndsWith(const char* suffix) const;
230
231 // Returns true iff this String ends with the given suffix, not considering
232 // case. Any String is considered to end with a NULL or empty suffix.
233 bool EndsWithCaseInsensitive(const char* suffix) const;
234
235 // Returns the length of the encapsulated string, or -1 if the
236 // string is NULL.
237 int GetLength() const {
238 return c_str_ ? static_cast<int>(strlen(c_str_)) : -1;
239 }
240
241 // Gets the 0-terminated C string this String object represents.
242 // The String object still owns the string. Therefore the caller
243 // should NOT delete the return value.
244 const char* c_str() const { return c_str_; }
245
246 // Sets the 0-terminated C string this String object represents.
247 // The old string in this object is deleted, and this object will
248 // own a clone of the input string. This function copies only up to
249 // length bytes (plus a terminating null byte), or until the first
250 // null byte, whichever comes first.
251 //
252 // This function works even when the c_str parameter has the same
253 // value as that of the c_str_ field.
254 void Set(const char* c_str, size_t length);
255
256 // Assigns a C string to this object. Self-assignment works.
257 const String& operator=(const char* c_str);
258
259 // Assigns a String object to this object. Self-assignment works.
260 const String& operator=(const String &rhs) {
261 *this = rhs.c_str_;
262 return *this;
263 }
264
265 private:
266 const char* c_str_;
267};
268
269// Streams a String to an ostream.
270inline ::std::ostream& operator <<(::std::ostream& os, const String& str) {
271 // We call String::ShowCString() to convert NULL to "(null)".
272 // Otherwise we'll get an access violation on Windows.
273 return os << String::ShowCString(str.c_str());
274}
275
276// Gets the content of the StrStream's buffer as a String. Each '\0'
277// character in the buffer is replaced with "\\0".
278String StrStreamToString(StrStream* stream);
279
280// Converts a streamable value to a String. A NULL pointer is
281// converted to "(null)". When the input value is a ::string,
282// ::std::string, ::wstring, or ::std::wstring object, each NUL
283// character in it is replaced with "\\0".
284
285// Declared here but defined in gtest.h, so that it has access
286// to the definition of the Message class, required by the ARM
287// compiler.
288template <typename T>
289String StreamableToString(const T& streamable);
290
291} // namespace internal
292} // namespace testing
293
294#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_