From 61ba18246522047488a7ce69d1cfff4294c50ee2 Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Thu, 30 Oct 2025 01:14:20 +0500 Subject: [PATCH] Introduce inline functions for efficient ASCII whitespace detection and uint32-to-string conversion --- include/gen_utils.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/include/gen_utils.h b/include/gen_utils.h index 12e498a73..904a4a276 100644 --- a/include/gen_utils.h +++ b/include/gen_utils.h @@ -394,4 +394,33 @@ std::unique_ptr get_SQLite3_resulset(MYSQL_RES* resultset); std::vector split_string(const std::string& str, char delimiter); +inline constexpr bool fast_isspace(unsigned char c) noexcept +{ + // Matches: '\t' (0x09) through '\r' (0x0D), and ' ' (0x20) + // That is: '\t', '\n', '\v', '\f', '\r', ' ' + // + // (c - '\t') < 5 -> true for 0x09-0x0D inclusive + // (c == ' ') -> true for space + // + // Use bitwise OR `|` (not logical `||`) to keep it branchless. + return (c == ' ') | (static_cast(c - '\t') < 5); +} + +inline constexpr char* fast_uint32toa(uint32_t value, char* out) { + char* p = out; + do { + *p++ = '0' + (value % 10); + value /= 10; + } while (value); + *p = '\0'; + char* start = out; + char* end = p - 1; + while (start < end) { + char t = *start; + *start++ = *end; + *end-- = t; + } + return p; +} + #endif /* __GEN_FUNCTIONS */ \ No newline at end of file