1c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Copyright 2005, Google Inc. 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// All rights reserved. 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Redistribution and use in source and binary forms, with or without 5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// modification, are permitted provided that the following conditions are 6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// met: 7c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// * Redistributions of source code must retain the above copyright 9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// notice, this list of conditions and the following disclaimer. 10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// * Redistributions in binary form must reproduce the above 11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// copyright notice, this list of conditions and the following disclaimer 12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// in the documentation and/or other materials provided with the 13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// distribution. 14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// * Neither the name of Google Inc. nor the names of its 15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// contributors may be used to endorse or promote products derived from 16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// this software without specific prior written permission. 17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee) 31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The Google C++ Testing Framework (Google Test) 33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This header file declares the String class and functions used internally by 35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Google Test. They are subject to change without notice. They should not used 36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// by code external to Google Test. 37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This header file is #included by testing/base/internal/gtest-internal.h. 39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// It should not be #included by other files. 40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <string.h> 45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <gtest/internal/gtest-port.h> 46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace testing { 48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace internal { 49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// String - a UTF-8 string class. 51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// We cannot use std::string as Microsoft's STL implementation in 53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Visual C++ 7.1 has problems when exception is disabled. There is a 54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// hack to work around this, but we've seen cases where the hack fails 55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// to work. 56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Also, String is different from std::string in that it can represent 58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// both NULL and the empty string, while std::string cannot represent 59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// NULL. 60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// NULL and the empty string are considered different. NULL is less 62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// than anything (including the empty string) except itself. 63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 64c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This class only provides minimum functionality necessary for 65c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// implementing Google Test. We do not intend to implement a full-fledged 66c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// string class here. 67c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 68c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Since the purpose of this class is to provide a substitute for 69c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// std::string on platforms where it cannot be used, we define a copy 70c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// constructor and assignment operators such that we don't need 71c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// conditional compilation in a lot of places. 72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// In order to make the representation efficient, the d'tor of String 74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// is not virtual. Therefore DO NOT INHERIT FROM String. 75c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass String { 76c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public: 77c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Static utility methods 78c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 79c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns the input if it's not NULL, otherwise returns "(null)". 80c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // This function serves two purposes: 81c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 82c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 1. ShowCString(NULL) has type 'const char *', instead of the 83c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // type of NULL (which is int). 84c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 85c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 2. In MSVC, streaming a null char pointer to StrStream generates 86c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // an access violation, so we need to convert NULL to "(null)" 87c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // before streaming it. 88c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static inline const char* ShowCString(const char* c_str) { 89c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return c_str ? c_str : "(null)"; 90c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 91c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 92c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns the input enclosed in double quotes if it's not NULL; 93c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // otherwise returns "(null)". For example, "\"Hello\"" is returned 94c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // for input "Hello". 95c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 96c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // This is useful for printing a C string in the syntax of a literal. 97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Known issue: escape sequences are not handled yet. 99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static String ShowCStringQuoted(const char* c_str); 100c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 101c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Clones a 0-terminated C string, allocating memory using new. The 102c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // caller is responsible for deleting the return value using 103c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // delete[]. Returns the cloned string, or NULL if the input is 104c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // NULL. 105c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 106c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // This is different from strdup() in string.h, which allocates 107c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // memory using malloc(). 108c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static const char* CloneCString(const char* c_str); 109c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 110c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Compares two C strings. Returns true iff they have the same content. 111c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 112c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Unlike strcmp(), this function can handle NULL argument(s). A 113c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // NULL C string is considered different to any non-NULL C string, 114c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // including the empty string. 115c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static bool CStringEquals(const char* lhs, const char* rhs); 116c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 117c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Converts a wide C string to a String using the UTF-8 encoding. 118c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // NULL will be converted to "(null)". If an error occurred during 119c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // the conversion, "(failed to convert from wide string)" is 120c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // returned. 121c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static String ShowWideCString(const wchar_t* wide_c_str); 122c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 123c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Similar to ShowWideCString(), except that this function encloses 124c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // the converted string in double quotes. 125c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static String ShowWideCStringQuoted(const wchar_t* wide_c_str); 126c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 127c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Compares two wide C strings. Returns true iff they have the same 128c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // content. 129c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 130c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Unlike wcscmp(), this function can handle NULL argument(s). A 131c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // NULL C string is considered different to any non-NULL C string, 132c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // including the empty string. 133c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs); 134c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 135c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Compares two C strings, ignoring case. Returns true iff they 136c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // have the same content. 137c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 138c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Unlike strcasecmp(), this function can handle NULL argument(s). 139c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // A NULL C string is considered different to any non-NULL C string, 140c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // including the empty string. 141c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static bool CaseInsensitiveCStringEquals(const char* lhs, 142c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const char* rhs); 143c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 144c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Formats a list of arguments to a String, using the same format 145c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // spec string as for printf. 146c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 147c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // We do not use the StringPrintf class as it is not universally 148c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // available. 149c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 150c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // The result is limited to 4096 characters (including the tailing 151c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 0). If 4096 characters are not enough to format the input, 152c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // "<buffer exceeded>" is returned. 153c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott static String Format(const char* format, ...); 154c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 155c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // C'tors 156c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 157c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // The default c'tor constructs a NULL string. 158c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott String() : c_str_(NULL) {} 159c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 160c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Constructs a String by cloning a 0-terminated C string. 161c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott String(const char* c_str) : c_str_(NULL) { // NOLINT 162c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott *this = c_str; 163c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 164c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 165c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Constructs a String by copying a given number of chars from a 166c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // buffer. E.g. String("hello", 3) will create the string "hel". 167c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott String(const char* buffer, size_t len); 168c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 169c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // The copy c'tor creates a new copy of the string. The two 170c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // String objects do not share content. 171c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott String(const String& str) : c_str_(NULL) { 172c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott *this = str; 173c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 174c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 175c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // D'tor. String is intended to be a final class, so the d'tor 176c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // doesn't need to be virtual. 177c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ~String() { delete[] c_str_; } 178c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 179c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns true iff this is an empty string (i.e. ""). 180c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool empty() const { 181c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return (c_str_ != NULL) && (*c_str_ == '\0'); 182c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 183c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 184c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Compares this with another String. 185c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns < 0 if this is less than rhs, 0 if this is equal to rhs, or > 0 186c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // if this is greater than rhs. 187c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott int Compare(const String& rhs) const; 188c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 189c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns true iff this String equals the given C string. A NULL 190c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // string and a non-NULL string are considered not equal. 191c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool operator==(const char* c_str) const { 192c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return CStringEquals(c_str_, c_str); 193c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 194c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 195c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns true iff this String doesn't equal the given C string. A NULL 196c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // string and a non-NULL string are considered not equal. 197c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool operator!=(const char* c_str) const { 198c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return !CStringEquals(c_str_, c_str); 199c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 200c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 201c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns true iff this String ends with the given suffix. *Any* 202c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // String is considered to end with a NULL or empty suffix. 203c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool EndsWith(const char* suffix) const; 204c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 205c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns true iff this String ends with the given suffix, not considering 206c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // case. Any String is considered to end with a NULL or empty suffix. 207c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott bool EndsWithCaseInsensitive(const char* suffix) const; 208c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 209c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Returns the length of the encapsulated string, or -1 if the 210c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // string is NULL. 211c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott int GetLength() const { 212c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return c_str_ ? static_cast<int>(strlen(c_str_)) : -1; 213c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 214c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 215c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Gets the 0-terminated C string this String object represents. 216c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // The String object still owns the string. Therefore the caller 217c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // should NOT delete the return value. 218c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const char* c_str() const { return c_str_; } 219c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 220c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Sets the 0-terminated C string this String object represents. 221c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // The old string in this object is deleted, and this object will 222c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // own a clone of the input string. This function copies only up to 223c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // length bytes (plus a terminating null byte), or until the first 224c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // null byte, whichever comes first. 225c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // 226c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // This function works even when the c_str parameter has the same 227c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // value as that of the c_str_ field. 228c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott void Set(const char* c_str, size_t length); 229c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 230c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Assigns a C string to this object. Self-assignment works. 231c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const String& operator=(const char* c_str); 232c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 233c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Assigns a String object to this object. Self-assignment works. 234c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const String& operator=(const String &rhs) { 235c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott *this = rhs.c_str_; 236c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return *this; 237c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 238c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 239c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private: 240c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const char* c_str_; 241c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}; 242c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 243c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Streams a String to an ostream. 244c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottinline ::std::ostream& operator <<(::std::ostream& os, const String& str) { 245c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // We call String::ShowCString() to convert NULL to "(null)". 246c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Otherwise we'll get an access violation on Windows. 247c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott return os << String::ShowCString(str.c_str()); 248c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 249c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 250c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Gets the content of the StrStream's buffer as a String. Each '\0' 251c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// character in the buffer is replaced with "\\0". 252c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottString StrStreamToString(StrStream* stream); 253c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 254c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Converts a streamable value to a String. A NULL pointer is 255c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// converted to "(null)". When the input value is a ::string, 256c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// ::std::string, ::wstring, or ::std::wstring object, each NUL 257c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// character in it is replaced with "\\0". 258c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 259c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Declared here but defined in gtest.h, so that it has access 260c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// to the definition of the Message class, required by the ARM 261c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// compiler. 262c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttemplate <typename T> 263c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottString StreamableToString(const T& streamable); 264c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 265c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} // namespace internal 266c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} // namespace testing 267c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 268c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 269