14b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Copyright 2005, Google Inc. 24b6829f0d28990dd645e16386eb226d0f10c8731shiqian// All rights reserved. 34b6829f0d28990dd645e16386eb226d0f10c8731shiqian// 44b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Redistribution and use in source and binary forms, with or without 54b6829f0d28990dd645e16386eb226d0f10c8731shiqian// modification, are permitted provided that the following conditions are 64b6829f0d28990dd645e16386eb226d0f10c8731shiqian// met: 74b6829f0d28990dd645e16386eb226d0f10c8731shiqian// 84b6829f0d28990dd645e16386eb226d0f10c8731shiqian// * Redistributions of source code must retain the above copyright 94b6829f0d28990dd645e16386eb226d0f10c8731shiqian// notice, this list of conditions and the following disclaimer. 104b6829f0d28990dd645e16386eb226d0f10c8731shiqian// * Redistributions in binary form must reproduce the above 114b6829f0d28990dd645e16386eb226d0f10c8731shiqian// copyright notice, this list of conditions and the following disclaimer 124b6829f0d28990dd645e16386eb226d0f10c8731shiqian// in the documentation and/or other materials provided with the 134b6829f0d28990dd645e16386eb226d0f10c8731shiqian// distribution. 144b6829f0d28990dd645e16386eb226d0f10c8731shiqian// * Neither the name of Google Inc. nor the names of its 154b6829f0d28990dd645e16386eb226d0f10c8731shiqian// contributors may be used to endorse or promote products derived from 164b6829f0d28990dd645e16386eb226d0f10c8731shiqian// this software without specific prior written permission. 174b6829f0d28990dd645e16386eb226d0f10c8731shiqian// 184b6829f0d28990dd645e16386eb226d0f10c8731shiqian// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 194b6829f0d28990dd645e16386eb226d0f10c8731shiqian// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 204b6829f0d28990dd645e16386eb226d0f10c8731shiqian// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 214b6829f0d28990dd645e16386eb226d0f10c8731shiqian// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 224b6829f0d28990dd645e16386eb226d0f10c8731shiqian// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 234b6829f0d28990dd645e16386eb226d0f10c8731shiqian// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 244b6829f0d28990dd645e16386eb226d0f10c8731shiqian// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 254b6829f0d28990dd645e16386eb226d0f10c8731shiqian// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 264b6829f0d28990dd645e16386eb226d0f10c8731shiqian// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 274b6829f0d28990dd645e16386eb226d0f10c8731shiqian// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 284b6829f0d28990dd645e16386eb226d0f10c8731shiqian// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 294b6829f0d28990dd645e16386eb226d0f10c8731shiqian// 304b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee) 314b6829f0d28990dd645e16386eb226d0f10c8731shiqian// 324b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The Google C++ Testing Framework (Google Test) 334b6829f0d28990dd645e16386eb226d0f10c8731shiqian// 344b6829f0d28990dd645e16386eb226d0f10c8731shiqian// This header file declares the String class and functions used internally by 354b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Google Test. They are subject to change without notice. They should not used 364b6829f0d28990dd645e16386eb226d0f10c8731shiqian// by code external to Google Test. 374b6829f0d28990dd645e16386eb226d0f10c8731shiqian// 3821d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// This header file is #included by <gtest/internal/gtest-internal.h>. 394b6829f0d28990dd645e16386eb226d0f10c8731shiqian// It should not be #included by other files. 404b6829f0d28990dd645e16386eb226d0f10c8731shiqian 414b6829f0d28990dd645e16386eb226d0f10c8731shiqian#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 424b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 434b6829f0d28990dd645e16386eb226d0f10c8731shiqian 44d6b4941ba371a4de8b8ea6b69bf50741cbe97292vladlosev#ifdef __BORLANDC__ 45d6b4941ba371a4de8b8ea6b69bf50741cbe97292vladlosev// string.h is not guaranteed to provide strcpy on C++ Builder. 46733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# include <mem.h> 47d6b4941ba371a4de8b8ea6b69bf50741cbe97292vladlosev#endif 48d6b4941ba371a4de8b8ea6b69bf50741cbe97292vladlosev 494b6829f0d28990dd645e16386eb226d0f10c8731shiqian#include <string.h> 50f179f4ee7cc76e7103a726ebf666902b07f89659vladlosev#include <string> 51f179f4ee7cc76e7103a726ebf666902b07f89659vladlosev 5293fed47dbf8e6bc3d39d3f769cb5039551747257vladlosev#include "gtest/internal/gtest-port.h" 5393fed47dbf8e6bc3d39d3f769cb5039551747257vladlosev 544b6829f0d28990dd645e16386eb226d0f10c8731shiqiannamespace testing { 554b6829f0d28990dd645e16386eb226d0f10c8731shiqiannamespace internal { 564b6829f0d28990dd645e16386eb226d0f10c8731shiqian 5703c314931649a999b0cf5deb0a434a1009157416jgm@google.com// String - an abstract class holding static string utilities. 58e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanclass GTEST_API_ String { 594b6829f0d28990dd645e16386eb226d0f10c8731shiqian public: 604b6829f0d28990dd645e16386eb226d0f10c8731shiqian // Static utility methods 614b6829f0d28990dd645e16386eb226d0f10c8731shiqian 624b6829f0d28990dd645e16386eb226d0f10c8731shiqian // Clones a 0-terminated C string, allocating memory using new. The 634b6829f0d28990dd645e16386eb226d0f10c8731shiqian // caller is responsible for deleting the return value using 644b6829f0d28990dd645e16386eb226d0f10c8731shiqian // delete[]. Returns the cloned string, or NULL if the input is 654b6829f0d28990dd645e16386eb226d0f10c8731shiqian // NULL. 664b6829f0d28990dd645e16386eb226d0f10c8731shiqian // 674b6829f0d28990dd645e16386eb226d0f10c8731shiqian // This is different from strdup() in string.h, which allocates 684b6829f0d28990dd645e16386eb226d0f10c8731shiqian // memory using malloc(). 694b6829f0d28990dd645e16386eb226d0f10c8731shiqian static const char* CloneCString(const char* c_str); 704b6829f0d28990dd645e16386eb226d0f10c8731shiqian 71fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan#if GTEST_OS_WINDOWS_MOBILE 72dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be 73dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // able to pass strings to Win32 APIs on CE we need to convert them 74dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // to 'Unicode', UTF-16. 75dd4a17bc20377d250ed116b1083d851adbe45f25shiqian 76dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // Creates a UTF-16 wide string from the given ANSI string, allocating 77dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // memory using new. The caller is responsible for deleting the return 78dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // value using delete[]. Returns the wide string, or NULL if the 79dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // input is NULL. 80dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // 81dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // The wide string is created using the ANSI codepage (CP_ACP) to 82dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // match the behaviour of the ANSI versions of Win32 calls and the 83dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // C runtime. 84dd4a17bc20377d250ed116b1083d851adbe45f25shiqian static LPCWSTR AnsiToUtf16(const char* c_str); 85dd4a17bc20377d250ed116b1083d851adbe45f25shiqian 86dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // Creates an ANSI string from the given wide string, allocating 87dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // memory using new. The caller is responsible for deleting the return 88dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // value using delete[]. Returns the ANSI string, or NULL if the 89dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // input is NULL. 90dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // 91dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // The returned string is created using the ANSI codepage (CP_ACP) to 92dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // match the behaviour of the ANSI versions of Win32 calls and the 93dd4a17bc20377d250ed116b1083d851adbe45f25shiqian // C runtime. 94dd4a17bc20377d250ed116b1083d851adbe45f25shiqian static const char* Utf16ToAnsi(LPCWSTR utf16_str); 95dd4a17bc20377d250ed116b1083d851adbe45f25shiqian#endif 96dd4a17bc20377d250ed116b1083d851adbe45f25shiqian 974b6829f0d28990dd645e16386eb226d0f10c8731shiqian // Compares two C strings. Returns true iff they have the same content. 984b6829f0d28990dd645e16386eb226d0f10c8731shiqian // 994b6829f0d28990dd645e16386eb226d0f10c8731shiqian // Unlike strcmp(), this function can handle NULL argument(s). A 1004b6829f0d28990dd645e16386eb226d0f10c8731shiqian // NULL C string is considered different to any non-NULL C string, 1014b6829f0d28990dd645e16386eb226d0f10c8731shiqian // including the empty string. 1024b6829f0d28990dd645e16386eb226d0f10c8731shiqian static bool CStringEquals(const char* lhs, const char* rhs); 1034b6829f0d28990dd645e16386eb226d0f10c8731shiqian 1044b6829f0d28990dd645e16386eb226d0f10c8731shiqian // Converts a wide C string to a String using the UTF-8 encoding. 1054b6829f0d28990dd645e16386eb226d0f10c8731shiqian // NULL will be converted to "(null)". If an error occurred during 1064b6829f0d28990dd645e16386eb226d0f10c8731shiqian // the conversion, "(failed to convert from wide string)" is 1074b6829f0d28990dd645e16386eb226d0f10c8731shiqian // returned. 10803c314931649a999b0cf5deb0a434a1009157416jgm@google.com static std::string ShowWideCString(const wchar_t* wide_c_str); 1094b6829f0d28990dd645e16386eb226d0f10c8731shiqian 1104b6829f0d28990dd645e16386eb226d0f10c8731shiqian // Compares two wide C strings. Returns true iff they have the same 1114b6829f0d28990dd645e16386eb226d0f10c8731shiqian // content. 1124b6829f0d28990dd645e16386eb226d0f10c8731shiqian // 1134b6829f0d28990dd645e16386eb226d0f10c8731shiqian // Unlike wcscmp(), this function can handle NULL argument(s). A 1144b6829f0d28990dd645e16386eb226d0f10c8731shiqian // NULL C string is considered different to any non-NULL C string, 1154b6829f0d28990dd645e16386eb226d0f10c8731shiqian // including the empty string. 1164b6829f0d28990dd645e16386eb226d0f10c8731shiqian static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs); 1174b6829f0d28990dd645e16386eb226d0f10c8731shiqian 1184b6829f0d28990dd645e16386eb226d0f10c8731shiqian // Compares two C strings, ignoring case. Returns true iff they 1194b6829f0d28990dd645e16386eb226d0f10c8731shiqian // have the same content. 1204b6829f0d28990dd645e16386eb226d0f10c8731shiqian // 1214b6829f0d28990dd645e16386eb226d0f10c8731shiqian // Unlike strcasecmp(), this function can handle NULL argument(s). 1224b6829f0d28990dd645e16386eb226d0f10c8731shiqian // A NULL C string is considered different to any non-NULL C string, 1234b6829f0d28990dd645e16386eb226d0f10c8731shiqian // including the empty string. 1244b6829f0d28990dd645e16386eb226d0f10c8731shiqian static bool CaseInsensitiveCStringEquals(const char* lhs, 1254b6829f0d28990dd645e16386eb226d0f10c8731shiqian const char* rhs); 1264b6829f0d28990dd645e16386eb226d0f10c8731shiqian 127e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian // Compares two wide C strings, ignoring case. Returns true iff they 128e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian // have the same content. 129e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian // 130e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian // Unlike wcscasecmp(), this function can handle NULL argument(s). 131e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian // A NULL C string is considered different to any non-NULL wide C string, 132e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian // including the empty string. 133e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian // NB: The implementations on different platforms slightly differ. 134e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian // On windows, this method uses _wcsicmp which compares according to LC_CTYPE 135e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian // environment variable. On GNU platform this method uses wcscasecmp 136e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian // which compares according to LC_CTYPE category of the current locale. 137e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the 138e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian // current locale. 139e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs, 140e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian const wchar_t* rhs); 141e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian 14203c314931649a999b0cf5deb0a434a1009157416jgm@google.com // Returns true iff the given string ends with the given suffix, ignoring 14303c314931649a999b0cf5deb0a434a1009157416jgm@google.com // case. Any string is considered to end with an empty suffix. 14403c314931649a999b0cf5deb0a434a1009157416jgm@google.com static bool EndsWithCaseInsensitive( 14503c314931649a999b0cf5deb0a434a1009157416jgm@google.com const std::string& str, const std::string& suffix); 14603c314931649a999b0cf5deb0a434a1009157416jgm@google.com 1473b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com // Formats an int value as "%02d". 1483b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com static std::string FormatIntWidth2(int value); // "%02d" for width == 2 1493b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com 1503b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com // Formats an int value as "%X". 1513b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com static std::string FormatHexInt(int value); 1523b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com 1533b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com // Formats a byte as "%02X". 1543b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com static std::string FormatByte(unsigned char value); 1554b6829f0d28990dd645e16386eb226d0f10c8731shiqian 1564b6829f0d28990dd645e16386eb226d0f10c8731shiqian private: 15703c314931649a999b0cf5deb0a434a1009157416jgm@google.com String(); // Not meant to be instantiated. 15889be5763249cbab785abfa310fb1cd6b5e9c4adfzhanyong.wan}; // class String 15989be5763249cbab785abfa310fb1cd6b5e9c4adfzhanyong.wan 16003c314931649a999b0cf5deb0a434a1009157416jgm@google.com// Gets the content of the stringstream's buffer as an std::string. Each '\0' 1614b6829f0d28990dd645e16386eb226d0f10c8731shiqian// character in the buffer is replaced with "\\0". 16203c314931649a999b0cf5deb0a434a1009157416jgm@google.comGTEST_API_ std::string StringStreamToString(::std::stringstream* stream); 1634b6829f0d28990dd645e16386eb226d0f10c8731shiqian 1644b6829f0d28990dd645e16386eb226d0f10c8731shiqian} // namespace internal 1654b6829f0d28990dd645e16386eb226d0f10c8731shiqian} // namespace testing 1664b6829f0d28990dd645e16386eb226d0f10c8731shiqian 1674b6829f0d28990dd645e16386eb226d0f10c8731shiqian#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ 168