~ubuntu-branches/ubuntu/precise/protobuf/precise

« back to all changes in this revision

Viewing changes to src/gtest/internal/gtest-string.h

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2009-11-16 10:41:33 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20091116104133-ykhy3deg5l4975tw
Tags: 2.1.0-1ubuntu1
* Merge from Debian testing.
* Remaining Ubuntu changes:
  - Disable the death tests on IA64, now as a quilt patch.
  - Don't use python2.4, also as a quilt patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2005, Google Inc.
2
 
// All rights reserved.
3
 
//
4
 
// Redistribution and use in source and binary forms, with or without
5
 
// modification, are permitted provided that the following conditions are
6
 
// met:
7
 
//
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
13
 
// distribution.
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.
17
 
//
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.
29
 
//
30
 
// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
31
 
//
32
 
// The Google C++ Testing Framework (Google Test)
33
 
//
34
 
// This header file declares the String class and functions used internally by
35
 
// Google Test.  They are subject to change without notice. They should not used
36
 
// by code external to Google Test.
37
 
//
38
 
// This header file is #included by testing/base/internal/gtest-internal.h.
39
 
// It should not be #included by other files.
40
 
 
41
 
#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
42
 
#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_
43
 
 
44
 
#include <string.h>
45
 
 
46
 
#if defined(__APPLE__) && !defined(GTEST_NOT_MAC_FRAMEWORK_MODE)
47
 
// When using Google Test on the Mac as a framework, all the includes will be
48
 
// in the framework headers folder along with gtest.h.
49
 
// Define GTEST_NOT_MAC_FRAMEWORK_MODE if you are building Google Test on
50
 
// the Mac and are not using it as a framework.
51
 
// More info on frameworks available here:
52
 
// http://developer.apple.com/documentation/MacOSX/Conceptual/BPFrameworks/
53
 
// Concepts/WhatAreFrameworks.html.
54
 
#include "gtest-port.h"  // NOLINT
55
 
#else
56
 
#include <gtest/internal/gtest-port.h>
57
 
#endif  // defined(__APPLE__) && !defined(GTEST_NOT_MAC_FRAMEWORK_MODE)
58
 
 
59
 
namespace testing {
60
 
namespace internal {
61
 
 
62
 
// String - a UTF-8 string class.
63
 
//
64
 
// We cannot use std::string as Microsoft's STL implementation in
65
 
// Visual C++ 7.1 has problems when exception is disabled.  There is a
66
 
// hack to work around this, but we've seen cases where the hack fails
67
 
// to work.
68
 
//
69
 
// Also, String is different from std::string in that it can represent
70
 
// both NULL and the empty string, while std::string cannot represent
71
 
// NULL.
72
 
//
73
 
// NULL and the empty string are considered different.  NULL is less
74
 
// than anything (including the empty string) except itself.
75
 
//
76
 
// This class only provides minimum functionality necessary for
77
 
// implementing Google Test.  We do not intend to implement a full-fledged
78
 
// string class here.
79
 
//
80
 
// Since the purpose of this class is to provide a substitute for
81
 
// std::string on platforms where it cannot be used, we define a copy
82
 
// constructor and assignment operators such that we don't need
83
 
// conditional compilation in a lot of places.
84
 
//
85
 
// In order to make the representation efficient, the d'tor of String
86
 
// is not virtual.  Therefore DO NOT INHERIT FROM String.
87
 
class String {
88
 
 public:
89
 
  // Static utility methods
90
 
 
91
 
  // Returns the input if it's not NULL, otherwise returns "(null)".
92
 
  // This function serves two purposes:
93
 
  //
94
 
  // 1. ShowCString(NULL) has type 'const char *', instead of the
95
 
  // type of NULL (which is int).
96
 
  //
97
 
  // 2. In MSVC, streaming a null char pointer to StrStream generates
98
 
  // an access violation, so we need to convert NULL to "(null)"
99
 
  // before streaming it.
100
 
  static inline const char* ShowCString(const char* c_str) {
101
 
    return c_str ? c_str : "(null)";
102
 
  }
103
 
 
104
 
