UI: Util/Net: Use QStringView

This commit is contained in:
Nodir Temirkhodjaev 2024-04-06 16:13:01 +03:00
parent 326adf8a43
commit 2ada1a9dc3
4 changed files with 56 additions and 45 deletions

View File

@ -184,7 +184,7 @@ IpRange::ParseError IpRange::parseIpLine(
: parseIp4Address(ip, mask, ip4RangeMap, pair4Size, maskSep);
}
IpRange::ParseError IpRange::parseIp4Address(const QString &ip, const QString &mask,
IpRange::ParseError IpRange::parseIp4Address(const QStringView ip, const QStringView mask,
ip4range_map_t &ip4RangeMap, int &pair4Size, char maskSep)
{
quint32 from, to = 0;
@ -211,7 +211,7 @@ IpRange::ParseError IpRange::parseIp4Address(const QString &ip, const QString &m
}
IpRange::ParseError IpRange::parseIp4AddressMask(
const QString &mask, quint32 &from, quint32 &to, char maskSep)
const QStringView mask, quint32 &from, quint32 &to, char maskSep)
{
switch (maskSep) {
case '-': // e.g. "127.0.0.0-127.255.255.255"
@ -225,7 +225,7 @@ IpRange::ParseError IpRange::parseIp4AddressMask(
}
IpRange::ParseError IpRange::parseIp4AddressMaskFull(
const QString &mask, quint32 &from, quint32 &to)
const QStringView mask, quint32 &from, quint32 &to)
{
bool ok;
to = NetUtil::textToIp4(mask, &ok);
@ -245,7 +245,7 @@ IpRange::ParseError IpRange::parseIp4AddressMaskFull(
}
IpRange::ParseError IpRange::parseIp4AddressMaskPrefix(
const QString &mask, quint32 &from, quint32 &to)
const QStringView mask, quint32 &from, quint32 &to)
{
bool ok = true;
const int nbits = mask.isEmpty() ? emptyNetMask() : mask.toInt(&ok);
@ -261,7 +261,8 @@ IpRange::ParseError IpRange::parseIp4AddressMaskPrefix(
return ErrorOk;
}
IpRange::ParseError IpRange::parseIp6Address(const QString &ip, const QString &mask, char maskSep)
IpRange::ParseError IpRange::parseIp6Address(
const QStringView ip, const QStringView mask, char maskSep)
{
bool hasMask = false;
ip6_addr_t from, to;
@ -289,7 +290,7 @@ IpRange::ParseError IpRange::parseIp6Address(const QString &ip, const QString &m
}
IpRange::ParseError IpRange::parseIp6AddressMask(
const QString &mask, ip6_addr_t &from, ip6_addr_t &to, bool &hasMask, char maskSep)
const QStringView mask, ip6_addr_t &from, ip6_addr_t &to, bool &hasMask, char maskSep)
{
hasMask = false;
@ -304,7 +305,7 @@ IpRange::ParseError IpRange::parseIp6AddressMask(
}
IpRange::ParseError IpRange::parseIp6AddressMaskFull(
const QString &mask, ip6_addr_t &to, bool &hasMask)
const QStringView mask, ip6_addr_t &to, bool &hasMask)
{
bool ok;
to = NetUtil::textToIp6(mask, &ok);
@ -320,7 +321,7 @@ IpRange::ParseError IpRange::parseIp6AddressMaskFull(
}
IpRange::ParseError IpRange::parseIp6AddressMaskPrefix(
const QString &mask, ip6_addr_t &from, ip6_addr_t &to, bool &hasMask)
const QStringView mask, ip6_addr_t &from, ip6_addr_t &to, bool &hasMask)
{
bool ok;
const int nbits = mask.toInt(&ok);

View File

@ -107,21 +107,23 @@ private:
IpRange::ParseError parseIpLine(
const QStringView line, ip4range_map_t &ip4RangeMap, int &pair4Size);
IpRange::ParseError parseIp4Address(const QString &ip, const QString &mask,
IpRange::ParseError parseIp4Address(const QStringView ip, const QStringView mask,
ip4range_map_t &ip4RangeMap, int &pair4Size, char maskSep);
IpRange::ParseError parseIp4AddressMask(
const QString &mask, quint32 &from, quint32 &to, char maskSep);
IpRange::ParseError parseIp4AddressMaskFull(const QString &mask, quint32 &from, quint32 &to);
IpRange::ParseError parseIp4AddressMaskPrefix(const QString &mask, quint32 &from, quint32 &to);
const QStringView mask, quint32 &from, quint32 &to, char maskSep);
IpRange::ParseError parseIp4AddressMaskFull(const QStringView mask, quint32 &from, quint32 &to);
IpRange::ParseError parseIp4AddressMaskPrefix(
const QStringView mask, quint32 &from, quint32 &to);
IpRange::ParseError parseIp6Address(const QString &ip, const QString &mask, char maskSep);
IpRange::ParseError parseIp6Address(const QStringView ip, const QStringView mask, char maskSep);
IpRange::ParseError parseIp6AddressMask(
const QString &mask, ip6_addr_t &from, ip6_addr_t &to, bool &hasMask, char maskSep);
IpRange::ParseError parseIp6AddressMaskFull(const QString &mask, ip6_addr_t &to, bool &hasMask);
const QStringView mask, ip6_addr_t &from, ip6_addr_t &to, bool &hasMask, char maskSep);
IpRange::ParseError parseIp6AddressMaskFull(
const QStringView mask, ip6_addr_t &to, bool &hasMask);
IpRange::ParseError parseIp6AddressMaskPrefix(
const QString &mask, ip6_addr_t &from, ip6_addr_t &to, bool &hasMask);
const QStringView mask, ip6_addr_t &from, ip6_addr_t &to, bool &hasMask);
void fillIp4Range(const ip4range_map_t &ipRangeMap, int pairSize);

View File

@ -22,7 +22,7 @@ struct sock_addr
#define sock_addr_get_inp(sap) ((void *) &(sap)->u.in.sin_addr)
quint32 NetUtil::textToIp4(const QString &text, bool *ok)
quint32 NetUtil::textToIp4(const QStringView text, bool *ok)
{
quint32 ip4;
@ -37,6 +37,11 @@ quint32 NetUtil::textToIp4(const QString &text, bool *ok)
return ntohl(*((unsigned long *) &ip4));
}
quint32 NetUtil::textToIp4(const char *text, bool *ok)
{
return textToIp4(QString::fromLatin1(text), ok);
}
QString NetUtil::ip4ToText(quint32 ip)
{
quint32 ip4 = htonl((unsigned long) ip);
@ -48,7 +53,7 @@ QString NetUtil::ip4ToText(quint32 ip)
return QString::fromWCharArray(buf);
}
ip6_addr_t NetUtil::textToIp6(const QString &text, bool *ok)
ip6_addr_t NetUtil::textToIp6(const QStringView text, bool *ok)
{
ip6_addr_t ip6;
@ -63,6 +68,11 @@ ip6_addr_t NetUtil::textToIp6(const QString &text, bool *ok)
return ip6;
}
ip6_addr_t NetUtil::textToIp6(const char *text, bool *ok)
{
return textToIp6(QString::fromLatin1(text), ok);
}
QString NetUtil::ip6ToText(const ip6_addr_t &ip)
{
wchar_t buf[MAX_IPV6_LEN];
@ -159,31 +169,27 @@ QString NetUtil::getHostName(const QString &address)
QStringList NetUtil::localIpNetworks()
{
static QStringList list = QStringList()
<< "0.0.0.0/32" // non-routable meta-address
<< "10.0.0.0/8"
<< "100.64.0.0/10" // for carrier-grade NAT deployment
<< "127.0.0.0/8" // Loopback
<< "169.254.0.0/16" // if cannot obtain a network address via DHCP
<< "172.16.0.0/12"
<< "192.168.0.0/16"
<< "239.255.255.250/32" // IP Multicast for DLNA/UPNP
<< "255.255.255.255/32" // IP Broadcast
<< "::/0" // non-routable meta-address
<< "::/128"
<< "::1/128" // Localhost
<< "::ffff:0:0/96"
<< "::ffff:0:0:0/96"
<< "64:ff9b::/96"
<< "100::/64"
<< "2001::/32" // Global Unique Addresses (GUA) - Routable IPv6 addresses
<< "2001:20::/28"
<< "2001:db8::/32" // Documentation prefix used for examples
<< "2002::/16"
<< "fc00::/7" // Unique Local Addresses (ULA) - also known as “Private” IPv6 addresses
<< "fe80::/10" // Link Local addresses, only valid inside a single broadcast domain
<< "ff00::/8" // Multicast addresses
;
static QStringList list = {
"0.0.0.0/32", // non-routable meta-address
"10.0.0.0/8",
"100.64.0.0/10" // for carrier-grade NAT deployment
"127.0.0.0/8", // Loopback
"169.254.0.0/16", // if cannot obtain a network address via DHCP
"172.16.0.0/12", "192.168.0.0/16",
"239.255.255.250/32", // IP Multicast for DLNA/UPNP
"255.255.255.255/32", // IP Broadcast
"::/0", // non-routable meta-address
"::/128",
"::1/128", // Localhost
"::ffff:0:0/96", "::ffff:0:0:0/96", "64:ff9b::/96", "100::/64",
"2001::/32", // Global Unique Addresses (GUA) - Routable IPv6 addresses
"2001:20::/28",
"2001:db8::/32", // Documentation prefix used for examples
"2002::/16",
"fc00::/7", // Unique Local Addresses (ULA) - also known as “Private” IPv6 addresses
"fe80::/10", // Link Local addresses, only valid inside a single broadcast domain
"ff00::/8", // Multicast addresses
};
return list;
}

View File

@ -10,13 +10,15 @@ class NetUtil
{
public:
// Convert IPv4 address from text to number
static quint32 textToIp4(const QString &text, bool *ok = nullptr);
static quint32 textToIp4(const QStringView text, bool *ok = nullptr);
static quint32 textToIp4(const char *text, bool *ok = nullptr);
// Convert IPv4 address from number to text
static QString ip4ToText(quint32 ip);
// Convert IPv6 address from text to number
static ip6_addr_t textToIp6(const QString &text, bool *ok = nullptr);
static ip6_addr_t textToIp6(const QStringView text, bool *ok = nullptr);
static ip6_addr_t textToIp6(const char *text, bool *ok = nullptr);
// Convert IPv6 address from number to text
static QString ip6ToText(const ip6_addr_t &ip);