Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/common/unicode/stringpiece.h
38827 views
// Copyright (C) 2009-2013, International Business Machines1// Corporation and others. All Rights Reserved.2//3// Copyright 2001 and onwards Google Inc.4// Author: Sanjay Ghemawat56// This code is a contribution of Google code, and the style used here is7// a compromise between the original Google code and the ICU coding guidelines.8// For example, data types are ICU-ified (size_t,int->int32_t),9// and API comments doxygen-ified, but function names and behavior are10// as in the original, if possible.11// Assertion-style error handling, not available in ICU, was changed to12// parameter "pinning" similar to UnicodeString.13//14// In addition, this is only a partial port of the original Google code,15// limited to what was needed so far. The (nearly) complete original code16// is in the ICU svn repository at icuhtml/trunk/design/strings/contrib17// (see ICU ticket 6765, r25517).1819#ifndef __STRINGPIECE_H__20#define __STRINGPIECE_H__2122/**23* \file24* \brief C++ API: StringPiece: Read-only byte string wrapper class.25*/2627#include "unicode/utypes.h"28#include "unicode/uobject.h"29#include "unicode/std_string.h"3031// Arghh! I wish C++ literals were "string".3233U_NAMESPACE_BEGIN3435/**36* A string-like object that points to a sized piece of memory.37*38* We provide non-explicit singleton constructors so users can pass39* in a "const char*" or a "string" wherever a "StringPiece" is40* expected.41*42* Functions or methods may use const StringPiece& parameters to accept either43* a "const char*" or a "string" value that will be implicitly converted to44* a StringPiece.45*46* Systematic usage of StringPiece is encouraged as it will reduce unnecessary47* conversions from "const char*" to "string" and back again.48*49* @stable ICU 4.250*/51class U_COMMON_API StringPiece : public UMemory {52private:53const char* ptr_;54int32_t length_;5556public:57/**58* Default constructor, creates an empty StringPiece.59* @stable ICU 4.260*/61StringPiece() : ptr_(NULL), length_(0) { }62/**63* Constructs from a NUL-terminated const char * pointer.64* @param str a NUL-terminated const char * pointer65* @stable ICU 4.266*/67StringPiece(const char* str);68#if U_HAVE_STD_STRING69/**70* Constructs from a std::string.71* @stable ICU 4.272*/73StringPiece(const std::string& str)74: ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }75#endif76/**77* Constructs from a const char * pointer and a specified length.78* @param offset a const char * pointer (need not be terminated)79* @param len the length of the string; must be non-negative80* @stable ICU 4.281*/82StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }83/**84* Substring of another StringPiece.85* @param x the other StringPiece86* @param pos start position in x; must be non-negative and <= x.length().87* @stable ICU 4.288*/89StringPiece(const StringPiece& x, int32_t pos);90/**91* Substring of another StringPiece.92* @param x the other StringPiece93* @param pos start position in x; must be non-negative and <= x.length().94* @param len length of the substring;95* must be non-negative and will be pinned to at most x.length() - pos.96* @stable ICU 4.297*/98StringPiece(const StringPiece& x, int32_t pos, int32_t len);99100/**101* Returns the string pointer. May be NULL if it is empty.102*103* data() may return a pointer to a buffer with embedded NULs, and the104* returned buffer may or may not be null terminated. Therefore it is105* typically a mistake to pass data() to a routine that expects a NUL106* terminated string.107* @return the string pointer108* @stable ICU 4.2109*/110const char* data() const { return ptr_; }111/**112* Returns the string length. Same as length().113* @return the string length114* @stable ICU 4.2115*/116int32_t size() const { return length_; }117/**118* Returns the string length. Same as size().119* @return the string length120* @stable ICU 4.2121*/122int32_t length() const { return length_; }123/**124* Returns whether the string is empty.125* @return TRUE if the string is empty126* @stable ICU 4.2127*/128UBool empty() const { return length_ == 0; }129130/**131* Sets to an empty string.132* @stable ICU 4.2133*/134void clear() { ptr_ = NULL; length_ = 0; }135136/**137* Reset the stringpiece to refer to new data.138* @param xdata pointer the new string data. Need not be nul terminated.139* @param len the length of the new data140* @stable ICU 4.8141*/142void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }143144/**145* Reset the stringpiece to refer to new data.146* @param str a pointer to a NUL-terminated string.147* @stable ICU 4.8148*/149void set(const char* str);150151/**152* Removes the first n string units.153* @param n prefix length, must be non-negative and <=length()154* @stable ICU 4.2155*/156void remove_prefix(int32_t n) {157if (n >= 0) {158if (n > length_) {159n = length_;160}161ptr_ += n;162length_ -= n;163}164}165166/**167* Removes the last n string units.168* @param n suffix length, must be non-negative and <=length()169* @stable ICU 4.2170*/171void remove_suffix(int32_t n) {172if (n >= 0) {173if (n <= length_) {174length_ -= n;175} else {176length_ = 0;177}178}179}180181/**182* Maximum integer, used as a default value for substring methods.183* @stable ICU 4.2184*/185static const int32_t npos; // = 0x7fffffff;186187/**188* Returns a substring of this StringPiece.189* @param pos start position; must be non-negative and <= length().190* @param len length of the substring;191* must be non-negative and will be pinned to at most length() - pos.192* @return the substring StringPiece193* @stable ICU 4.2194*/195StringPiece substr(int32_t pos, int32_t len = npos) const {196return StringPiece(*this, pos, len);197}198};199200/**201* Global operator == for StringPiece202* @param x The first StringPiece to compare.203* @param y The second StringPiece to compare.204* @return TRUE if the string data is equal205* @stable ICU 4.8206*/207U_EXPORT UBool U_EXPORT2208operator==(const StringPiece& x, const StringPiece& y);209210/**211* Global operator != for StringPiece212* @param x The first StringPiece to compare.213* @param y The second StringPiece to compare.214* @return TRUE if the string data is not equal215* @stable ICU 4.8216*/217inline UBool operator!=(const StringPiece& x, const StringPiece& y) {218return !(x == y);219}220221U_NAMESPACE_END222223#endif // __STRINGPIECE_H__224225226