Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/native/common/unicode/stringpiece.h
48773 views
// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2// Copyright (C) 2009-2013, International Business Machines3// Corporation and others. All Rights Reserved.4//5// Copyright 2001 and onwards Google Inc.6// Author: Sanjay Ghemawat78// This code is a contribution of Google code, and the style used here is9// a compromise between the original Google code and the ICU coding guidelines.10// For example, data types are ICU-ified (size_t,int->int32_t),11// and API comments doxygen-ified, but function names and behavior are12// as in the original, if possible.13// Assertion-style error handling, not available in ICU, was changed to14// parameter "pinning" similar to UnicodeString.15//16// In addition, this is only a partial port of the original Google code,17// limited to what was needed so far. The (nearly) complete original code18// is in the ICU svn repository at icuhtml/trunk/design/strings/contrib19// (see ICU ticket 6765, r25517).2021#ifndef __STRINGPIECE_H__22#define __STRINGPIECE_H__2324/**25* \file26* \brief C++ API: StringPiece: Read-only byte string wrapper class.27*/2829#include "unicode/utypes.h"30#include "unicode/uobject.h"31#include "unicode/std_string.h"3233// Arghh! I wish C++ literals were "string".3435U_NAMESPACE_BEGIN3637/**38* A string-like object that points to a sized piece of memory.39*40* We provide non-explicit singleton constructors so users can pass41* in a "const char*" or a "string" wherever a "StringPiece" is42* expected.43*44* Functions or methods may use StringPiece parameters to accept either a45* "const char*" or a "string" value that will be implicitly converted to a46* StringPiece.47*48* Systematic usage of StringPiece is encouraged as it will reduce unnecessary49* conversions from "const char*" to "string" and back again.50*51* @stable ICU 4.252*/53class U_COMMON_API StringPiece : public UMemory {54private:55const char* ptr_;56int32_t length_;5758public:59/**60* Default constructor, creates an empty StringPiece.61* @stable ICU 4.262*/63StringPiece() : ptr_(NULL), length_(0) { }64/**65* Constructs from a NUL-terminated const char * pointer.66* @param str a NUL-terminated const char * pointer67* @stable ICU 4.268*/69StringPiece(const char* str);70/**71* Constructs from a std::string.72* @stable ICU 4.273*/74StringPiece(const std::string& str)75: ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }76/**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