char_separator.htm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Language" content="en-us">
  5. <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
  6. <meta name="GENERATOR" content="Microsoft FrontPage 6.0">
  7. <meta name="ProgId" content="FrontPage.Editor.Document">
  8. <title>Boost Char Separator</title>
  9. </head>
  10. <body bgcolor="#FFFFFF" text="#000000" link="#0000EE" vlink="#551A8B" alink=
  11. "#FF0000">
  12. <p><img src="../../../boost.png" alt="C++ Boost" width="277" height=
  13. "86"><br></p>
  14. <h1>char_separator&lt;Char, Traits&gt;</h1>
  15. <p>The <tt>char_separator</tt> class breaks a sequence of characters into
  16. tokens based on character delimiters much in the same way that
  17. <tt>strtok()</tt> does (but without all the evils of non-reentrancy and
  18. destruction of the input sequence).</p>
  19. <p>The <tt>char_separator</tt> class is used in conjunction with the
  20. <a href="token_iterator.htm"><tt>token_iterator</tt></a> or <a href=
  21. "tokenizer.htm"><tt>tokenizer</tt></a> to perform tokenizing.</p>
  22. <h2>Definitions</h2>
  23. <p>The <tt>strtok()</tt> function does not include matches with the
  24. character delimiters in the output sequence of tokens. However, sometimes
  25. it is useful to have the delimiters show up in the output sequence,
  26. therefore <tt>char_separator</tt> provides this as an option. We refer to
  27. delimiters that show up as output tokens as <b><i>kept delimiters</i></b>
  28. and delimiters that do now show up as output tokens as <b><i>dropped
  29. delimiters</i></b>.</p>
  30. <p>When two delimiters appear next to each other in the input sequence,
  31. there is the question of whether to output an <b><i>empty token</i></b> or
  32. to skip ahead. The behaviour of <tt>strtok()</tt> is to skip ahead. The
  33. <tt>char_separator</tt> class provides both options.</p>
  34. <h2>Examples</h2>
  35. <p>This first examples shows how to use <tt>char_separator</tt> as a
  36. replacement for the <tt>strtok()</tt> function. We've specified three
  37. character delimiters, and they will not show up as output tokens. We have
  38. not specified any kept delimiters, and by default any empty tokens will be
  39. ignored.</p>
  40. <blockquote>
  41. <pre>
  42. // char_sep_example_1.cpp
  43. #include &lt;iostream&gt;
  44. #include &lt;boost/tokenizer.hpp&gt;
  45. #include &lt;string&gt;
  46. int main()
  47. {
  48. std::string str = ";;Hello|world||-foo--bar;yow;baz|";
  49. typedef boost::tokenizer&lt;boost::char_separator&lt;char&gt; &gt;
  50. tokenizer;
  51. boost::char_separator&lt;char&gt; sep("-;|");
  52. tokenizer tokens(str, sep);
  53. for (tokenizer::iterator tok_iter = tokens.begin();
  54. tok_iter != tokens.end(); ++tok_iter)
  55. std::cout &lt;&lt; "&lt;" &lt;&lt; *tok_iter &lt;&lt; "&gt; ";
  56. std::cout &lt;&lt; "\n";
  57. return EXIT_SUCCESS;
  58. }
  59. </pre>
  60. </blockquote>The output is:
  61. <blockquote>
  62. <pre>
  63. &lt;Hello&gt; &lt;world&gt; &lt;foo&gt; &lt;bar&gt; &lt;yow&gt; &lt;baz&gt;
  64. </pre>
  65. </blockquote>
  66. <p>The next example shows tokenizing with two dropped delimiters '-' and
  67. ';' and a single kept delimiter '|'. We also specify that empty tokens
  68. should show up in the output when two delimiters are next to each
  69. other.</p>
  70. <blockquote>
  71. <pre>
  72. // char_sep_example_2.cpp
  73. #include &lt;iostream&gt;
  74. #include &lt;boost/tokenizer.hpp&gt;
  75. #include &lt;string&gt;
  76. int main()
  77. {
  78. std::string str = ";;Hello|world||-foo--bar;yow;baz|";
  79. typedef boost::tokenizer&lt;boost::char_separator&lt;char&gt; &gt;
  80. tokenizer;
  81. boost::char_separator&lt;char&gt; sep("-;", "|", boost::keep_empty_tokens);
  82. tokenizer tokens(str, sep);
  83. for (tokenizer::iterator tok_iter = tokens.begin();
  84. tok_iter != tokens.end(); ++tok_iter)
  85. std::cout &lt;&lt; "&lt;" &lt;&lt; *tok_iter &lt;&lt; "&gt; ";
  86. std::cout &lt;&lt; "\n";
  87. return EXIT_SUCCESS;
  88. }
  89. </pre>
  90. </blockquote>The output is:
  91. <blockquote>
  92. <pre>
  93. &lt;&gt; &lt;&gt; &lt;Hello&gt; &lt;|&gt; &lt;world&gt; &lt;|&gt; &lt;&gt; &lt;|&gt; &lt;&gt; &lt;foo&gt; &lt;&gt; &lt;bar&gt; &lt;yow&gt; &lt;baz&gt; &lt;|&gt; &lt;&gt;
  94. </pre>
  95. </blockquote>
  96. <p>The final example shows tokenizing on punctuation and whitespace
  97. characters using the default constructor of the
  98. <tt>char_separator</tt>.</p>
  99. <blockquote>
  100. <pre>
  101. // char_sep_example_3.cpp
  102. #include &lt;iostream&gt;
  103. #include &lt;boost/tokenizer.hpp&gt;
  104. #include &lt;string&gt;
  105. int main()
  106. {
  107. std::string str = "This is, a test";
  108. typedef boost::tokenizer&lt;boost::char_separator&lt;char&gt; &gt; Tok;
  109. boost::char_separator&lt;char&gt; sep; // default constructed
  110. Tok tok(str, sep);
  111. for(Tok::iterator tok_iter = tok.begin(); tok_iter != tok.end(); ++tok_iter)
  112. std::cout &lt;&lt; "&lt;" &lt;&lt; *tok_iter &lt;&lt; "&gt; ";
  113. std::cout &lt;&lt; "\n";
  114. return EXIT_SUCCESS;
  115. }
  116. </pre>
  117. </blockquote>The output is:
  118. <blockquote>
  119. <pre>
  120. &lt;This&gt; &lt;is&gt; &lt;,&gt; &lt;a&gt; &lt;test&gt;
  121. </pre>
  122. </blockquote>
  123. <h2>Template parameters</h2>
  124. <table border summary="">
  125. <tr>
  126. <th>Parameter</th>
  127. <th>Description</th>
  128. <th>Default</th>
  129. </tr>
  130. <tr>
  131. <td><tt>Char</tt></td>
  132. <td>The type of elements within a token, typically <tt>char</tt>.</td>
  133. <td>&nbsp;</td>
  134. </tr>
  135. <tr>
  136. <td><tt>Traits</tt></td>
  137. <td>The <tt>char_traits</tt> for the character type.</td>
  138. <td><tt>char_traits&lt;char&gt;</tt></td>
  139. </tr>
  140. </table>
  141. <h2>Model of</h2><a href="tokenizerfunction.htm">Tokenizer Function</a>
  142. <h2>Members</h2>
  143. <hr>
  144. <pre>
  145. explicit char_separator(const Char* dropped_delims,
  146. const Char* kept_delims = "",
  147. empty_token_policy empty_tokens = drop_empty_tokens)
  148. </pre>
  149. <p>This creates a <tt>char_separator</tt> object, which can then be used to
  150. create a <a href="token_iterator.htm"><tt>token_iterator</tt></a> or
  151. <a href="tokenizer.htm"><tt>tokenizer</tt></a> to perform tokenizing. The
  152. <tt>dropped_delims</tt> and <tt>kept_delims</tt> are strings of characters
  153. where each character is used as delimiter during tokenizing. Whenever a
  154. delimiter is seen in the input sequence, the current token is finished, and
  155. a new token begins. The delimiters in <tt>dropped_delims</tt> do not show
  156. up as tokens in the output whereas the delimiters in <tt>kept_delims</tt>
  157. do show up as tokens. If <tt>empty_tokens</tt> is
  158. <tt>drop_empty_tokens</tt>, then empty tokens will not show up in the
  159. output. If <tt>empty_tokens</tt> is <tt>keep_empty_tokens</tt> then empty
  160. tokens will show up in the output.</p>
  161. <hr>
  162. <pre>
  163. explicit char_separator()
  164. </pre>
  165. <p>The function <tt>std::isspace()</tt> is used to identify dropped
  166. delimiters and <tt>std::ispunct()</tt> is used to identify kept delimiters.
  167. In addition, empty tokens are dropped.</p>
  168. <hr>
  169. <pre>
  170. template &lt;typename InputIterator, typename Token&gt;
  171. bool operator()(InputIterator&amp; next, InputIterator end, Token&amp; tok)
  172. </pre>
  173. <p>This function is called by the <a href=
  174. "token_iterator.htm"><tt>token_iterator</tt></a> to perform tokenizing. The
  175. user typically does not call this function directly.</p>
  176. <hr>
  177. <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  178. "../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
  179. height="31" width="88"></a></p>
  180. <p>Revised
  181. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->25
  182. December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38518" --></p>
  183. <p><i>Copyright &copy; 2001-2002 Jeremy Siek and John R. Bandela</i></p>
  184. <p><i>Distributed under the Boost Software License, Version 1.0. (See
  185. accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
  186. copy at <a href=
  187. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
  188. </body>
  189. </html>