stringpiece.h
Go to the documentation of this file.
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
// Copyright (C) 2009-2013, International Business Machines
// Corporation and others. All Rights Reserved.
//
// Copyright 2001 and onwards Google Inc.
// Author: Sanjay Ghemawat
// This code is a contribution of Google code, and the style used here is
// a compromise between the original Google code and the ICU coding guidelines.
// For example, data types are ICU-ified (size_t,int->int32_t),
// and API comments doxygen-ified, but function names and behavior are
// as in the original, if possible.
// Assertion-style error handling, not available in ICU, was changed to
// parameter "pinning" similar to UnicodeString.
//
// In addition, this is only a partial port of the original Google code,
// limited to what was needed so far. The (nearly) complete original code
// is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
// (see ICU ticket 6765, r25517).
#ifndef __STRINGPIECE_H__
#define __STRINGPIECE_H__
#include "unicode/utypes.h"
#if U_SHOW_CPLUSPLUS_API
#include <cstddef>
#include <type_traits>
#include "unicode/uobject.h"
#include "unicode/std_string.h"
// Arghh! I wish C++ literals were "string".
U_NAMESPACE_BEGIN
class U_COMMON_API StringPiece : public UMemory {
private:
const char* ptr_;
int32_t length_;
public:
StringPiece() : ptr_(nullptr), length_(0) { }
StringPiece(const char* str);
#if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
StringPiece(const char8_t* str) : StringPiece(reinterpret_cast<const char*>(str)) {}
#endif
StringPiece(std::nullptr_t p) : ptr_(p), length_(0) {}
StringPiece(const std::string& str)
: ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
#if defined(__cpp_lib_char8_t) || defined(U_IN_DOXYGEN)
StringPiece(const std::u8string& str)
: ptr_(reinterpret_cast<const char*>(str.data())),
length_(static_cast<int32_t>(str.size())) { }
#endif
template <typename T,
typename = typename std::enable_if<
(std::is_same<decltype(T().data()), const char*>::value
#if defined(__cpp_char8_t)
|| std::is_same<decltype(T().data()), const char8_t*>::value
#endif
) &&
std::is_same<decltype(T().size()), size_t>::value>::type>
StringPiece(T str)
: ptr_(reinterpret_cast<const char*>(str.data())),
length_(static_cast<int32_t>(str.size())) {}
StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
#if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
StringPiece(const char8_t* str, int32_t len) :
StringPiece(reinterpret_cast<const char*>(str), len) {}
#endif
StringPiece(const StringPiece& x, int32_t pos);
StringPiece(const StringPiece& x, int32_t pos, int32_t len);
const char* data() const { return ptr_; }
int32_t size() const { return length_; }
int32_t length() const { return length_; }
UBool empty() const { return length_ == 0; }
void clear() { ptr_ = nullptr; length_ = 0; }
void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
void set(const char* str);
#if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
inline void set(const char8_t* xdata, int32_t len) {
set(reinterpret_cast<const char*>(xdata), len);
}
inline void set(const char8_t* str) {
set(reinterpret_cast<const char*>(str));
}
#endif
void remove_prefix(int32_t n) {
if (n >= 0) {
if (n > length_) {
n = length_;
}
ptr_ += n;
length_ -= n;
}
}
void remove_suffix(int32_t n) {
if (n >= 0) {
if (n <= length_) {
length_ -= n;
} else {
length_ = 0;
}
}
}
int32_t find(StringPiece needle, int32_t offset);
int32_t compare(StringPiece other);
static const int32_t npos; // = 0x7fffffff;
StringPiece substr(int32_t pos, int32_t len = npos) const {
return StringPiece(*this, pos, len);
}
};
U_EXPORT UBool U_EXPORT2
operator==(const StringPiece& x, const StringPiece& y);
inline bool operator!=(const StringPiece& x, const StringPiece& y) {
return !(x == y);
}
U_NAMESPACE_END
#endif /* U_SHOW_CPLUSPLUS_API */
#endif // __STRINGPIECE_H__
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:60
icu::StringPiece::substr
StringPiece substr(int32_t pos, int32_t len=npos) const
Returns a substring of this StringPiece.
Definition: stringpiece.h:313
icu::StringPiece::empty
UBool empty() const
Returns whether the string is empty.
Definition: stringpiece.h:207
icu::StringPiece::StringPiece
StringPiece(const char *str)
Constructs from a NUL-terminated const char * pointer.
icu::StringPiece::set
void set(const char *str)
Reset the stringpiece to refer to new data.
icu::StringPiece::StringPiece
StringPiece(const StringPiece &x, int32_t pos, int32_t len)
Substring of another StringPiece.
icu::StringPiece::data
const char * data() const
Returns the string pointer.
Definition: stringpiece.h:189
icu::StringPiece::StringPiece
StringPiece(T str)
Constructs from some other implementation of a string piece class, from any C++ record type that has ...
Definition: stringpiece.h:140
icu::StringPiece::StringPiece
StringPiece(const char8_t *str)
Constructs from a NUL-terminated const char8_t * pointer.
Definition: stringpiece.h:84
icu::StringPiece::StringPiece
StringPiece(const std::u8string &str)
Constructs from a std::u8string.
Definition: stringpiece.h:105
icu::StringPiece::find
int32_t find(StringPiece needle, int32_t offset)
Searches the StringPiece for the given search string (needle);.
icu::StringPiece::npos
static const int32_t npos
Maximum integer, used as a default value for substring methods.
Definition: stringpiece.h:303
icu::StringPiece::clear
void clear()
Sets to an empty string.
Definition: stringpiece.h:213
icu::StringPiece::set
void set(const char8_t *xdata, int32_t len)
Resets the stringpiece to refer to new data.
Definition: stringpiece.h:237
icu::StringPiece::StringPiece
StringPiece()
Default constructor, creates an empty StringPiece.
Definition: stringpiece.h:70
icu::StringPiece::StringPiece
StringPiece(std::nullptr_t p)
Constructs an empty StringPiece.
Definition: stringpiece.h:92
icu::StringPiece::set
void set(const char8_t *str)
Resets the stringpiece to refer to new data.
Definition: stringpiece.h:246
icu::StringPiece::set
void set(const char *xdata, int32_t len)
Reset the stringpiece to refer to new data.
Definition: stringpiece.h:221
icu::StringPiece::size
int32_t size() const
Returns the string length.
Definition: stringpiece.h:195
icu::StringPiece::remove_prefix
void remove_prefix(int32_t n)
Removes the first n string units.
Definition: stringpiece.h:256
icu::StringPiece::compare
int32_t compare(StringPiece other)
Compares this StringPiece with the other StringPiece, with semantics similar to std::string::compare(...
icu::StringPiece::remove_suffix
void remove_suffix(int32_t n)
Removes the last n string units.
Definition: stringpiece.h:271
icu::StringPiece::StringPiece
StringPiece(const char8_t *str, int32_t len)
Constructs from a const char8_t * pointer and a specified length.
Definition: stringpiece.h:158
icu::StringPiece::length
int32_t length() const
Returns the string length.
Definition: stringpiece.h:201
icu::StringPiece::StringPiece
StringPiece(const StringPiece &x, int32_t pos)
Substring of another StringPiece.
icu::StringPiece::StringPiece
StringPiece(const std::string &str)
Constructs from a std::string.
Definition: stringpiece.h:98
icu::StringPiece::StringPiece
StringPiece(const char *offset, int32_t len)
Constructs from a const char * pointer and a specified length.
Definition: stringpiece.h:150
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
icu::operator!=
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:335
U_EXPORT
#define U_EXPORT
Definition: platform.h:809
std_string.h
C++ API: Central ICU header for including the C++ standard <string> header and for related definition...
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
uobject.h
C++ API: Common ICU base class UObject.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
U_COMMON_API
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition: utypes.h:300