1

I want to list all matching groups in the input. For example, I want to print all browsers I care about in a User-Agent HTTP header. One match is enough. Is there a way to get rid of the inner for loop in the code below. I looked in the boost/regex/sub_match.hpp and I have no ideas.

#include <string> 
#include <iostream> 
#include <boost/regex.hpp> // Boost 1.59, no C++14 for me

enum BrowserType
{
    FIREFOX = 0 ,
    CHROME,
    SAFARI,
    OPERA,
    IE,
    EDGE,
    OTHER,
};

const boost::regex BROWSERS_REGEX("(Firefox)|(Chrome)|(Safari)|(Opera)|(MSIE)|(Edge)|(Trident)");

int main()
{
        // I expect two matches here CHROME and SAFARI
        std::string input("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 Chrome/62.0.3202.94");
        boost::sregex_iterator res(input.begin(), input.end(), BROWSERS_REGEX);
        boost::sregex_iterator end;
        for(; res != end; ++res)
        {
                // elude copy here ?
                boost::smatch what = *res;
                // Can I know the index of the matching group w/o 'for'?
                for (int type = 0;type < OTHER;type++) 
                {
                        int groupIndex = type+1;
                        if (what[groupIndex].matched) 
                                std::cout << (BrowserType)type << ",";
                }
                std::cout << "\n";

        }

        return 0;
}

Important update. The strstr() based code is the fastest approach for strings shorter than 1K bytes

    const char *input("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 Chrome/62.0.3202.94");
    const char *browsers[OTHER+1] = {"Firefox", "Chrome", "Safari", "Opera", "MSIE", "Edge", "Trident"};
    int i = 0;
    for (const char **browser = &browsers[0];browser <= &browsers[OTHER];browser++, i++)
    {
            if (strstr(input, *browser))
            {
                    groupindex = i;
            }
    }
Larytet
  • 648
  • 3
  • 13
  • 1
    Just use `Firefox|Chrome|Safari|Opera|MSIE|Edge|Trident` pattern and grab the whole match. See https://ideone.com/6qOfrV – Wiktor Stribiżew May 16 '18 at 14:13
  • @WiktorStribiżew, this is not quite what I am looking for. I need the group index for further processing. The goal is to get an enumeration of the browser type(s) in the input. – Larytet May 16 '18 at 14:28
  • You have the access to the index of each match, see https://ideone.com/9bcXHI. If you mean to map browser name to some integer, it is not the regex problem then. You may use a vector of pair, and map the match value to the int. – Wiktor Stribiżew May 16 '18 at 14:30
  • @WiktorStribiżew this is exactly what my code does. I wonder if I can shave CPU cycles. – Larytet May 16 '18 at 14:35
  • Ok, so your code works, please consider posting it as [codereview.se]. – Wiktor Stribiżew May 16 '18 at 20:43

1 Answers1

1

You can use named captures.

I just used it earlier today: While doing url encoding , The std::regex_replace doesn't work properly for character "+" . Can some body help me out?

In your case, though, I'd use qi::symbols:

struct browser_type_sym : boost::spirit::qi::symbols<char, BrowserType> {
    browser_type_sym() {
        this->add
            ("Firefox", FIREFOX)
            ("Chrome",  CHROME)
            ("Safari",  SAFARI)
            ("Opera",   OPERA)
            ("MSIE",    IE)
            ("Edge",    EDGE)
            ("Trident", OTHER);
    }
} static const browser_type;

You can simply use it with any container of BrowserType:

template <typename Types>
bool extract_browser_ids(std::string const& userAgent, Types& into) {
    using boost::spirit::repository::qi::seek;
    return parse(userAgent.begin(), userAgent.end(), *seek [ browser_type ], into);
}

Using vector<BrowserType>

As you will see, if you use vector<BrowserType it will retain order and duplicates:

Live On Coliru

