00001 // Copyright 2005, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee) 00031 // 00032 // The Google C++ Testing Framework (Google Test) 00033 // 00034 // This header file declares the String class and functions used internally by 00035 // Google Test. They are subject to change without notice. They should not used 00036 // by code external to Google Test. 00037 // 00038 // This header file is #included by <gtest/internal/gtest-internal.h>. 00039 // It should not be #included by other files. 00040 00041 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 00042 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 00043 00044 #ifdef __BORLANDC__ 00045 // string.h is not guaranteed to provide strcpy on C++ Builder. 00046 # include <mem.h> 00047 #endif 00048 00049 #include <string.h> 00050 #include <string> 00051 00052 #include "gtest/internal/gtest-port.h" 00053 00054 namespace testing { 00055 namespace internal { 00056 00057 // String - an abstract class holding static string utilities. 00058 class GTEST_API_ String { 00059 public: 00060 // Static utility methods 00061 00062 // Clones a 0-terminated C string, allocating memory using new. The 00063 // caller is responsible for deleting the return value using 00064 // delete[]. Returns the cloned string, or NULL if the input is 00065 // NULL. 00066 // 00067 // This is different from strdup() in string.h, which allocates 00068 // memory using malloc(). 00069 static const char* CloneCString(const char* c_str); 00070 00071 #if GTEST_OS_WINDOWS_MOBILE 00072 // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be 00073 // able to pass strings to Win32 APIs on CE we need to convert them 00074 // to 'Unicode', UTF-16. 00075 00076 // Creates a UTF-16 wide string from the given ANSI string, allocating 00077 // memory using new. The caller is responsible for deleting the return 00078 // value using delete[]. Returns the wide string, or NULL if the 00079 // input is NULL. 00080 // 00081 // The wide string is created using the ANSI codepage (CP_ACP) to 00082 // match the behaviour of the ANSI versions of Win32 calls and the 00083 // C runtime. 00084 static LPCWSTR AnsiToUtf16(const char* c_str); 00085 00086 // Creates an ANSI string from the given wide string, allocating 00087 // memory using new. The caller is responsible for deleting the return 00088 // value using delete[]. Returns the ANSI string, or NULL if the 00089 // input is NULL. 00090 // 00091 // The returned string is created using the ANSI codepage (CP_ACP) to 00092 // match the behaviour of the ANSI versions of Win32 calls and the 00093 // C runtime. 00094 static const char* Utf16ToAnsi(LPCWSTR utf16_str); 00095 #endif 00096 00097 // Compares two C strings. Returns true iff they have the same content. 00098 // 00099 // Unlike strcmp(), this function can handle NULL argument(s). A 00100 // NULL C string is considered different to any non-NULL C string, 00101 // including the empty string. 00102 static bool CStringEquals(const char* lhs, const char* rhs); 00103 00104 // Converts a wide C string to a String using the UTF-8 encoding. 00105 // NULL will be converted to "(null)". If an error occurred during 00106 // the conversion, "(failed to convert from wide string)" is 00107 // returned. 00108 static std::string ShowWideCString(const wchar_t* wide_c_str); 00109 00110 // Compares two wide C strings. Returns true iff they have the same 00111 // content. 00112 // 00113 // Unlike wcscmp(), this function can handle NULL argument(s). A 00114 // NULL C string is considered different to any non-NULL C string, 00115 // including the empty string. 00116 static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs); 00117 00118 // Compares two C strings, ignoring case. Returns true iff they 00119 // have the same content. 00120 // 00121 // Unlike strcasecmp(), this function can handle NULL argument(s). 00122 // A NULL C string is considered different to any non-NULL C string, 00123 // including the empty string. 00124 static bool CaseInsensitiveCStringEquals(const char* lhs, 00125 const char* rhs); 00126 00127 // Compares two wide C strings, ignoring case. Returns true iff they 00128 // have the same content. 00129 // 00130 // Unlike wcscasecmp(), this function can handle NULL argument(s). 00131 // A NULL C string is considered different to any non-NULL wide C string, 00132 // including the empty string. 00133 // NB: The implementations on different platforms slightly differ. 00134 // On windows, this method uses _wcsicmp which compares according to LC_CTYPE 00135 // environment variable. On GNU platform this method uses wcscasecmp 00136 // which compares according to LC_CTYPE category of the current locale. 00137 // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the 00138 // current locale. 00139 static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs, 00140 const wchar_t* rhs); 00141 00142 // Returns true iff the given string ends with the given suffix, ignoring 00143 // case. Any string is considered to end with an empty suffix. 00144 static bool EndsWithCaseInsensitive( 00145 const std::string& str, const std::string& suffix); 00146 00147 // Formats an int value as "%02d". 00148 static std::string FormatIntWidth2(int value); // "%02d" for width == 2 00149 00150 // Formats an int value as "%X". 00151 static std::string FormatHexInt(int value); 00152 00153 // Formats a byte as "%02X". 00154 static std::string FormatByte(unsigned char value); 00155 00156 private: 00157 String(); // Not meant to be instantiated. 00158 }; // class String 00159 00160 // Gets the content of the stringstream's buffer as an std::string. Each '\0' 00161 // character in the buffer is replaced with "\\0". 00162 GTEST_API_ std::string StringStreamToString(::std::stringstream* stream); 00163 00164 } // namespace internal 00165 } // namespace testing 00166 00167 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_