A most vexing issues with the "search" algorithm and the "istream_iterator"
{ edited by mod to shorten lines to ~70 characters. -mod }
A little background on how I encountered this odd behaviour:
I ran into this particular vexing behaviour when I had the need to parse
and clean up noisy binary satellite data.
For whatever reason about 0.01% of the data has garbage in it that make
it impossible properly parse fields of remaining data once garbage has
been detected... Luckily the data has sync points named "PKT:" followed
by the a 32bit sequence number.
Once it is discovered we processing garbage all the application need do
is traverse to the sync point and hopefully everything after the sync
point will be in alignment and the parsing process can be happy with
want follows:
The search algorithms {http://www.cplusplus.com/reference/algorithm/search/}
clearly states that an iterator to the first element of pattern. So
dutifully I wrote the code in LISTING 1 --the test.binary file is at
the bottom of this article.
LISTING 1 execution returned back "01implementationssothatBoostli";
an iterator that pointed to after the pattern was found --Also note
that I had to pre increment the iterator else I would have gotten the
following result upon execution "P01implementationssothatBoostl" --Where
the 'P' came from is unknown to me --see the test.binary file.
LISTING 2 places me exactly where I want to be; that being at the start
of the sync point; "PKT:01implementationssothatBoo". Notice here I still
have to pre-increment the iterator returned by the search algorithm else
'P' magically appear and I get "PPKT:01implementationssothatBo"
For my particular application, I am fortunate that search algorithm does
find the sync point and I have no use for the returned iterator; I can
use the LISTING 2 exemplar to continue processing satellite data
I found this unexpected behaviour particular vexing until I discovered
how search was behaving using stream iterators --it works as expected
with normal std container iterators.
Something for others to be wary of when using stream iterators with std
algorithms...
For the record I am using openSuSE 13.2 and gcc -v gives "gcc version
4.8.3 20140627 [gcc-4_8-branch revision 212064] (SUSE Linux) "
Let me know if you do not see this behaviour on your compile and system
##########################################################################
LISTING 1:
##########################################################################
int main(int argc, char **argv)
{
std::ifstream is("test.binary", std::ifstream::binary);
const auto SYNC = "PKT:";
std::istream_iterator<char> eos;
std::istream_iterator<char> sos(is);
auto sf = std::search(sos, eos, SYNC, SYNC+4);
for (auto i = 0u; i < 30u; i++)
std::cout << *++sf;
std::cout << std::endl;
return EXIT_SUCCESS;
}
##########################################################################
LISTING 2:
##########################################################################
int main(int argc, char **argv)
{
std::ifstream is("test.binary", std::ifstream::binary);
const auto SYNC = "PKT:";
std::istream_iterator<char> eos;
std::istream_iterator<char> sos(is);
auto sf = std::search(sos, eos, SYNC, SYNC+4);
is.seekg(is.tellg() - static_cast<fdecltype(is.tellg())>(4),
std::ios_base::beg);
for (auto i = 0u; i < 30u; i++)
std::cout << *++sf;
std::cout << std::endl;
return EXIT_SUCCESS;
}
##########################################################################
test.binary: Yes I copies some text from the boost.org page and stuck
PKT:01 in the middle of it
##########################################################################
We aim to establish "existing practice" and provide reference PKT:01
implementations so that Boost libraries are suitable for eventual
standardization. Ten Boost libraries are included in the C++ Standards
Committee's Library Technical Report (TR1) and in the new C++11 Standard.
C++11 also includes several more Boost libraries in addition to those
from TR1. More Boost libraries are proposed for standardization in C++17.
##########################################################################
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]