ICU 56.1: stringpiece.h Source File

ICU 56.1 56.1

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

- File List
- Globals

stringpiece.h
Go to the documentation of this file.
1 // Copyright (C) 2009-2013, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 //
4 // Copyright 2001 and onwards Google Inc.
5 // Author: Sanjay Ghemawat
6
7 // This code is a contribution of Google code, and the style used here is
8 // a compromise between the original Google code and the ICU coding guidelines.
9 // For example, data types are ICU-ified (size_t,int->int32_t),
10 // and API comments doxygen-ified, but function names and behavior are
11 // as in the original, if possible.
12 // Assertion-style error handling, not available in ICU, was changed to
13 // parameter "pinning" similar to UnicodeString.
14 //
15 // In addition, this is only a partial port of the original Google code,
16 // limited to what was needed so far. The (nearly) complete original code
17 // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
18 // (see ICU ticket 6765, r25517).
19
20 #ifndef __STRINGPIECE_H__
21 #define __STRINGPIECE_H__
22
28 #include "unicode/utypes.h"
29 #include "unicode/uobject.h"
30 #include "unicode/std_string.h"
31
32 // Arghh! I wish C++ literals were "string".
33
34 U_NAMESPACE_BEGIN
35
52 class U_COMMON_API StringPiece : public UMemory {
53 private:
54 const char* ptr_;
55 int32_t length_;
56
57 public:
62 StringPiece() : ptr_(NULL), length_(0) { }
68 StringPiece(const char* str);
69 #if U_HAVE_STD_STRING
70
74 StringPiece(const std::string& str)
75 : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
76 #endif
77
83 StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
90 StringPiece(const StringPiece& x, int32_t pos);
99 StringPiece(const StringPiece& x, int32_t pos, int32_t len);
100
111 const char* data() const { return ptr_; }
117 int32_t size() const { return length_; }
123 int32_t length() const { return length_; }
129 UBool empty() const { return length_ == 0; }
130
135 void clear() { ptr_ = NULL; length_ = 0; }
136
143 void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
144
150 void set(const char* str);
151
157 void remove_prefix(int32_t n) {
158 if (n >= 0) {
159 if (n > length_) {
160 n = length_;
161 }
162 ptr_ += n;
163 length_ -= n;
164 }
165 }
166
172 void remove_suffix(int32_t n) {
173 if (n >= 0) {
174 if (n <= length_) {
175 length_ -= n;
176 } else {
177 length_ = 0;
178 }
179 }
180 }
181
186 static const int32_t npos; // = 0x7fffffff;
187
196 StringPiece substr(int32_t pos, int32_t len = npos) const {
197 return StringPiece(*this, pos, len);
198 }
199 };
200
208 U_EXPORT UBool U_EXPORT2
209 operator==(const StringPiece& x, const StringPiece& y);
210
218 inline UBool operator!=(const StringPiece& x, const StringPiece& y) {
219 return !(x == y);
220 }
221
222 U_NAMESPACE_END
223
224 #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:83
icu::StringPiece::remove_prefix
void remove_prefix(int32_t n)
Removes the first n string units.
Definition: stringpiece.h:157
icu::StringPiece::length
int32_t length() const
Returns the string length.
Definition: stringpiece.h:123
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:129
icu::StringPiece::set
void set(const char *xdata, int32_t len)
Reset the stringpiece to refer to new data.
Definition: stringpiece.h:143
icu::StringPiece::StringPiece
StringPiece()
Default constructor, creates an empty StringPiece.
Definition: stringpiece.h:62
icu::StringPiece::StringPiece
StringPiece(const std::string &str)
Constructs from a std::string.
Definition: stringpiece.h:74
icu::operator!=
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:218
icu::StringPiece::size
int32_t size() const
Returns the string length.
Definition: stringpiece.h:117
NULL
#define NULL
Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.
Definition: utypes.h:186
std_string.h
C++ API: Central ICU header for including the C++ standard 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:130
U_EXPORT
#define U_EXPORT
Definition: platform.h:788
icu::StringPiece::empty
UBool empty() const
Returns whether the string is empty.
Definition: stringpiece.h:129
icu::StringPiece::substr
StringPiece substr(int32_t pos, int32_t len=npos) const
Returns a substring of this StringPiece.
Definition: stringpiece.h:196
icu::StringPiece::clear
void clear()
Sets to an empty string.
Definition: stringpiece.h:135
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:357
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:52
icu::StringPiece::remove_suffix
void remove_suffix(int32_t n)
Removes the last n string units.
Definition: stringpiece.h:172
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:110
icu::StringPiece::data
const char * data() const
Returns the string pointer.
Definition: stringpiece.h:111
icu::StringPiece::npos
static const int32_t npos
Maximum integer, used as a default value for substring methods.
Definition: stringpiece.h:186
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:234