ICU 66.0.1  66.0.1
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_(NULL), length_(0) { }
76  StringPiece(const char* str);
81  StringPiece(const std::string& str)
82  : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
83 #ifndef U_HIDE_DRAFT_API
84 
103  template <typename T,
104  typename = typename std::enable_if<
105  std::is_same<decltype(T().data()), const char*>::value &&
106  std::is_same<decltype(T().size()), size_t>::value>::type>
107  StringPiece(T str)
108  : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) {}
109 #endif // U_HIDE_DRAFT_API
110 
116  StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
123  StringPiece(const StringPiece& x, int32_t pos);
132  StringPiece(const StringPiece& x, int32_t pos, int32_t len);
133 
144  const char* data() const { return ptr_; }
150  int32_t size() const { return length_; }
156  int32_t length() const { return length_; }
162  UBool empty() const { return length_ == 0; }
163 
168  void clear() { ptr_ = NULL; length_ = 0; }
169 
176  void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
177 
183  void set(const char* str);
184 
190  void remove_prefix(int32_t n) {
191  if (n >= 0) {
192  if (n > length_) {
193  n = length_;
194  }
195  ptr_ += n;
196  length_ -= n;
197  }
198  }
199 
205  void remove_suffix(int32_t n) {
206  if (n >= 0) {
207  if (n <= length_) {
208  length_ -= n;
209  } else {
210  length_ = 0;
211  }
212  }
213  }
214 
219  static const int32_t npos; // = 0x7fffffff;
220 
229  StringPiece substr(int32_t pos, int32_t len = npos) const {
230  return StringPiece(*this, pos, len);
231  }
232 };
233 
241 U_EXPORT UBool U_EXPORT2
242 operator==(const StringPiece& x, const StringPiece& y);
243 
251 inline UBool operator!=(const StringPiece& x, const StringPiece& y) {
252  return !(x == y);
253 }
254 
255 U_NAMESPACE_END
256 
257 #endif /* U_SHOW_CPLUSPLUS_API */
258 
259 #endif // __STRINGPIECE_H__
StringPiece(const char *offset, int32_t len)
Constructs from a const char * pointer and a specified length.
Definition: stringpiece.h:116
void remove_prefix(int32_t n)
Removes the first n string units.
Definition: stringpiece.h:190
int32_t size() const
Returns the string length.
Definition: stringpiece.h:150
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
UBool empty() const
Returns whether the string is empty.
Definition: stringpiece.h:162
StringPiece()
Default constructor, creates an empty StringPiece.
Definition: stringpiece.h:70
StringPiece(const std::string &str)
Constructs from a std::string.
Definition: stringpiece.h:81
StringPiece(T str)
Constructs from some other implementation of a string piece class, from any C++ record type that has ...
Definition: stringpiece.h:107
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:251
#define NULL
Define NULL if necessary, to nullptr for C++ and to ((void *)0) for C.
Definition: utypes.h:188
C++ API: Central ICU header for including the C++ standard <string> header and for related definition...
int32_t length() const
Returns the string length.
Definition: stringpiece.h:156
C++ API: Common ICU base class UObject.
StringPiece substr(int32_t pos, int32_t len=npos) const
Returns a substring of this StringPiece.
Definition: stringpiece.h:229
#define U_EXPORT
Definition: platform.h:828
const char * data() const
Returns the string pointer.
Definition: stringpiece.h:144
void clear()
Sets to an empty string.
Definition: stringpiece.h:168
Basic definitions for ICU, for both C and C++ APIs.
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside...
Definition: utypes.h:300
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:60
void remove_suffix(int32_t n)
Removes the last n string units.
Definition: stringpiece.h:205
UMemory is the common ICU base class.
Definition: uobject.h:115
static const int32_t npos
Maximum integer, used as a default value for substring methods.
Definition: stringpiece.h:219
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261