18

I have read http://www.codeproject.com/KB/recipes/Tokenizer.aspx and I want to have the last example ( at the end, just before all the graphs) "Extending Delimiter Predicates" in my main, but I don't get the same output tokens as the author of the article does when I assign the token_list to a vector, why?

How to put the real result in a list or a vector? I want to have this :

  • list0 abc
  • list1 123, mno xyz
  • list2 i\,jk

But I have something like :

  • list0 abc;"123, mno xyz",i\,jk
  • list1 123, mno xyz",i\,jk
  • list2 i\,jk

Source sample :

class extended_predicate
{
public:

   extended_predicate(const std::string& delimiters)
   : escape_(false),
     in_bracket_range_(false),
     mdp_(delimiters)
   {}

   inline bool operator()(const unsigned char c) const
   {
      if (escape_)
      {
         escape_ = false;
         return false;
      }
      else if ('\\' == c)
      {
         escape_ = true;
         return false;
      }
      else if ('"' == c)
      {
         in_bracket_range_ = !in_bracket_range_;
         return true;
      }
      else if (in_bracket_range_)
         return false;
      else
         return mdp_(c);
   }

   inline void reset()
   {
      escape_ = false;
      in_bracket_range_ = false;
   }

private:

   mutable bool escape_;
   mutable bool in_bracket_range_;
   mutable strtk::multiple_char_delimiter_predicate mdp_;
};

int main()
{
   std::string str = "abc;\"123, mno xyz\",i\\,jk";
   strtk::std_string::token_list_type token_list;
   strtk::split(extended_predicate(".,; "),
                str,
                std::back_inserter(token_list),
                strtk::split_options::compress_delimiters);

   return 0;
}
Marcos Gonzalez
  • 1,086
  • 2
  • 12
  • 19
bdelmas
  • 922
  • 2
  • 12
  • 20

1 Answers1

18

I could same results in codeproject. What version of gcc do you use? My gcc's version is following.

g++ (GCC) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

UPDATE: My tested code is here: https://gist.github.com/1037493

mattn
  • 7,571
  • 30
  • 54
  • I use VisualStudio 2010 Ultimate. – bdelmas Jun 21 '11 at 09:37
  • 18
    I tried with vc10. `"cl -Dstrtk_no_tr1_or_boost -DWIN32 -I"C:\Program Files\Boost_1_46_0" -I. test.cxx "`. But I got same results.(working good) – mattn Jun 21 '11 at 09:46
  • When I put token_list in a vector for exemple I haven't the good result. And when I see the values of token_list in set to set mode, I have the values that I wrote and not the 3 right values. How can I put the real values in a vector for example? – bdelmas Jun 21 '11 at 14:14