<!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>ucommon: string.cpp</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.3 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="main.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li><a href="files.html"><span>Files</span></a></li> <li><a href="examples.html"><span>Examples</span></a></li> </ul> </div> </div> <div class="contents"> <h1>string.cpp</h1><p>An example of the string class.</p> <div class="fragment"><pre class="fragment"><span class="comment">// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.</span> <span class="comment">//</span> <span class="comment">// This file is part of GNU uCommon C++.</span> <span class="comment">//</span> <span class="comment">// GNU uCommon C++ is free software: you can redistribute it and/or modify</span> <span class="comment">// it under the terms of the GNU Lesser General Public License as published</span> <span class="comment">// by the Free Software Foundation, either version 3 of the License, or</span> <span class="comment">// (at your option) any later version.</span> <span class="comment">//</span> <span class="comment">// GNU uCommon C++ is distributed in the hope that it will be useful,</span> <span class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <span class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span> <span class="comment">// GNU Lesser General Public License for more details.</span> <span class="comment">//</span> <span class="comment">// You should have received a copy of the GNU Lesser General Public License</span> <span class="comment">// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.</span> <span class="preprocessor">#ifndef DEBUG</span> <span class="preprocessor"></span><span class="preprocessor">#define DEBUG</span> <span class="preprocessor"></span><span class="preprocessor">#endif</span> <span class="preprocessor"></span> <span class="preprocessor">#include <<a class="code" href="a00204.html" title="Top level include file for the GNU uCommon C++ core library.">ucommon/ucommon.h</a>></span> <span class="preprocessor">#include <stdio.h></span> <span class="keyword">using namespace </span>UCOMMON_NAMESPACE; <span class="keyword">static</span> <a name="a0"></a><a class="code" href="a00208.html#a111b48f4bb3203a18bab710b093ac628" title="A convenience type for string.">string_t</a> testing(<span class="stringliteral">"second test"</span>); <span class="keyword">extern</span> <span class="stringliteral">"C"</span> <span class="keywordtype">int</span> main() { <span class="keywordtype">char</span> buff[33]; <span class="keywordtype">char</span> *tokens = NULL; <span class="keywordtype">unsigned</span> count = 0; <span class="keyword">const</span> <span class="keywordtype">char</span> *tp; <span class="keyword">const</span> <span class="keywordtype">char</span> *array[5]; String::fill(buff, 32, <span class="charliteral">' '</span>); stringbuf<128> mystr; mystr = (<a class="code" href="a00208.html#a111b48f4bb3203a18bab710b093ac628" title="A convenience type for string.">string_t</a>)<span class="stringliteral">"hello"</span> + (<a class="code" href="a00208.html#a111b48f4bb3203a18bab710b093ac628" title="A convenience type for string.">string_t</a>)<span class="stringliteral">" this is a test"</span>; assert(<a name="a1"></a><a class="code" href="a00208.html#aed17b3db1b6589811a2f2f1567775177" title="Compare two null terminated strings if equal ignoring case.">case_eq</a>(<span class="stringliteral">"hello this is a test"</span>, *mystr)); assert(<a class="code" href="a00208.html#aed17b3db1b6589811a2f2f1567775177" title="Compare two null terminated strings if equal ignoring case.">case_eq</a>(<span class="stringliteral">"second test"</span>, *testing)); assert(<a class="code" href="a00208.html#aed17b3db1b6589811a2f2f1567775177" title="Compare two null terminated strings if equal ignoring case.">case_eq</a>(<span class="stringliteral">" Is a test"</span>, mystr(-10))); mystr = <span class="stringliteral">" abc 123 \n "</span>; assert(<a class="code" href="a00208.html#aed17b3db1b6589811a2f2f1567775177" title="Compare two null terminated strings if equal ignoring case.">case_eq</a>(<span class="stringliteral">"abc 123"</span>, String::strip(mystr.c_mem(), <span class="stringliteral">" \n"</span>))); String::set(buff, <span class="keyword">sizeof</span>(buff), <span class="stringliteral">"this is \"a test\""</span>); <span class="keywordflow">while</span>(NULL != (tp = String::token(buff, &tokens, <span class="stringliteral">" "</span>, <span class="stringliteral">"\"\""</span>)) && count < 4) array[count++] = tp; assert(count == 3); assert(<a class="code" href="a00208.html#aed17b3db1b6589811a2f2f1567775177" title="Compare two null terminated strings if equal ignoring case.">case_eq</a>(array[1], <span class="stringliteral">"is"</span>)); assert(<a class="code" href="a00208.html#aed17b3db1b6589811a2f2f1567775177" title="Compare two null terminated strings if equal ignoring case.">case_eq</a>(array[2], <span class="stringliteral">"a test"</span>)); <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> core[4] = {0x01, 0x10, 0x2f, 0x45}; <span class="keywordtype">char</span> hexbuf[12]; assert(String::hexdump(core, hexbuf, <span class="stringliteral">"3-1"</span>) == 9); assert(<a name="a2"></a><a class="code" href="a00208.html#affb4ebc34459cfbe1d2965c178e52ee9" title="Compare two socket addresses to see if equal.">eq</a>(hexbuf, <span class="stringliteral">"01102f-45"</span>)); <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> hcore[4]; String::hexpack(hcore, hexbuf, <span class="stringliteral">"3-1"</span>); assert(String::hexdump(hcore, hexbuf, <span class="stringliteral">"3-1"</span>) == 9); assert(<a class="code" href="a00208.html#affb4ebc34459cfbe1d2965c178e52ee9" title="Compare two socket addresses to see if equal.">eq</a>(hexbuf, <span class="stringliteral">"01102f-45"</span>)); <a name="a3"></a><a class="code" href="a00208.html#a9045ddbf2980f6330c065a6935f40161" title="A convenience type when mixing std::string in old compilers that are bad with namespaces...">String</a> numstr = <span class="stringliteral">"-33.5,25"</span>; Real num1; Unsigned num2; numstr % num1 % <span class="stringliteral">","</span> % num2; assert(num1 == -33.5); assert(num2 == 25); assert(numstr.len() == 0); <span class="keywordtype">char</span> *test = strdup(str(<span class="stringliteral">"hello"</span>) + <span class="stringliteral">" test"</span> + str((<span class="keywordtype">short</span>)13)); assert(<a class="code" href="a00208.html#affb4ebc34459cfbe1d2965c178e52ee9" title="Compare two socket addresses to see if equal.">eq</a>(test, <span class="stringliteral">"hello test13"</span>)); <span class="keywordtype">char</span> *cdup = dup<char>(test[6]); assert(<a class="code" href="a00208.html#affb4ebc34459cfbe1d2965c178e52ee9" title="Compare two socket addresses to see if equal.">eq</a>(cdup, <span class="stringliteral">"test13"</span>)); <span class="keywordflow">return</span> 0; } </pre></div> </div> <hr class="footer"/><address style="text-align: right;"><small>Generated on Thu Jul 14 16:38:56 2011 for ucommon by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address> </body> </html>