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

- File List
- Globals

# stringpiece.h

Go to the documentation of this file.

    00001 // Copyright (C) 2010, International Business Machines
    00002 // Corporation and others. All Rights Reserved.
    00003 //
    00004 // Copyright 2001 and onwards Google Inc.
    00005 // Author: Sanjay Ghemawat
    00006
    00007 // This code is a contribution of Google code, and the style used here is
    00008 // a compromise between the original Google code and the ICU coding guidelines.
    00009 // For example, data types are ICU-ified (size_t,int->int32_t),
    00010 // and API comments doxygen-ified, but function names and behavior are
    00011 // as in the original, if possible.
    00012 // Assertion-style error handling, not available in ICU, was changed to
    00013 // parameter "pinning" similar to UnicodeString.
    00014 //
    00015 // In addition, this is only a partial port of the original Google code,
    00016 // limited to what was needed so far. The (nearly) complete original code
    00017 // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
    00018 // (see ICU ticket 6765, r25517).
    00019
    00020 #ifndef __STRINGPIECE_H__
    00021 #define __STRINGPIECE_H__
    00022
    00028 #include "unicode/utypes.h"
    00029 #include "unicode/uobject.h"
    00030 #include "unicode/std_string.h"
    00031
    00032 // Arghh!  I wish C++ literals were "string".
    00033
    00034 U_NAMESPACE_BEGIN
    00035
    00052 class U_COMMON_API StringPiece : public UMemory {
    00053  private:
    00054   const char*   ptr_;
    00055   int32_t       length_;
    00056
    00057  public:
    00062   StringPiece() : ptr_(NULL), length_(0) { }
    00068   StringPiece(const char* str);
    00069 #if U_HAVE_STD_STRING
    00070
    00074   StringPiece(const U_STD_NSQ string& str)
    00075     : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
    00076 #endif
    00077
    00083   StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
    00090   StringPiece(const StringPiece& x, int32_t pos);
    00099   StringPiece(const StringPiece& x, int32_t pos, int32_t len);
    00100
    00111   const char* data() const { return ptr_; }
    00117   int32_t size() const { return length_; }
    00123   int32_t length() const { return length_; }
    00129   UBool empty() const { return length_ == 0; }
    00130
    00135   void clear() { ptr_ = NULL; length_ = 0; }
    00136
    00143   void set(const char* data, int32_t len) { ptr_ = data; length_ = len; }
    00144
    00150   void set(const char* str);
    00151
    00157   void remove_prefix(int32_t n) {
    00158     if (n >= 0) {
    00159       if (n > length_) {
    00160         n = length_;
    00161       }
    00162       ptr_ += n;
    00163       length_ -= n;
    00164     }
    00165   }
    00166
    00172   void remove_suffix(int32_t n) {
    00173     if (n >= 0) {
    00174       if (n <= length_) {
    00175         length_ -= n;
    00176       } else {
    00177         length_ = 0;
    00178       }
    00179     }
    00180   }
    00181
    00186   static const int32_t npos = 0x7fffffff;
    00187
    00196   StringPiece substr(int32_t pos, int32_t len = npos) const {
    00197     return StringPiece(*this, pos, len);
    00198   }
    00199 };
    00200
    00208 U_EXPORT UBool U_EXPORT2
    00209 operator==(const StringPiece& x, const StringPiece& y);
    00210
    00218 inline UBool operator!=(const StringPiece& x, const StringPiece& y) {
    00219   return !(x == y);
    00220 }
    00221
    00222 U_NAMESPACE_END
    00223
    00224 #endif  // __STRINGPIECE_H__

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines