Commit 8a28f7b0 authored by Max Kellermann's avatar Max Kellermann

tag/FixString: add optimistic quick check

Optimizes a few nanoseconds from the common code path.
parent cc72ceb3
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "util/StringView.hxx" #include "util/StringView.hxx"
#include "util/UTF8.hxx" #include "util/UTF8.hxx"
#include <algorithm>
#include <cassert> #include <cassert>
#include <stdlib.h> #include <stdlib.h>
...@@ -115,9 +116,23 @@ clear_non_printable(StringView src) ...@@ -115,9 +116,23 @@ clear_non_printable(StringView src)
return { dest, src.size }; 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> WritableBuffer<char>
FixTagString(StringView p) FixTagString(StringView p)
{ {
if (IsSafe(p))
/* optimistic optimization for the common case */
return nullptr;
auto utf8 = fix_utf8(p); auto utf8 = fix_utf8(p);
if (!utf8.IsNull()) if (!utf8.IsNull())
p = {utf8.data, utf8.size}; p = {utf8.data, utf8.size};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment