3
/* Copyright 2010 10gen Inc.
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
27
/** A StringData object wraps a 'const string&' or a 'const char*' without
28
* copying its contents. The most common usage is as a function argument that
29
* takes any of the two forms of strings above. Fundamentally, this class tries
30
* go around the fact that string literals in C++ are char[N]'s.
32
* Note that the object StringData wraps around must be alive while the StringData
37
/** Construct a StringData, for the case where the length of
38
* string is not known. 'c' must be a pointer to a null-terminated string.
40
StringData( const char* c )
41
: _data(c), _size(string::npos){}
43
/** Construct a StringData explicitly, for the case where the length of the string
44
* is already known. 'c' must be a pointer to a null-terminated string, and strlenOfc
45
* must be the length that strlen(c) would return, a.k.a the index of the
48
StringData( const char* c, size_t len )
49
: _data(c), _size(len) {}
51
/** Construct a StringData, for the case of a string. */
52
StringData( const std::string& s )
53
: _data(s.c_str()), _size(s.size()) {}
55
// Construct a StringData explicitly, for the case of a literal whose size is
56
// known at compile time.
59
StringData( const char (&val)[N], LiteralTag )
60
: _data(&val[0]), _size(N-1) {}
63
const char* data() const { return _data; }
65
if ( _size == string::npos )
66
_size = strlen( _data );
71
const char* const _data; // is always null terminated
72
mutable size_t _size; // 'size' does not include the null terminator