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