Skip to content

Commit 8216e4a

Browse files
committed
chore: improve ip_utils
1 parent 429ec6e commit 8216e4a

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/utils/ip_utils.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace duckdb
6565
}
6666
else
6767
{
68-
// For /32, the IP itself is the only host ( Hostroute )
68+
// For /32, the IP itself is the only host (Hostroute)
6969
info.network = ip;
7070
}
7171

@@ -74,7 +74,7 @@ namespace duckdb
7474

7575
bool IPCalculator::isValidInput (const std::string &input)
7676
{
77-
std::regex pattern (R"((\d{1,3}\.){3}\d{1,3}(/\d{1,2})?)");
77+
static const std::regex pattern (R"((\d{1,3}\.){3}\d{1,3}(/\d{1,2})?)");
7878
return std::regex_match (input, pattern);
7979
}
8080

@@ -91,14 +91,15 @@ namespace duckdb
9191
return true;
9292
}
9393

94-
std::vector<int> IPCalculator::parseIP (const std::string &ip)
94+
std::array<int, 4> IPCalculator::parseIP (const std::string &ip)
9595
{
96-
std::vector<int> octets;
96+
std::array<int, 4> octets{};
9797
std::stringstream ss (ip);
9898
std::string octet;
99-
while (std::getline (ss, octet, '.'))
99+
for (int i = 0; i < 4; ++i)
100100
{
101-
octets.push_back (std::stoi (octet));
101+
std::getline (ss, octet, '.');
102+
octets[i] = std::stoi (octet);
102103
}
103104
return octets;
104105
}
@@ -190,7 +191,7 @@ namespace duckdb
190191
return ss.str ();
191192
}
192193

193-
std::string IPCalculator::intToIP (const std::vector<int> &octets)
194+
std::string IPCalculator::intToIP (const std::array<int, 4> &octets)
194195
{
195196
std::stringstream ss;
196197
for (int i = 0; i < 4; ++i)

src/utils/ip_utils.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace duckdb
2727
private:
2828
static bool isValidInput (const std::string &input);
2929
static bool isValidIP (const std::string &ip);
30-
static std::vector<int> parseIP (const std::string &ip);
30+
static std::array<int, 4> parseIP (const std::string &ip);
3131
static std::string getSubnetMask (int maskBits);
3232
static std::string getWildcardMask (const std::string &subnetMask);
3333
static std::string getNetworkAddress (const std::string &ip, const std::string &subnetMask, const int &maskBits);
@@ -37,7 +37,7 @@ namespace duckdb
3737
static int getHostsPerNet (int maskBits);
3838
static std::string getIPClass (const std::string &ip);
3939
static std::string intToIP (uint32_t ip);
40-
static std::string intToIP (const std::vector<int> &octets);
40+
static std::string intToIP (const std::array<int, 4> &octets);
4141
};
4242
} // namespace netquack
4343
} // namespace duckdb

0 commit comments

Comments
 (0)