<!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"/> <meta http-equiv="X-UA-Compatible" content="IE=9"/> <title>ICU 4.8.1.1: stringpiece.h Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="dynsections.js"></script> <link href="search/search.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="search/search.js"></script> <script type="text/javascript"> $(document).ready(function() { searchBox.OnSelectItem(0); }); </script> <link href="doxygen.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="top"><!-- do not remove this div, it is closed by doxygen! --> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td style="padding-left: 0.5em;"> <div id="projectname">ICU 4.8.1.1  <span id="projectnumber">4.8.1.1</span> </div> </td> </tr> </tbody> </table> </div> <!-- end header part --> <!-- Generated by Doxygen 1.8.1.1 --> <script type="text/javascript"> var searchBox = new SearchBox("searchBox", "search",false,'Search'); </script> <div id="navrow1" class="tabs"> <ul class="tablist"> <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"> <span class="left"> <img id="MSearchSelect" src="search/mag_sel.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)"/> </span><span class="right"> <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a> </span> </div> </li> </ul> </div> <div id="navrow2" class="tabs2"> <ul class="tablist"> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>Globals</span></a></li> </ul> </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>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark"> </span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(11)"><span class="SelectionMark"> </span>Pages</a></div> <!-- iframe showing the search results (closed by default) --> <div id="MSearchResultsWindow"> <iframe src="javascript:void(0)" frameborder="0" name="MSearchResults" id="MSearchResults"> </iframe> </div> <div id="nav-path" class="navpath"> <ul> <li class="navelem"><a class="el" href="dir_bdd9a5d540de89e9fe90efdfc6973a4f.html">common</a></li><li class="navelem"><a class="el" href="dir_f59c6450ff718ff1b085cfd1c36acbd7.html">unicode</a></li> </ul> </div> </div><!-- top --> <div class="header"> <div class="headertitle"> <div class="title">stringpiece.h</div> </div> </div><!--header--> <div class="contents"> <a href="stringpiece_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment">// Copyright (C) 2011, International Business Machines</span></div> <div class="line"><a name="l00002"></a><span class="lineno"> 2</span> <span class="comment">// Corporation and others. All Rights Reserved.</span></div> <div class="line"><a name="l00003"></a><span class="lineno"> 3</span> <span class="comment">//</span></div> <div class="line"><a name="l00004"></a><span class="lineno"> 4</span> <span class="comment">// Copyright 2001 and onwards Google Inc.</span></div> <div class="line"><a name="l00005"></a><span class="lineno"> 5</span> <span class="comment">// Author: Sanjay Ghemawat</span></div> <div class="line"><a name="l00006"></a><span class="lineno"> 6</span> </div> <div class="line"><a name="l00007"></a><span class="lineno"> 7</span> <span class="comment">// This code is a contribution of Google code, and the style used here is</span></div> <div class="line"><a name="l00008"></a><span class="lineno"> 8</span> <span class="comment">// a compromise between the original Google code and the ICU coding guidelines.</span></div> <div class="line"><a name="l00009"></a><span class="lineno"> 9</span> <span class="comment">// For example, data types are ICU-ified (size_t,int->int32_t),</span></div> <div class="line"><a name="l00010"></a><span class="lineno"> 10</span> <span class="comment">// and API comments doxygen-ified, but function names and behavior are</span></div> <div class="line"><a name="l00011"></a><span class="lineno"> 11</span> <span class="comment">// as in the original, if possible.</span></div> <div class="line"><a name="l00012"></a><span class="lineno"> 12</span> <span class="comment">// Assertion-style error handling, not available in ICU, was changed to</span></div> <div class="line"><a name="l00013"></a><span class="lineno"> 13</span> <span class="comment">// parameter "pinning" similar to UnicodeString.</span></div> <div class="line"><a name="l00014"></a><span class="lineno"> 14</span> <span class="comment">//</span></div> <div class="line"><a name="l00015"></a><span class="lineno"> 15</span> <span class="comment">// In addition, this is only a partial port of the original Google code,</span></div> <div class="line"><a name="l00016"></a><span class="lineno"> 16</span> <span class="comment">// limited to what was needed so far. The (nearly) complete original code</span></div> <div class="line"><a name="l00017"></a><span class="lineno"> 17</span> <span class="comment">// is in the ICU svn repository at icuhtml/trunk/design/strings/contrib</span></div> <div class="line"><a name="l00018"></a><span class="lineno"> 18</span> <span class="comment">// (see ICU ticket 6765, r25517).</span></div> <div class="line"><a name="l00019"></a><span class="lineno"> 19</span> </div> <div class="line"><a name="l00020"></a><span class="lineno"> 20</span> <span class="preprocessor">#ifndef __STRINGPIECE_H__</span></div> <div class="line"><a name="l00021"></a><span class="lineno"> 21</span> <span class="preprocessor"></span><span class="preprocessor">#define __STRINGPIECE_H__</span></div> <div class="line"><a name="l00022"></a><span class="lineno"> 22</span> <span class="preprocessor"></span></div> <div class="line"><a name="l00028"></a><span class="lineno"> 28</span> <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></div> <div class="line"><a name="l00029"></a><span class="lineno"> 29</span> <span class="preprocessor">#include "<a class="code" href="uobject_8h.html" title="C++ API: Common ICU base class UObject.">unicode/uobject.h</a>"</span></div> <div class="line"><a name="l00030"></a><span class="lineno"> 30</span> <span class="preprocessor">#include "<a class="code" href="std__string_8h.html" title="C++ API: Central ICU header for including the C++ standard <string> header and for related definition...">unicode/std_string.h</a>"</span></div> <div class="line"><a name="l00031"></a><span class="lineno"> 31</span> </div> <div class="line"><a name="l00032"></a><span class="lineno"> 32</span> <span class="comment">// Arghh! I wish C++ literals were "string".</span></div> <div class="line"><a name="l00033"></a><span class="lineno"> 33</span> </div> <div class="line"><a name="l00034"></a><span class="lineno"> 34</span> <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></div> <div class="line"><a name="l00035"></a><span class="lineno"> 35</span> </div> <div class="line"><a name="l00052"></a><span class="lineno"><a class="code" href="classStringPiece.html"> 52</a></span> <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 from outside...">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> {</div> <div class="line"><a name="l00053"></a><span class="lineno"> 53</span>  <span class="keyword">private</span>:</div> <div class="line"><a name="l00054"></a><span class="lineno"> 54</span>  <span class="keyword">const</span> <span class="keywordtype">char</span>* ptr_;</div> <div class="line"><a name="l00055"></a><span class="lineno"> 55</span>  int32_t length_;</div> <div class="line"><a name="l00056"></a><span class="lineno"> 56</span> </div> <div class="line"><a name="l00057"></a><span class="lineno"> 57</span>  <span class="keyword">public</span>:</div> <div class="line"><a name="l00062"></a><span class="lineno"><a class="code" href="classStringPiece.html#a0f45b54fa594663333d1ac3c8d7c8997"> 62</a></span>  <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) { }</div> <div class="line"><a name="l00068"></a><span class="lineno"> 68</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">const</span> <span class="keywordtype">char</span>* str);</div> <div class="line"><a name="l00069"></a><span class="lineno"> 69</span> <span class="preprocessor">#if U_HAVE_STD_STRING</span></div> <div class="line"><a name="l00070"></a><span class="lineno"> 70</span> <span class="preprocessor"></span></div> <div class="line"><a name="l00074"></a><span class="lineno"> 74</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">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)</div> <div class="line"><a name="l00075"></a><span class="lineno"> 75</span>  : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }</div> <div class="line"><a name="l00076"></a><span class="lineno"> 76</span> <span class="preprocessor">#endif</span></div> <div class="line"><a name="l00077"></a><span class="lineno"> 77</span> <span class="preprocessor"></span></div> <div class="line"><a name="l00083"></a><span class="lineno"><a class="code" href="classStringPiece.html#a1ebd684e27ba92ffdc17e9870355c56c"> 83</a></span>  <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) { }</div> <div class="line"><a name="l00090"></a><span class="lineno"> 90</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">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);</div> <div class="line"><a name="l00099"></a><span class="lineno"> 99</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">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);</div> <div class="line"><a name="l00100"></a><span class="lineno"> 100</span> </div> <div class="line"><a name="l00111"></a><span class="lineno"><a class="code" href="classStringPiece.html#a8c0f2264485f90b1d60aab587cab352f"> 111</a></span>  <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_; }</div> <div class="line"><a name="l00117"></a><span class="lineno"><a class="code" href="classStringPiece.html#a31645a6f83a6e338936007135d8f8a4d"> 117</a></span>  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_; }</div> <div class="line"><a name="l00123"></a><span class="lineno"><a class="code" href="classStringPiece.html#a56e2e8113c07d379a3deaaeed05dca60"> 123</a></span>  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_; }</div> <div class="line"><a name="l00129"></a><span class="lineno"><a class="code" href="classStringPiece.html#a4789a54e17fc082bdbbb615a67a19120"> 129</a></span>  <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; }</div> <div class="line"><a name="l00130"></a><span class="lineno"> 130</span> </div> <div class="line"><a name="l00135"></a><span class="lineno"><a class="code" href="classStringPiece.html#aba33c86f6353d50835ee4627a7b70326"> 135</a></span>  <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; }</div> <div class="line"><a name="l00136"></a><span class="lineno"> 136</span> </div> <div class="line"><a name="l00143"></a><span class="lineno"><a class="code" href="classStringPiece.html#a23fb19e624ea43cacdcb8f60273cdecf"> 143</a></span>  <span class="keywordtype">void</span> <span class="keyword">set</span>(<span class="keyword">const</span> <span class="keywordtype">char</span>* xdata, int32_t len) { ptr_ = xdata; length_ = len; }</div> <div class="line"><a name="l00144"></a><span class="lineno"> 144</span> </div> <div class="line"><a name="l00150"></a><span class="lineno"> 150</span>  <span class="keywordtype">void</span> <span class="keyword">set</span>(<span class="keyword">const</span> <span class="keywordtype">char</span>* str);</div> <div class="line"><a name="l00151"></a><span class="lineno"> 151</span> </div> <div class="line"><a name="l00157"></a><span class="lineno"><a class="code" href="classStringPiece.html#a52b2f79cab62b8bc75e204dcc896baae"> 157</a></span>  <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) {</div> <div class="line"><a name="l00158"></a><span class="lineno"> 158</span>  <span class="keywordflow">if</span> (n >= 0) {</div> <div class="line"><a name="l00159"></a><span class="lineno"> 159</span>  <span class="keywordflow">if</span> (n > length_) {</div> <div class="line"><a name="l00160"></a><span class="lineno"> 160</span>  n = length_;</div> <div class="line"><a name="l00161"></a><span class="lineno"> 161</span>  }</div> <div class="line"><a name="l00162"></a><span class="lineno"> 162</span>  ptr_ += n;</div> <div class="line"><a name="l00163"></a><span class="lineno"> 163</span>  length_ -= n;</div> <div class="line"><a name="l00164"></a><span class="lineno"> 164</span>  }</div> <div class="line"><a name="l00165"></a><span class="lineno"> 165</span>  }</div> <div class="line"><a name="l00166"></a><span class="lineno"> 166</span> </div> <div class="line"><a name="l00172"></a><span class="lineno"><a class="code" href="classStringPiece.html#ad1ed9b146e3ba745f3b91debff9511ac"> 172</a></span>  <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) {</div> <div class="line"><a name="l00173"></a><span class="lineno"> 173</span>  <span class="keywordflow">if</span> (n >= 0) {</div> <div class="line"><a name="l00174"></a><span class="lineno"> 174</span>  <span class="keywordflow">if</span> (n <= length_) {</div> <div class="line"><a name="l00175"></a><span class="lineno"> 175</span>  length_ -= n;</div> <div class="line"><a name="l00176"></a><span class="lineno"> 176</span>  } <span class="keywordflow">else</span> {</div> <div class="line"><a name="l00177"></a><span class="lineno"> 177</span>  length_ = 0;</div> <div class="line"><a name="l00178"></a><span class="lineno"> 178</span>  }</div> <div class="line"><a name="l00179"></a><span class="lineno"> 179</span>  }</div> <div class="line"><a name="l00180"></a><span class="lineno"> 180</span>  }</div> <div class="line"><a name="l00181"></a><span class="lineno"> 181</span> </div> <div class="line"><a name="l00186"></a><span class="lineno"><a class="code" href="classStringPiece.html#a640079800d8239870fa07c84b7c5895b"> 186</a></span>  <span class="keyword">static</span> <span class="keyword">const</span> int32_t npos = 0x7fffffff;</div> <div class="line"><a name="l00187"></a><span class="lineno"> 187</span> </div> <div class="line"><a name="l00196"></a><span class="lineno"><a class="code" href="classStringPiece.html#aad0e95f16e143ccfa69afbb33a9cf702"> 196</a></span>  <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>{</div> <div class="line"><a name="l00197"></a><span class="lineno"> 197</span>  <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);</div> <div class="line"><a name="l00198"></a><span class="lineno"> 198</span>  }</div> <div class="line"><a name="l00199"></a><span class="lineno"> 199</span> };</div> <div class="line"><a name="l00200"></a><span class="lineno"> 200</span> </div> <div class="line"><a name="l00208"></a><span class="lineno"> 208</span> <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> </div> <div class="line"><a name="l00209"></a><span class="lineno"> 209</span> <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);</div> <div class="line"><a name="l00210"></a><span class="lineno"> 210</span> </div> <div class="line"><a name="l00218"></a><span class="lineno"><a class="code" href="stringpiece_8h.html#abf0ccab9536bab551afc07de65eb5d95"> 218</a></span> <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) {</div> <div class="line"><a name="l00219"></a><span class="lineno"> 219</span>  <span class="keywordflow">return</span> !(x == y);</div> <div class="line"><a name="l00220"></a><span class="lineno"> 220</span> }</div> <div class="line"><a name="l00221"></a><span class="lineno"> 221</span> </div> <div class="line"><a name="l00222"></a><span class="lineno"> 222</span> <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></div> <div class="line"><a name="l00223"></a><span class="lineno"> 223</span> </div> <div class="line"><a name="l00224"></a><span class="lineno"> 224</span> <span class="preprocessor">#endif // __STRINGPIECE_H__</span></div> </div><!-- fragment --></div><!-- contents --> <!-- start footer part --> <hr class="footer"/><address class="footer"><small> Generated by  <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/> </a> 1.8.1.1 </small></address> </body> </html>