13d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Copyright 2005, Google Inc. 23d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// All rights reserved. 33d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 43d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Redistribution and use in source and binary forms, with or without 53d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// modification, are permitted provided that the following conditions are 63d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// met: 73d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 83d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// * Redistributions of source code must retain the above copyright 93d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// notice, this list of conditions and the following disclaimer. 103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// * Redistributions in binary form must reproduce the above 113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// copyright notice, this list of conditions and the following disclaimer 123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// in the documentation and/or other materials provided with the 133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// distribution. 143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// * Neither the name of Google Inc. nor the names of its 153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// contributors may be used to endorse or promote products derived from 163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// this software without specific prior written permission. 173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee) 313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The Google C++ Testing Framework (Google Test) 333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This header file declares the String class and functions used internally by 353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Google Test. They are subject to change without notice. They should not used 363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// by code external to Google Test. 373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This header file is #included by <gtest/internal/gtest-internal.h>. 393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// It should not be #included by other files. 403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#ifdef __BORLANDC__ 453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// string.h is not guaranteed to provide strcpy on C++ Builder. 463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# include <mem.h> 473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif 483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include <string.h> 503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include "gtest/internal/gtest-port.h" 513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include <string> 533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicknamespace testing { 553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicknamespace internal { 563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// String - a UTF-8 string class. 583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// For historic reasons, we don't use std::string. 603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// TODO(wan@google.com): replace this class with std::string or 623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// implement it in terms of the latter. 633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Note that String can represent both NULL and the empty string, 653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// while std::string cannot represent NULL. 663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// NULL and the empty string are considered different. NULL is less 683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// than anything (including the empty string) except itself. 693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This class only provides minimum functionality necessary for 713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// implementing Google Test. We do not intend to implement a full-fledged 723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// string class here. 733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Since the purpose of this class is to provide a substitute for 753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// std::string on platforms where it cannot be used, we define a copy 763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// constructor and assignment operators such that we don't need 773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// conditional compilation in a lot of places. 783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// 793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// In order to make the representation efficient, the d'tor of String 803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// is not virtual. Therefore DO NOT INHERIT FROM String. 813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass GTEST_API_ String { 823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public: 833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Static utility methods 843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Returns the input enclosed in double quotes if it's not NULL; 863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // otherwise returns "(null)". For example, "\"Hello\"" is returned 873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // for input "Hello". 883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // 893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // This is useful for printing a C string in the syntax of a literal. 903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // 913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Known issue: escape sequences are not handled yet. 923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick static String ShowCStringQuoted(const char* c_str); 933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Clones a 0-terminated C string, allocating memory using new. The 953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // caller is responsible for deleting the return value using 963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // delete[]. Returns the cloned string, or NULL if the input is 973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // NULL. 983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // 993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // This is different from strdup() in string.h, which allocates 1003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // memory using malloc(). 1013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick static const char* CloneCString(const char* c_str); 1023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 1033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if GTEST_OS_WINDOWS_MOBILE 1043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be 1053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // able to pass strings to Win32 APIs on CE we need to convert them 1063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // to 'Unicode', UTF-16. 1073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 1083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Creates a UTF-16 wide string from the given ANSI string, allocating 1093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // memory using new. The caller is responsible for deleting the return 1103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // value using delete[]. Returns the wide string, or NULL if the 1113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // input is NULL. 1123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // 1133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // The wide string is created using the ANSI codepage (CP_ACP) to 1143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // match the behaviour of the ANSI versions of Win32 calls and the 1153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // C runtime. 1163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick static LPCWSTR AnsiToUtf16(const char* c_str); 1173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 1183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Creates an ANSI string from the given wide string, allocating 1193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // memory using new. The caller is responsible for deleting the return 1203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // value using delete[]. Returns the ANSI string, or NULL if the 1213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // input is NULL. 1223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // 1233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // The returned string is created using the ANSI codepage (CP_ACP) to 1243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // match the behaviour of the ANSI versions of Win32 calls and the 1253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // C runtime. 1263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick static const char* Utf16ToAnsi(LPCWSTR utf16_str); 1273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif 1283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 1293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Compares two C strings. Returns true iff they have the same content. 1303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // 1313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Unlike strcmp(), this function can handle NULL argument(s). A 1323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // NULL C string is considered different to any non-NULL C string, 1333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // including the empty string. 1343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick static bool CStringEquals(const char* lhs, const char* rhs); 1353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 1363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Converts a wide C string to a String using the UTF-8 encoding. 1373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // NULL will be converted to "(null)". If an error occurred during 1383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // the conversion, "(failed to convert from wide string)" is 1393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // returned. 1403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick static String ShowWideCString(const wchar_t* wide_c_str); 1413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 1423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Similar to ShowWideCString(), except that this function encloses 1433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // the converted string in double quotes. 1443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick static String ShowWideCStringQuoted(const wchar_t* wide_c_str); 1453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 1463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Compares two wide C strings. Returns true iff they have the same 1473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // content. 1483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // 1493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Unlike wcscmp(), this function can handle NULL argument(s). A 1503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // NULL C string is considered different to any non-NULL C string, 1513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // including the empty string. 1523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs); 1533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 1543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Compares two C strings, ignoring case. Returns true iff they 1553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // have the same content. 1563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // 1573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Unlike strcasecmp(), this function can handle NULL argument(s). 1583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // A NULL C string is considered different to any non-NULL C string, 1593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // including the empty string. 1603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick static bool CaseInsensitiveCStringEquals(const char* lhs, 1613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick const char* rhs); 1623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 1633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Compares two wide C strings, ignoring case. Returns true iff they 1643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // have the same content. 1653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // 1663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Unlike wcscasecmp(), this function can handle NULL argument(s). 1673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // A NULL C string is considered different to any non-NULL wide C string, 1683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // including the empty string. 1693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // NB: The implementations on different platforms slightly differ. 1703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // On windows, this method uses _wcsicmp which compares according to LC_CTYPE 1713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // environment variable. On GNU platform this method uses wcscasecmp 1723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // which compares according to LC_CTYPE category of the current locale. 1733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the 1743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // current locale. 1753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs, 1763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick const wchar_t* rhs); 1773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 1783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Formats a list of arguments to a String, using the same format 1793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // spec string as for printf. 1803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // 1813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // We do not use the StringPrintf class as it is not universally 1823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // available. 1833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // 1843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // The result is limited to 4096 characters (including the tailing 1853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // 0). If 4096 characters are not enough to format the input, 1863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // "<buffer exceeded>" is returned. 1873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick static String Format(const char* format, ...); 1883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 1893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // C'tors 1903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 1913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // The default c'tor constructs a NULL string. 1923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick String() : c_str_(NULL), length_(0) {} 1933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 1943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Constructs a String by cloning a 0-terminated C string. 1953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick String(const char* a_c_str) { // NOLINT 1963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick if (a_c_str == NULL) { 1973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick c_str_ = NULL; 1983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick length_ = 0; 1993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } else { 2003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick ConstructNonNull(a_c_str, strlen(a_c_str)); 2013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } 2023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } 2033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Constructs a String by copying a given number of chars from a 2053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // buffer. E.g. String("hello", 3) creates the string "hel", 2063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // String("a\0bcd", 4) creates "a\0bc", String(NULL, 0) creates "", 2073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // and String(NULL, 1) results in access violation. 2083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick String(const char* buffer, size_t a_length) { 2093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick ConstructNonNull(buffer, a_length); 2103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } 2113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // The copy c'tor creates a new copy of the string. The two 2133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // String objects do not share content. 2143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick String(const String& str) : c_str_(NULL), length_(0) { *this = str; } 2153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // D'tor. String is intended to be a final class, so the d'tor 2173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // doesn't need to be virtual. 2183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick ~String() { delete[] c_str_; } 2193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Allows a String to be implicitly converted to an ::std::string or 2213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // ::string, and vice versa. Converting a String containing a NULL 2223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // pointer to ::std::string or ::string is undefined behavior. 2233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Converting a ::std::string or ::string containing an embedded NUL 2243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // character to a String will result in the prefix up to the first 2253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // NUL character. 2263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick String(const ::std::string& str) { 2273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick ConstructNonNull(str.c_str(), str.length()); 2283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } 2293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick operator ::std::string() const { return ::std::string(c_str(), length()); } 2313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if GTEST_HAS_GLOBAL_STRING 2333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick String(const ::string& str) { 2343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick ConstructNonNull(str.c_str(), str.length()); 2353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } 2363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick operator ::string() const { return ::string(c_str(), length()); } 2383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif // GTEST_HAS_GLOBAL_STRING 2393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Returns true iff this is an empty string (i.e. ""). 2413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick bool empty() const { return (c_str() != NULL) && (length() == 0); } 2423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Compares this with another String. 2443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Returns < 0 if this is less than rhs, 0 if this is equal to rhs, or > 0 2453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // if this is greater than rhs. 2463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick int Compare(const String& rhs) const; 2473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Returns true iff this String equals the given C string. A NULL 2493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // string and a non-NULL string are considered not equal. 2503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick bool operator==(const char* a_c_str) const { return Compare(a_c_str) == 0; } 2513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Returns true iff this String is less than the given String. A 2533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // NULL string is considered less than "". 2543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick bool operator<(const String& rhs) const { return Compare(rhs) < 0; } 2553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Returns true iff this String doesn't equal the given C string. A NULL 2573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // string and a non-NULL string are considered not equal. 2583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick bool operator!=(const char* a_c_str) const { return !(*this == a_c_str); } 2593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Returns true iff this String ends with the given suffix. *Any* 2613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // String is considered to end with a NULL or empty suffix. 2623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick bool EndsWith(const char* suffix) const; 2633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Returns true iff this String ends with the given suffix, not considering 2653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // case. Any String is considered to end with a NULL or empty suffix. 2663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick bool EndsWithCaseInsensitive(const char* suffix) const; 2673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Returns the length of the encapsulated string, or 0 if the 2693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // string is NULL. 2703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick size_t length() const { return length_; } 2713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Gets the 0-terminated C string this String object represents. 2733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // The String object still owns the string. Therefore the caller 2743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // should NOT delete the return value. 2753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick const char* c_str() const { return c_str_; } 2763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Assigns a C string to this object. Self-assignment works. 2783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick const String& operator=(const char* a_c_str) { 2793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick return *this = String(a_c_str); 2803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } 2813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Assigns a String object to this object. Self-assignment works. 2833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick const String& operator=(const String& rhs) { 2843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick if (this != &rhs) { 2853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick delete[] c_str_; 2863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick if (rhs.c_str() == NULL) { 2873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick c_str_ = NULL; 2883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick length_ = 0; 2893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } else { 2903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick ConstructNonNull(rhs.c_str(), rhs.length()); 2913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } 2923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } 2933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick return *this; 2953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } 2963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 2973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick private: 2983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // Constructs a non-NULL String from the given content. This 2993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // function can only be called when c_str_ has not been allocated. 3003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // ConstructNonNull(NULL, 0) results in an empty string (""). 3013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick // ConstructNonNull(NULL, non_zero) is undefined behavior. 3023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick void ConstructNonNull(const char* buffer, size_t a_length) { 3033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick char* const str = new char[a_length + 1]; 3043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick memcpy(str, buffer, a_length); 3053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick str[a_length] = '\0'; 3063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick c_str_ = str; 3073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick length_ = a_length; 3083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } 3093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 3103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick const char* c_str_; 3113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick size_t length_; 3123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick}; // class String 3133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 3143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Streams a String to an ostream. Each '\0' character in the String 3153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// is replaced with "\\0". 3163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickinline ::std::ostream& operator<<(::std::ostream& os, const String& str) { 3173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick if (str.c_str() == NULL) { 3183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick os << "(null)"; 3193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } else { 3203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick const char* const c_str = str.c_str(); 3213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick for (size_t i = 0; i != str.length(); i++) { 3223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick if (c_str[i] == '\0') { 3233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick os << "\\0"; 3243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } else { 3253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick os << c_str[i]; 3263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } 3273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } 3283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick } 3293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick return os; 3303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick} 3313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 3323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Gets the content of the stringstream's buffer as a String. Each '\0' 3333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// character in the buffer is replaced with "\\0". 3343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ String StringStreamToString(::std::stringstream* stream); 3353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 3363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Converts a streamable value to a String. A NULL pointer is 3373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// converted to "(null)". When the input value is a ::string, 3383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// ::std::string, ::wstring, or ::std::wstring object, each NUL 3393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// character in it is replaced with "\\0". 3403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 3413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Declared here but defined in gtest.h, so that it has access 3423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// to the definition of the Message class, required by the ARM 3433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// compiler. 3443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicktemplate <typename T> 3453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickString StreamableToString(const T& streamable); 3463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 3473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick} // namespace internal 3483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick} // namespace testing 3493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick 3503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 351