You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
13 KiB

  1. // Copyright 2005, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
  31. //
  32. // The Google C++ Testing Framework (Google Test)
  33. //
  34. // This header file declares the String class and functions used internally by
  35. // Google Test. They are subject to change without notice. They should not used
  36. // by code external to Google Test.
  37. //
  38. // This header file is #included by <gtest/internal/gtest-internal.h>.
  39. // It should not be #included by other files.
  40. #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
  41. #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
  42. #ifdef __BORLANDC__
  43. // string.h is not guaranteed to provide strcpy on C++ Builder.
  44. # include <mem.h>
  45. #endif
  46. #include <string.h>
  47. #include "gtest/internal/gtest-port.h"
  48. #include <string>
  49. namespace testing {
  50. namespace internal {
  51. // String - a UTF-8 string class.
  52. //
  53. // For historic reasons, we don't use std::string.
  54. //
  55. // TODO(wan@google.com): replace this class with std::string or
  56. // implement it in terms of the latter.
  57. //
  58. // Note that String can represent both NULL and the empty string,
  59. // while std::string cannot represent NULL.
  60. //
  61. // NULL and the empty string are considered different. NULL is less
  62. // than anything (including the empty string) except itself.
  63. //
  64. // This class only provides minimum functionality necessary for
  65. // implementing Google Test. We do not intend to implement a full-fledged
  66. // string class here.
  67. //
  68. // Since the purpose of this class is to provide a substitute for
  69. // std::string on platforms where it cannot be used, we define a copy
  70. // constructor and assignment operators such that we don't need
  71. // conditional compilation in a lot of places.
  72. //
  73. // In order to make the representation efficient, the d'tor of String
  74. // is not virtual. Therefore DO NOT INHERIT FROM String.
  75. class GTEST_API_ String {
  76. public:
  77. // Static utility methods
  78. // Returns the input enclosed in double quotes if it's not NULL;
  79. // otherwise returns "(null)". For example, "\"Hello\"" is returned
  80. // for input "Hello".
  81. //
  82. // This is useful for printing a C string in the syntax of a literal.
  83. //
  84. // Known issue: escape sequences are not handled yet.
  85. static String ShowCStringQuoted(const char* c_str);
  86. // Clones a 0-terminated C string, allocating memory using new. The
  87. // caller is responsible for deleting the return value using
  88. // delete[]. Returns the cloned string, or NULL if the input is
  89. // NULL.
  90. //
  91. // This is different from strdup() in string.h, which allocates
  92. // memory using malloc().
  93. static const char* CloneCString(const char* c_str);
  94. #if GTEST_OS_WINDOWS_MOBILE
  95. // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be
  96. // able to pass strings to Win32 APIs on CE we need to convert them
  97. // to 'Unicode', UTF-16.
  98. // Creates a UTF-16 wide string from the given ANSI string, allocating
  99. // memory using new. The caller is responsible for deleting the return
  100. // value using delete[]. Returns the wide string, or NULL if the
  101. // input is NULL.
  102. //
  103. // The wide string is created using the ANSI codepage (CP_ACP) to
  104. // match the behaviour of the ANSI versions of Win32 calls and the
  105. // C runtime.
  106. static LPCWSTR AnsiToUtf16(const char* c_str);
  107. // Creates an ANSI string from the given wide string, allocating
  108. // memory using new. The caller is responsible for deleting the return
  109. // value using delete[]. Returns the ANSI string, or NULL if the
  110. // input is NULL.
  111. //
  112. // The returned string is created using the ANSI codepage (CP_ACP) to
  113. // match the behaviour of the ANSI versions of Win32 calls and the
  114. // C runtime.
  115. static const char* Utf16ToAnsi(LPCWSTR utf16_str);
  116. #endif
  117. // Compares two C strings. Returns true iff they have the same content.
  118. //
  119. // Unlike strcmp(), this function can handle NULL argument(s). A
  120. // NULL C string is considered different to any non-NULL C string,
  121. // including the empty string.
  122. static bool CStringEquals(const char* lhs, const char* rhs);
  123. // Converts a wide C string to a String using the UTF-8 encoding.
  124. // NULL will be converted to "(null)". If an error occurred during
  125. // the conversion, "(failed to convert from wide string)" is
  126. // returned.
  127. static String ShowWideCString(const wchar_t* wide_c_str);
  128. // Similar to ShowWideCString(), except that this function encloses
  129. // the converted string in double quotes.
  130. static String ShowWideCStringQuoted(const wchar_t* wide_c_str);
  131. // Compares two wide C strings. Returns true iff they have the same
  132. // content.
  133. //
  134. // Unlike wcscmp(), this function can handle NULL argument(s). A
  135. // NULL C string is considered different to any non-NULL C string,
  136. // including the empty string.
  137. static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs);
  138. // Compares two C strings, ignoring case. Returns true iff they
  139. // have the same content.
  140. //
  141. // Unlike strcasecmp(), this function can handle NULL argument(s).
  142. // A NULL C string is considered different to any non-NULL C string,
  143. // including the empty string.
  144. static bool CaseInsensitiveCStringEquals(const char* lhs,
  145. const char* rhs);
  146. // Compares two wide C strings, ignoring case. Returns true iff they
  147. // have the same content.
  148. //
  149. // Unlike wcscasecmp(), this function can handle NULL argument(s).
  150. // A NULL C string is considered different to any non-NULL wide C string,
  151. // including the empty string.
  152. // NB: The implementations on different platforms slightly differ.
  153. // On windows, this method uses _wcsicmp which compares according to LC_CTYPE
  154. // environment variable. On GNU platform this method uses wcscasecmp
  155. // which compares according to LC_CTYPE category of the current locale.
  156. // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the
  157. // current locale.
  158. static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs,
  159. const wchar_t* rhs);
  160. // Formats a list of arguments to a String, using the same format
  161. // spec string as for printf.
  162. //
  163. // We do not use the StringPrintf class as it is not universally
  164. // available.
  165. //
  166. // The result is limited to 4096 characters (including the tailing
  167. // 0). If 4096 characters are not enough to format the input,
  168. // "<buffer exceeded>" is returned.
  169. static String Format(const char* format, ...);
  170. // C'tors
  171. // The default c'tor constructs a NULL string.
  172. String() : c_str_(NULL), length_(0) {}
  173. // Constructs a String by cloning a 0-terminated C string.
  174. String(const char* a_c_str) { // NOLINT
  175. if (a_c_str == NULL) {
  176. c_str_ = NULL;
  177. length_ = 0;
  178. } else {
  179. ConstructNonNull(a_c_str, strlen(a_c_str));
  180. }
  181. }
  182. // Constructs a String by copying a given number of chars from a
  183. // buffer. E.g. String("hello", 3) creates the string "hel",
  184. // String("a\0bcd", 4) creates "a\0bc", String(NULL, 0) creates "",
  185. // and String(NULL, 1) results in access violation.
  186. String(const char* buffer, size_t a_length) {
  187. ConstructNonNull(buffer, a_length);
  188. }
  189. // The copy c'tor creates a new copy of the string. The two
  190. // String objects do not share content.
  191. String(const String& str) : c_str_(NULL), length_(0) { *this = str; }
  192. // D'tor. String is intended to be a final class, so the d'tor
  193. // doesn't need to be virtual.
  194. ~String() { delete[] c_str_; }
  195. // Allows a String to be implicitly converted to an ::std::string or
  196. // ::string, and vice versa. Converting a String containing a NULL
  197. // pointer to ::std::string or ::string is undefined behavior.
  198. // Converting a ::std::string or ::string containing an embedded NUL
  199. // character to a String will result in the prefix up to the first
  200. // NUL character.
  201. String(const ::std::string& str) {
  202. ConstructNonNull(str.c_str(), str.length());
  203. }
  204. operator ::std::string() const { return ::std::string(c_str(), length()); }
  205. #if GTEST_HAS_GLOBAL_STRING
  206. String(const ::string& str) {
  207. ConstructNonNull(str.c_str(), str.length());
  208. }
  209. operator ::string() const { return ::string(c_str(), length()); }
  210. #endif // GTEST_HAS_GLOBAL_STRING
  211. // Returns true iff this is an empty string (i.e. "").
  212. bool empty() const { return (c_str() != NULL) && (length() == 0); }
  213. // Compares this with another String.
  214. // Returns < 0 if this is less than rhs, 0 if this is equal to rhs, or > 0
  215. // if this is greater than rhs.
  216. int Compare(const String& rhs) const;
  217. // Returns true iff this String equals the given C string. A NULL
  218. // string and a non-NULL string are considered not equal.
  219. bool operator==(const char* a_c_str) const { return Compare(a_c_str) == 0; }
  220. // Returns true iff this String is less than the given String. A
  221. // NULL string is considered less than "".
  222. bool operator<(const String& rhs) const { return Compare(rhs) < 0; }
  223. // Returns true iff this String doesn't equal the given C string. A NULL
  224. // string and a non-NULL string are considered not equal.
  225. bool operator!=(const char* a_c_str) const { return !(*this == a_c_str); }
  226. // Returns true iff this String ends with the given suffix. *Any*
  227. // String is considered to end with a NULL or empty suffix.
  228. bool EndsWith(const char* suffix) const;
  229. // Returns true iff this String ends with the given suffix, not considering
  230. // case. Any String is considered to end with a NULL or empty suffix.
  231. bool EndsWithCaseInsensitive(const char* suffix) const;
  232. // Returns the length of the encapsulated string, or 0 if the
  233. // string is NULL.
  234. size_t length() const { return length_; }
  235. // Gets the 0-terminated C string this String object represents.
  236. // The String object still owns the string. Therefore the caller
  237. // should NOT delete the return value.
  238. const char* c_str() const { return c_str_; }
  239. // Assigns a C string to this object. Self-assignment works.
  240. const String& operator=(const char* a_c_str) {
  241. return *this = String(a_c_str);
  242. }
  243. // Assigns a String object to this object. Self-assignment works.
  244. const String& operator=(const String& rhs) {
  245. if (this != &rhs) {
  246. delete[] c_str_;
  247. if (rhs.c_str() == NULL) {
  248. c_str_ = NULL;
  249. length_ = 0;
  250. } else {
  251. ConstructNonNull(rhs.c_str(), rhs.length());
  252. }
  253. }
  254. return *this;
  255. }
  256. private:
  257. // Constructs a non-NULL String from the given content. This
  258. // function can only be called when c_str_ has not been allocated.
  259. // ConstructNonNull(NULL, 0) results in an empty string ("").
  260. // ConstructNonNull(NULL, non_zero) is undefined behavior.
  261. void ConstructNonNull(const char* buffer, size_t a_length) {
  262. char* const str = new char[a_length + 1];
  263. memcpy(str, buffer, a_length);
  264. str[a_length] = '\0';
  265. c_str_ = str;
  266. length_ = a_length;
  267. }
  268. const char* c_str_;
  269. size_t length_;
  270. }; // class String
  271. // Streams a String to an ostream. Each '\0' character in the String
  272. // is replaced with "\\0".
  273. inline ::std::ostream& operator<<(::std::ostream& os, const String& str) {
  274. if (str.c_str() == NULL) {
  275. os << "(null)";
  276. } else {
  277. const char* const c_str = str.c_str();
  278. for (size_t i = 0; i != str.length(); i++) {
  279. if (c_str[i] == '\0') {
  280. os << "\\0";
  281. } else {
  282. os << c_str[i];
  283. }
  284. }
  285. }
  286. return os;
  287. }
  288. // Gets the content of the stringstream's buffer as a String. Each '\0'
  289. // character in the buffer is replaced with "\\0".
  290. GTEST_API_ String StringStreamToString(::std::stringstream* stream);
  291. // Converts a streamable value to a String. A NULL pointer is
  292. // converted to "(null)". When the input value is a ::string,
  293. // ::std::string, ::wstring, or ::std::wstring object, each NUL
  294. // character in it is replaced with "\\0".
  295. // Declared here but defined in gtest.h, so that it has access
  296. // to the definition of the Message class, required by the ARM
  297. // compiler.
  298. template <typename T>
  299. String StreamableToString(const T& streamable);
  300. } // namespace internal
  301. } // namespace testing
  302. #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_