<!-- This comment will put IE 6, 7 and 8 in quirks mode --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>ICU 4.4.1: stringpiece.h Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="search/search.css" rel="stylesheet" type="text/css"/> <script type="text/javaScript" src="search/search.js"></script> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body onload='searchBox.OnSelectItem(0);'> <!-- Generated by Doxygen 1.6.2-20100208 --> <script type="text/javascript"><!-- var searchBox = new SearchBox("searchBox", "search",false,'Search'); --></script> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li> <div id="MSearchBox" class="MSearchBoxInactive"> <img id="MSearchSelect" src="search/search.png" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" alt=""/> <input type="text" id="MSearchField" value="Search" accesskey="S" onfocus="searchBox.OnSearchFieldFocus(true)" onblur="searchBox.OnSearchFieldFocus(false)" onkeyup="searchBox.OnSearchFieldChange(event)"/> <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a> </div> </li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>Globals</span></a></li> </ul> </div> <h1>stringpiece.h</h1><a href="stringpiece_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright (C) 2010, International Business Machines</span> <a name="l00002"></a>00002 <span class="comment">// Corporation and others. All Rights Reserved.</span> <a name="l00003"></a>00003 <span class="comment">//</span> <a name="l00004"></a>00004 <span class="comment">// Copyright 2001 and onwards Google Inc.</span> <a name="l00005"></a>00005 <span class="comment">// Author: Sanjay Ghemawat</span> <a name="l00006"></a>00006 <a name="l00007"></a>00007 <span class="comment">// This code is a contribution of Google code, and the style used here is</span> <a name="l00008"></a>00008 <span class="comment">// a compromise between the original Google code and the ICU coding guidelines.</span> <a name="l00009"></a>00009 <span class="comment">// For example, data types are ICU-ified (size_t,int->int32_t),</span> <a name="l00010"></a>00010 <span class="comment">// and API comments doxygen-ified, but function names and behavior are</span> <a name="l00011"></a>00011 <span class="comment">// as in the original, if possible.</span> <a name="l00012"></a>00012 <span class="comment">// Assertion-style error handling, not available in ICU, was changed to</span> <a name="l00013"></a>00013 <span class="comment">// parameter "pinning" similar to UnicodeString.</span> <a name="l00014"></a>00014 <span class="comment">//</span> <a name="l00015"></a>00015 <span class="comment">// In addition, this is only a partial port of the original Google code,</span> <a name="l00016"></a>00016 <span class="comment">// limited to what was needed so far. The (nearly) complete original code</span> <a name="l00017"></a>00017 <span class="comment">// is in the ICU svn repository at icuhtml/trunk/design/strings/contrib</span> <a name="l00018"></a>00018 <span class="comment">// (see ICU ticket 6765, r25517).</span> <a name="l00019"></a>00019 <a name="l00020"></a>00020 <span class="preprocessor">#ifndef __STRINGPIECE_H__</span> <a name="l00021"></a>00021 <span class="preprocessor"></span><span class="preprocessor">#define __STRINGPIECE_H__</span> <a name="l00022"></a>00022 <span class="preprocessor"></span> <a name="l00028"></a>00028 <span class="preprocessor">#include "<a class="code" href="utypes_8h.html" title="Basic definitions for ICU, for both C and C++ APIs.">unicode/utypes.h</a>"</span> <a name="l00029"></a>00029 <span class="preprocessor">#include "<a class="code" href="uobject_8h.html" title="C++ API: Common ICU base class UObject.">unicode/uobject.h</a>"</span> <a name="l00030"></a>00030 <span class="preprocessor">#include "<a class="code" href="std__string_8h.html" title="C++ API: Central ICU header for including the C++ standard &lt;string&gt; header...">unicode/std_string.h</a>"</span> <a name="l00031"></a>00031 <a name="l00032"></a>00032 <span class="comment">// Arghh! I wish C++ literals were "string".</span> <a name="l00033"></a>00033 <a name="l00034"></a>00034 <a class="code" href="uversion_8h.html#a7d9374e037299886d4192b763b4c5e8b" title="This is used to begin a declaration of a public ICU C++ API.">U_NAMESPACE_BEGIN</a> <a name="l00035"></a>00035 <a name="l00052"></a><a class="code" href="classStringPiece.html">00052</a> <span class="keyword">class </span><a class="code" href="utypes_8h.html#a559955ec3bc692657c5d8b1c6de8e334" title="Set to export library symbols from inside the common library, and to import them...">U_COMMON_API</a> <a class="code" href="classStringPiece.html" title="A string-like object that points to a sized piece of memory.">StringPiece</a> : <span class="keyword">public</span> <a class="code" href="classUMemory.html" title="UMemory is the common ICU base class.">UMemory</a> { <a name="l00053"></a>00053 <span class="keyword">private</span>: <a name="l00054"></a>00054 <span class="keyword">const</span> <span class="keywordtype">char</span>* ptr_; <a name="l00055"></a>00055 int32_t length_; <a name="l00056"></a>00056 <a name="l00057"></a>00057 <span class="keyword">public</span>: <a name="l00062"></a><a class="code" href="classStringPiece.html#a0f45b54fa594663333d1ac3c8d7c8997">00062</a> <a class="code" href="classStringPiece.html#a0f45b54fa594663333d1ac3c8d7c8997" title="Default constructor, creates an empty StringPiece.">StringPiece</a>() : ptr_(<a class="code" href="utypes_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4" title="Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.">NULL</a>), length_(0) { } <a name="l00068"></a>00068 <a class="code" href="classStringPiece.html" title="A string-like object that points to a sized piece of memory.">StringPiece</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* str); <a name="l00069"></a>00069 <span class="preprocessor">#if U_HAVE_STD_STRING</span> <a name="l00070"></a>00070 <span class="preprocessor"></span> <a name="l00074"></a><a class="code" href="classStringPiece.html#a698a3300cd837fbf68746ffc0c1ef3f3">00074</a> <a class="code" href="classStringPiece.html" title="A string-like object that points to a sized piece of memory.">StringPiece</a>(<span class="keyword">const</span> <a class="code" href="std__string_8h.html#a09ea9d17cd51b895003ea80a4e07b6dd" title="Define the namespace qualifier to use for standard C++ (STL) classes.">U_STD_NSQ</a> <span class="keywordtype">string</span>& str) <a name="l00075"></a>00075 : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { } <a name="l00076"></a>00076 <span class="preprocessor">#endif</span> <a name="l00077"></a>00077 <span class="preprocessor"></span> <a name="l00083"></a><a class="code" href="classStringPiece.html#a1ebd684e27ba92ffdc17e9870355c56c">00083</a> <a class="code" href="classStringPiece.html#a1ebd684e27ba92ffdc17e9870355c56c" title="Constructs from a const char * pointer and a specified length.">StringPiece</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* offset, int32_t len) : ptr_(offset), length_(len) { } <a name="l00090"></a>00090 <a class="code" href="classStringPiece.html" title="A string-like object that points to a sized piece of memory.">StringPiece</a>(<span class="keyword">const</span> <a class="code" href="classStringPiece.html" title="A string-like object that points to a sized piece of memory.">StringPiece</a>& x, int32_t pos); <a name="l00099"></a>00099 <a class="code" href="classStringPiece.html" title="A string-like object that points to a sized piece of memory.">StringPiece</a>(<span class="keyword">const</span> <a class="code" href="classStringPiece.html" title="A string-like object that points to a sized piece of memory.">StringPiece</a>& x, int32_t pos, int32_t len); <a name="l00100"></a>00100 <a name="l00111"></a><a class="code" href="classStringPiece.html#a8c0f2264485f90b1d60aab587cab352f">00111</a> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="classStringPiece.html#a8c0f2264485f90b1d60aab587cab352f" title="Returns the string pointer.">data</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> ptr_; } <a name="l00117"></a><a class="code" href="classStringPiece.html#a31645a6f83a6e338936007135d8f8a4d">00117</a> int32_t <a class="code" href="classStringPiece.html#a31645a6f83a6e338936007135d8f8a4d" title="Returns the string length.">size</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> length_; } <a name="l00123"></a><a class="code" href="classStringPiece.html#a56e2e8113c07d379a3deaaeed05dca60">00123</a> int32_t <a class="code" href="classStringPiece.html#a56e2e8113c07d379a3deaaeed05dca60" title="Returns the string length.">length</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> length_; } <a name="l00129"></a><a class="code" href="classStringPiece.html#a4789a54e17fc082bdbbb615a67a19120">00129</a> <a class="code" href="umachine_8h.html#a349ef00011f20ccd1d3b424445681aa5" title="The ICU boolean type.">UBool</a> <a class="code" href="classStringPiece.html#a4789a54e17fc082bdbbb615a67a19120" title="Returns whether the string is empty.">empty</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> length_ == 0; } <a name="l00130"></a>00130 <a name="l00135"></a><a class="code" href="classStringPiece.html#aba33c86f6353d50835ee4627a7b70326">00135</a> <span class="keywordtype">void</span> <a class="code" href="classStringPiece.html#aba33c86f6353d50835ee4627a7b70326" title="Sets to an empty string.">clear</a>() { ptr_ = <a class="code" href="utypes_8h.html#a070d2ce7b6bb7e5c05602aa8c308d0c4" title="Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.">NULL</a>; length_ = 0; } <a name="l00136"></a>00136 <a name="l00143"></a><a class="code" href="classStringPiece.html#abe0d6966ad467ae5fa675e3ecb16be77">00143</a> <span class="keywordtype">void</span> <span class="keyword">set</span>(<span class="keyword">const</span> <span class="keywordtype">char</span>* data, int32_t len) { ptr_ = data; length_ = len; } <a name="l00144"></a>00144 <a name="l00150"></a>00150 <span class="keywordtype">void</span> <span class="keyword">set</span>(<span class="keyword">const</span> <span class="keywordtype">char</span>* str); <a name="l00151"></a>00151 <a name="l00157"></a><a class="code" href="classStringPiece.html#a52b2f79cab62b8bc75e204dcc896baae">00157</a> <span class="keywordtype">void</span> <a class="code" href="classStringPiece.html#a52b2f79cab62b8bc75e204dcc896baae" title="Removes the first n string units.">remove_prefix</a>(int32_t n) { <a name="l00158"></a>00158 <span class="keywordflow">if</span> (n >= 0) { <a name="l00159"></a>00159 <span class="keywordflow">if</span> (n > length_) { <a name="l00160"></a>00160 n = length_; <a name="l00161"></a>00161 } <a name="l00162"></a>00162 ptr_ += n; <a name="l00163"></a>00163 length_ -= n; <a name="l00164"></a>00164 } <a name="l00165"></a>00165 } <a name="l00166"></a>00166 <a name="l00172"></a><a class="code" href="classStringPiece.html#ad1ed9b146e3ba745f3b91debff9511ac">00172</a> <span class="keywordtype">void</span> <a class="code" href="classStringPiece.html#ad1ed9b146e3ba745f3b91debff9511ac" title="Removes the last n string units.">remove_suffix</a>(int32_t n) { <a name="l00173"></a>00173 <span class="keywordflow">if</span> (n >= 0) { <a name="l00174"></a>00174 <span class="keywordflow">if</span> (n <= length_) { <a name="l00175"></a>00175 length_ -= n; <a name="l00176"></a>00176 } <span class="keywordflow">else</span> { <a name="l00177"></a>00177 length_ = 0; <a name="l00178"></a>00178 } <a name="l00179"></a>00179 } <a name="l00180"></a>00180 } <a name="l00181"></a>00181 <a name="l00186"></a><a class="code" href="classStringPiece.html#a640079800d8239870fa07c84b7c5895b">00186</a> <span class="keyword">static</span> <span class="keyword">const</span> int32_t npos = 0x7fffffff; <a name="l00187"></a>00187 <a name="l00196"></a><a class="code" href="classStringPiece.html#aad0e95f16e143ccfa69afbb33a9cf702">00196</a> <a class="code" href="classStringPiece.html" title="A string-like object that points to a sized piece of memory.">StringPiece</a> <a class="code" href="classStringPiece.html#aad0e95f16e143ccfa69afbb33a9cf702" title="Returns a substring of this StringPiece.">substr</a>(int32_t pos, int32_t len = npos)<span class="keyword"> const </span>{ <a name="l00197"></a>00197 <span class="keywordflow">return</span> <a class="code" href="classStringPiece.html" title="A string-like object that points to a sized piece of memory.">StringPiece</a>(*<span class="keyword">this</span>, pos, len); <a name="l00198"></a>00198 } <a name="l00199"></a>00199 }; <a name="l00200"></a>00200 <a name="l00208"></a>00208 <a class="code" href="platform_8h.html#a7d78411159504c2d6770763f924a1792">U_EXPORT</a> <a class="code" href="umachine_8h.html#a349ef00011f20ccd1d3b424445681aa5" title="The ICU boolean type.">UBool</a> <a class="code" href="platform_8h.html#a72bf6d8ec0640a37b1915e5d853b2016">U_EXPORT2</a> <a name="l00209"></a>00209 <a class="code" href="stringpiece_8h.html#a7652049cbac0bfeae2cd2aa4514a3352" title="Global operator == for StringPiece.">operator==</a>(<span class="keyword">const</span> <a class="code" href="classStringPiece.html" title="A string-like object that points to a sized piece of memory.">StringPiece</a>& x, <span class="keyword">const</span> <a class="code" href="classStringPiece.html" title="A string-like object that points to a sized piece of memory.">StringPiece</a>& y); <a name="l00210"></a>00210 <a name="l00218"></a><a class="code" href="stringpiece_8h.html#abf0ccab9536bab551afc07de65eb5d95">00218</a> <span class="keyword">inline</span> <a class="code" href="umachine_8h.html#a349ef00011f20ccd1d3b424445681aa5" title="The ICU boolean type.">UBool</a> <a class="code" href="stringpiece_8h.html#abf0ccab9536bab551afc07de65eb5d95" title="Global operator != for StringPiece.">operator!=</a>(<span class="keyword">const</span> <a class="code" href="classStringPiece.html" title="A string-like object that points to a sized piece of memory.">StringPiece</a>& x, <span class="keyword">const</span> <a class="code" href="classStringPiece.html" title="A string-like object that points to a sized piece of memory.">StringPiece</a>& y) { <a name="l00219"></a>00219 <span class="keywordflow">return</span> !(x == y); <a name="l00220"></a>00220 } <a name="l00221"></a>00221 <a name="l00222"></a>00222 <a class="code" href="uversion_8h.html#a3da1f00f3b02976ac53c014a3e4c0933" title="This is used to end a declaration of a public ICU C++ API.">U_NAMESPACE_END</a> <a name="l00223"></a>00223 <a name="l00224"></a>00224 <span class="preprocessor">#endif // __STRINGPIECE_H__</span> </pre></div></div> <!--- window showing the filter options --> <div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)"> <a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark"> </span>Defines</a></div> <!-- iframe showing the search results (closed by default) --> <div id="MSearchResultsWindow"> <iframe src="" frameborder="0" name="MSearchResults" id="MSearchResults"> </iframe> </div> <hr class="footer"/><address style="text-align: right;"><small>Generated by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2-20100208 </small></address> </body> </html>