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
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