B
Buster Copley
While playing around with my quine I was reminded
of a situation I have come across before: when
the exit-condition for a loop needs to be checked
in the middle of a loop, rather than at the
beginning (while) or the end (do ... while). It
always seems to come up when I'm hand-rolling very
simple parsers. (Doctor, it hurts when I do this ...)
Anyone want to argue for or against any of these
three alternatives, or suggest something better?
template <typename RulesFuction>
void format (const string & s, RulesFunction f)
{
// f will be applied to an iterator to a '%' character.
// Applying f should return an iterator to the character
// after the whole '%' escape sequence.
// Side-effects of f should be limited to output on cout.
iter b (s.begin ()), e (s.end ()), i;
ostream_iterator <char> out (cout);
#if defined A
copy (b, i = find (b, e, '%'), out);
while (i != e) {
b = f (i);
copy (b, i = find (b, e, '%'), out);
}
#elif defined B
while (true) {
copy (b, i = find (b, e, '%'), out);
if (i == e) break;
b = f (i);
}
#else
goto start;
while (i != e) {
b = f (i);
start: copy (b, i = find (b, e, '%'), out);
}
#endif
}
of a situation I have come across before: when
the exit-condition for a loop needs to be checked
in the middle of a loop, rather than at the
beginning (while) or the end (do ... while). It
always seems to come up when I'm hand-rolling very
simple parsers. (Doctor, it hurts when I do this ...)
Anyone want to argue for or against any of these
three alternatives, or suggest something better?
template <typename RulesFuction>
void format (const string & s, RulesFunction f)
{
// f will be applied to an iterator to a '%' character.
// Applying f should return an iterator to the character
// after the whole '%' escape sequence.
// Side-effects of f should be limited to output on cout.
iter b (s.begin ()), e (s.end ()), i;
ostream_iterator <char> out (cout);
#if defined A
copy (b, i = find (b, e, '%'), out);
while (i != e) {
b = f (i);
copy (b, i = find (b, e, '%'), out);
}
#elif defined B
while (true) {
copy (b, i = find (b, e, '%'), out);
if (i == e) break;
b = f (i);
}
#else
goto start;
while (i != e) {
b = f (i);
start: copy (b, i = find (b, e, '%'), out);
}
#endif
}