Improve NetAdr2 regex

This commit is contained in:
Kawe Mazidjatari 2022-05-13 20:59:41 +02:00
parent 70503bd975
commit ffa6eb55e8

View File

@ -225,12 +225,18 @@ bool CNetAdr2::SetFromSockadr(sockaddr_storage* s)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
string CNetAdr2::GetBase(void) const string CNetAdr2::GetBase(void) const
{ {
string svIpAdr = m_svip; static std::regex rx("[^\\[]*.(.*)(\\]).*");
static std::regex rx("\\].*"); std::smatch smRegexMatches;
svIpAdr.erase(0, 1); std::regex_search(m_svip, smRegexMatches, rx);
svIpAdr = std::regex_replace(svIpAdr, rx, "");
return svIpAdr; if (smRegexMatches.size() > 0)
{
return smRegexMatches[1].str();
}
else
{
return "127.0.0.1";
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -239,11 +245,18 @@ string CNetAdr2::GetBase(void) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
string CNetAdr2::GetBase(string svInAdr) const string CNetAdr2::GetBase(string svInAdr) const
{ {
static std::regex rx("\\].*"); static std::regex rx("[^\\[]*.(.*)(\\]).*");
svInAdr.erase(0, 1); std::smatch smRegexMatches;
svInAdr = std::regex_replace(svInAdr, rx, ""); std::regex_search(svInAdr, smRegexMatches, rx);
return svInAdr; if (smRegexMatches.size() > 0)
{
return smRegexMatches[1].str();
}
else
{
return "127.0.0.1";
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------