mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
Micro-optimize parsing
This commit is contained in:
@ -2102,21 +2102,21 @@ struct id_adapter {
|
|||||||
Handler &handler;
|
Handler &handler;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <bool IS_CONSTEXPR, class InputIt, class T>
|
// Return the result via the out param to workaround gcc bug 77539.
|
||||||
FMT_CONSTEXPR InputIt find(InputIt first, InputIt last, const T &value) {
|
template <bool IS_CONSTEXPR, typename T, typename Ptr = const T*>
|
||||||
for (; first != last; ++first) {
|
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out) {
|
||||||
if (*first == value)
|
for (out = first; out != last; ++out) {
|
||||||
return first;
|
if (*out == value)
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return last;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
inline const char *find<false, const char*, char>(
|
inline bool find<false, char>(
|
||||||
const char *first, const char *last, const char &value) {
|
const char *first, const char *last, char value, const char *&out) {
|
||||||
auto result = static_cast<const char*>(
|
out = static_cast<const char*>(std::memchr(first, value, last - first));
|
||||||
std::memchr(first, value, last - first));
|
return out != FMT_NULL;
|
||||||
return result ? result : last;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool IS_CONSTEXPR, typename Char, typename Handler>
|
template <bool IS_CONSTEXPR, typename Char, typename Handler>
|
||||||
@ -2125,8 +2125,8 @@ FMT_CONSTEXPR void parse_format_string(
|
|||||||
struct writer {
|
struct writer {
|
||||||
FMT_CONSTEXPR void operator()(const Char *begin, const Char *end) {
|
FMT_CONSTEXPR void operator()(const Char *begin, const Char *end) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
auto p = find<IS_CONSTEXPR>(begin, end, '}');
|
const Char *p = FMT_NULL;
|
||||||
if (p == end) {
|
if (!find<IS_CONSTEXPR>(begin, end, '}', p)) {
|
||||||
handler_.on_text(begin, end);
|
handler_.on_text(begin, end);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -2146,8 +2146,8 @@ FMT_CONSTEXPR void parse_format_string(
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
// Doing two passes with memchr (one for '{' and another for '}') is up to
|
// Doing two passes with memchr (one for '{' and another for '}') is up to
|
||||||
// 2.5x faster than the naive one-pass implementation on long format strings.
|
// 2.5x faster than the naive one-pass implementation on long format strings.
|
||||||
auto p = find<IS_CONSTEXPR>(begin, end, '{');
|
const Char *p = FMT_NULL;
|
||||||
if (p == end) {
|
if (!find<IS_CONSTEXPR>(begin, end, '{', p)) {
|
||||||
if (begin != end)
|
if (begin != end)
|
||||||
write(begin, end);
|
write(begin, end);
|
||||||
return;
|
return;
|
||||||
@ -2176,6 +2176,8 @@ FMT_CONSTEXPR void parse_format_string(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
begin = pointer_from(it) + 1;
|
begin = pointer_from(it) + 1;
|
||||||
|
if (begin == end)
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user