11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2005, Google Inc. 21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All rights reserved. 31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Redistribution and use in source and binary forms, with or without 51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// modification, are permitted provided that the following conditions are 61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// met: 71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Redistributions of source code must retain the above copyright 91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// notice, this list of conditions and the following disclaimer. 101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Redistributions in binary form must reproduce the above 111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// copyright notice, this list of conditions and the following disclaimer 121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the documentation and/or other materials provided with the 131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// distribution. 141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Neither the name of Google Inc. nor the names of its 151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// contributors may be used to endorse or promote products derived from 161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this software without specific prior written permission. 171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee) 311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The Google C++ Testing Framework (Google Test) 331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This header file declares the String class and functions used internally by 351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test. They are subject to change without notice. They should not used 361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// by code external to Google Test. 371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This header file is #included by <gtest/internal/gtest-internal.h>. 391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// It should not be #included by other files. 401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#ifdef __BORLANDC__ 4541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// string.h is not guaranteed to provide strcpy on C++ Builder. 4641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <mem.h> 4741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 4841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <string.h> 5041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-port.h" 511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <string> 531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing { 551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// String - a UTF-8 string class. 581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 5941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// For historic reasons, we don't use std::string. 6041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 6141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// TODO(wan@google.com): replace this class with std::string or 6241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// implement it in terms of the latter. 631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 6441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Note that String can represent both NULL and the empty string, 6541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// while std::string cannot represent NULL. 661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// NULL and the empty string are considered different. NULL is less 681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// than anything (including the empty string) except itself. 691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class only provides minimum functionality necessary for 711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// implementing Google Test. We do not intend to implement a full-fledged 721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// string class here. 731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Since the purpose of this class is to provide a substitute for 751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// std::string on platforms where it cannot be used, we define a copy 761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// constructor and assignment operators such that we don't need 771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// conditional compilation in a lot of places. 781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In order to make the representation efficient, the d'tor of String 801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is not virtual. Therefore DO NOT INHERIT FROM String. 8141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ String { 821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Static utility methods 841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the input enclosed in double quotes if it's not NULL; 861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // otherwise returns "(null)". For example, "\"Hello\"" is returned 871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // for input "Hello". 881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This is useful for printing a C string in the syntax of a literal. 901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Known issue: escape sequences are not handled yet. 921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static String ShowCStringQuoted(const char* c_str); 931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Clones a 0-terminated C string, allocating memory using new. The 951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // caller is responsible for deleting the return value using 961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // delete[]. Returns the cloned string, or NULL if the input is 971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NULL. 981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This is different from strdup() in string.h, which allocates 1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // memory using malloc(). 1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const char* CloneCString(const char* c_str); 1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS_MOBILE 1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be 1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // able to pass strings to Win32 APIs on CE we need to convert them 1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // to 'Unicode', UTF-16. 1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Creates a UTF-16 wide string from the given ANSI string, allocating 1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // memory using new. The caller is responsible for deleting the return 1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // value using delete[]. Returns the wide string, or NULL if the 1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // input is NULL. 1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The wide string is created using the ANSI codepage (CP_ACP) to 1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // match the behaviour of the ANSI versions of Win32 calls and the 1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // C runtime. 1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static LPCWSTR AnsiToUtf16(const char* c_str); 1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Creates an ANSI string from the given wide string, allocating 1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // memory using new. The caller is responsible for deleting the return 1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // value using delete[]. Returns the ANSI string, or NULL if the 1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // input is NULL. 1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The returned string is created using the ANSI codepage (CP_ACP) to 1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // match the behaviour of the ANSI versions of Win32 calls and the 1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // C runtime. 1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const char* Utf16ToAnsi(LPCWSTR utf16_str); 1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif 1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Compares two C strings. Returns true iff they have the same content. 1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Unlike strcmp(), this function can handle NULL argument(s). A 1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NULL C string is considered different to any non-NULL C string, 1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // including the empty string. 1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool CStringEquals(const char* lhs, const char* rhs); 1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Converts a wide C string to a String using the UTF-8 encoding. 1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NULL will be converted to "(null)". If an error occurred during 1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the conversion, "(failed to convert from wide string)" is 1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // returned. 1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static String ShowWideCString(const wchar_t* wide_c_str); 1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Similar to ShowWideCString(), except that this function encloses 1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the converted string in double quotes. 1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static String ShowWideCStringQuoted(const wchar_t* wide_c_str); 1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Compares two wide C strings. Returns true iff they have the same 1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // content. 1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Unlike wcscmp(), this function can handle NULL argument(s). A 1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NULL C string is considered different to any non-NULL C string, 1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // including the empty string. 1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs); 1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Compares two C strings, ignoring case. Returns true iff they 1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // have the same content. 1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Unlike strcasecmp(), this function can handle NULL argument(s). 1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A NULL C string is considered different to any non-NULL C string, 1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // including the empty string. 1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool CaseInsensitiveCStringEquals(const char* lhs, 1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* rhs); 1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Compares two wide C strings, ignoring case. Returns true iff they 1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // have the same content. 1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Unlike wcscasecmp(), this function can handle NULL argument(s). 1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A NULL C string is considered different to any non-NULL wide C string, 1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // including the empty string. 1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NB: The implementations on different platforms slightly differ. 1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // On windows, this method uses _wcsicmp which compares according to LC_CTYPE 1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // environment variable. On GNU platform this method uses wcscasecmp 1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // which compares according to LC_CTYPE category of the current locale. 1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the 1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // current locale. 1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs, 1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const wchar_t* rhs); 1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Formats a list of arguments to a String, using the same format 1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // spec string as for printf. 1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We do not use the StringPrintf class as it is not universally 1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // available. 1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The result is limited to 4096 characters (including the tailing 1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 0). If 4096 characters are not enough to format the input, 1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // "<buffer exceeded>" is returned. 1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static String Format(const char* format, ...); 1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // C'tors 1901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The default c'tor constructs a NULL string. 19241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot String() : c_str_(NULL), length_(0) {} 1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Constructs a String by cloning a 0-terminated C string. 19541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot String(const char* a_c_str) { // NOLINT 19641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (a_c_str == NULL) { 19741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot c_str_ = NULL; 19841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot length_ = 0; 19941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else { 20041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ConstructNonNull(a_c_str, strlen(a_c_str)); 20141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 2031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Constructs a String by copying a given number of chars from a 20541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // buffer. E.g. String("hello", 3) creates the string "hel", 20641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // String("a\0bcd", 4) creates "a\0bc", String(NULL, 0) creates "", 20741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // and String(NULL, 1) results in access violation. 20841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot String(const char* buffer, size_t a_length) { 20941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ConstructNonNull(buffer, a_length); 21041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 2111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The copy c'tor creates a new copy of the string. The two 2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // String objects do not share content. 21441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot String(const String& str) : c_str_(NULL), length_(0) { *this = str; } 2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // D'tor. String is intended to be a final class, so the d'tor 2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // doesn't need to be virtual. 2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ~String() { delete[] c_str_; } 2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Allows a String to be implicitly converted to an ::std::string or 2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // ::string, and vice versa. Converting a String containing a NULL 2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // pointer to ::std::string or ::string is undefined behavior. 2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Converting a ::std::string or ::string containing an embedded NUL 2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // character to a String will result in the prefix up to the first 2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NUL character. 22641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot String(const ::std::string& str) { 22741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ConstructNonNull(str.c_str(), str.length()); 22841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot operator ::std::string() const { return ::std::string(c_str(), length()); } 2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_GLOBAL_STRING 23341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot String(const ::string& str) { 23441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ConstructNonNull(str.c_str(), str.length()); 23541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot operator ::string() const { return ::string(c_str(), length()); } 2381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_GLOBAL_STRING 2391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff this is an empty string (i.e. ""). 24141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool empty() const { return (c_str() != NULL) && (length() == 0); } 2421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Compares this with another String. 2441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns < 0 if this is less than rhs, 0 if this is equal to rhs, or > 0 2451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // if this is greater than rhs. 2461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int Compare(const String& rhs) const; 2471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff this String equals the given C string. A NULL 2491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // string and a non-NULL string are considered not equal. 25041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool operator==(const char* a_c_str) const { return Compare(a_c_str) == 0; } 2511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 25241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff this String is less than the given String. A 25341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // NULL string is considered less than "". 2541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool operator<(const String& rhs) const { return Compare(rhs) < 0; } 2551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff this String doesn't equal the given C string. A NULL 2571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // string and a non-NULL string are considered not equal. 25841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool operator!=(const char* a_c_str) const { return !(*this == a_c_str); } 2591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff this String ends with the given suffix. *Any* 2611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // String is considered to end with a NULL or empty suffix. 2621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool EndsWith(const char* suffix) const; 2631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff this String ends with the given suffix, not considering 2651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // case. Any String is considered to end with a NULL or empty suffix. 2661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool EndsWithCaseInsensitive(const char* suffix) const; 2671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 26841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the length of the encapsulated string, or 0 if the 2691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // string is NULL. 27041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot size_t length() const { return length_; } 2711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the 0-terminated C string this String object represents. 2731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The String object still owns the string. Therefore the caller 2741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // should NOT delete the return value. 2751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* c_str() const { return c_str_; } 2761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Assigns a C string to this object. Self-assignment works. 27841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const String& operator=(const char* a_c_str) { 27941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return *this = String(a_c_str); 28041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 2811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Assigns a String object to this object. Self-assignment works. 28341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const String& operator=(const String& rhs) { 28441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (this != &rhs) { 28541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot delete[] c_str_; 28641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (rhs.c_str() == NULL) { 28741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot c_str_ = NULL; 28841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot length_ = 0; 28941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else { 29041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ConstructNonNull(rhs.c_str(), rhs.length()); 29141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 29241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 29341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 2941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return *this; 2951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 2961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 29841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Constructs a non-NULL String from the given content. This 29941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // function can only be called when c_str_ has not been allocated. 30041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // ConstructNonNull(NULL, 0) results in an empty string (""). 30141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // ConstructNonNull(NULL, non_zero) is undefined behavior. 30241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void ConstructNonNull(const char* buffer, size_t a_length) { 30341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot char* const str = new char[a_length + 1]; 30441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot memcpy(str, buffer, a_length); 30541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot str[a_length] = '\0'; 30641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot c_str_ = str; 30741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot length_ = a_length; 30841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 3091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 31041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* c_str_; 31141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot size_t length_; 31241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; // class String 31341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 31441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Streams a String to an ostream. Each '\0' character in the String 31541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// is replaced with "\\0". 31641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline ::std::ostream& operator<<(::std::ostream& os, const String& str) { 31741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (str.c_str() == NULL) { 31841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot os << "(null)"; 31941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else { 32041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* const c_str = str.c_str(); 32141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i != str.length(); i++) { 32241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (c_str[i] == '\0') { 32341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot os << "\\0"; 32441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else { 32541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot os << c_str[i]; 32641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 32741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 32841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 32941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return os; 3301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 33241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the content of the stringstream's buffer as a String. Each '\0' 3331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// character in the buffer is replaced with "\\0". 33441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ String StringStreamToString(::std::stringstream* stream); 3351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts a streamable value to a String. A NULL pointer is 3371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// converted to "(null)". When the input value is a ::string, 3381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ::std::string, ::wstring, or ::std::wstring object, each NUL 3391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// character in it is replaced with "\\0". 3401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Declared here but defined in gtest.h, so that it has access 3421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to the definition of the Message class, required by the ARM 3431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// compiler. 3441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 3451be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString StreamableToString(const T& streamable); 3461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 3481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace testing 3491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 351