  // Returns the input enclosed in double quotes if it's not NULL;
105
 
  // otherwise returns "(null)".  For example, "\"Hello\"" is returned
106
 
  // for input "Hello".
107
 
  //
108
 
  // This is useful for printing a C string in the syntax of a literal.
109
 
  //
110
 
  // Known issue: escape sequences are not handled yet.
111
 
  static String ShowCStringQuoted(const char* c_str);
112
 
 
113
 
  // Clones a 0-terminated C string, allocating memory using new.  The
114
 
  // caller is responsible for deleting the return value using
115
 
  // delete[].  Returns the cloned string, or NULL if the input is
116
 
  // NULL.
117
 
  //
118
 
  // This is different from strdup() in string.h, which allocates
119
 
  // memory using malloc().
120
 
  static const char* CloneCString(const char* c_str);
121
 
 
122
 
  // Compares two C strings.  Returns true iff they have the same content.
123
 
  //
124
 
  // Unlike strcmp(), this function can handle NULL argument(s).  A
125
 
  // NULL C string is considered different to any non-NULL C string,
126
 
  // including the empty string.
127
 
  static bool CStringEquals(const char* lhs, const char* rhs);
128
 
 
129
 
  // Converts a wide C string to a String using the UTF-8 encoding.
130
 
  // NULL will be converted to "(null)".  If an error occurred during
131
 
  // the conversion, "(failed to convert from wide string)" is
132
 
  // returned.
133
 
  static String ShowWideCString(const wchar_t* wide_c_str);
134
 
 
135
 
  // Similar to ShowWideCString(), except that this function encloses
136
 
  // the converted string in double quotes.
137
 
  static String ShowWideCStringQuoted(const wchar_t* wide_c_str);
138
 
 
139
 
  // Compares two wide C strings.  Returns true iff they have the same
140
 
  // content.
141
 
  //
142
 
  // Unlike wcscmp(), this function can handle NULL argument(s).  A
143
 
  // NULL C string is considered different to any non-NULL C string,
144
 
  // including the empty string.
145
 
  static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs);
146
 
 
147
 
  // Compares two C strings, ignoring case.  Returns true iff they
148
 
  // have the same content.
149
 
  //
150
 
  // Unlike strcasecmp(), this function can handle NULL argument(s).
151
 
  // A NULL C string is considered different to any non-NULL C string,
152
 
  // including the empty string.
153
 
  static bool CaseInsensitiveCStringEquals(const char* lhs,
154
 
                                           const char* rhs);
155
 
 
156
 
  // Formats a list of arguments to a String, using the same format
157
 
  // spec string as for printf.
158
 
  //
159
 
  // We do not use the StringPrintf class as it is not universally
160
 
  // available.
161
 
  //
162
 
  // The result is limited to 4096 characters (including the tailing
163
 
  // 0).  If 4096 characters are not enough to format the input,
164
 
  // "<buffer exceeded>" is returned.
165
 
  static String Format(const char* format, ...);
166
 
 
167
 
  // C'tors
168
 
 
169
 
  // The default c'tor constructs a NULL string.
170
 
  String() : c_str_(NULL) {}
171
 
 
172
 
  // Constructs a String by cloning a 0-terminated C string.
173
 
  String(const char* c_str) : c_str_(NULL) {  // NOLINT
174
 
    *this = c_str;
175
 
  }
176
 
 
177
 
  // Constructs a String by copying a given number of chars from a
178
 
  // buffer.  E.g. String("hello", 3) will create the string "hel".
179
 
  String(const char* buffer, size_t len);
180
 
 
181
 
  // The copy c'tor creates a new copy of the string.  The two
182
 
  // String objects do not share content.
183
 
  String(const String& str) : c_str_(NULL) {
184
 
    *this = str;
185
 
  }
186
 
 
187
 
  // D'tor.  String is intended to be a final class, so the d'tor
188
 
  // doesn't need to be virtual.
189
 
  ~String() { delete[] c_str_; }
190
 
 
191
 
