1e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// Copyright 2005, Google Inc.
2e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// All rights reserved.
3e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
4e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// Redistribution and use in source and binary forms, with or without
5e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// modification, are permitted provided that the following conditions are
6e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// met:
7e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
8e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//     * Redistributions of source code must retain the above copyright
9e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// notice, this list of conditions and the following disclaimer.
10e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//     * Redistributions in binary form must reproduce the above
11e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// copyright notice, this list of conditions and the following disclaimer
12e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// in the documentation and/or other materials provided with the
13e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// distribution.
14e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//     * Neither the name of Google Inc. nor the names of its
15e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// contributors may be used to endorse or promote products derived from
16e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// this software without specific prior written permission.
17e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
18e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
30e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
31e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
32e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// The Google C++ Testing Framework (Google Test)
33e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
34e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// This header file declares the String class and functions used internally by
35e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// Google Test.  They are subject to change without notice. They should not used
36e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// by code external to Google Test.
37e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
38e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// This header file is #included by <gtest/internal/gtest-internal.h>.
39e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// It should not be #included by other files.
40e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
41e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
42e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
43e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
44e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen#ifdef __BORLANDC__
45e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// string.h is not guaranteed to provide strcpy on C++ Builder.
46e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen#include <mem.h>
47e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen#endif
48e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
49e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen#include <string.h>
50e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen#include <gtest/internal/gtest-port.h>
51e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
52e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen#include <string>
53e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
54e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chennamespace testing {
55e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chennamespace internal {
56e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
57e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// String - a UTF-8 string class.
58e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
59e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// For historic reasons, we don't use std::string.
60e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
61e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// TODO(wan@google.com): replace this class with std::string or
62e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// implement it in terms of the latter.
63e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
64e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// Note that String can represent both NULL and the empty string,
65e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// while std::string cannot represent NULL.
66e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
67e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// NULL and the empty string are considered different.  NULL is less
68e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// than anything (including the empty string) except itself.
69e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
70e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// This class only provides minimum functionality necessary for
71e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// implementing Google Test.  We do not intend to implement a full-fledged
72e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// string class here.
73e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
74e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// Since the purpose of this class is to provide a substitute for
75e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// std::string on platforms where it cannot be used, we define a copy
76e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// constructor and assignment operators such that we don't need
77e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// conditional compilation in a lot of places.
78e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen//
79e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// In order to make the representation efficient, the d'tor of String
80e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen// is not virtual.  Therefore DO NOT INHERIT FROM String.
81e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chenclass GTEST_API_ String {
82e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen public:
83e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Static utility methods
84e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
85e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Returns the input enclosed in double quotes if it's not NULL;
86e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // otherwise returns "(null)".  For example, "\"Hello\"" is returned
87e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // for input "Hello".
88e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  //
89e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // This is useful for printing a C string in the syntax of a literal.
90e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  //
91e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Known issue: escape sequences are not handled yet.
92e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  static String ShowCStringQuoted(const char* c_str);
93e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
94e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Clones a 0-terminated C string, allocating memory using new.  The
95e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // caller is responsible for deleting the return value using
96e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // delete[].  Returns the cloned string, or NULL if the input is
97e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // NULL.
98e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  //
99e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // This is different from strdup() in string.h, which allocates
100e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // memory using malloc().
101e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  static const char* CloneCString(const char* c_str);
102e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
103e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen#if GTEST_OS_WINDOWS_MOBILE
104e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be
105e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // able to pass strings to Win32 APIs on CE we need to convert them
106e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // to 'Unicode', UTF-16.
107e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
108e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Creates a UTF-16 wide string from the given ANSI string, allocating
109e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // memory using new. The caller is responsible for deleting the return
110e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // value using delete[]. Returns the wide string, or NULL if the
111e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // input is NULL.
112e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  //
113e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // The wide string is created using the ANSI codepage (CP_ACP) to
114e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // match the behaviour of the ANSI versions of Win32 calls and the
115e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // C runtime.
116e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  static LPCWSTR AnsiToUtf16(const char* c_str);
117e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
118e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Creates an ANSI string from the given wide string, allocating
119e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // memory using new. The caller is responsible for deleting the return
120e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // value using delete[]. Returns the ANSI string, or NULL if the
121e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // input is NULL.
122e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  //
123e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // The returned string is created using the ANSI codepage (CP_ACP) to
124e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // match the behaviour of the ANSI versions of Win32 calls and the
125e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // C runtime.
126e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  static const char* Utf16ToAnsi(LPCWSTR utf16_str);
127e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen#endif
128e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
129e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Compares two C strings.  Returns true iff they have the same content.
130e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  //
131e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Unlike strcmp(), this function can handle NULL argument(s).  A
132e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // NULL C string is considered different to any non-NULL C string,
133e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // including the empty string.
134e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  static bool CStringEquals(const char* lhs, const char* rhs);
135e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
136e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Converts a wide C string to a String using the UTF-8 encoding.
137e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // NULL will be converted to "(null)".  If an error occurred during
138e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // the conversion, "(failed to convert from wide string)" is
139e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // returned.
140e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  static String ShowWideCString(const wchar_t* wide_c_str);
141e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
142e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Similar to ShowWideCString(), except that this function encloses
143e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // the converted string in double quotes.
144e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  static String ShowWideCStringQuoted(const wchar_t* wide_c_str);
145e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
146e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Compares two wide C strings.  Returns true iff they have the same
147e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // content.
148e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  //
149e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Unlike wcscmp(), this function can handle NULL argument(s).  A
150e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // NULL C string is considered different to any non-NULL C string,
151e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // including the empty string.
152e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs);
153e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
154e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Compares two C strings, ignoring case.  Returns true iff they
155e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // have the same content.
156e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  //
157e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Unlike strcasecmp(), this function can handle NULL argument(s).
158e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // A NULL C string is considered different to any non-NULL C string,
159e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // including the empty string.
160e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  static bool CaseInsensitiveCStringEquals(const char* lhs,
161e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen                                           const char* rhs);
162e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
163e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Compares two wide C strings, ignoring case.  Returns true iff they
164e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // have the same content.
165e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  //
166e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Unlike wcscasecmp(), this function can handle NULL argument(s).
167e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // A NULL C string is considered different to any non-NULL wide C string,
168e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // including the empty string.
169e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // NB: The implementations on different platforms slightly differ.
170e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // On windows, this method uses _wcsicmp which compares according to LC_CTYPE
171e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // environment variable. On GNU platform this method uses wcscasecmp
172e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // which compares according to LC_CTYPE category of the current locale.
173e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the
174e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // current locale.
175e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs,
176e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen                                               const wchar_t* rhs);
177e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
178e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Formats a list of arguments to a String, using the same format
179e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // spec string as for printf.
180e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  //
181e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // We do not use the StringPrintf class as it is not universally
182e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // available.
183e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  //
184e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // The result is limited to 4096 characters (including the tailing
185e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // 0).  If 4096 characters are not enough to format the input,
186e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // "<buffer exceeded>" is returned.
187e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  static String Format(const char* format, ...);
188e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
189e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // C'tors
190e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
191e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // The default c'tor constructs a NULL string.
192e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  String() : c_str_(NULL), length_(0) {}
193e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
194e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Constructs a String by cloning a 0-terminated C string.
195e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  String(const char* a_c_str) {  // NOLINT
196e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen    if (a_c_str == NULL) {
197e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen      c_str_ = NULL;
198e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen      length_ = 0;
199e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen    } else {
200e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen      ConstructNonNull(a_c_str, strlen(a_c_str));
201e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen    }
202e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  }
203e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
204e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Constructs a String by copying a given number of chars from a
205e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // buffer.  E.g. String("hello", 3) creates the string "hel",
206e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // String("a\0bcd", 4) creates "a\0bc", String(NULL, 0) creates "",
207e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // and String(NULL, 1) results in access violation.
208e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  String(const char* buffer, size_t a_length) {
209e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen    ConstructNonNull(buffer, a_length);
210e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  }
211e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
212e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // The copy c'tor creates a new copy of the string.  The two
213e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // String objects do not share content.
214e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  String(const String& str) : c_str_(NULL), length_(0) { *this = str; }
215e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
216e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // D'tor.  String is intended to be a final class, so the d'tor
217e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // doesn't need to be virtual.
218e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  ~String() { delete[] c_str_; }
219e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
220e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Allows a String to be implicitly converted to an ::std::string or
221e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // ::string, and vice versa.  Converting a String containing a NULL
222e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // pointer to ::std::string or ::string is undefined behavior.
223e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Converting a ::std::string or ::string containing an embedded NUL
224e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // character to a String will result in the prefix up to the first
225e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // NUL character.
226e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  String(const ::std::string& str) {
227e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen    ConstructNonNull(str.c_str(), str.length());
228e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  }
229e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
230e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  operator ::std::string() const { return ::std::string(c_str(), length()); }
231e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
232e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen#if GTEST_HAS_GLOBAL_STRING
233e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  String(const ::string& str) {
234e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen    ConstructNonNull(str.c_str(), str.length());
235e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  }
236e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
237e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  operator ::string() const { return ::string(c_str(), length()); }
238e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen#endif  // GTEST_HAS_GLOBAL_STRING
239e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
240e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Returns true iff this is an empty string (i.e. "").
241e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  bool empty() const { return (c_str() != NULL) && (length() == 0); }
242e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
243e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Compares this with another String.
244e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Returns < 0 if this is less than rhs, 0 if this is equal to rhs, or > 0
245e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // if this is greater than rhs.
246e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  int Compare(const String& rhs) const;
247e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
248e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Returns true iff this String equals the given C string.  A NULL
249e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // string and a non-NULL string are considered not equal.
250e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  bool operator==(const char* a_c_str) const { return Compare(a_c_str) == 0; }
251e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
252e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Returns true iff this String is less than the given String.  A
253e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // NULL string is considered less than "".
254e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  bool operator<(const String& rhs) const { return Compare(rhs) < 0; }
255e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
256e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Returns true iff this String doesn't equal the given C string.  A NULL
257e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // string and a non-NULL string are considered not equal.
258e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  bool operator!=(const char* a_c_str) const { return !(*this == a_c_str); }
259e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen
260e295e32b68cf04f0d99138bf4a6d25555f3aef99Wei-Ta Chen  // Returns true iff this String ends with the given suffix.  *Any*
261  // String is considered to end with a NULL or empty suffix.
262  bool EndsWith(const char* suffix) const;
263
264  // Returns true iff this String ends with the given suffix, not considering
265  // case. Any String is considered to end with a NULL or empty suffix.
266  bool EndsWithCaseInsensitive(const char* suffix) const;
267
268  // Returns the length of the encapsulated string, or 0 if the
269  // string is NULL.
270  size_t length() const { return length_; }
271
272  // Gets the 0-terminated C string this String object represents.
273  // The String object still owns the string.  Therefore the caller
274  // should NOT delete the return value.
275  const char* c_str() const { return c_str_; }
276
277  // Assigns a C string to this object.  Self-assignment works.
278  const String& operator=(const char* a_c_str) {
279    return *this = String(a_c_str);
280  }
281
282  // Assigns a String object to this object.  Self-assignment works.
283  const String& operator=(const String& rhs) {
284    if (this != &rhs) {
285      delete[] c_str_;
286      if (rhs.c_str() == NULL) {
287        c_str_ = NULL;
288        length_ = 0;
289      } else {
290        ConstructNonNull(rhs.c_str(), rhs.length());
291      }
292    }
293
294    return *this;
295  }
296
297 private:
298  // Constructs a non-NULL String from the given content.  This
299  // function can only be called when data_ has not been allocated.
300  // ConstructNonNull(NULL, 0) results in an empty string ("").
301  // ConstructNonNull(NULL, non_zero) is undefined behavior.
302  void ConstructNonNull(const char* buffer, size_t a_length) {
303    char* const str = new char[a_length + 1];
304    memcpy(str, buffer, a_length);
305    str[a_length] = '\0';
306    c_str_ = str;
307    length_ = a_length;
308  }
309
310  const char* c_str_;
311  size_t length_;
312};  // class String
313
314// Streams a String to an ostream.  Each '\0' character in the String
315// is replaced with "\\0".
316inline ::std::ostream& operator<<(::std::ostream& os, const String& str) {
317  if (str.c_str() == NULL) {
318    os << "(null)";
319  } else {
320    const char* const c_str = str.c_str();
321    for (size_t i = 0; i != str.length(); i++) {
322      if (c_str[i] == '\0') {
323        os << "\\0";
324      } else {
325        os << c_str[i];
326      }
327    }
328  }
329  return os;
330}
331
332// Gets the content of the StrStream's buffer as a String.  Each '\0'
333// character in the buffer is replaced with "\\0".
334GTEST_API_ String StrStreamToString(StrStream* stream);
335
336// Converts a streamable value to a String.  A NULL pointer is
337// converted to "(null)".  When the input value is a ::string,
338// ::std::string, ::wstring, or ::std::wstring object, each NUL
339// character in it is replaced with "\\0".
340
341// Declared here but defined in gtest.h, so that it has access
342// to the definition of the Message class, required by the ARM
343// compiler.
344template <typename T>
345String StreamableToString(const T& streamable);
346
347}  // namespace internal
348}  // namespace testing
349
350#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
351