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 "gtest/internal/gtest-port.h" 00051 00052 #include <string> 00053 00054 namespace testing { 00055 namespace internal { 00056 00057 // String - a UTF-8 string class. 00058 // 00059 // For historic reasons, we don't use std::string. 00060 // 00061 // TODO(wan@google.com): replace this class with std::string or 00062 // implement it in terms of the latter. 00063 // 00064 // Note that String can represent both NULL and the empty string, 00065 // while std::string cannot represent NULL. 00066 // 00067 // NULL and the empty string are considered different. NULL is less 00068 // than anything (including the empty string) except itself. 00069 // 00070 // This class only provides minimum functionality necessary for 00071 // implementing Google Test. We do not intend to implement a full-fledged 00072 // string class here. 00073 // 00074 // Since the purpose of this class is to provide a substitute for 00075 // std::string on platforms where it cannot be used, we define a copy 00076 // constructor and assignment operators such that we don't need 00077 // conditional compilation in a lot of places. 00078 // 00079 // In order to make the representation efficient, the d'tor of String 00080 // is not virtual. Therefore DO NOT INHERIT FROM String. 00081 class GTEST_API_ String { 00082 public: 00083 // Static utility methods 00084 00085 // Returns the input enclosed in double quotes if it's not NULL; 00086 // otherwise returns "(null)". For example, "\"Hello\"" is returned 00087 // for input "Hello". 00088 // 00089 // This is useful for printing a C string in the syntax of a literal. 00090 // 00091 // Known issue: escape sequences are not handled yet. 00092 static String ShowCStringQuoted(const char* c_str); 00093 00094 // Clones a 0-terminated C string, allocating memory using new. The 00095 // caller is responsible for deleting the return value using 00096 // delete[]. Returns the cloned string, or NULL if the input is 00097 // NULL. 00098 // 00099 // This is different from strdup() in string.h, which allocates 00100 // memory using malloc(). 00101 static const char* CloneCString(const char* c_str); 00102 00103 #if GTEST_OS_WINDOWS_MOBILE 00104 // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be 00105 // able to pass strings to Win32 APIs on CE we need to convert them 00106 // to 'Unicode', UTF-16. 00107 00108 // Creates a UTF-16 wide string from the given ANSI string, allocating 00109 // memory using new. The caller is responsible for deleting the return 00110 // value using delete[]. Returns the wide string, or NULL if the 00111 // input is NULL. 00112 // 00113 // The wide string is created using the ANSI codepage (CP_ACP) to 00114 // match the behaviour of the ANSI versions of Win32 calls and the 00115 // C runtime. 00116 static LPCWSTR AnsiToUtf16(const char* c_str); 00117 00118 // Creates an ANSI string from the given wide string, allocating 00119 // memory using new. The caller is responsible for deleting the return 00120 // value using delete[]. Returns the ANSI string, or NULL if the 00121 // input is NULL. 00122 // 00123 // The returned string is created using the ANSI codepage (CP_ACP) to 00124 // match the behaviour of the ANSI versions of Win32 calls and the 00125 // C runtime. 00126 static const char* Utf16ToAnsi(LPCWSTR utf16_str); 00127 #endif 00128 00129 // Compares two C strings. Returns true iff they have the same content. 00130 // 00131 // Unlike strcmp(), this function can handle NULL argument(s). A 00132 // NULL C string is considered different to any non-NULL C string, 00133 // including the empty string. 00134 static bool CStringEquals(const char* lhs, const char* rhs); 00135 00136 // Converts a wide C string to a String using the UTF-8 encoding. 00137 // NULL will be converted to "(null)". If an error occurred during 00138 // the conversion, "(failed to convert from wide string)" is 00139 // returned. 00140 static String ShowWideCString(const wchar_t* wide_c_str); 00141 00142 // Similar to ShowWideCString(), except that this function encloses 00143 // the converted string in double quotes. 00144 static String ShowWideCStringQuoted(const wchar_t* wide_c_str); 00145 00146 // Compares two wide C strings. Returns true iff they have the same 00147 // content. 00148 // 00149 // Unlike wcscmp(), this function can handle NULL argument(s). A 00150 // NULL C string is considered different to any non-NULL C string, 00151 // including the empty string. 00152 static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs); 00153 00154 // Compares two C strings, ignoring case. Returns true iff they 00155 // have the same content. 00156 // 00157 // Unlike strcasecmp(), this function can handle NULL argument(s). 00158 // A NULL C string is considered different to any non-NULL C string, 00159 // including the empty string. 00160 static bool CaseInsensitiveCStringEquals(const char* lhs, 00161 const char* rhs); 00162 00163 // Compares two wide C strings, ignoring case. Returns true iff they 00164 // have the same content. 00165 // 00166 // Unlike wcscasecmp(), this function can handle NULL argument(s). 00167 // A NULL C string is considered different to any non-NULL wide C string, 00168 // including the empty string. 00169 // NB: The implementations on different platforms slightly differ. 00170 // On windows, this method uses _wcsicmp which compares according to LC_CTYPE 00171 // environment variable. On GNU platform this method uses wcscasecmp 00172 // which compares according to LC_CTYPE category of the current locale. 00173 // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the 00174 // current locale. 00175 static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs, 00176 const wchar_t* rhs); 00177 00178 // Formats a list of arguments to a String, using the same format 00179 // spec string as for printf. 00180 // 00181 // We do not use the StringPrintf class as it is not universally 00182 // available. 00183 // 00184 // The result is limited to 4096 characters (including the tailing 00185 // 0). If 4096 characters are not enough to format the input, 00186 // "<buffer exceeded>" is returned. 00187 static String Format(const char* format, ...); 00188 00189 // C'tors 00190 00191 // The default c'tor constructs a NULL string. 00192 String() : c_str_(NULL), length_(0) {} 00193 00194 // Constructs a String by cloning a 0-terminated C string. 00195 String(const char* a_c_str) { // NOLINT 00196 if (a_c_str == NULL) { 00197 c_str_ = NULL; 00198 length_ = 0; 00199 } else { 00200 ConstructNonNull(a_c_str, strlen(a_c_str)); 00201 } 00202 } 00203 00204 // Constructs a String by copying a given number of chars from a 00205 // buffer. E.g. String("hello", 3) creates the string "hel", 00206 // String("a\0bcd", 4) creates "a\0bc", String(NULL, 0) creates "", 00207 // and String(NULL, 1) results in access violation. 00208 String(const char* buffer, size_t a_length) { 00209 ConstructNonNull(buffer, a_length); 00210 } 00211 00212 // The copy c'tor creates a new copy of the string. The two 00213 // String objects do not share content. 00214 String(const String& str) : c_str_(NULL), length_(0) { *this = str; } 00215 00216 // D'tor. String is intended to be a final class, so the d'tor 00217 // doesn't need to be virtual. 00218 ~String() { delete[] c_str_; } 00219 00220 // Allows a String to be implicitly converted to an ::std::string or 00221 // ::string, and vice versa. Converting a String containing a NULL 00222 // pointer to ::std::string or ::string is undefined behavior. 00223 // Converting a ::std::string or ::string containing an embedded NUL 00224 // character to a String will result in the prefix up to the first 00225 // NUL character. 00226 String(const ::std::string& str) { 00227 ConstructNonNull(str.c_str(), str.length()); 00228 } 00229 00230 operator ::std::string() const { return ::std::string(c_str(), length()); } 00231 00232 #if GTEST_HAS_GLOBAL_STRING 00233 String(const ::string& str) { 00234 ConstructNonNull(str.c_str(), str.length()); 00235 } 00236 00237 operator ::string() const { return ::string(c_str(), length()); } 00238 #endif // GTEST_HAS_GLOBAL_STRING 00239 00240 // Returns true iff this is an empty string (i.e. ""). 00241 bool empty() const { return (c_str() != NULL) && (length() == 0); } 00242 00243 // Compares this with another String. 00244 // Returns < 0 if this is less than rhs, 0 if this is equal to rhs, or > 0 00245 // if this is greater than rhs. 00246 int Compare(const String& rhs) const; 00247 00248 // Returns true iff this String equals the given C string. A NULL 00249 // string and a non-NULL string are considered not equal. 00250 bool operator==(const char* a_c_str) const { return Compare(a_c_str) == 0; } 00251 00252 // Returns true iff this String is less than the given String. A 00253 // NULL string is considered less than "". 00254 bool operator<(const String& rhs) const { return Compare(rhs) < 0; } 00255 00256 // Returns true iff this String doesn't equal the given C string. A NULL 00257 // string and a non-NULL string are considered not equal. 00258 bool operator!=(const char* a_c_str) const { return !(*this == a_c_str); } 00259 00260 // Returns true iff this String ends with the given suffix. *Any* 00261 // String is considered to end with a NULL or empty suffix. 00262 bool EndsWith(const char* suffix) const; 00263 00264 // Returns true iff this String ends with the given suffix, not considering 00265 // case. Any String is considered to end with a NULL or empty suffix. 00266 bool EndsWithCaseInsensitive(const char* suffix) const; 00267 00268 // Returns the length of the encapsulated string, or 0 if the 00269 // string is NULL. 00270 size_t length() const { return length_; } 00271 00272 // Gets the 0-terminated C string this String object represents. 00273 // The String object still owns the string. Therefore the caller 00274 // should NOT delete the return value. 00275 const char* c_str() const { return c_str_; } 00276 00277 // Assigns a C string to this object. Self-assignment works. 00278 const String& operator=(const char* a_c_str) { 00279 return *this = String(a_c_str); 00280 } 00281 00282 // Assigns a String object to this object. Self-assignment works. 00283 const String& operator=(const String& rhs) { 00284 if (this != &rhs) { 00285 delete[] c_str_; 00286 if (rhs.c_str() == NULL) { 00287 c_str_ = NULL; 00288 length_ = 0; 00289 } else { 00290 ConstructNonNull(rhs.c_str(), rhs.length()); 00291 } 00292 } 00293 00294 return *this; 00295 } 00296 00297 private: 00298 // Constructs a non-NULL String from the given content. This 00299 // function can only be called when c_str_ has not been allocated. 00300 // ConstructNonNull(NULL, 0) results in an empty string (""). 00301 // ConstructNonNull(NULL, non_zero) is undefined behavior. 00302 void ConstructNonNull(const char* buffer, size_t a_length) { 00303 char* const str = new char[a_length + 1]; 00304 memcpy(str, buffer, a_length); 00305 str[a_length] = '\0'; 00306 c_str_ = str; 00307 length_ = a_length; 00308 } 00309 00310 const char* c_str_; 00311 size_t length_; 00312 }; // class String 00313 00314 // Streams a String to an ostream. Each '\0' character in the String 00315 // is replaced with "\\0". 00316 inline ::std::ostream& operator<<(::std::ostream& os, const String& str) { 00317 if (str.c_str() == NULL) { 00318 os << "(null)"; 00319 } else { 00320 const char* const c_str = str.c_str(); 00321 for (size_t i = 0; i != str.length(); i++) { 00322 if (c_str[i] == '\0') { 00323 os << "\\0"; 00324 } else { 00325 os << c_str[i]; 00326 } 00327 } 00328 } 00329 return os; 00330 } 00331 00332 // Gets the content of the stringstream's buffer as a String. Each '\0' 00333 // character in the buffer is replaced with "\\0". 00334 GTEST_API_ String StringStreamToString(::std::stringstream* stream); 00335 00336 // Converts a streamable value to a String. A NULL pointer is 00337 // converted to "(null)". When the input value is a ::string, 00338 // ::std::string, ::wstring, or ::std::wstring object, each NUL 00339 // character in it is replaced with "\\0". 00340 00341 // Declared here but defined in gtest.h, so that it has access 00342 // to the definition of the Message class, required by the ARM 00343 // compiler. 00344 template <typename T> 00345 String StreamableToString(const T& streamable); 00346 00347 } // namespace internal 00348 } // namespace testing 00349 00350 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_