gtest-string.h revision 1be2c9def7187e4e643c00a31dd9986395795d7d
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 441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <string.h> 451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <gtest/internal/gtest-port.h> 461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_GLOBAL_STRING || GTEST_HAS_STD_STRING 481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <string> 491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_GLOBAL_STRING || GTEST_HAS_STD_STRING 501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing { 521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// String - a UTF-8 string class. 551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We cannot use std::string as Microsoft's STL implementation in 571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Visual C++ 7.1 has problems when exception is disabled. There is a 581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// hack to work around this, but we've seen cases where the hack fails 591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to work. 601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Also, String is different from std::string in that it can represent 621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// both NULL and the empty string, while std::string cannot represent 631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// NULL. 641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// NULL and the empty string are considered different. NULL is less 661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// than anything (including the empty string) except itself. 671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class only provides minimum functionality necessary for 691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// implementing Google Test. We do not intend to implement a full-fledged 701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// string class here. 711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Since the purpose of this class is to provide a substitute for 731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// std::string on platforms where it cannot be used, we define a copy 741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// constructor and assignment operators such that we don't need 751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// conditional compilation in a lot of places. 761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In order to make the representation efficient, the d'tor of String 781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is not virtual. Therefore DO NOT INHERIT FROM String. 791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass String { 801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Static utility methods 821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the input if it's not NULL, otherwise returns "(null)". 841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This function serves two purposes: 851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1. ShowCString(NULL) has type 'const char *', instead of the 871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // type of NULL (which is int). 881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 2. In MSVC, streaming a null char pointer to StrStream generates 901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // an access violation, so we need to convert NULL to "(null)" 911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // before streaming it. 921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static inline const char* ShowCString(const char* c_str) { 931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return c_str ? c_str : "(null)"; 941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the input enclosed in double quotes if it's not NULL; 971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // otherwise returns "(null)". For example, "\"Hello\"" is returned 981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // for input "Hello". 991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This is useful for printing a C string in the syntax of a literal. 1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Known issue: escape sequences are not handled yet. 1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static String ShowCStringQuoted(const char* c_str); 1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Clones a 0-terminated C string, allocating memory using new. The 1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // caller is responsible for deleting the return value using 1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // delete[]. Returns the cloned string, or NULL if the input is 1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NULL. 1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This is different from strdup() in string.h, which allocates 1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // memory using malloc(). 1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const char* CloneCString(const char* c_str); 1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifdef _WIN32_WCE 1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be 1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // able to pass strings to Win32 APIs on CE we need to convert them 1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // to 'Unicode', UTF-16. 1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Creates a UTF-16 wide string from the given ANSI string, allocating 1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // memory using new. The caller is responsible for deleting the return 1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // value using delete[]. Returns the wide string, or NULL if the 1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // input is NULL. 1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The wide string is created using the ANSI codepage (CP_ACP) to 1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // match the behaviour of the ANSI versions of Win32 calls and the 1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // C runtime. 1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static LPCWSTR AnsiToUtf16(const char* c_str); 1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Creates an ANSI string from the given wide string, allocating 1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // memory using new. The caller is responsible for deleting the return 1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // value using delete[]. Returns the ANSI string, or NULL if the 1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // input is NULL. 1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The returned string is created using the ANSI codepage (CP_ACP) to 1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // match the behaviour of the ANSI versions of Win32 calls and the 1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // C runtime. 1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const char* Utf16ToAnsi(LPCWSTR utf16_str); 1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif 1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Compares two C strings. Returns true iff they have the same content. 1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Unlike strcmp(), this function can handle NULL argument(s). A 1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NULL C string is considered different to any non-NULL C string, 1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // including the empty string. 1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool CStringEquals(const char* lhs, const char* rhs); 1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Converts a wide C string to a String using the UTF-8 encoding. 1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NULL will be converted to "(null)". If an error occurred during 1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the conversion, "(failed to convert from wide string)" is 1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // returned. 1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static String ShowWideCString(const wchar_t* wide_c_str); 1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Similar to ShowWideCString(), except that this function encloses 1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the converted string in double quotes. 1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static String ShowWideCStringQuoted(const wchar_t* wide_c_str); 1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Compares two wide C strings. Returns true iff they have the same 1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // content. 1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Unlike wcscmp(), this function can handle NULL argument(s). A 1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NULL C string is considered different to any non-NULL C string, 1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // including the empty string. 1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs); 1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Compares two C strings, ignoring case. Returns true iff they 1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // have the same content. 1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Unlike strcasecmp(), this function can handle NULL argument(s). 1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A NULL C string is considered different to any non-NULL C string, 1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // including the empty string. 1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool CaseInsensitiveCStringEquals(const char* lhs, 1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* rhs); 1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Compares two wide C strings, ignoring case. Returns true iff they 1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // have the same content. 1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Unlike wcscasecmp(), this function can handle NULL argument(s). 1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A NULL C string is considered different to any non-NULL wide C string, 1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // including the empty string. 1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NB: The implementations on different platforms slightly differ. 1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // On windows, this method uses _wcsicmp which compares according to LC_CTYPE 1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // environment variable. On GNU platform this method uses wcscasecmp 1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // which compares according to LC_CTYPE category of the current locale. 1841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the 1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // current locale. 1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs, 1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const wchar_t* rhs); 1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Formats a list of arguments to a String, using the same format 1901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // spec string as for printf. 1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We do not use the StringPrintf class as it is not universally 1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // available. 1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The result is limited to 4096 characters (including the tailing 1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 0). If 4096 characters are not enough to format the input, 1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // "<buffer exceeded>" is returned. 1981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static String Format(const char* format, ...); 1991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // C'tors 2011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The default c'tor constructs a NULL string. 2031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String() : c_str_(NULL) {} 2041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Constructs a String by cloning a 0-terminated C string. 2061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String(const char* c_str) : c_str_(NULL) { // NOLINT 2071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *this = c_str; 2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 2091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Constructs a String by copying a given number of chars from a 2111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // buffer. E.g. String("hello", 3) will create the string "hel". 2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String(const char* buffer, size_t len); 2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The copy c'tor creates a new copy of the string. The two 2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // String objects do not share content. 2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String(const String& str) : c_str_(NULL) { 2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *this = str; 2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // D'tor. String is intended to be a final class, so the d'tor 2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // doesn't need to be virtual. 2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ~String() { delete[] c_str_; } 2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Allows a String to be implicitly converted to an ::std::string or 2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // ::string, and vice versa. Converting a String containing a NULL 2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // pointer to ::std::string or ::string is undefined behavior. 2271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Converting a ::std::string or ::string containing an embedded NUL 2281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // character to a String will result in the prefix up to the first 2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NUL character. 2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_STD_STRING 2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String(const ::std::string& str) : c_str_(NULL) { *this = str.c_str(); } 2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania operator ::std::string() const { return ::std::string(c_str_); } 2341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_STD_STRING 2351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_GLOBAL_STRING 2371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String(const ::string& str) : c_str_(NULL) { *this = str.c_str(); } 2381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania operator ::string() const { return ::string(c_str_); } 2401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_GLOBAL_STRING 2411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff this is an empty string (i.e. ""). 2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool empty() const { 2441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return (c_str_ != NULL) && (*c_str_ == '\0'); 2451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 2461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Compares this with another String. 2481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns < 0 if this is less than rhs, 0 if this is equal to rhs, or > 0 2491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // if this is greater than rhs. 2501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int Compare(const String& rhs) const; 2511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff this String equals the given C string. A NULL 2531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // string and a non-NULL string are considered not equal. 2541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool operator==(const char* c_str) const { 2551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return CStringEquals(c_str_, c_str); 2561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 2571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff this String is less than the given C string. A NULL 2591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // string is considered less than "". 2601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool operator<(const String& rhs) const { return Compare(rhs) < 0; } 2611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff this String doesn't equal the given C string. A NULL 2631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // string and a non-NULL string are considered not equal. 2641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool operator!=(const char* c_str) const { 2651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return !CStringEquals(c_str_, c_str); 2661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 2671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff this String ends with the given suffix. *Any* 2691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // String is considered to end with a NULL or empty suffix. 2701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool EndsWith(const char* suffix) const; 2711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff this String ends with the given suffix, not considering 2731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // case. Any String is considered to end with a NULL or empty suffix. 2741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool EndsWithCaseInsensitive(const char* suffix) const; 2751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the length of the encapsulated string, or -1 if the 2771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // string is NULL. 2781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int GetLength() const { 2791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return c_str_ ? static_cast<int>(strlen(c_str_)) : -1; 2801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 2811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the 0-terminated C string this String object represents. 2831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The String object still owns the string. Therefore the caller 2841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // should NOT delete the return value. 2851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* c_str() const { return c_str_; } 2861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets the 0-terminated C string this String object represents. 2881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The old string in this object is deleted, and this object will 2891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // own a clone of the input string. This function copies only up to 2901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // length bytes (plus a terminating null byte), or until the first 2911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // null byte, whichever comes first. 2921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 2931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This function works even when the c_str parameter has the same 2941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // value as that of the c_str_ field. 2951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void Set(const char* c_str, size_t length); 2961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Assigns a C string to this object. Self-assignment works. 2981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const String& operator=(const char* c_str); 2991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Assigns a String object to this object. Self-assignment works. 3011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const String& operator=(const String &rhs) { 3021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *this = rhs.c_str_; 3031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return *this; 3041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 3051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 3071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* c_str_; 3081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 3091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Streams a String to an ostream. 3111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline ::std::ostream& operator <<(::std::ostream& os, const String& str) { 3121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We call String::ShowCString() to convert NULL to "(null)". 3131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Otherwise we'll get an access violation on Windows. 3141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return os << String::ShowCString(str.c_str()); 3151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the content of the StrStream's buffer as a String. Each '\0' 3181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// character in the buffer is replaced with "\\0". 3191be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString StrStreamToString(StrStream* stream); 3201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts a streamable value to a String. A NULL pointer is 3221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// converted to "(null)". When the input value is a ::string, 3231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ::std::string, ::wstring, or ::std::wstring object, each NUL 3241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// character in it is replaced with "\\0". 3251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Declared here but defined in gtest.h, so that it has access 3271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to the definition of the Message class, required by the ARM 3281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// compiler. 3291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 3301be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString StreamableToString(const T& streamable); 3311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 3331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace testing 3341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 336