int main() {
    for (std::string const input : {
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 Chrome/62.0.3202.94",
            "Chrome; Opera; Chrome again!"
    }) {
        std::vector<BrowserType> types;
        extract_browser_ids(input, types);
        for(auto type : types)
            std::cout << type << ",";

        std::cout << "\n";
    }
}

Prints:

2,1,
1,3,1,

Using set<BrowserType>

When using set<> it will order and de-duplicate:

Live On Coliru

    std::set<BrowserType> types;

Prints:

1,2,
1,3,

Case Insensitivity

Just to show minor tweaks:

struct browser_type_sym : boost::spirit::qi::symbols<char, BrowserType> {
    browser_type_sym() {
        this->add
            ("firefox", FIREFOX)
            ("chrome",  CHROME)
            ("safari",  SAFARI)
            ("opera",   OPERA)
            ("msie",    IE)
            ("edge",    EDGE)
            ("trident", OTHER);
    }
} static const browser_type;

template <typename Types>
bool extract_browser_ids(std::string const& userAgent, Types& into) {
    using boost::spirit::repository::qi::seek;
    using boost::spirit::qi::no_case;
    return parse(userAgent.begin(), userAgent.end(), *seek [ no_case [ browser_type ] ], into);
}

Now case doesn't matter: Live On Coliru

Alternative: Boost Xpressive

Similarly you can use Boost Xpressive, which is slightly closer to the regex approach (though internally it still builds a trie of strings from the map).

The semantic actions require more manual labour, making it also less generic (it won't work with std::set without changes, unlike the Spirit Qi approach already shown).

Nevertheless, for completeness:

Live On Coliru

std::map<std::string, BrowserType> s_browser_type_map {
        {"Firefox", FIREFOX},
        {"Chrome",  CHROME},
        {"Safari",  SAFARI},
        {"Opera",   OPERA},
        {"MSIE",    IE},
        {"Edge",    EDGE},
        {"Trident", OTHER},
    };

#include <boost/xpressive/xpressive.hpp>
#include <boost/xpressive/regex_actions.hpp>

template <typename Types>
void extract_browser_ids(std::string const& userAgent, Types& into) {
    using namespace boost::xpressive;

    placeholder<Types> _result;
    sregex type = (a1 = s_browser_type_map) [ push_back(_result, a1) ];

    for (sregex_iterator it(userAgent.begin(), userAgent.end(), type, let(_result=into)),
            end; it != end; ++it) 
    { } // all side-effects in the semantic action
}

Which prints the same output as the vector<> Spirit example:

2,1,
1,3,1,
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Added some live demos using Boost Spirit. – sehe May 16 '18 at 22:08
  • Added an [alternative take](http://coliru.stacked-crooked.com/a/16f15237011b16e8) using Boost Xpressive. – sehe May 16 '18 at 22:45
  • The performance of the original regex based solution is about 50% better than the one based on the lookup in a vector. There is another problem - I do not have C++ 11 support, I use boost 1.49. That said you write outstanding C++ code. – Larytet May 17 '18 at 06:04
  • Do you have the benchmark code? I'm happy to translate the things to C++11. Not sure about Xpressive in Boost 1.49 (why though, even Ubuntu 12.04, which an [LTS release already past its End-Of-Life](https://www.ubuntu.com/info/release-end-of-life) (!!!) contained [Boost 1.54.0](https://packages.ubuntu.com/trusty/libboost-all-dev) e.g.) – sehe May 17 '18 at 07:43
  • No C++11 support in my case (work in progress though). I am running on CentOS 6 for production. This is not quite a ready to go benchmark, but close https://gist.github.com/larytet/247f09c0edae49fd2f2cea1fdfdc68d0 I tested on Ubuntu 17.10 – Larytet May 17 '18 at 07:51
  • My bad - I use Boost 1.59 – Larytet May 17 '18 at 08:38
  • I'm still interested in replicating the benchmark. I'm also happy to translate to C++03 – sehe May 17 '18 at 08:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171225/discussion-between-larytet-and-sehe). – Larytet May 17 '18 at 08:54