diff options
author | Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> | 2022-05-13 21:54:39 +0200 |
---|---|---|
committer | Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> | 2022-05-13 21:54:39 +0200 |
commit | 76ed31a29822e40d157792fcbccc604587c3d790 (patch) | |
tree | d2f63a4e45fde87775749944f507484784510f8b | |
parent | af9e2fe286e97140c40b2461bbd5d131125ac768 (diff) | |
download | NorthstarLauncher-76ed31a29822e40d157792fcbccc604587c3d790.tar.gz NorthstarLauncher-76ed31a29822e40d157792fcbccc604587c3d790.zip |
Improve regex in 'CNetAdr2::GetBase'
-rw-r--r-- | NorthstarDedicatedTest/NetAdr2.cpp | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/NorthstarDedicatedTest/NetAdr2.cpp b/NorthstarDedicatedTest/NetAdr2.cpp index edf2d180..1a44390b 100644 --- a/NorthstarDedicatedTest/NetAdr2.cpp +++ b/NorthstarDedicatedTest/NetAdr2.cpp @@ -224,12 +224,18 @@ bool CNetAdr2::SetFromSockadr(sockaddr_storage* s) //----------------------------------------------------------------------------- std::string CNetAdr2::GetBase(void) const { - std::string svIpAdr = m_svip; - static std::regex rx("\\].*"); - svIpAdr.erase(0, 1); - svIpAdr = std::regex_replace(svIpAdr, rx, ""); + static std::regex rx("[^\\[]*.(.*)(\\]).*"); + std::smatch smRegexMatches; + std::regex_search(m_svip, smRegexMatches, rx); - return svIpAdr; + if (smRegexMatches.size() > 0) + { + return smRegexMatches[1].str(); + } + else + { + return "127.0.0.1"; + } } //----------------------------------------------------------------------------- @@ -238,11 +244,18 @@ std::string CNetAdr2::GetBase(void) const //----------------------------------------------------------------------------- std::string CNetAdr2::GetBase(std::string svInAdr) const { - static std::regex rx("\\].*"); - svInAdr.erase(0, 1); - svInAdr = std::regex_replace(svInAdr, rx, ""); + static std::regex rx("[^\\[]*.(.*)(\\]).*"); + std::smatch smRegexMatches; + std::regex_search(svInAdr, smRegexMatches, rx); - return svInAdr; + if (smRegexMatches.size() > 0) + { + return smRegexMatches[1].str(); + } + else + { + return "127.0.0.1"; + } } //----------------------------------------------------------------------------- |