/Unittests/googletest/include/gtest/internal/gtest-string.h

http://unladen-swallow.googlecode.com/ · C++ Header · 348 lines · 106 code · 47 blank · 195 comment · 22 complexity · f8c8e53f11bb0f0f4987e3735f76a563 MD5 · raw file

  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. #include <string.h>
  43. #include <gtest/internal/gtest-port.h>
  44. #if GTEST_HAS_GLOBAL_STRING || GTEST_HAS_STD_STRING
  45. #include <string>
  46. #endif // GTEST_HAS_GLOBAL_STRING || GTEST_HAS_STD_STRING
  47. namespace testing {
  48. namespace internal {
  49. // String - a UTF-8 string class.
  50. //
  51. // We cannot use std::string as Microsoft's STL implementation in
  52. // Visual C++ 7.1 has problems when exception is disabled. There is a
  53. // hack to work around this, but we've seen cases where the hack fails
  54. // to work.
  55. //
  56. // Also, String is different from std::string in that it can represent
  57. // both NULL and the empty string, while std::string cannot represent
  58. // NULL.
  59. //
  60. // NULL and the empty string are considered different. NULL is less
  61. // than anything (including the empty string) except itself.
  62. //
  63. // This class only provides minimum functionality necessary for
  64. // implementing Google Test. We do not intend to implement a full-fledged
  65. // string class here.
  66. //
  67. // Since the purpose of this class is to provide a substitute for
  68. // std::string on platforms where it cannot be used, we define a copy
  69. // constructor and assignment operators such that we don't need
  70. // conditional compilation in a lot of places.
  71. //
  72. // In order to make the representation efficient, the d'tor of String
  73. // is not virtual. Therefore DO NOT INHERIT FROM String.
  74. class String {
  75. public:
  76. // Static utility methods
  77. // Returns the input enclosed in double quotes if it's not NULL;
  78. // otherwise returns "(null)". For example, "\"Hello\"" is returned
  79. // for input "Hello".
  80. //
  81. // This is useful for printing a C string in the syntax of a literal.
  82. //
  83. // Known issue: escape sequences are not handled yet.
  84. static String ShowCStringQuoted(const char* c_str);
  85. // Clones a 0-terminated C string, allocating memory using new. The
  86. // caller is responsible for deleting the return value using
  87. // delete[]. Returns the cloned string, or NULL if the input is
  88. // NULL.
  89. //
  90. // This is different from strdup() in string.h, which allocates
  91. // memory using malloc().
  92. static const char* CloneCString(const char* c_str);
  93. #if GTEST_OS_WINDOWS_MOBILE
  94. // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be
  95. // able to pass strings to Win32 APIs on CE we need to convert them
  96. // to 'Unicode', UTF-16.
  97. // Creates a UTF-16 wide string from the given ANSI string, allocating
  98. // memory using new. The caller is responsible for deleting the return
  99. // value using delete[]. Returns the wide string, or NULL if the
  100. // input is NULL.
  101. //
  102. // The wide string is created using the ANSI codepage (CP_ACP) to
  103. // match the behaviour of the ANSI versions of Win32 calls and the
  104. // C runtime.
  105. static LPCWSTR AnsiToUtf16(const char* c_str);
  106. // Creates an ANSI string from the given wide string, allocating
  107. // memory using new. The caller is responsible for deleting the return
  108. // value using delete[]. Returns the ANSI string, or NULL if the
  109. // input is NULL.
  110. //
  111. // The returned string is created using the ANSI codepage (CP_ACP) to
  112. // match the behaviour of the ANSI versions of Win32 calls and the
  113. // C runtime.
  114. static const char* Utf16ToAnsi(LPCWSTR utf16_str);
  115. #endif
  116. // Compares two C strings. Returns true iff they have the same content.
  117. //
  118. // Unlike strcmp(), this function can handle NULL argument(s). A
  119. // NULL C string is considered different to any non-NULL C string,
  120. // including the empty string.
  121. static bool CStringEquals(const char* lhs, const char* rhs);
  122. // Converts a wide C string to a String using the UTF-8 encoding.
  123. // NULL will be converted to "(null)". If an error occurred during
  124. // the conversion, "(failed to convert from wide string)" is
  125. // returned.
  126. static String ShowWideCString(const wchar_t* wide_c_str);
  127. // Similar to ShowWideCString(), except that this function encloses
  128. // the converted string in double quotes.
  129. static String ShowWideCStringQuoted(const wchar_t* wide_c_str);
  130. // Compares two wide C strings. Returns true iff they have the same
  131. // content.
  132. //
  133. // Unlike wcscmp(), this function can handle NULL argument(s). A
  134. // NULL C string is considered different to any non-NULL C string,
  135. // including the empty string.
  136. static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs);
  137. // Compares two C strings, ignoring case. Returns true iff they
  138. // have the same content.
  139. //
  140. // Unlike strcasecmp(), this function can handle NULL argument(s).
  141. // A NULL C string is considered different to any non-NULL C string,
  142. // including the empty string.
  143. static bool CaseInsensitiveCStringEquals(const char* lhs,
  144. const char* rhs);
  145. // Compares two wide C strings, ignoring case. Returns true iff they
  146. // have the same content.
  147. //
  148. // Unlike wcscasecmp(), this function can handle NULL argument(s).
  149. // A NULL C string is considered different to any non-NULL wide C string,
  150. // including the empty string.
  151. // NB: The implementations on different platforms slightly differ.
  152. // On windows, this method uses _wcsicmp which compares according to LC_CTYPE
  153. // environment variable. On GNU platform this method uses wcscasecmp
  154. // which compares according to LC_CTYPE category of the current locale.
  155. // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the
  156. // current locale.
  157. static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs,
  158. const wchar_t* rhs);
  159. // Formats a list of arguments to a String, using the same format
  160. // spec string as for printf.
  161. //
  162. // We do not use the StringPrintf class as it is not universally
  163. // available.
  164. //
  165. // The result is limited to 4096 characters (including the tailing
  166. // 0). If 4096 characters are not enough to format the input,
  167. // "<buffer exceeded>" is returned.
  168. static String Format(const char* format, ...);
  169. // C'tors
  170. // The default c'tor constructs a NULL string.
  171. String() : c_str_(NULL), length_(0) {}
  172. // Constructs a String by cloning a 0-terminated C string.
  173. String(const char* c_str) { // NOLINT
  174. if (c_str == NULL) {
  175. c_str_ = NULL;
  176. length_ = 0;
  177. } else {
  178. ConstructNonNull(c_str, strlen(c_str));
  179. }
  180. }
  181. // Constructs a String by copying a given number of chars from a
  182. // buffer. E.g. String("hello", 3) creates the string "hel",
  183. // String("a\0bcd", 4) creates "a\0bc", String(NULL, 0) creates "",
  184. // and String(NULL, 1) results in access violation.
  185. String(const char* buffer, size_t length) {
  186. ConstructNonNull(buffer, length);
  187. }
  188. // The copy c'tor creates a new copy of the string. The two
  189. // String objects do not share content.
  190. String(const String& str) : c_str_(NULL), length_(0) { *this = str; }
  191. // D'tor. String is intended to be a final class, so the d'tor
  192. // doesn't need to be virtual.
  193. ~String() { delete[] c_str_; }
  194. // Allows a String to be implicitly converted to an ::std::string or
  195. // ::string, and vice versa. Converting a String containing a NULL
  196. // pointer to ::std::string or ::string is undefined behavior.
  197. // Converting a ::std::string or ::string containing an embedded NUL
  198. // character to a String will result in the prefix up to the first
  199. // NUL character.
  200. #if GTEST_HAS_STD_STRING
  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. #endif // GTEST_HAS_STD_STRING
  206. #if GTEST_HAS_GLOBAL_STRING
  207. String(const ::string& str) {
  208. ConstructNonNull(str.c_str(), str.length());
  209. }
  210. operator ::string() const { return ::string(c_str(), length()); }
  211. #endif // GTEST_HAS_GLOBAL_STRING
  212. // Returns true iff this is an empty string (i.e. "").
  213. bool empty() const { return (c_str() != NULL) && (length() == 0); }
  214. // Compares this with another String.
  215. // Returns < 0 if this is less than rhs, 0 if this is equal to rhs, or > 0
  216. // if this is greater than rhs.
  217. int Compare(const String& rhs) const;
  218. // Returns true iff this String equals the given C string. A NULL
  219. // string and a non-NULL string are considered not equal.
  220. bool operator==(const char* c_str) const { return Compare(c_str) == 0; }
  221. // Returns true iff this String is less than the given String. A
  222. // NULL string is considered less than "".
  223. bool operator<(const String& rhs) const { return Compare(rhs) < 0; }
  224. // Returns true iff this String doesn't equal the given C string. A NULL
  225. // string and a non-NULL string are considered not equal.
  226. bool operator!=(const char* c_str) const { return !(*this == c_str); }
  227. // Returns true iff this String ends with the given suffix. *Any*
  228. // String is considered to end with a NULL or empty suffix.
  229. bool EndsWith(const char* suffix) const;
  230. // Returns true iff this String ends with the given suffix, not considering
  231. // case. Any String is considered to end with a NULL or empty suffix.
  232. bool EndsWithCaseInsensitive(const char* suffix) const;
  233. // Returns the length of the encapsulated string, or 0 if the
  234. // string is NULL.
  235. size_t length() const { return length_; }
  236. // Gets the 0-terminated C string this String object represents.
  237. // The String object still owns the string. Therefore the caller
  238. // should NOT delete the return value.
  239. const char* c_str() const { return c_str_; }
  240. // Assigns a C string to this object. Self-assignment works.
  241. const String& operator=(const char* c_str) { return *this = String(c_str); }
  242. // Assigns a String object to this object. Self-assignment works.
  243. const String& operator=(const String& rhs) {
  244. if (this != &rhs) {
  245. delete[] c_str_;
  246. if (rhs.c_str() == NULL) {
  247. c_str_ = NULL;
  248. length_ = 0;
  249. } else {
  250. ConstructNonNull(rhs.c_str(), rhs.length());
  251. }
  252. }
  253. return *this;
  254. }
  255. private:
  256. // Constructs a non-NULL String from the given content. This
  257. // function can only be called when data_ has not been allocated.
  258. // ConstructNonNull(NULL, 0) results in an empty string ("").
  259. // ConstructNonNull(NULL, non_zero) is undefined behavior.
  260. void ConstructNonNull(const char* buffer, size_t length) {
  261. char* const str = new char[length + 1];
  262. memcpy(str, buffer, length);
  263. str[length] = '\0';
  264. c_str_ = str;
  265. length_ = length;
  266. }
  267. const char* c_str_;
  268. size_t length_;
  269. }; // class String
  270. // Streams a String to an ostream. Each '\0' character in the String
  271. // is replaced with "\\0".
  272. inline ::std::ostream& operator<<(::std::ostream& os, const String& str) {
  273. if (str.c_str() == NULL) {
  274. os << "(null)";
  275. } else {
  276. const char* const c_str = str.c_str();
  277. for (size_t i = 0; i != str.length(); i++) {
  278. if (c_str[i] == '\0') {
  279. os << "\\0";
  280. } else {
  281. os << c_str[i];
  282. }
  283. }
  284. }
  285. return os;
  286. }
  287. // Gets the content of the StrStream's buffer as a String. Each '\0'
  288. // character in the buffer is replaced with "\\0".
  289. String StrStreamToString(StrStream* stream);
  290. // Converts a streamable value to a String. A NULL pointer is
  291. // converted to "(null)". When the input value is a ::string,
  292. // ::std::string, ::wstring, or ::std::wstring object, each NUL
  293. // character in it is replaced with "\\0".
  294. // Declared here but defined in gtest.h, so that it has access
  295. // to the definition of the Message class, required by the ARM
  296. // compiler.
  297. template <typename T>
  298. String StreamableToString(const T& streamable);
  299. } // namespace internal
  300. } // namespace testing
  301. #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_