intl/icu/source/common/unicode/stringpiece.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/unicode/stringpiece.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,224 @@
     1.4 +// Copyright (C) 2009-2013, International Business Machines
     1.5 +// Corporation and others. All Rights Reserved.
     1.6 +//
     1.7 +// Copyright 2001 and onwards Google Inc.
     1.8 +// Author: Sanjay Ghemawat
     1.9 +
    1.10 +// This code is a contribution of Google code, and the style used here is
    1.11 +// a compromise between the original Google code and the ICU coding guidelines.
    1.12 +// For example, data types are ICU-ified (size_t,int->int32_t),
    1.13 +// and API comments doxygen-ified, but function names and behavior are
    1.14 +// as in the original, if possible.
    1.15 +// Assertion-style error handling, not available in ICU, was changed to
    1.16 +// parameter "pinning" similar to UnicodeString.
    1.17 +//
    1.18 +// In addition, this is only a partial port of the original Google code,
    1.19 +// limited to what was needed so far. The (nearly) complete original code
    1.20 +// is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
    1.21 +// (see ICU ticket 6765, r25517).
    1.22 +
    1.23 +#ifndef __STRINGPIECE_H__
    1.24 +#define __STRINGPIECE_H__
    1.25 +
    1.26 +/**
    1.27 + * \file 
    1.28 + * \brief C++ API: StringPiece: Read-only byte string wrapper class.
    1.29 + */
    1.30 +
    1.31 +#include "unicode/utypes.h"
    1.32 +#include "unicode/uobject.h"
    1.33 +#include "unicode/std_string.h"
    1.34 +
    1.35 +// Arghh!  I wish C++ literals were "string".
    1.36 +
    1.37 +U_NAMESPACE_BEGIN
    1.38 +
    1.39 +/**
    1.40 + * A string-like object that points to a sized piece of memory.
    1.41 + *
    1.42 + * We provide non-explicit singleton constructors so users can pass
    1.43 + * in a "const char*" or a "string" wherever a "StringPiece" is
    1.44 + * expected.
    1.45 + *
    1.46 + * Functions or methods may use const StringPiece& parameters to accept either
    1.47 + * a "const char*" or a "string" value that will be implicitly converted to
    1.48 + * a StringPiece.
    1.49 + *
    1.50 + * Systematic usage of StringPiece is encouraged as it will reduce unnecessary
    1.51 + * conversions from "const char*" to "string" and back again.
    1.52 + *
    1.53 + * @stable ICU 4.2
    1.54 + */
    1.55 +class U_COMMON_API StringPiece : public UMemory {
    1.56 + private:
    1.57 +  const char*   ptr_;
    1.58 +  int32_t       length_;
    1.59 +
    1.60 + public:
    1.61 +  /**
    1.62 +   * Default constructor, creates an empty StringPiece.
    1.63 +   * @stable ICU 4.2
    1.64 +   */
    1.65 +  StringPiece() : ptr_(NULL), length_(0) { }
    1.66 +  /**
    1.67 +   * Constructs from a NUL-terminated const char * pointer.
    1.68 +   * @param str a NUL-terminated const char * pointer
    1.69 +   * @stable ICU 4.2
    1.70 +   */
    1.71 +  StringPiece(const char* str);
    1.72 +#if U_HAVE_STD_STRING
    1.73 +  /**
    1.74 +   * Constructs from a std::string.
    1.75 +   * @stable ICU 4.2
    1.76 +   */
    1.77 +  StringPiece(const std::string& str)
    1.78 +    : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
    1.79 +#endif
    1.80 +  /**
    1.81 +   * Constructs from a const char * pointer and a specified length.
    1.82 +   * @param offset a const char * pointer (need not be terminated)
    1.83 +   * @param len the length of the string; must be non-negative
    1.84 +   * @stable ICU 4.2
    1.85 +   */
    1.86 +  StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
    1.87 +  /**
    1.88 +   * Substring of another StringPiece.
    1.89 +   * @param x the other StringPiece
    1.90 +   * @param pos start position in x; must be non-negative and <= x.length().
    1.91 +   * @stable ICU 4.2
    1.92 +   */
    1.93 +  StringPiece(const StringPiece& x, int32_t pos);
    1.94 +  /**
    1.95 +   * Substring of another StringPiece.
    1.96 +   * @param x the other StringPiece
    1.97 +   * @param pos start position in x; must be non-negative and <= x.length().
    1.98 +   * @param len length of the substring;
    1.99 +   *            must be non-negative and will be pinned to at most x.length() - pos.
   1.100 +   * @stable ICU 4.2
   1.101 +   */
   1.102 +  StringPiece(const StringPiece& x, int32_t pos, int32_t len);
   1.103 +
   1.104 +  /**
   1.105 +   * Returns the string pointer. May be NULL if it is empty.
   1.106 +   *
   1.107 +   * data() may return a pointer to a buffer with embedded NULs, and the
   1.108 +   * returned buffer may or may not be null terminated.  Therefore it is
   1.109 +   * typically a mistake to pass data() to a routine that expects a NUL
   1.110 +   * terminated string.
   1.111 +   * @return the string pointer
   1.112 +   * @stable ICU 4.2
   1.113 +   */
   1.114 +  const char* data() const { return ptr_; }
   1.115 +  /**
   1.116 +   * Returns the string length. Same as length().
   1.117 +   * @return the string length
   1.118 +   * @stable ICU 4.2
   1.119 +   */
   1.120 +  int32_t size() const { return length_; }
   1.121 +  /**
   1.122 +   * Returns the string length. Same as size().
   1.123 +   * @return the string length
   1.124 +   * @stable ICU 4.2
   1.125 +   */
   1.126 +  int32_t length() const { return length_; }
   1.127 +  /**
   1.128 +   * Returns whether the string is empty.
   1.129 +   * @return TRUE if the string is empty
   1.130 +   * @stable ICU 4.2
   1.131 +   */
   1.132 +  UBool empty() const { return length_ == 0; }
   1.133 +
   1.134 +  /**
   1.135 +   * Sets to an empty string.
   1.136 +   * @stable ICU 4.2
   1.137 +   */
   1.138 +  void clear() { ptr_ = NULL; length_ = 0; }
   1.139 +
   1.140 +  /**
   1.141 +   * Reset the stringpiece to refer to new data.
   1.142 +   * @param xdata pointer the new string data.  Need not be nul terminated.
   1.143 +   * @param len the length of the new data
   1.144 +   * @stable ICU 4.8
   1.145 +   */
   1.146 +  void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
   1.147 +
   1.148 +  /**
   1.149 +   * Reset the stringpiece to refer to new data.
   1.150 +   * @param str a pointer to a NUL-terminated string. 
   1.151 +   * @stable ICU 4.8
   1.152 +   */
   1.153 +  void set(const char* str);
   1.154 +
   1.155 +  /**
   1.156 +   * Removes the first n string units.
   1.157 +   * @param n prefix length, must be non-negative and <=length()
   1.158 +   * @stable ICU 4.2
   1.159 +   */
   1.160 +  void remove_prefix(int32_t n) {
   1.161 +    if (n >= 0) {
   1.162 +      if (n > length_) {
   1.163 +        n = length_;
   1.164 +      }
   1.165 +      ptr_ += n;
   1.166 +      length_ -= n;
   1.167 +    }
   1.168 +  }
   1.169 +
   1.170 +  /**
   1.171 +   * Removes the last n string units.
   1.172 +   * @param n suffix length, must be non-negative and <=length()
   1.173 +   * @stable ICU 4.2
   1.174 +   */
   1.175 +  void remove_suffix(int32_t n) {
   1.176 +    if (n >= 0) {
   1.177 +      if (n <= length_) {
   1.178 +        length_ -= n;
   1.179 +      } else {
   1.180 +        length_ = 0;
   1.181 +      }
   1.182 +    }
   1.183 +  }
   1.184 +
   1.185 +  /**
   1.186 +   * Maximum integer, used as a default value for substring methods.
   1.187 +   * @stable ICU 4.2
   1.188 +   */
   1.189 +  static const int32_t npos; // = 0x7fffffff;
   1.190 +
   1.191 +  /**
   1.192 +   * Returns a substring of this StringPiece.
   1.193 +   * @param pos start position; must be non-negative and <= length().
   1.194 +   * @param len length of the substring;
   1.195 +   *            must be non-negative and will be pinned to at most length() - pos.
   1.196 +   * @return the substring StringPiece
   1.197 +   * @stable ICU 4.2
   1.198 +   */
   1.199 +  StringPiece substr(int32_t pos, int32_t len = npos) const {
   1.200 +    return StringPiece(*this, pos, len);
   1.201 +  }
   1.202 +};
   1.203 +
   1.204 +/**
   1.205 + * Global operator == for StringPiece
   1.206 + * @param x The first StringPiece to compare.
   1.207 + * @param y The second StringPiece to compare.
   1.208 + * @return TRUE if the string data is equal
   1.209 + * @stable ICU 4.8
   1.210 + */
   1.211 +U_EXPORT UBool U_EXPORT2 
   1.212 +operator==(const StringPiece& x, const StringPiece& y);
   1.213 +
   1.214 +/**
   1.215 + * Global operator != for StringPiece
   1.216 + * @param x The first StringPiece to compare.
   1.217 + * @param y The second StringPiece to compare.
   1.218 + * @return TRUE if the string data is not equal
   1.219 + * @stable ICU 4.8
   1.220 + */
   1.221 +inline UBool operator!=(const StringPiece& x, const StringPiece& y) {
   1.222 +  return !(x == y);
   1.223 +}
   1.224 +
   1.225 +U_NAMESPACE_END
   1.226 +
   1.227 +#endif  // __STRINGPIECE_H__

mercurial