MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
mongo::StringData Class Reference

A StringData object wraps a 'const string&' or a 'const char*' without copying its contents. More...

#include <string_data.h>

Classes

struct  Hasher
 Functor compatible with std::hash for std::unordered_{map,set} Warning: The hash function is subject to change. More...
 
struct  LiteralTag
 Constructs a StringData explicitly, for the case of a literal whose size is known at compile time. More...
 

Public Member Functions

 StringData ()
 Constructs an empty string data.
 
 StringData (const char *str)
 Constructs a StringData, for the case where the length of string is not known.
 
 StringData (const char *c, size_t len)
 Constructs a StringData explicitly, for the case where the length of the string is already known.
 
 StringData (const std::string &s)
 Constructs a StringData, for the case of a string.
 
int compare (const StringData &other) const
 Returns -1, 0, or 1 if 'this' is less, equal, or greater than 'other' in lexicographical order.
 
bool equalCaseInsensitive (const StringData &other) const
 note: this uses tolower, and therefore does not handle come languages correctly.
 
bool startsWith (const StringData &prefix) const
 Returns true if 'prefix' is a substring of this instance, anchored at position 0.
 
bool endsWith (const StringData &suffix) const
 Returns true if 'suffix' is a substring of this instance, anchored at the end.
 
const char * rawData () const
 Get the pointer to the first byte of StringData.
 

Detailed Description

A StringData object wraps a 'const string&' or a 'const char*' without copying its contents.

The most common usage is as a function argument that takes any of the two forms of strings above. Fundamentally, this class tries go around the fact that string literals in C++ are char[N]'s.

Notes:

Constructor & Destructor Documentation

◆ StringData() [1/2]

mongo::StringData::StringData ( const char *  str)
inline

Constructs a StringData, for the case where the length of string is not known.

'c' must be a pointer to a null-terminated string.

◆ StringData() [2/2]

mongo::StringData::StringData ( const char *  c,
size_t  len 
)
inline

Constructs a StringData explicitly, for the case where the length of the string is already known.

'c' must be a pointer to a null-terminated string, and len must be the length that strlen(c) would return, a.k.a the index of the terminator in c.

Member Function Documentation

◆ equalCaseInsensitive()

bool mongo::StringData::equalCaseInsensitive ( const StringData other) const
inline

note: this uses tolower, and therefore does not handle come languages correctly.

should be use sparingly

◆ rawData()

const char * mongo::StringData::rawData ( ) const
inline

Get the pointer to the first byte of StringData.

This is not guaranteed to be null-terminated, so if using this without checking size(), you are likely doing something wrong.


The documentation for this class was generated from the following files: