Search code examples
c++cparsingbooststl

What is the nicest way to parse this in C++?


In my program, I have a list of "server address" in the following format:

host[:port]

The brackets here, indicate that the port is optional.

  • host can be a hostname, an IPv4 or IPv6 address (possibly in "bracket-enclosed" notation).
  • port, if present can be a numeric port number or a service string (like: "http" or "ssh").

If port is present and host is an IPv6 address, host must be in "bracket-enclosed" notation (Example: [::1])

Here are some valid examples:

localhost
localhost:11211
127.0.0.1:http
[::1]:11211
::1
[::1]

And an invalid example:

::1:80 // Invalid: Is this the IPv6 address ::1:80 and a default port, or the IPv6 address ::1 and the port 80 ?
::1:http // This is not ambigous, but for simplicity sake, let's consider this is forbidden as well.

My goal is to separate such entries in two parts (obviously host and port). I don't care if either the host or port are invalid as long as they don't contain a non-bracket-enclosed : (290.234.34.34.5 is ok for host, it will be rejected in the next process); I just want to separate the two parts, or if there is no port part, to know it somehow.

I tried to do something with std::stringstream but everything I come up to seems hacky and not really elegant.

How would you do this in C++ ?

I don't mind answers in C but C++ is prefered. Any boost solution is welcome as well.

Thank you.


Solution

  • Have you looked at boost::spirit? It might be overkill for your task, though.