|
Boost Users : |
From: Eric Niebler (eric_at_[hidden])
Date: 2008-08-13 18:14:55
Olaf Peter wrote:
> Hi,
>
> my member function write() get called with string arguments like:
>
> Hello
> \n
> World
> \n
> Traceback ...\n
> NameError
> :
> name ... not defined
> \n
I'm a little confused. Is there a newline after "Hello", or two?
> etc.
>
> I want to add a prefix (a prompt '>>>' concete) so that the result will
> become:
>
> >>> Hello
> >>> World
> >>> Traceback ....
> >>> NameError: name ... not defined
And how do you know to put "NameError", ":", and "name ... not defined"
all on one line, but "Hello" and "World" on different lines?
> Yes, this comes from redirected python output.
>
> The simplest solution seems to use regex/xpressive:
>
> class redirector
> {
> const std::string m_prompt;
> const xpr::sregex m_re;
> const std::string m_re_fmt;
>
> public:
> redirector(const std::string& prompt = ">>> ")
> : m_prompt(prompt),
> m_re(xpr::sregex::compile("(\\n)!")),
> m_re_fmt(m_prompt + "\n")
> { }
>
> void write(const std::string& text) {
>
> std::cout << m_prompt
> << xpr::regex_replace(text, m_re, m_re_fmt);
> }
> ...
> };
>
> where my regex won't work. What is the correct one since I can get a
> single '\n', a terminated '\n'
What is a "terminated '\n'"?
> or multiline strings separated by '\n'.
> Is this approach good for this problem?
A regex is probably your best bet. If you define the problem better, I
or someone else here can probably help. Or, there are plenty of online
resources that could help you author the regex. Check out the Regex
Coach: http://www.weitz.de/regex-coach/
> Anyway, I get a lot of warnings from constructor and write function, any
> ideas why?
You would need to provide a complete, compilable example and the
compiler output, as well as giving me a clue what compiler you're using.
-- Eric Niebler BoostPro Computing http://www.boostpro.com
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net