Merge pull request #8514 from lioncash/sview

Common/Network: Make StringToMacAddress use a string_view
This commit is contained in:
JosJuice 2019-12-06 17:00:42 +01:00 committed by GitHub
commit 5a5c46a8b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 6 deletions

View File

@ -6,8 +6,6 @@
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <cstring>
#include <ctime>
#include <fmt/format.h> #include <fmt/format.h>
@ -43,10 +41,10 @@ std::string MacAddressToString(const MACAddress& mac)
mac[4], mac[5]); mac[4], mac[5]);
} }
std::optional<MACAddress> StringToMacAddress(const std::string& mac_string) std::optional<MACAddress> StringToMacAddress(std::string_view mac_string)
{ {
if (mac_string.empty()) if (mac_string.empty())
return {}; return std::nullopt;
int x = 0; int x = 0;
MACAddress mac{}; MACAddress mac{};
@ -70,7 +68,7 @@ std::optional<MACAddress> StringToMacAddress(const std::string& mac_string)
// nibble is a character in the MAC address, making 12 characters // nibble is a character in the MAC address, making 12 characters
// in total. // in total.
if (x / 2 != MAC_ADDRESS_SIZE) if (x / 2 != MAC_ADDRESS_SIZE)
return {}; return std::nullopt;
return std::make_optional(mac); return std::make_optional(mac);
} }

View File

@ -7,6 +7,7 @@
#include <array> #include <array>
#include <optional> #include <optional>
#include <string> #include <string>
#include <string_view>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -27,5 +28,5 @@ using MACAddress = std::array<u8, MAC_ADDRESS_SIZE>;
MACAddress GenerateMacAddress(MACConsumer type); MACAddress GenerateMacAddress(MACConsumer type);
std::string MacAddressToString(const MACAddress& mac); std::string MacAddressToString(const MACAddress& mac);
std::optional<MACAddress> StringToMacAddress(const std::string& mac_string); std::optional<MACAddress> StringToMacAddress(std::string_view mac_string);
} // namespace Common } // namespace Common