Loading...
Searching...
No Matches
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 <string_view>
#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 = std::enable_if_t<
(std::is_same_v<decltype(T().data()), const char*>
#if defined(__cpp_char8_t)
|| std::is_same_v<decltype(T().data()), const char8_t*>
#endif
) &&
std::is_same_v<decltype(T().size()), size_t>>>
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);
#ifndef U_HIDE_INTERNAL_API
inline operator std::string_view() const {
return {data(), static_cast<std::string_view::size_type>(size())};
}
#endif // U_HIDE_INTERNAL_API
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_COMMON_API 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:61
icu::StringPiece::substr
StringPiece substr(int32_t pos, int32_t len=npos) const
Returns a substring of this StringPiece.
Definition stringpiece.h:324
icu::StringPiece::empty
UBool empty() const
Returns whether the string is empty.
Definition stringpiece.h:218
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::StringPiece
StringPiece(T str)
Constructs from some other implementation of a string piece class, from any C++ record type that has ...
Definition stringpiece.h:141
icu::StringPiece::StringPiece
StringPiece(const char8_t *str)
Constructs from a NUL-terminated const char8_t * pointer.
Definition stringpiece.h:85
icu::StringPiece::StringPiece
StringPiece(const std::u8string &str)
Constructs from a std::u8string.
Definition stringpiece.h:106
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:314
icu::StringPiece::clear
void clear()
Sets to an empty string.
Definition stringpiece.h:224
icu::StringPiece::set
void set(const char8_t *xdata, int32_t len)
Resets the stringpiece to refer to new data.
Definition stringpiece.h:248
icu::StringPiece::data
const char * data() const
Returns the string pointer.
Definition stringpiece.h:200
icu::StringPiece::StringPiece
StringPiece()
Default constructor, creates an empty StringPiece.
Definition stringpiece.h:71
icu::StringPiece::StringPiece
StringPiece(std::nullptr_t p)
Constructs an empty StringPiece.
Definition stringpiece.h:93
icu::StringPiece::set
void set(const char8_t *str)
Resets the stringpiece to refer to new data.
Definition stringpiece.h:257
icu::StringPiece::set
void set(const char *xdata, int32_t len)
Reset the stringpiece to refer to new data.
Definition stringpiece.h:232
icu::StringPiece::size
int32_t size() const
Returns the string length.
Definition stringpiece.h:206
icu::StringPiece::remove_prefix
void remove_prefix(int32_t n)
Removes the first n string units.
Definition stringpiece.h:267
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:282
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:159
icu::StringPiece::length
int32_t length() const
Returns the string length.
Definition stringpiece.h:212
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:99
icu::StringPiece::StringPiece
StringPiece(const char *offset, int32_t len)
Constructs from a const char * pointer and a specified length.
Definition stringpiece.h:151
icu::UMemory
UMemory is the common ICU base class.
Definition uobject.h:115
icu::operator==
U_COMMON_API 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:346
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:269
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:315