Conditionally copying text to string

T

Thomas Matthews

Hi,

My goal is to extract a text field into a string type.
The text field is from a buffer (array) of characters
and delimited by white space[1] or double quotes.
*** I'm looking to perform this operation in one pass.

I have searched the FAQ and the newsgroup, but have
not found anything related to this.

I would like to use STL algorithms and predicates,
but I have found anything suitable (something like
copy_until). I have searched Josutti's book[2] and
did not see any algorithms that would accomplish my
objective in one pass. I could use the string::find
method, then a copy, but that is 2 passes.

I want to copy from the text field until a predicate
function returns true.

void Extract_Name_Field(const char *& buffer_ptr,
std::string& name_field)
{
name_field.erase();
if (isspace(*buffer_ptr))
{
// If the first character is white space,
// the field is empty.
return;
}
if (*buffer_ptr == '"')
{
// Copy text until a '"' is found.
// The field is not allowed to have embedded
// double quotes (").
copy_if(); // What STL algorithms to use here?
}
else
{
// Copy text until a whitespace is found.
// Use the std::isspace() function as a binary
// predicate.
copy_if(); // What STL algorithms to use here?
}
return;
}

The 'old fashioned' way:
void Old_Extract_Name(const char *& buffer_ptr,
std::string& name_field)
{
name_field.erase();
if (isspace(*buffer_ptr))
{
// If the first character is white space,
// the field is empty.
return;
}
if (*buffer_ptr == '"')
{
while (*++bufptr != '"') /* [3] */
{
name_field.append(*buf_ptr);
}
}
else
{
while (!isspace(*buf_ptr)) /* [3] */
{
name_field.append(*buf_ptr++);
}
}
return;
}

----
[1] White space is defined as isspace(x) == true;
[2] The Standard C++ Library, A Tutorial & Reference.
by Nicolai M. Josuttis
published by Addison-Wesley
ISBN 0-201-37926-0
[3] For simplicity, I have not put in the checks for
null terminated C style string.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
P

Peter Kragh

Thomas said:
I would like to use STL algorithms and predicates,
but I have found anything suitable (something like
copy_until). I have searched Josutti's book[2] and
did not see any algorithms that would accomplish my
objective in one pass. I could use the string::find
method, then a copy, but that is 2 passes.

Then why not make a copy_until :)

Wanting to use the standard algorithms is IMHO a good thing, but when they
don't fit, I see no reason for not making a new one by myself.

Try something like this:

template<typename InIt, typename OutIt, typename Pred>
OutIt copy_until(InIt first, InIt end, OutIt x, Pred pred)
{
for(; first != end; ++first, ++x)
{
if(pred(*first))
{
return x;
}
*x = *first;
}
return x;
}

You can use it like this:

// Copy until space
copy_until(
buffer_ptr,
buffer_ptr + strlen(buffer_ptr),
std::back_inserter(name_field),
isspace);

.... or

// Copy until "
copy_until(
buffer_ptr,
buffer_ptr + strlen(buffer_ptr),
std::back_inserter(name_field),
std::bind2nd(std::equal_to<char>(), '"');

HTH.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top