lib/upnp/Discovery: use string_view::starts_with() instead of strncmp()

This commit is contained in:
Max Kellermann 2024-01-04 12:53:57 +01:00
parent afa77099cf
commit 8d1b73ae89

View File

@ -14,7 +14,6 @@
#include <upnptools.h> #include <upnptools.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
UPnPDeviceDirectory::Downloader::Downloader(UPnPDeviceDirectory &_parent, UPnPDeviceDirectory::Downloader::Downloader(UPnPDeviceDirectory &_parent,
const UpnpDiscovery &disco) const UpnpDiscovery &disco)
@ -86,10 +85,12 @@ static constexpr char ContentDirectorySType[] = "urn:schemas-upnp-org:service:Co
// version 1 // version 1
[[gnu::pure]] [[gnu::pure]]
static bool static bool
isCDService(const char *st) noexcept isCDService(std::string_view st) noexcept
{ {
constexpr size_t sz = sizeof(ContentDirectorySType) - 3; std::string_view prefix = ContentDirectorySType;
return strncmp(ContentDirectorySType, st, sz) == 0; prefix.remove_suffix(2);
return st.starts_with(prefix);
} }
// The type of device we're asking for in search // The type of device we're asking for in search
@ -97,17 +98,19 @@ static constexpr char MediaServerDType[] = "urn:schemas-upnp-org:device:MediaSer
[[gnu::pure]] [[gnu::pure]]
static bool static bool
isMSDevice(const char *st) noexcept isMSDevice(std::string_view st) noexcept
{ {
constexpr size_t sz = sizeof(MediaServerDType) - 3; std::string_view prefix = MediaServerDType;
return strncmp(MediaServerDType, st, sz) == 0; prefix.remove_suffix(2);
return st.starts_with(prefix);
} }
static void static void
AnnounceFoundUPnP(UPnPDiscoveryListener &listener, const UPnPDevice &device) AnnounceFoundUPnP(UPnPDiscoveryListener &listener, const UPnPDevice &device)
{ {
for (const auto &service : device.services) for (const auto &service : device.services)
if (isCDService(service.serviceType.c_str())) if (isCDService(service.serviceType))
listener.FoundUPnP(ContentDirectoryService(device, listener.FoundUPnP(ContentDirectoryService(device,
service)); service));
} }
@ -116,7 +119,7 @@ static void
AnnounceLostUPnP(UPnPDiscoveryListener &listener, const UPnPDevice &device) AnnounceLostUPnP(UPnPDiscoveryListener &listener, const UPnPDevice &device)
{ {
for (const auto &service : device.services) for (const auto &service : device.services)
if (isCDService(service.serviceType.c_str())) if (isCDService(service.serviceType))
listener.LostUPnP(ContentDirectoryService(device, listener.LostUPnP(ContentDirectoryService(device,
service)); service));
} }
@ -298,7 +301,7 @@ UPnPDeviceDirectory::GetDirectories()
std::vector<ContentDirectoryService> out; std::vector<ContentDirectoryService> out;
for (const auto &descriptor : directories) { for (const auto &descriptor : directories) {
for (const auto &service : descriptor.device.services) { for (const auto &service : descriptor.device.services) {
if (isCDService(service.serviceType.c_str())) { if (isCDService(service.serviceType)) {
out.emplace_back(descriptor.device, service); out.emplace_back(descriptor.device, service);
} }
} }
@ -321,7 +324,7 @@ UPnPDeviceDirectory::GetServer(std::string_view friendly_name)
continue; continue;
for (const auto &service : device.services) for (const auto &service : device.services)
if (isCDService(service.serviceType.c_str())) if (isCDService(service.serviceType))
return {device, service}; return {device, service};
} }