From 1860f37169ddc8abfea2e33a71fab1b9c8433204 Mon Sep 17 00:00:00 2001 From: rimathia Date: Thu, 3 Sep 2020 11:47:02 +0200 Subject: [PATCH] use memchr for finding '%' in formatting string in printf.h --- include/fmt/printf.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/include/fmt/printf.h b/include/fmt/printf.h index 8c28ac232727..ad35d5b82a89 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -474,8 +474,18 @@ OutputIt basic_printf_context::format() { const Char* end = parse_ctx_.end(); auto it = start; while (it != end) { + if (end - it < 32) { + // Use a simple linear search instead of memchr for small strings. + it = std::find(it, end, '%'); + } else { + if (!detail::find(it, end, '%', it)) { + it = end; + } + } + if (it == end) { + continue; + } char_type c = *it++; - if (c != '%') continue; if (it != end && *it == c) { out = std::copy(start, it, out); start = ++it;