|
Boost : |
From: Ben Young (ben.young_at_[hidden])
Date: 2002-12-09 07:09:45
I have recently implemented a set of custom output iterators that allow
you to very trivially do escaping and quoting on data from any kind of
iterator source. Example usage would be
std::string input = "a bdsf 'foo bar baz'";
input.append( 1, '\0' );
std::string expectedOutput = "a bdsf \\'foo bar baz\\'";
expectedOutput.append( "\\0" );
std::string output;
std::copy( input.begin(), input.end(),
boost::escaping_iterator( std::back_inserter(output),
'\0', std::string("\\0") )('\'', std::string("\\'") ) );
TEST_EQUAL( output, expectedOutput );
There are also quoting iterators:
e.g 1 2 3 4 5 => <1> <2> <3> <4> <5>
and seperating iterators:
e.g 1 2 3 4 5 => 1,2,3,4,5
The escaping iterator can escape any number of different "characters"
via a simple syntx: e.g
escaping_iterator(outputTo, '\032', std::string("\\Z") )
('\0', std::string("\\0") )
('\b', std::string("\\b") )
('\t', std::string("\\t") )
('\n', std::string("\\n") )
('\r', std::string("\\r") )
('\"', std::string("\\\"") )
('\'', std::string("\\\'") )
('\\', std::string("\\\\") );
is an escaper that escapes data for mysql.
There is a slight performance hit, depending on inlining depth and other
factors, but the above complex iterator, is only about three times
slower than an explicit switch based escaper with gcc 3.2. Simpler
examples are comparable in speed to hand coded cases
Would anyone be interested in these iterators being added to boost? They
are fully generic, and have some test cases/example usage (though not much
more complicated than above)
Cheers
Ben
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk