1 // Copyright 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // The Google C++ Testing and Mocking Framework (Google Test)
32 // This header file declares the String class and functions used internally by
33 // Google Test. They are subject to change without notice. They should not used
34 // by code external to Google Test.
36 // This header file is #included by gtest-internal.h.
37 // It should not be #included by other files.
39 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
40 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
43 // string.h is not guaranteed to provide strcpy on C++ Builder.
51 #include "gtest/internal/gtest-port.h"
56 // String - an abstract class holding static string utilities.
57 class GTEST_API_ String {
59 // Static utility methods
61 // Clones a 0-terminated C string, allocating memory using new. The
62 // caller is responsible for deleting the return value using
63 // delete[]. Returns the cloned string, or NULL if the input is
66 // This is different from strdup() in string.h, which allocates
67 // memory using malloc().
68 static const char* CloneCString(const char* c_str);
70 #if GTEST_OS_WINDOWS_MOBILE
71 // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be
72 // able to pass strings to Win32 APIs on CE we need to convert them
73 // to 'Unicode', UTF-16.
75 // Creates a UTF-16 wide string from the given ANSI string, allocating
76 // memory using new. The caller is responsible for deleting the return
77 // value using delete[]. Returns the wide string, or NULL if the
80 // The wide string is created using the ANSI codepage (CP_ACP) to
81 // match the behaviour of the ANSI versions of Win32 calls and the
83 static LPCWSTR AnsiToUtf16(const char* c_str);
85 // Creates an ANSI string from the given wide string, allocating
86 // memory using new. The caller is responsible for deleting the return
87 // value using delete[]. Returns the ANSI string, or NULL if the
90 // The returned string is created using the ANSI codepage (CP_ACP) to
91 // match the behaviour of the ANSI versions of Win32 calls and the
93 static const char* Utf16ToAnsi(LPCWSTR utf16_str);
96 // Compares two C strings. Returns true if and only if they have the same
99 // Unlike strcmp(), this function can handle NULL argument(s). A
100 // NULL C string is considered different to any non-NULL C string,
101 // including the empty string.
102 static bool CStringEquals(const char* lhs, const char* rhs);
104 // Converts a wide C string to a String using the UTF-8 encoding.
105 // NULL will be converted to "(null)". If an error occurred during
106 // the conversion, "(failed to convert from wide string)" is
108 static std::string ShowWideCString(const wchar_t* wide_c_str);
110 // Compares two wide C strings. Returns true if and only if they have the
113 // Unlike wcscmp(), this function can handle NULL argument(s). A
114 // NULL C string is considered different to any non-NULL C string,
115 // including the empty string.
116 static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs);
118 // Compares two C strings, ignoring case. Returns true if and only if
119 // they have the same content.
121 // Unlike strcasecmp(), this function can handle NULL argument(s).
122 // A NULL C string is considered different to any non-NULL C string,
123 // including the empty string.
124 static bool CaseInsensitiveCStringEquals(const char* lhs,
127 // Compares two wide C strings, ignoring case. Returns true if and only if
128 // they have the same content.
130 // Unlike wcscasecmp(), this function can handle NULL argument(s).
131 // A NULL C string is considered different to any non-NULL wide C string,
132 // including the empty string.
133 // NB: The implementations on different platforms slightly differ.
134 // On windows, this method uses _wcsicmp which compares according to LC_CTYPE
135 // environment variable. On GNU platform this method uses wcscasecmp
136 // which compares according to LC_CTYPE category of the current locale.
137 // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the
139 static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs,
142 // Returns true if and only if the given string ends with the given suffix,
143 // ignoring case. Any string is considered to end with an empty suffix.
144 static bool EndsWithCaseInsensitive(
145 const std::string& str, const std::string& suffix);
147 // Formats an int value as "%02d".
148 static std::string FormatIntWidth2(int value); // "%02d" for width == 2
150 // Formats an int value to given width with leading zeros.
151 static std::string FormatIntWidthN(int value, int width);
153 // Formats an int value as "%X".
154 static std::string FormatHexInt(int value);
156 // Formats an int value as "%X".
157 static std::string FormatHexUInt32(uint32_t value);
159 // Formats a byte as "%02X".
160 static std::string FormatByte(unsigned char value);
163 String(); // Not meant to be instantiated.
166 // Gets the content of the stringstream's buffer as an std::string. Each '\0'
167 // character in the buffer is replaced with "\\0".
168 GTEST_API_ std::string StringStreamToString(::std::stringstream* stream);
170 } // namespace internal
171 } // namespace testing
173 #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_