11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2005, Google Inc.
21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All rights reserved.
31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Redistribution and use in source and binary forms, with or without
51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// modification, are permitted provided that the following conditions are
61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// met:
71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions of source code must retain the above copyright
91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// notice, this list of conditions and the following disclaimer.
101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions in binary form must reproduce the above
111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// copyright notice, this list of conditions and the following disclaimer
121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the documentation and/or other materials provided with the
131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// distribution.
141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Neither the name of Google Inc. nor the names of its
151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// contributors may be used to endorse or promote products derived from
161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this software without specific prior written permission.
171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The Google C++ Testing Framework (Google Test)
331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This header file declares the String class and functions used internally by
351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test.  They are subject to change without notice. They should not used
361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// by code external to Google Test.
371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This header file is #included by <gtest/internal/gtest-internal.h>.
391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// It should not be #included by other files.
401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#ifdef __BORLANDC__
4541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// string.h is not guaranteed to provide strcpy on C++ Builder.
4641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <mem.h>
4741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif
4841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <string.h>
501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <string>
511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
52fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#include "gtest/internal/gtest-port.h"
53fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing {
551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
57fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// String - an abstract class holding static string utilities.
5841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ String {
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Static utility methods
611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Clones a 0-terminated C string, allocating memory using new.  The
631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // caller is responsible for deleting the return value using
641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // delete[].  Returns the cloned string, or NULL if the input is
651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // NULL.
661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // This is different from strdup() in string.h, which allocates
681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // memory using malloc().
691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static const char* CloneCString(const char* c_str);
701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS_MOBILE
721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be
731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // able to pass strings to Win32 APIs on CE we need to convert them
741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // to 'Unicode', UTF-16.
751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Creates a UTF-16 wide string from the given ANSI string, allocating
771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // memory using new. The caller is responsible for deleting the return
781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // value using delete[]. Returns the wide string, or NULL if the
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // input is NULL.
801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The wide string is created using the ANSI codepage (CP_ACP) to
821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // match the behaviour of the ANSI versions of Win32 calls and the
831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // C runtime.
841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static LPCWSTR AnsiToUtf16(const char* c_str);
851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Creates an ANSI string from the given wide string, allocating
871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // memory using new. The caller is responsible for deleting the return
881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // value using delete[]. Returns the ANSI string, or NULL if the
891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // input is NULL.
901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The returned string is created using the ANSI codepage (CP_ACP) to
921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // match the behaviour of the ANSI versions of Win32 calls and the
931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // C runtime.
941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static const char* Utf16ToAnsi(LPCWSTR utf16_str);
951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif
961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Compares two C strings.  Returns true iff they have the same content.
981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Unlike strcmp(), this function can handle NULL argument(s).  A
1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // NULL C string is considered different to any non-NULL C string,
1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // including the empty string.
1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static bool CStringEquals(const char* lhs, const char* rhs);
1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Converts a wide C string to a String using the UTF-8 encoding.
1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // NULL will be converted to "(null)".  If an error occurred during
1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // the conversion, "(failed to convert from wide string)" is
1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // returned.
108fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static std::string ShowWideCString(const wchar_t* wide_c_str);
1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Compares two wide C strings.  Returns true iff they have the same
1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // content.
1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Unlike wcscmp(), this function can handle NULL argument(s).  A
1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // NULL C string is considered different to any non-NULL C string,
1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // including the empty string.
1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs);
1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Compares two C strings, ignoring case.  Returns true iff they
1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // have the same content.
1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Unlike strcasecmp(), this function can handle NULL argument(s).
1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // A NULL C string is considered different to any non-NULL C string,
1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // including the empty string.
1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static bool CaseInsensitiveCStringEquals(const char* lhs,
1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                           const char* rhs);
1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Compares two wide C strings, ignoring case.  Returns true iff they
1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // have the same content.
1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Unlike wcscasecmp(), this function can handle NULL argument(s).
1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // A NULL C string is considered different to any non-NULL wide C string,
1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // including the empty string.
1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // NB: The implementations on different platforms slightly differ.
1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // On windows, this method uses _wcsicmp which compares according to LC_CTYPE
1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // environment variable. On GNU platform this method uses wcscasecmp
1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // which compares according to LC_CTYPE category of the current locale.
1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the
1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // current locale.
1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs,
1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                               const wchar_t* rhs);
1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
142fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // Returns true iff the given string ends with the given suffix, ignoring
143fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // case. Any string is considered to end with an empty suffix.
144fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static bool EndsWithCaseInsensitive(
145fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      const std::string& str, const std::string& suffix);
1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
147fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // Formats an int value as "%02d".
148fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static std::string FormatIntWidth2(int value);  // "%02d" for width == 2
1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
150fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // Formats an int value as "%X".
151fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static std::string FormatHexInt(int value);
1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
153fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // Formats a byte as "%02X".
154fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static std::string FormatByte(unsigned char value);
1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
157fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  String();  // Not meant to be instantiated.
15841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot};  // class String
15941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
160fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Gets the content of the stringstream's buffer as an std::string.  Each '\0'
1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// character in the buffer is replaced with "\\0".
162fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesGTEST_API_ std::string StringStreamToString(::std::stringstream* stream);
1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace internal
1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace testing
1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
168