ICU 58.2 58.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

stringpiece.h
Go to the documentation of this file.
1 // Copyright (C) 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 #include "unicode/uobject.h"
32 #include "unicode/std_string.h"
33
34 // Arghh! I wish C++ literals were "string".
35
36 U_NAMESPACE_BEGIN
37
54 class U_COMMON_API StringPiece : public UMemory {
55 private:
56 const char* ptr_;
57 int32_t length_;
58
59 public:
64 StringPiece() : ptr_(NULL), length_(0) { }
70 StringPiece(const char* str);
71 #if U_HAVE_STD_STRING
72
76 StringPiece(const std::string& str)
77 : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
78 #endif
79
85 StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
92 StringPiece(const StringPiece& x, int32_t pos);
101 StringPiece(const StringPiece& x, int32_t pos, int32_t len);
102
113 const char* data() const { return ptr_; }
119 int32_t size() const { return length_; }
125 int32_t length() const { return length_; }
131 UBool empty() const { return length_ == 0; }
132
137 void clear() { ptr_ = NULL; length_ = 0; }
138
145 void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
146
152 void set(const char* str);
153
159 void remove_prefix(int32_t n) {
160 if (n >= 0) {
161 if (n > length_) {
162 n = length_;
163 }
164 ptr_ += n;
165 length_ -= n;
166 }
167 }
168
174 void remove_suffix(int32_t n) {
175 if (n >= 0) {
176 if (n <= length_) {
177 length_ -= n;
178 } else {
179 length_ = 0;
180 }
181 }
182 }
183
188 static const int32_t npos; // = 0x7fffffff;
189
198 StringPiece substr(int32_t pos, int32_t len = npos) const {
199 return StringPiece(*this, pos, len);
200 }
201 };
202
210 U_EXPORT UBool U_EXPORT2
211 operator==(const StringPiece& x, const StringPiece& y);
212
220 inline UBool operator!=(const StringPiece& x, const StringPiece& y) {
221 return !(x == y);
222 }
223
224 U_NAMESPACE_END
225
226 #endif // __STRINGPIECE_H__
icu::StringPiece::StringPiece
StringPiece(const char *offset, int32_t len)
Constructs from a const char * pointer and a specified length.
Definition: stringpiece.h:85
icu::StringPiece::remove_prefix
void remove_prefix(int32_t n)
Removes the first n string units.
Definition: stringpiece.h:159
icu::StringPiece::length
int32_t length() const
Returns the string length.
Definition: stringpiece.h:125
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:131
icu::StringPiece::StringPiece
StringPiece()
Default constructor, creates an empty StringPiece.
Definition: stringpiece.h:64
icu::StringPiece::StringPiece
StringPiece(const std::string &str)
Constructs from a std::string.
Definition: stringpiece.h:76
icu::operator!=
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:220
icu::StringPiece::size
int32_t size() const
Returns the string length.
Definition: stringpiece.h:119
NULL
#define NULL
Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.
Definition: utypes.h:188
std_string.h
C++ API: Central ICU header for including the C++ standard <string> header and for related definition...
uobject.h
C++ API: Common ICU base class UObject.
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:132
U_EXPORT
#define U_EXPORT
Definition: platform.h:816
icu::StringPiece::empty
UBool empty() const
Returns whether the string is empty.
Definition: stringpiece.h:131
icu::StringPiece::substr
StringPiece substr(int32_t pos, int32_t len=npos) const
Returns a substring of this StringPiece.
Definition: stringpiece.h:198
icu::StringPiece::clear
void clear()
Sets to an empty string.
Definition: stringpiece.h:137
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:359
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:54
icu::StringPiece::remove_suffix
void remove_suffix(int32_t n)
Removes the last n string units.
Definition: stringpiece.h:174
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:112
icu::StringPiece::data
const char * data() const
Returns the string pointer.
Definition: stringpiece.h:113
icu::StringPiece::npos
static const int32_t npos
Maximum integer, used as a default value for substring methods.
Definition: stringpiece.h:188
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:259