examples.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Boost tokenizer examples -------------------------------------------------//
  2. // (c) Copyright John R. Bandela 2001.
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org for updates, documentation, and revision history.
  7. #include <iostream>
  8. #include <iterator>
  9. #include <string>
  10. #include <algorithm>
  11. #include <boost/tokenizer.hpp>
  12. #include <boost/array.hpp>
  13. #include <boost/test/minimal.hpp>
  14. int test_main( int /*argc*/, char* /*argv*/[] )
  15. {
  16. using namespace boost;
  17. // Use tokenizer
  18. {
  19. const std::string test_string = ";;Hello|world||-foo--bar;yow;baz|";
  20. std::string answer[] = { "Hello", "world", "foo", "bar", "yow", "baz" };
  21. typedef tokenizer<char_separator<char> > Tok;
  22. char_separator<char> sep("-;|");
  23. Tok t(test_string, sep);
  24. BOOST_REQUIRE(std::equal(t.begin(),t.end(),answer));
  25. }
  26. {
  27. const std::string test_string = ";;Hello|world||-foo--bar;yow;baz|";
  28. std::string answer[] = { "", "", "Hello", "|", "world", "|", "", "|", "",
  29. "foo", "", "bar", "yow", "baz", "|", "" };
  30. typedef tokenizer<char_separator<char> > Tok;
  31. char_separator<char> sep("-;", "|", boost::keep_empty_tokens);
  32. Tok t(test_string, sep);
  33. BOOST_REQUIRE(std::equal(t.begin(), t.end(), answer));
  34. }
  35. {
  36. const std::string test_string = "This,,is, a.test..";
  37. std::string answer[] = {"This","is","a","test"};
  38. typedef tokenizer<> Tok;
  39. Tok t(test_string);
  40. BOOST_REQUIRE(std::equal(t.begin(),t.end(),answer));
  41. }
  42. {
  43. const std::string test_string = "Field 1,\"embedded,comma\",quote \\\", escape \\\\";
  44. std::string answer[] = {"Field 1","embedded,comma","quote \""," escape \\"};
  45. typedef tokenizer<escaped_list_separator<char> > Tok;
  46. Tok t(test_string);
  47. BOOST_REQUIRE(std::equal(t.begin(),t.end(),answer));
  48. }
  49. {
  50. const std::string test_string = ",1,;2\\\";3\\;,4,5^\\,\'6,7\';";
  51. std::string answer[] = {"","1","","2\"","3;","4","5\\","6,7",""};
  52. typedef tokenizer<escaped_list_separator<char> > Tok;
  53. escaped_list_separator<char> sep("\\^",",;","\"\'");
  54. Tok t(test_string,sep);
  55. BOOST_REQUIRE(std::equal(t.begin(),t.end(),answer));
  56. }
  57. {
  58. const std::string test_string = "12252001";
  59. std::string answer[] = {"12","25","2001"};
  60. typedef tokenizer<offset_separator > Tok;
  61. boost::array<int,3> offsets = {{2,2,4}};
  62. offset_separator func(offsets.begin(),offsets.end());
  63. Tok t(test_string,func);
  64. BOOST_REQUIRE(std::equal(t.begin(),t.end(),answer));
  65. }
  66. // Use token_iterator_generator
  67. {
  68. const std::string test_string = "This,,is, a.test..";
  69. std::string answer[] = {"This","is","a","test"};
  70. typedef token_iterator_generator<char_delimiters_separator<char> >::type Iter;
  71. Iter begin = make_token_iterator<std::string>(test_string.begin(),
  72. test_string.end(),char_delimiters_separator<char>());
  73. Iter end;
  74. BOOST_REQUIRE(std::equal(begin,end,answer));
  75. }
  76. {
  77. const std::string test_string = "Field 1,\"embedded,comma\",quote \\\", escape \\\\";
  78. std::string answer[] = {"Field 1","embedded,comma","quote \""," escape \\"};
  79. typedef token_iterator_generator<escaped_list_separator<char> >::type Iter;
  80. Iter begin = make_token_iterator<std::string>(test_string.begin(),
  81. test_string.end(),escaped_list_separator<char>());
  82. Iter begin_c(begin);
  83. Iter end;
  84. BOOST_REQUIRE(std::equal(begin,end,answer));
  85. while(begin_c != end)
  86. {
  87. BOOST_REQUIRE(begin_c.at_end() == 0);
  88. ++begin_c;
  89. }
  90. BOOST_REQUIRE(begin_c.at_end());
  91. }
  92. {
  93. const std::string test_string = "12252001";
  94. std::string answer[] = {"12","25","2001"};
  95. typedef token_iterator_generator<offset_separator>::type Iter;
  96. boost::array<int,3> offsets = {{2,2,4}};
  97. offset_separator func(offsets.begin(),offsets.end());
  98. Iter begin = make_token_iterator<std::string>(test_string.begin(),
  99. test_string.end(),func);
  100. Iter end= make_token_iterator<std::string>(test_string.end(),
  101. test_string.end(),func);
  102. BOOST_REQUIRE(std::equal(begin,end,answer));
  103. }
  104. // Test copying
  105. {
  106. const std::string test_string = "abcdef";
  107. token_iterator_generator<offset_separator>::type beg, end, other;
  108. boost::array<int,3> ar = {{1,2,3}};
  109. offset_separator f(ar.begin(),ar.end());
  110. beg = make_token_iterator<std::string>(test_string.begin(),test_string.end(),f);
  111. ++beg;
  112. other = beg;
  113. ++other;
  114. BOOST_REQUIRE(*beg=="bc");
  115. BOOST_REQUIRE(*other=="def");
  116. other = make_token_iterator<std::string>(test_string.begin(),
  117. test_string.end(),f);
  118. BOOST_REQUIRE(*other=="a");
  119. }
  120. // Test non-default constructed char_delimiters_separator
  121. {
  122. const std::string test_string = "how,are you, doing";
  123. std::string answer[] = {"how",",","are you",","," doing"};
  124. tokenizer<> t(test_string,char_delimiters_separator<char>(true,",",""));
  125. BOOST_REQUIRE(std::equal(t.begin(),t.end(),answer));
  126. }
  127. return 0;
  128. }