179 lines · c
1// Copyright 2005, Google Inc.2// All rights reserved.3//4// Redistribution and use in source and binary forms, with or without5// modification, are permitted provided that the following conditions are6// met:7//8// * Redistributions of source code must retain the above copyright9// notice, this list of conditions and the following disclaimer.10// * Redistributions in binary form must reproduce the above11// copyright notice, this list of conditions and the following disclaimer12// in the documentation and/or other materials provided with the13// distribution.14// * Neither the name of Google Inc. nor the names of its15// contributors may be used to endorse or promote products derived from16// this software without specific prior written permission.17//18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29 30// The Google C++ Testing and Mocking Framework (Google Test)31//32// This header file declares the String class and functions used internally by33// Google Test. They are subject to change without notice. They should not used34// by code external to Google Test.35//36// This header file is #included by gtest-internal.h.37// It should not be #included by other files.38 39// IWYU pragma: private, include "gtest/gtest.h"40// IWYU pragma: friend gtest/.*41// IWYU pragma: friend gmock/.*42 43#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_44#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_45 46#ifdef __BORLANDC__47// string.h is not guaranteed to provide strcpy on C++ Builder.48#include <mem.h>49#endif50 51#include <string.h>52 53#include <cstdint>54#include <sstream>55#include <string>56 57#include "gtest/internal/gtest-port.h"58 59namespace testing {60namespace internal {61 62// String - an abstract class holding static string utilities.63class GTEST_API_ String {64 public:65 // Static utility methods66 67 // Clones a 0-terminated C string, allocating memory using new. The68 // caller is responsible for deleting the return value using69 // delete[]. Returns the cloned string, or NULL if the input is70 // NULL.71 //72 // This is different from strdup() in string.h, which allocates73 // memory using malloc().74 static const char* CloneCString(const char* c_str);75 76#ifdef GTEST_OS_WINDOWS_MOBILE77 // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be78 // able to pass strings to Win32 APIs on CE we need to convert them79 // to 'Unicode', UTF-16.80 81 // Creates a UTF-16 wide string from the given ANSI string, allocating82 // memory using new. The caller is responsible for deleting the return83 // value using delete[]. Returns the wide string, or NULL if the84 // input is NULL.85 //86 // The wide string is created using the ANSI codepage (CP_ACP) to87 // match the behaviour of the ANSI versions of Win32 calls and the88 // C runtime.89 static LPCWSTR AnsiToUtf16(const char* c_str);90 91 // Creates an ANSI string from the given wide string, allocating92 // memory using new. The caller is responsible for deleting the return93 // value using delete[]. Returns the ANSI string, or NULL if the94 // input is NULL.95 //96 // The returned string is created using the ANSI codepage (CP_ACP) to97 // match the behaviour of the ANSI versions of Win32 calls and the98 // C runtime.99 static const char* Utf16ToAnsi(LPCWSTR utf16_str);100#endif101 102 // Compares two C strings. Returns true if and only if they have the same103 // content.104 //105 // Unlike strcmp(), this function can handle NULL argument(s). A106 // NULL C string is considered different to any non-NULL C string,107 // including the empty string.108 static bool CStringEquals(const char* lhs, const char* rhs);109 110 // Converts a wide C string to a String using the UTF-8 encoding.111 // NULL will be converted to "(null)". If an error occurred during112 // the conversion, "(failed to convert from wide string)" is113 // returned.114 static std::string ShowWideCString(const wchar_t* wide_c_str);115 116 // Compares two wide C strings. Returns true if and only if they have the117 // same content.118 //119 // Unlike wcscmp(), this function can handle NULL argument(s). A120 // NULL C string is considered different to any non-NULL C string,121 // including the empty string.122 static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs);123 124 // Compares two C strings, ignoring case. Returns true if and only if125 // they have the same content.126 //127 // Unlike strcasecmp(), this function can handle NULL argument(s).128 // A NULL C string is considered different to any non-NULL C string,129 // including the empty string.130 static bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs);131 132 // Compares two wide C strings, ignoring case. Returns true if and only if133 // they have the same content.134 //135 // Unlike wcscasecmp(), this function can handle NULL argument(s).136 // A NULL C string is considered different to any non-NULL wide C string,137 // including the empty string.138 // NB: The implementations on different platforms slightly differ.139 // On windows, this method uses _wcsicmp which compares according to LC_CTYPE140 // environment variable. On GNU platform this method uses wcscasecmp141 // which compares according to LC_CTYPE category of the current locale.142 // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the143 // current locale.144 static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs,145 const wchar_t* rhs);146 147 // Returns true if and only if the given string ends with the given suffix,148 // ignoring case. Any string is considered to end with an empty suffix.149 static bool EndsWithCaseInsensitive(const std::string& str,150 const std::string& suffix);151 152 // Formats an int value as "%02d".153 static std::string FormatIntWidth2(int value); // "%02d" for width == 2154 155 // Formats an int value to given width with leading zeros.156 static std::string FormatIntWidthN(int value, int width);157 158 // Formats an int value as "%X".159 static std::string FormatHexInt(int value);160 161 // Formats an int value as "%X".162 static std::string FormatHexUInt32(uint32_t value);163 164 // Formats a byte as "%02X".165 static std::string FormatByte(unsigned char value);166 167 private:168 String(); // Not meant to be instantiated.169}; // class String170 171// Gets the content of the stringstream's buffer as an std::string. Each '\0'172// character in the buffer is replaced with "\\0".173GTEST_API_ std::string StringStreamToString(::std::stringstream* stream);174 175} // namespace internal176} // namespace testing177 178#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_179