diff options
author | Max Kellermann <max@musicpd.org> | 2020-04-24 15:40:51 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2020-04-24 15:57:40 +0200 |
commit | 8a28f7b0a133bbf74312a78ea33603e92b5f8375 (patch) | |
tree | dcb5a29417de984e0be6c77b7cfc85d52a2977c2 /src/tag | |
parent | cc72ceb368185381851bec2d7d4fe1c4d69b3f46 (diff) |
tag/FixString: add optimistic quick check
Optimizes a few nanoseconds from the common code path.
Diffstat (limited to 'src/tag')
-rw-r--r-- | src/tag/FixString.cxx | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/tag/FixString.cxx b/src/tag/FixString.cxx index 6432a476e..ee6553bd8 100644 --- a/src/tag/FixString.cxx +++ b/src/tag/FixString.cxx @@ -24,6 +24,7 @@ #include "util/StringView.hxx" #include "util/UTF8.hxx" +#include <algorithm> #include <cassert> #include <stdlib.h> @@ -115,9 +116,23 @@ clear_non_printable(StringView src) return { dest, src.size }; } +gcc_pure +static bool +IsSafe(StringView s) noexcept +{ + return std::all_of(s.begin(), s.end(), + [](char ch){ + return IsASCII(ch) && IsPrintableASCII(ch); + }); +} + WritableBuffer<char> FixTagString(StringView p) { + if (IsSafe(p)) + /* optimistic optimization for the common case */ + return nullptr; + auto utf8 = fix_utf8(p); if (!utf8.IsNull()) p = {utf8.data, utf8.size}; |