gtest-string.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <string>
  48. #include "gtest/internal/gtest-port.h"
  49. namespace testing {
  50. namespace internal {
  51. // String - an abstract class holding static string utilities.
  52. class GTEST_API_ String {
  53. public:
  54. // Static utility methods
  55. // Clones a 0-terminated C string, allocating memory using new. The
  56. // caller is responsible for deleting the return value using
  57. // delete[]. Returns the cloned string, or NULL if the input is
  58. // NULL.
  59. //
  60. // This is different from strdup() in string.h, which allocates
  61. // memory using malloc().
  62. static const char* CloneCString(const char* c_str);
  63. #if GTEST_OS_WINDOWS_MOBILE
  64. // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be
  65. // able to pass strings to Win32 APIs on CE we need to convert them
  66. // to 'Unicode', UTF-16.
  67. // Creates a UTF-16 wide string from the given ANSI string, allocating
  68. // memory using new. The caller is responsible for deleting the return
  69. // value using delete[]. Returns the wide string, or NULL if the
  70. // input is NULL.
  71. //
  72. // The wide string is created using the ANSI codepage (CP_ACP) to
  73. // match the behaviour of the ANSI versions of Win32 calls and the
  74. // C runtime.
  75. static LPCWSTR AnsiToUtf16(const char* c_str);
  76. // Creates an ANSI string from the given wide string, allocating
  77. // memory using new. The caller is responsible for deleting the return
  78. // value using delete[]. Returns the ANSI string, or NULL if the
  79. // input is NULL.
  80. //
  81. // The returned string is created using the ANSI codepage (CP_ACP) to
  82. // match the behaviour of the ANSI versions of Win32 calls and the
  83. // C runtime.
  84. static const char* Utf16ToAnsi(LPCWSTR utf16_str);
  85. #endif
  86. // Compares two C strings. Returns true iff they have the same content.
  87. //
  88. // Unlike strcmp(), this function can handle NULL argument(s). A
  89. // NULL C string is considered different to any non-NULL C string,
  90. // including the empty string.
  91. static bool CStringEquals(const char* lhs, const char* rhs);
  92. // Converts a wide C string to a String using the UTF-8 encoding.
  93. // NULL will be converted to "(null)". If an error occurred during
  94. // the conversion, "(failed to convert from wide string)" is
  95. // returned.
  96. static std::string ShowWideCString(const wchar_t* wide_c_str);
  97. // Compares two wide C strings. Returns true iff they have the same
  98. // content.
  99. //
  100. // Unlike wcscmp(), this function can handle NULL argument(s). A
  101. // NULL C string is considered different to any non-NULL C string,
  102. // including the empty string.
  103. static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs);
  104. // Compares two C strings, ignoring case. Returns true iff they
  105. // have the same content.
  106. //
  107. // Unlike strcasecmp(), this function can handle NULL argument(s).
  108. // A NULL C string is considered different to any non-NULL C string,
  109. // including the empty string.
  110. static bool CaseInsensitiveCStringEquals(const char* lhs,
  111. const char* rhs);
  112. // Compares two wide C strings, ignoring case. Returns true iff they
  113. // have the same content.
  114. //
  115. // Unlike wcscasecmp(), this function can handle NULL argument(s).
  116. // A NULL C string is considered different to any non-NULL wide C string,
  117. // including the empty string.
  118. // NB: The implementations on different platforms slightly differ.
  119. // On windows, this method uses _wcsicmp which compares according to LC_CTYPE
  120. // environment variable. On GNU platform this method uses wcscasecmp
  121. // which compares according to LC_CTYPE category of the current locale.
  122. // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the
  123. // current locale.
  124. static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs,
  125. const wchar_t* rhs);
  126. // Returns true iff the given string ends with the given suffix, ignoring
  127. // case. Any string is considered to end with an empty suffix.
  128. static bool EndsWithCaseInsensitive(
  129. const std::string& str, const std::string& suffix);
  130. // Formats an int value as "%02d".
  131. static std::string FormatIntWidth2(int value); // "%02d" for width == 2
  132. // Formats an int value as "%X".
  133. static std::string FormatHexInt(int value);
  134. // Formats a byte as "%02X".
  135. static std::string FormatByte(unsigned char value);
  136. private:
  137. String(); // Not meant to be instantiated.
  138. }; // class String
  139. // Gets the content of the stringstream's buffer as an std::string. Each '\0'
  140. // character in the buffer is replaced with "\\0".
  141. GTEST_API_ std::string StringStreamToString(::std::stringstream* stream);
  142. } // namespace internal
  143. } // namespace testing
  144. #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_