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