summaryrefslogtreecommitdiff
path: root/src/command/FileCommands.cxx
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2019-06-06 12:02:55 +0200
committerMax Kellermann <max@musicpd.org>2019-06-06 13:00:53 +0200
commit548aa00111e781c6b31e9a2486306d607081b1ec (patch)
treef44be7fd991040473082f1b2431c06c8e4c7aeb4 /src/command/FileCommands.cxx
parent76eb550011312cf5c096c86e7920decfe139b63d (diff)
tag/Handler: pass StringView to OnTag() and OnPair()
Eliminates a number of allocations, because callers don't need to copy the strings to a newly allocated buffer only to null-terminate them. And most callers don't need to have a null-terminated string.
Diffstat (limited to 'src/command/FileCommands.cxx')
-rw-r--r--src/command/FileCommands.cxx20
1 files changed, 10 insertions, 10 deletions
diff --git a/src/command/FileCommands.cxx b/src/command/FileCommands.cxx
index 9f040bd8b..82101470a 100644
--- a/src/command/FileCommands.cxx
+++ b/src/command/FileCommands.cxx
@@ -27,6 +27,7 @@
#include "client/Client.hxx"
#include "client/Response.hxx"
#include "util/CharUtil.hxx"
+#include "util/StringView.hxx"
#include "util/UriUtil.hxx"
#include "tag/Handler.hxx"
#include "tag/Generic.hxx"
@@ -110,13 +111,12 @@ handle_listfiles_local(Response &r, Path path_fs)
gcc_pure
static bool
-IsValidName(const char *p) noexcept
+IsValidName(const StringView s) noexcept
{
- if (!IsAlphaASCII(*p))
+ if (s.empty() || !IsAlphaASCII(s.front()))
return false;
- while (*++p) {
- const char ch = *p;
+ for (const char ch : s) {
if (!IsAlphaASCII(ch) && ch != '_' && ch != '-')
return false;
}
@@ -126,11 +126,9 @@ IsValidName(const char *p) noexcept
gcc_pure
static bool
-IsValidValue(const char *p) noexcept
+IsValidValue(const StringView s) noexcept
{
- while (*p) {
- const char ch = *p++;
-
+ for (const char ch : s) {
if ((unsigned char)ch < 0x20)
return false;
}
@@ -145,9 +143,11 @@ public:
explicit PrintCommentHandler(Response &_response) noexcept
:NullTagHandler(WANT_PAIR), response(_response) {}
- void OnPair(const char *key, const char *value) noexcept override {
+ void OnPair(StringView key, StringView value) noexcept override {
if (IsValidName(key) && IsValidValue(value))
- response.Format("%s: %s\n", key, value);
+ response.Format("%.*s: %.*s\n",
+ int(key.size), key.data,
+ int(value.size), value.data);
}
};