Add alternate form string parsing to IPEndpoint

This commit is contained in:
Vinnie Falco
2013-09-29 17:37:01 -07:00
parent d0a0dbf430
commit ae551cde63
2 changed files with 63 additions and 0 deletions

View File

@@ -235,6 +235,14 @@ public:
*/
static IPEndpoint from_string (std::string const& s);
/** Create an IPEndpoint from a string.
If a parsing error occurs, the endpoint will be empty.
This recognizes an alternate form of the text. Instead of a colon
separating the optional port specification, any amount of whitespace
is allowed.
*/
static IPEndpoint from_string_altform (std::string const& s);
/** Copy assign an IPv4 address.
The port is set to zero.
*/

View File

@@ -500,6 +500,61 @@ std::istream& operator>> (std::istream &is, IPEndpoint& ep)
//------------------------------------------------------------------------------
IPEndpoint IPEndpoint::from_string_altform (std::string const& s)
{
// Accept the regular form if it parses
{
IPEndpoint ep (IPEndpoint::from_string (s));
if (! ep.empty())
return ep;
}
// Now try the alt form
std::stringstream is (s);
IPEndpoint::V4 v4;
is >> v4;
if (! is.fail())
{
IPEndpoint ep (v4);
if (is.rdbuf()->in_avail()>0)
{
if (! parse::expect (is, ' '))
return IPEndpoint();
while (is.rdbuf()->in_avail()>0)
{
char c;
is.get(c);
if (c != ' ')
{
is.unget();
break;
}
}
uint16 port;
is >> port;
if (is.fail())
return IPEndpoint();
return ep.withPort (port);
}
else
{
// Just an address with no port
return ep;
}
}
// Could be V6 here...
return IPEndpoint();
}
//------------------------------------------------------------------------------
int compare (IPEndpoint::V4 const& lhs, IPEndpoint::V4 const& rhs)
{
if (lhs.value < rhs.value)