stringpiece.h

Go to the documentation of this file.
00001 // Copyright (C) 2009, 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 
00142   void remove_prefix(int32_t n) {
00143     if (n >= 0) {
00144       if (n > length_) {
00145         n = length_;
00146       }
00147       ptr_ += n;
00148       length_ -= n;
00149     }
00150   }
00151 
00157   void remove_suffix(int32_t n) {
00158     if (n >= 0) {
00159       if (n <= length_) {
00160         length_ -= n;
00161       } else {
00162         length_ = 0;
00163       }
00164     }
00165   }
00166 
00171   static const int32_t npos = 0x7fffffff;
00172 
00181   StringPiece substr(int32_t pos, int32_t len = npos) const {
00182     return StringPiece(*this, pos, len);
00183   }
00184 };
00185 
00186 U_NAMESPACE_END
00187 
00188 #endif  // __STRINGPIECE_H__

Generated on 18 Sep 2013 for ICU 4.2.1 by  doxygen 1.4.7