Boost logo

Boost Users :

Subject: [Boost-users] [string_algo] find_format_all calls formatter with invalid match?
From: Eric MALENFANT (Eric.Malenfant_at_[hidden])
Date: 2009-02-16 12:28:58


It seems that find_format_all() calls the formatter with an invalid
match when the end of the input is reached.

Is this a bug? Am I missing something?

Example:

// A Boost.StringAlgo Formatter expecting to be called for regex matches
for (\d+\.)(\d+)
struct NextVersionNumber
{
    template<class RegexMatchT>
    std::string operator()(const RegexMatchT& M)const
    {
        // Without this check, we end up with a bad_lexical_cast
exception,
        // because operator() ends up being called twice:
        // First with a good match, and then with an emtpy one
        if (!Match){
          return "";
        }

        using boost::lexical_cast;
        return M.match_results()[1] +
               lexical_cast<std::string>(lexical_cast<unsigned
int>(M.match_results()[2]) + 1);
    }
};

int main()
{
    namespace bsa = boost::algorithm;
    const std::string s("version is 1.0");
    
    // Outputs:
    // Next version is: 1.1
    std::cout << "Next version is: " <<
        bsa::find_format_all_copy(
            s, bsa::regex_finder(boost::regex("(\\d+\\.)(\\d+)")),
            NextVersionNumber()
        );
    return 0;
}


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