I’ve got a bunch of text like this:
foo
bar
baz
What’s likely to be the most efficient way in C++ of transforming that to this:
<p>foo<br />bar</p>
<p>baz</p>
for large(ish) quantities of text (up to 8000 characters).
I’m happy to use boost’s regex_replace, but I was wondering if string searching for \n\n might be more efficient? Any thoughts? Any other approaches?
Most third-party libraries are not available to me in the environment I’m working in.
I would use a simple state-machine. It does require
comparison of the state for each time through the loop, but
it should not matter (it could be optimised by having a sub
loop in the third state – see below). The start state would
be the same as when two newlines have be encountered. There
would be a variable for the previous character and one for
keeping track of the position of the last newline (used for
generating output).
The states would be:
encountered double new line. Action when enter into state: output of <p>, the line and </p>
encountered single new line. Action when enter into state: output of the line and
encountered normal character
The program would look more like a C-program, though…