  // Returns true iff this is an empty string (i.e. "").
192
 
  bool empty() const {
193
 
    return (c_str_ != NULL) && (*c_str_ == '\0');
194
 
  }
195
 
 
196
 
  // Compares this with another String.
197
 
  // Returns < 0 if this is less than rhs, 0 if this is equal to rhs, or > 0
198
 
  // if this is greater than rhs.
199
 
  int Compare(const String& rhs) const;
200
 
 
201
 
  // Returns true iff this String equals the given C string.  A NULL
202
 
  // string and a non-NULL string are considered not equal.
203
 
  bool operator==(const char* c_str) const {
204
 
    return CStringEquals(c_str_, c_str);
205
 
  }
206
 
 
207
 
  // Returns true iff this String doesn't equal the given C string.  A NULL
208
 
  // string and a non-NULL string are considered not equal.
209
 
  bool operator!=(const char* c_str) const {
210
 
    return !CStringEquals(c_str_, c_str);
211
 
  }
212
 
 
213
 
  // Returns true iff this String ends with the given suffix.  *Any*
214
 
  // String is considered to end with a NULL or empty suffix.
215
 
  bool EndsWith(const char* suffix) const;
216
 
 
217
 
  // Returns true iff this String ends with the given suffix, not considering
218
 
  // case. Any String is considered to end with a NULL or empty suffix.
219
 
  bool EndsWithCaseInsensitive(const char* suffix) const;
220
 
 
221
 
  // Returns the length of the encapsulated string, or -1 if the
222
 
  // string is NULL.
223
 
  int GetLength() const {
224
 
    return c_str_ ? static_cast<int>(strlen(c_str_)) : -1;
225
 
  }
226
 
 
227
 
  // Gets the 0-terminated C string this String object represents.
228
 
  // The String object still owns the string.  Therefore the caller
229
 
  // should NOT delete the return value.
230
 
  const char* c_str() const { return c_str_; }
231
 
 
232
 
  // Sets the 0-terminated C string this String object represents.
233
 
  // The old string in this object is deleted, and this object will
234
 
  // own a clone of the input string.  This function copies only up to
235
 
  // length bytes (plus a terminating null byte), or until the first
236
 
  // null byte, whichever comes first.
237
 
  //
238
 
  // This function works even when the c_str parameter has the same
239
 
  // value as that of the c_str_ field.
240
 
  void Set(const char* c_str, size_t length);
241
 
 
242
 
  // Assigns a C string to this object.  Self-assignment works.
243
 
  const String& operator=(const char* c_str);
244
 
 
245
 
  // Assigns a String object to this object.  Self-assignment works.
246
 
  const String& operator=(const String &rhs) {
247
 
    *this = rhs.c_str_;
248
 
    return *this;
249
 
  }
250
 
 
251
 
 private:
252
 
  const char* c_str_;
253
 
};
254
 
 
255
 
// Streams a String to an ostream.
256
 
inline ::std::ostream& operator <<(::std::ostream& os, const String& str) {
257
 
  // We call String::ShowCString() to convert NULL to "(null)".
258
 
  // Otherwise we'll get an access violation on Windows.
259
 
  return os << String::ShowCString(str.c_str());
260
 
}
261
 
 
262
 
// Gets the content of the StrStream's buffer as a String.  Each '\0'
263
 
// character in the buffer is replaced with "\\0".
264
 
String StrStreamToString(StrStream* stream);
265
 
 
266
 
// Converts a streamable value to a String.  A NULL pointer is
267
 
// converted to "(null)".  When the input value is a ::string,
268
 
// ::std::string, ::wstring, or ::std::wstring object, each NUL
269
 
// character in it is replaced with "\\0".
270
 
 
271
 
// Declared here but defined in gtest.h, so that it has access
272
 
// to the definition of the Message class, required by the ARM
273
 
// compiler.
274
 
template <typename T>
275
 
String StreamableToString(const T& streamable);
276
 
 
277
 
}  // namespace internal
278
 
}  // namespace testing
279
 
 
280
 
#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_