1a11cd0d9STom Stellard // Copyright 2005, Google Inc. 2a11cd0d9STom Stellard // All rights reserved. 3a11cd0d9STom Stellard // 4a11cd0d9STom Stellard // Redistribution and use in source and binary forms, with or without 5a11cd0d9STom Stellard // modification, are permitted provided that the following conditions are 6a11cd0d9STom Stellard // met: 7a11cd0d9STom Stellard // 8a11cd0d9STom Stellard // * Redistributions of source code must retain the above copyright 9a11cd0d9STom Stellard // notice, this list of conditions and the following disclaimer. 10a11cd0d9STom Stellard // * Redistributions in binary form must reproduce the above 11a11cd0d9STom Stellard // copyright notice, this list of conditions and the following disclaimer 12a11cd0d9STom Stellard // in the documentation and/or other materials provided with the 13a11cd0d9STom Stellard // distribution. 14a11cd0d9STom Stellard // * Neither the name of Google Inc. nor the names of its 15a11cd0d9STom Stellard // contributors may be used to endorse or promote products derived from 16a11cd0d9STom Stellard // this software without specific prior written permission. 17a11cd0d9STom Stellard // 18a11cd0d9STom Stellard // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19a11cd0d9STom Stellard // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20a11cd0d9STom Stellard // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21a11cd0d9STom Stellard // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22a11cd0d9STom Stellard // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23a11cd0d9STom Stellard // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24a11cd0d9STom Stellard // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25a11cd0d9STom Stellard // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26a11cd0d9STom Stellard // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27a11cd0d9STom Stellard // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28a11cd0d9STom Stellard // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29a11cd0d9STom Stellard 30a11cd0d9STom Stellard // The Google C++ Testing and Mocking Framework (Google Test) 31a11cd0d9STom Stellard // 32a11cd0d9STom Stellard // This header file declares the String class and functions used internally by 33a11cd0d9STom Stellard // Google Test. They are subject to change without notice. They should not used 34a11cd0d9STom Stellard // by code external to Google Test. 35a11cd0d9STom Stellard // 36a11cd0d9STom Stellard // This header file is #included by gtest-internal.h. 37a11cd0d9STom Stellard // It should not be #included by other files. 38a11cd0d9STom Stellard 39a11cd0d9STom Stellard // IWYU pragma: private, include "gtest/gtest.h" 40a11cd0d9STom Stellard // IWYU pragma: friend gtest/.* 41a11cd0d9STom Stellard // IWYU pragma: friend gmock/.* 42a11cd0d9STom Stellard 43*a866ce78SHaowei Wu #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 44*a866ce78SHaowei Wu #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 45a11cd0d9STom Stellard 46a11cd0d9STom Stellard #ifdef __BORLANDC__ 47a11cd0d9STom Stellard // string.h is not guaranteed to provide strcpy on C++ Builder. 48a11cd0d9STom Stellard #include <mem.h> 49a11cd0d9STom Stellard #endif 50a11cd0d9STom Stellard 51a11cd0d9STom Stellard #include <string.h> 52*a866ce78SHaowei Wu 53*a866ce78SHaowei Wu #include <cstdint> 54*a866ce78SHaowei Wu #include <sstream> 55a11cd0d9STom Stellard #include <string> 56a11cd0d9STom Stellard 57a11cd0d9STom Stellard #include "gtest/internal/gtest-port.h" 58a11cd0d9STom Stellard 59a11cd0d9STom Stellard namespace testing { 60a11cd0d9STom Stellard namespace internal { 61a11cd0d9STom Stellard 62a11cd0d9STom Stellard // String - an abstract class holding static string utilities. 63a11cd0d9STom Stellard class GTEST_API_ String { 64a11cd0d9STom Stellard public: 65a11cd0d9STom Stellard // Static utility methods 66a11cd0d9STom Stellard 67a11cd0d9STom Stellard // Clones a 0-terminated C string, allocating memory using new. The 68a11cd0d9STom Stellard // caller is responsible for deleting the return value using 69a11cd0d9STom Stellard // delete[]. Returns the cloned string, or NULL if the input is 70a11cd0d9STom Stellard // NULL. 71a11cd0d9STom Stellard // 72a11cd0d9STom Stellard // This is different from strdup() in string.h, which allocates 73a11cd0d9STom Stellard // memory using malloc(). 74a11cd0d9STom Stellard static const char* CloneCString(const char* c_str); 75a11cd0d9STom Stellard 76*a866ce78SHaowei Wu #ifdef GTEST_OS_WINDOWS_MOBILE 77a11cd0d9STom Stellard // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be 78a11cd0d9STom Stellard // able to pass strings to Win32 APIs on CE we need to convert them 79a11cd0d9STom Stellard // to 'Unicode', UTF-16. 80a11cd0d9STom Stellard 81a11cd0d9STom Stellard // Creates a UTF-16 wide string from the given ANSI string, allocating 82a11cd0d9STom Stellard // memory using new. The caller is responsible for deleting the return 83a11cd0d9STom Stellard // value using delete[]. Returns the wide string, or NULL if the 84a11cd0d9STom Stellard // input is NULL. 85a11cd0d9STom Stellard // 86a11cd0d9STom Stellard // The wide string is created using the ANSI codepage (CP_ACP) to 87a11cd0d9STom Stellard // match the behaviour of the ANSI versions of Win32 calls and the 88a11cd0d9STom Stellard // C runtime. 89a11cd0d9STom Stellard static LPCWSTR AnsiToUtf16(const char* c_str); 90a11cd0d9STom Stellard 91a11cd0d9STom Stellard // Creates an ANSI string from the given wide string, allocating 92a11cd0d9STom Stellard // memory using new. The caller is responsible for deleting the return 93a11cd0d9STom Stellard // value using delete[]. Returns the ANSI string, or NULL if the 94a11cd0d9STom Stellard // input is NULL. 95a11cd0d9STom Stellard // 96a11cd0d9STom Stellard // The returned string is created using the ANSI codepage (CP_ACP) to 97a11cd0d9STom Stellard // match the behaviour of the ANSI versions of Win32 calls and the 98a11cd0d9STom Stellard // C runtime. 99a11cd0d9STom Stellard static const char* Utf16ToAnsi(LPCWSTR utf16_str); 100a11cd0d9STom Stellard #endif 101a11cd0d9STom Stellard 102a11cd0d9STom Stellard // Compares two C strings. Returns true if and only if they have the same 103a11cd0d9STom Stellard // content. 104a11cd0d9STom Stellard // 105a11cd0d9STom Stellard // Unlike strcmp(), this function can handle NULL argument(s). A 106a11cd0d9STom Stellard // NULL C string is considered different to any non-NULL C string, 107a11cd0d9STom Stellard // including the empty string. 108a11cd0d9STom Stellard static bool CStringEquals(const char* lhs, const char* rhs); 109a11cd0d9STom Stellard 110a11cd0d9STom Stellard // Converts a wide C string to a String using the UTF-8 encoding. 111a11cd0d9STom Stellard // NULL will be converted to "(null)". If an error occurred during 112a11cd0d9STom Stellard // the conversion, "(failed to convert from wide string)" is 113a11cd0d9STom Stellard // returned. 114a11cd0d9STom Stellard static std::string ShowWideCString(const wchar_t* wide_c_str); 115a11cd0d9STom Stellard 116a11cd0d9STom Stellard // Compares two wide C strings. Returns true if and only if they have the 117a11cd0d9STom Stellard // same content. 118a11cd0d9STom Stellard // 119a11cd0d9STom Stellard // Unlike wcscmp(), this function can handle NULL argument(s). A 120a11cd0d9STom Stellard // NULL C string is considered different to any non-NULL C string, 121a11cd0d9STom Stellard // including the empty string. 122a11cd0d9STom Stellard static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs); 123a11cd0d9STom Stellard 124a11cd0d9STom Stellard // Compares two C strings, ignoring case. Returns true if and only if 125a11cd0d9STom Stellard // they have the same content. 126a11cd0d9STom Stellard // 127a11cd0d9STom Stellard // Unlike strcasecmp(), this function can handle NULL argument(s). 128a11cd0d9STom Stellard // A NULL C string is considered different to any non-NULL C string, 129a11cd0d9STom Stellard // including the empty string. 130*a866ce78SHaowei Wu static bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs); 131a11cd0d9STom Stellard 132a11cd0d9STom Stellard // Compares two wide C strings, ignoring case. Returns true if and only if 133a11cd0d9STom Stellard // they have the same content. 134a11cd0d9STom Stellard // 135a11cd0d9STom Stellard // Unlike wcscasecmp(), this function can handle NULL argument(s). 136a11cd0d9STom Stellard // A NULL C string is considered different to any non-NULL wide C string, 137a11cd0d9STom Stellard // including the empty string. 138a11cd0d9STom Stellard // NB: The implementations on different platforms slightly differ. 139a11cd0d9STom Stellard // On windows, this method uses _wcsicmp which compares according to LC_CTYPE 140a11cd0d9STom Stellard // environment variable. On GNU platform this method uses wcscasecmp 141a11cd0d9STom Stellard // which compares according to LC_CTYPE category of the current locale. 142a11cd0d9STom Stellard // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the 143a11cd0d9STom Stellard // current locale. 144a11cd0d9STom Stellard static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs, 145a11cd0d9STom Stellard const wchar_t* rhs); 146a11cd0d9STom Stellard 147a11cd0d9STom Stellard // Returns true if and only if the given string ends with the given suffix, 148a11cd0d9STom Stellard // ignoring case. Any string is considered to end with an empty suffix. 149*a866ce78SHaowei Wu static bool EndsWithCaseInsensitive(const std::string& str, 150*a866ce78SHaowei Wu const std::string& suffix); 151a11cd0d9STom Stellard 152a11cd0d9STom Stellard // Formats an int value as "%02d". 153a11cd0d9STom Stellard static std::string FormatIntWidth2(int value); // "%02d" for width == 2 154a11cd0d9STom Stellard 155*a866ce78SHaowei Wu // Formats an int value to given width with leading zeros. 156*a866ce78SHaowei Wu static std::string FormatIntWidthN(int value, int width); 157*a866ce78SHaowei Wu 158a11cd0d9STom Stellard // Formats an int value as "%X". 159a11cd0d9STom Stellard static std::string FormatHexInt(int value); 160a11cd0d9STom Stellard 161a11cd0d9STom Stellard // Formats an int value as "%X". 162*a866ce78SHaowei Wu static std::string FormatHexUInt32(uint32_t value); 163a11cd0d9STom Stellard 164a11cd0d9STom Stellard // Formats a byte as "%02X". 165a11cd0d9STom Stellard static std::string FormatByte(unsigned char value); 166a11cd0d9STom Stellard 167a11cd0d9STom Stellard private: 168a11cd0d9STom Stellard String(); // Not meant to be instantiated. 169a11cd0d9STom Stellard }; // class String 170a11cd0d9STom Stellard 171a11cd0d9STom Stellard // Gets the content of the stringstream's buffer as an std::string. Each '\0' 172a11cd0d9STom Stellard // character in the buffer is replaced with "\\0". 173a11cd0d9STom Stellard GTEST_API_ std::string StringStreamToString(::std::stringstream* stream); 174a11cd0d9STom Stellard 175a11cd0d9STom Stellard } // namespace internal 176a11cd0d9STom Stellard } // namespace testing 177a11cd0d9STom Stellard 178*a866ce78SHaowei Wu #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 179