Boost logo

Boost Users :

Subject: [Boost-users] [String_Ref] Working with other parts of boost
From: Norbert Wenzel (norbert.wenzel.lists_at_[hidden])
Date: 2013-11-17 14:47:57


Hi,

I wanted to play around with Boost.String_Ref and found some old code
that I thought would be the ideal case for string_refs. It's basically a
config file reader that stores the whole line and also a key/value pair
that may be found in that line. So a single line looked something like this:

std::string line; //original line as is in the file
boost::string_ref key; //extracted key subpart
boost::string_ref value; //extracted value subpart

But when I wanted to trim the string_refs (eg. for lexical_casting them)
I got compile errors, because boost::trim uses erase, which of course is
not supported by string_ref. But string_ref has the remove_prefix/suffix
member functions, which would be exactly what trim needs. So I quickly
hacked an additional overload of trim_*_if that calls the remove_*fix
instead of the .erase() member function and it seemed to work.

The second problem occurred when I tried to extract my key/value pair
from a line using the Tokenizer, which seems to be old, but it still
works for simple cases. Tokenizer internally uses assign() to fill an
internal buffer with the next token, which of course is no member
function of string_ref. Since it seems Tokenizer only uses the assign
version that takes two iterators it might be possible to add such an
assign() function to string_ref. At least I added one as a testcase
locally and it worked in my limited testcase.

Are there any plans to add either trim() or Tokenizer support for
string_ref? I'm not sure about the Tokenizer/assign stuff, since one
would have to change string_ref but I really think the trim() support
would be helpful. Anyway, these issues kept me from actually using
string_ref, since the code did not compile (or required changes to boost
headers in order to compile). And I did not want to redo some of the
boost string algorithm stuff in order to use string_refs.

Norbert


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net