Path: blob/master/dep/googletest/include/gtest/internal/gtest-string.h
4808 views
// Copyright 2005, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// The Google C++ Testing and Mocking Framework (Google Test)30//31// This header file declares the String class and functions used internally by32// Google Test. They are subject to change without notice. They should not used33// by code external to Google Test.34//35// This header file is #included by gtest-internal.h.36// It should not be #included by other files.3738// IWYU pragma: private, include "gtest/gtest.h"39// IWYU pragma: friend gtest/.*40// IWYU pragma: friend gmock/.*4142#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_43#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_4445#ifdef __BORLANDC__46// string.h is not guaranteed to provide strcpy on C++ Builder.47#include <mem.h>48#endif4950#include <string.h>5152#include <cstdint>53#include <sstream>54#include <string>5556#include "gtest/internal/gtest-port.h"5758namespace testing {59namespace internal {6061// String - an abstract class holding static string utilities.62class GTEST_API_ String {63public:64// Static utility methods6566// Clones a 0-terminated C string, allocating memory using new. The67// caller is responsible for deleting the return value using68// delete[]. Returns the cloned string, or NULL if the input is69// NULL.70//71// This is different from strdup() in string.h, which allocates72// memory using malloc().73static const char* CloneCString(const char* c_str);7475#ifdef GTEST_OS_WINDOWS_MOBILE76// Windows CE does not have the 'ANSI' versions of Win32 APIs. To be77// able to pass strings to Win32 APIs on CE we need to convert them78// to 'Unicode', UTF-16.7980// Creates a UTF-16 wide string from the given ANSI string, allocating81// memory using new. The caller is responsible for deleting the return82// value using delete[]. Returns the wide string, or NULL if the83// input is NULL.84//85// The wide string is created using the ANSI codepage (CP_ACP) to86// match the behaviour of the ANSI versions of Win32 calls and the87// C runtime.88static LPCWSTR AnsiToUtf16(const char* c_str);8990// Creates an ANSI string from the given wide string, allocating91// memory using new. The caller is responsible for deleting the return92// value using delete[]. Returns the ANSI string, or NULL if the93// input is NULL.94//95// The returned string is created using the ANSI codepage (CP_ACP) to96// match the behaviour of the ANSI versions of Win32 calls and the97// C runtime.98static const char* Utf16ToAnsi(LPCWSTR utf16_str);99#endif100101// Compares two C strings. Returns true if and only if they have the same102// content.103//104// Unlike strcmp(), this function can handle NULL argument(s). A105// NULL C string is considered different to any non-NULL C string,106// including the empty string.107static bool CStringEquals(const char* lhs, const char* rhs);108109// Converts a wide C string to a String using the UTF-8 encoding.110// NULL will be converted to "(null)". If an error occurred during111// the conversion, "(failed to convert from wide string)" is112// returned.113static std::string ShowWideCString(const wchar_t* wide_c_str);114115// Compares two wide C strings. Returns true if and only if they have the116// same content.117//118// Unlike wcscmp(), this function can handle NULL argument(s). A119// NULL C string is considered different to any non-NULL C string,120// including the empty string.121static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs);122123// Compares two C strings, ignoring case. Returns true if and only if124// they have the same content.125//126// Unlike strcasecmp(), this function can handle NULL argument(s).127// A NULL C string is considered different to any non-NULL C string,128// including the empty string.129static bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs);130131// Compares two wide C strings, ignoring case. Returns true if and only if132// they have the same content.133//134// Unlike wcscasecmp(), this function can handle NULL argument(s).135// A NULL C string is considered different to any non-NULL wide C string,136// including the empty string.137// NB: The implementations on different platforms slightly differ.138// On windows, this method uses _wcsicmp which compares according to LC_CTYPE139// environment variable. On GNU platform this method uses wcscasecmp140// which compares according to LC_CTYPE category of the current locale.141// On MacOS X, it uses towlower, which also uses LC_CTYPE category of the142// current locale.143static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs,144const wchar_t* rhs);145146// Returns true if and only if the given string ends with the given suffix,147// ignoring case. Any string is considered to end with an empty suffix.148static bool EndsWithCaseInsensitive(const std::string& str,149const std::string& suffix);150151// Formats an int value as "%02d".152static std::string FormatIntWidth2(int value); // "%02d" for width == 2153154// Formats an int value to given width with leading zeros.155static std::string FormatIntWidthN(int value, int width);156157// Formats an int value as "%X".158static std::string FormatHexInt(int value);159160// Formats an int value as "%X".161static std::string FormatHexUInt32(uint32_t value);162163// Formats a byte as "%02X".164static std::string FormatByte(unsigned char value);165166private:167String(); // Not meant to be instantiated.168}; // class String169170// Gets the content of the stringstream's buffer as an std::string. Each '\0'171// character in the buffer is replaced with "\\0".172GTEST_API_ std::string StringStreamToString(::std::stringstream* stream);173174} // namespace internal175} // namespace testing176177#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_178179180