1
// Copyright 2005, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
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
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.
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.
30
// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
32
// The Google C++ Testing Framework (Google Test)
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.
38
// This header file is #included by testing/base/internal/gtest-internal.h.
39
// It should not be #included by other files.
41
#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
42
#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
46
#if defined(__APPLE__) && !defined(GTEST_NOT_MAC_FRAMEWORK_MODE)
47
// When using Google Test on the Mac as a framework, all the includes will be
48
// in the framework headers folder along with gtest.h.
49
// Define GTEST_NOT_MAC_FRAMEWORK_MODE if you are building Google Test on
50
// the Mac and are not using it as a framework.
51
// More info on frameworks available here:
52
// http://developer.apple.com/documentation/MacOSX/Conceptual/BPFrameworks/
53
// Concepts/WhatAreFrameworks.html.
54
#include "gtest-port.h" // NOLINT
56
#include <gtest/internal/gtest-port.h>
57
#endif // defined(__APPLE__) && !defined(GTEST_NOT_MAC_FRAMEWORK_MODE)
62
// String - a UTF-8 string class.
64
// We cannot use std::string as Microsoft's STL implementation in
65
// Visual C++ 7.1 has problems when exception is disabled. There is a
66
// hack to work around this, but we've seen cases where the hack fails
69
// Also, String is different from std::string in that it can represent
70
// both NULL and the empty string, while std::string cannot represent
73
// NULL and the empty string are considered different. NULL is less
74
// than anything (including the empty string) except itself.
76
// This class only provides minimum functionality necessary for
77
// implementing Google Test. We do not intend to implement a full-fledged
80
// Since the purpose of this class is to provide a substitute for
81
// std::string on platforms where it cannot be used, we define a copy
82
// constructor and assignment operators such that we don't need
83
// conditional compilation in a lot of places.
85
// In order to make the representation efficient, the d'tor of String
86
// is not virtual. Therefore DO NOT INHERIT FROM String.
89
// Static utility methods
91
// Returns the input if it's not NULL, otherwise returns "(null)".
92
// This function serves two purposes:
94
// 1. ShowCString(NULL) has type 'const char *', instead of the
95
// type of NULL (which is int).
97
// 2. In MSVC, streaming a null char pointer to StrStream generates
98
// an access violation, so we need to convert NULL to "(null)"
99
// before streaming it.
100
static inline const char* ShowCString(const char* c_str) {
101
return c_str ? c_str : "(null)";
104
// Returns the input enclosed in double quotes if it's not NULL;
105
// otherwise returns "(null)". For example, "\"Hello\"" is returned
106
// for input "Hello".
108
// This is useful for printing a C string in the syntax of a literal.
110
// Known issue: escape sequences are not handled yet.
111
static String ShowCStringQuoted(const char* c_str);
113
// Clones a 0-terminated C string, allocating memory using new. The
114
// caller is responsible for deleting the return value using
115
// delete[]. Returns the cloned string, or NULL if the input is
118
// This is different from strdup() in string.h, which allocates
119
// memory using malloc().
120
static const char* CloneCString(const char* c_str);
122
// Compares two C strings. Returns true iff they have the same content.
124
// Unlike strcmp(), this function can handle NULL argument(s). A
125
// NULL C string is considered different to any non-NULL C string,
126
// including the empty string.
127
static bool CStringEquals(const char* lhs, const char* rhs);
129
// Converts a wide C string to a String using the UTF-8 encoding.
130
// NULL will be converted to "(null)". If an error occurred during
131
// the conversion, "(failed to convert from wide string)" is
133
static String ShowWideCString(const wchar_t* wide_c_str);
135
// Similar to ShowWideCString(), except that this function encloses
136
// the converted string in double quotes.
137
static String ShowWideCStringQuoted(const wchar_t* wide_c_str);
139
// Compares two wide C strings. Returns true iff they have the same
142
// Unlike wcscmp(), this function can handle NULL argument(s). A
143
// NULL C string is considered different to any non-NULL C string,
144
// including the empty string.
145
static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs);
147
// Compares two C strings, ignoring case. Returns true iff they
148
// have the same content.
150
// Unlike strcasecmp(), this function can handle NULL argument(s).
151
// A NULL C string is considered different to any non-NULL C string,
152
// including the empty string.
153
static bool CaseInsensitiveCStringEquals(const char* lhs,
156
// Formats a list of arguments to a String, using the same format
157
// spec string as for printf.
159
// We do not use the StringPrintf class as it is not universally
162
// The result is limited to 4096 characters (including the tailing
163
// 0). If 4096 characters are not enough to format the input,
164
// "<buffer exceeded>" is returned.
165
static String Format(const char* format, ...);
169
// The default c'tor constructs a NULL string.
170
String() : c_str_(NULL) {}
172
// Constructs a String by cloning a 0-terminated C string.
173
String(const char* c_str) : c_str_(NULL) { // NOLINT
177
// Constructs a String by copying a given number of chars from a
178
// buffer. E.g. String("hello", 3) will create the string "hel".
179
String(const char* buffer, size_t len);
181
// The copy c'tor creates a new copy of the string. The two
182
// String objects do not share content.
183
String(const String& str) : c_str_(NULL) {
187
// D'tor. String is intended to be a final class, so the d'tor
188
// doesn't need to be virtual.
189
~String() { delete[] c_str_; }
191
// Returns true iff this is an empty string (i.e. "").
193
return (c_str_ != NULL) && (*c_str_ == '\0');
196
// Compares this with another String.
197
// Returns < 0 if this is less than rhs, 0 if this is equal to rhs, or > 0
198
// if this is greater than rhs.
199
int Compare(const String& rhs) const;
201
// Returns true iff this String equals the given C string. A NULL
202
// string and a non-NULL string are considered not equal.
203
bool operator==(const char* c_str) const {
204
return CStringEquals(c_str_, c_str);
207
// Returns true iff this String doesn't equal the given C string. A NULL
208
// string and a non-NULL string are considered not equal.
209
bool operator!=(const char* c_str) const {
210
return !CStringEquals(c_str_, c_str);
213
// Returns true iff this String ends with the given suffix. *Any*
214
// String is considered to end with a NULL or empty suffix.
215
bool EndsWith(const char* suffix) const;
217
// Returns true iff this String ends with the given suffix, not considering
218
// case. Any String is considered to end with a NULL or empty suffix.
219
bool EndsWithCaseInsensitive(const char* suffix) const;
221
// Returns the length of the encapsulated string, or -1 if the
223
int GetLength() const {
224
return c_str_ ? static_cast<int>(strlen(c_str_)) : -1;
227
// Gets the 0-terminated C string this String object represents.
228
// The String object still owns the string. Therefore the caller
229
// should NOT delete the return value.
230
const char* c_str() const { return c_str_; }
232
// Sets the 0-terminated C string this String object represents.
233
// The old string in this object is deleted, and this object will
234
// own a clone of the input string. This function copies only up to
235
// length bytes (plus a terminating null byte), or until the first
236
// null byte, whichever comes first.
238
// This function works even when the c_str parameter has the same
239
// value as that of the c_str_ field.
240
void Set(const char* c_str, size_t length);
242
// Assigns a C string to this object. Self-assignment works.
243
const String& operator=(const char* c_str);
245
// Assigns a String object to this object. Self-assignment works.
246
const String& operator=(const String &rhs) {
255
// Streams a String to an ostream.
256
inline ::std::ostream& operator <<(::std::ostream& os, const String& str) {
257
// We call String::ShowCString() to convert NULL to "(null)".
258
// Otherwise we'll get an access violation on Windows.
259
return os << String::ShowCString(str.c_str());
262
// Gets the content of the StrStream's buffer as a String. Each '\0'
263
// character in the buffer is replaced with "\\0".
264
String StrStreamToString(StrStream* stream);
266
// Converts a streamable value to a String. A NULL pointer is
267
// converted to "(null)". When the input value is a ::string,
268
// ::std::string, ::wstring, or ::std::wstring object, each NUL
269
// character in it is replaced with "\\0".
271
// Declared here but defined in gtest.h, so that it has access
272
// to the definition of the Message class, required by the ARM
274
template <typename T>
275
String StreamableToString(const T& streamable);
277
} // namespace internal
278
} // namespace testing
280
#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_