ICU 68.2  68.2
stringpiece.h
Go to the documentation of this file.
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 // Copyright (C) 2009-2013, International Business Machines
4 // Corporation and others. All Rights Reserved.
5 //
6 // Copyright 2001 and onwards Google Inc.
7 // Author: Sanjay Ghemawat
8 
9 // This code is a contribution of Google code, and the style used here is
10 // a compromise between the original Google code and the ICU coding guidelines.
11 // For example, data types are ICU-ified (size_t,int->int32_t),
12 // and API comments doxygen-ified, but function names and behavior are
13 // as in the original, if possible.
14 // Assertion-style error handling, not available in ICU, was changed to
15 // parameter "pinning" similar to UnicodeString.
16 //
17 // In addition, this is only a partial port of the original Google code,
18 // limited to what was needed so far. The (nearly) complete original code
19 // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
20 // (see ICU ticket 6765, r25517).
21 
22 #ifndef __STRINGPIECE_H__
23 #define __STRINGPIECE_H__
24 
30 #include "unicode/utypes.h"
31 
32 #if U_SHOW_CPLUSPLUS_API
33 
34 #include <cstddef>
35 #include <type_traits>
36 
37 #include "unicode/uobject.h"
38 #include "unicode/std_string.h"
39 
40 // Arghh! I wish C++ literals were "string".
41 
42 U_NAMESPACE_BEGIN
43 
61  private:
62  const char* ptr_;
63  int32_t length_;
64 
65  public:
70  StringPiece() : ptr_(nullptr), length_(0) { }
71 
77  StringPiece(const char* str);
78 #ifndef U_HIDE_DRAFT_API
79 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
80 
85  StringPiece(const char8_t* str) : StringPiece(reinterpret_cast<const char*>(str)) {}
86 #endif
87 
93  StringPiece(std::nullptr_t p) : ptr_(p), length_(0) {}
94 #endif // U_HIDE_DRAFT_API
95 
100  StringPiece(const std::string& str)
101  : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
102 #ifndef U_HIDE_DRAFT_API
103 #if defined(__cpp_lib_char8_t) || defined(U_IN_DOXYGEN)
104 
108  StringPiece(const std::u8string& str)
109  : ptr_(reinterpret_cast<const char*>(str.data())),
110  length_(static_cast<int32_t>(str.size())) { }
111 #endif
112 #endif // U_HIDE_DRAFT_API
113 
136  template <typename T,
137  typename = typename std::enable_if<
138  (std::is_same<decltype(T().data()), const char*>::value
139 #if defined(__cpp_char8_t)
140  || std::is_same<decltype(T().data()), const char8_t*>::value
141 #endif
142  ) &&
143  std::is_same<decltype(T().size()), size_t>::value>::type>
144  StringPiece(T str)
145  : ptr_(reinterpret_cast<const char*>(str.data())),
146  length_(static_cast<int32_t>(str.size())) {}
147 
154  StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
155 #ifndef U_HIDE_DRAFT_API
156 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
157 
163  StringPiece(const char8_t* str, int32_t len) :
164  StringPiece(reinterpret_cast<const char*>(str), len) {}
165 #endif
166 #endif // U_HIDE_DRAFT_API
167 
174  StringPiece(const StringPiece& x, int32_t pos);
183  StringPiece(const StringPiece& x, int32_t pos, int32_t len);
184 
195  const char* data() const { return ptr_; }
201  int32_t size() const { return length_; }
207  int32_t length() const { return length_; }
213  UBool empty() const { return length_ == 0; }
214 
219  void clear() { ptr_ = nullptr; length_ = 0; }
220 
227  void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
228 
234  void set(const char* str);
235 
236 #ifndef U_HIDE_DRAFT_API
237 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
238 
244  inline void set(const char8_t* xdata, int32_t len) {
245  set(reinterpret_cast<const char*>(xdata), len);
246  }
247 
253  inline void set(const char8_t* str) {
254  set(reinterpret_cast<const char*>(str));
255  }
256 #endif
257 #endif // U_HIDE_DRAFT_API
258 
264  void remove_prefix(int32_t n) {
265  if (n >= 0) {
266  if (n > length_) {
267  n = length_;
268  }
269  ptr_ += n;
270  length_ -= n;
271  }
272  }
273 
279  void remove_suffix(int32_t n) {
280  if (n >= 0) {
281  if (n <= length_) {
282  length_ -= n;
283  } else {
284  length_ = 0;
285  }
286  }
287  }
288 
289 #ifndef U_HIDE_DRAFT_API
290 
297  int32_t find(StringPiece needle, int32_t offset);
298 
306  int32_t compare(StringPiece other);
307 #endif // U_HIDE_DRAFT_API
308 
313  static const int32_t npos; // = 0x7fffffff;
314 
323  StringPiece substr(int32_t pos, int32_t len = npos) const {
324  return StringPiece(*this, pos, len);
325  }
326 };
327 
335 U_EXPORT UBool U_EXPORT2
336 operator==(const StringPiece& x, const StringPiece& y);
337 
345 inline UBool operator!=(const StringPiece& x, const StringPiece& y) {
346  return !(x == y);
347 }
348 
349 U_NAMESPACE_END
350 
351 #endif /* U_SHOW_CPLUSPLUS_API */
352 
353 #endif // __STRINGPIECE_H__
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::StringPiece::set
void set(const char8_t *xdata, int32_t len)
Resets the stringpiece to refer to new data.
Definition: stringpiece.h:244
icu::StringPiece::StringPiece
StringPiece(const std::u8string &str)
Constructs from a std::u8string.
Definition: stringpiece.h:108
icu::StringPiece::compare
int32_t compare(StringPiece other)
Compares this StringPiece with the other StringPiece, with semantics similar to std::string::compare(...
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
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 char *offset, int32_t len)
Constructs from a const char * pointer and a specified length.
Definition: stringpiece.h:154
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
icu::StringPiece::StringPiece
StringPiece(std::nullptr_t p)
Constructs an empty StringPiece.
Definition: stringpiece.h:93
icu::StringPiece::set
void set(const char *str)
Reset the stringpiece to refer to new data.
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
icu::StringPiece::remove_prefix
void remove_prefix(int32_t n)
Removes the first n string units.
Definition: stringpiece.h:264
icu::StringPiece::StringPiece
StringPiece(const std::string &str)
Constructs from a std::string.
Definition: stringpiece.h:100
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:144
icu::StringPiece::set
void set(const char8_t *str)
Resets the stringpiece to refer to new data.
Definition: stringpiece.h:253
icu::StringPiece::StringPiece
StringPiece(const StringPiece &x, int32_t pos)
Substring of another StringPiece.
icu::StringPiece::set
void set(const char *xdata, int32_t len)
Reset the stringpiece to refer to new data.
Definition: stringpiece.h:227
icu::StringPiece::size
int32_t size() const
Returns the string length.
Definition: stringpiece.h:201
icu::StringPiece::empty
UBool empty() const
Returns whether the string is empty.
Definition: stringpiece.h:213
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
icu::operator!=
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:345
icu::StringPiece::data
const char * data() const
Returns the string pointer.
Definition: stringpiece.h:195
icu::StringPiece::StringPiece
StringPiece(const StringPiece &x, int32_t pos, int32_t len)
Substring of another StringPiece.
std_string.h
C++ API: Central ICU header for including the C++ standard <string> header and for related definition...
icu::StringPiece::clear
void clear()
Sets to an empty string.
Definition: stringpiece.h:219
icu::StringPiece::StringPiece
StringPiece()
Default constructor, creates an empty StringPiece.
Definition: stringpiece.h:70
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:163
icu::StringPiece::find
int32_t find(StringPiece needle, int32_t offset)
Searches the StringPiece for the given search string (needle);.
icu::StringPiece::remove_suffix
void remove_suffix(int32_t n)
Removes the last n string units.
Definition: stringpiece.h:279
icu::StringPiece::StringPiece
StringPiece(const char *str)
Constructs from a NUL-terminated const char * pointer.
icu::StringPiece::npos
static const int32_t npos
Maximum integer, used as a default value for substring methods.
Definition: stringpiece.h:313
icu::StringPiece::length
int32_t length() const
Returns the string length.
Definition: stringpiece.h:207
uobject.h
C++ API: Common ICU base class UObject.
icu::StringPiece::substr
StringPiece substr(int32_t pos, int32_t len=npos) const
Returns a substring of this StringPiece.
Definition: stringpiece.h:323
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:60
U_EXPORT
#define U_EXPORT
Definition: platform.h:828