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