Skip to content

Commit 61ba182

Browse files
committed
Introduce inline functions for efficient ASCII whitespace detection and uint32-to-string conversion
1 parent 9eb934e commit 61ba182

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

include/gen_utils.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,33 @@ std::unique_ptr<SQLite3_result> get_SQLite3_resulset(MYSQL_RES* resultset);
394394

395395
std::vector<std::string> split_string(const std::string& str, char delimiter);
396396

397+
inline constexpr bool fast_isspace(unsigned char c) noexcept
398+
{
399+
// Matches: '\t' (0x09) through '\r' (0x0D), and ' ' (0x20)
400+
// That is: '\t', '\n', '\v', '\f', '\r', ' '
401+
//
402+
// (c - '\t') < 5 -> true for 0x09-0x0D inclusive
403+
// (c == ' ') -> true for space
404+
//
405+
// Use bitwise OR `|` (not logical `||`) to keep it branchless.
406+
return (c == ' ') | (static_cast<unsigned char>(c - '\t') < 5);
407+
}
408+
409+
inline constexpr char* fast_uint32toa(uint32_t value, char* out) {
410+
char* p = out;
411+
do {
412+
*p++ = '0' + (value % 10);
413+
value /= 10;
414+
} while (value);
415+
*p = '\0';
416+
char* start = out;
417+
char* end = p - 1;
418+
while (start < end) {
419+
char t = *start;
420+
*start++ = *end;
421+
*end-- = t;
422+
}
423+
return p;
424+
}
425+
397426
#endif /* __GEN_FUNCTIONS */

0 commit comments

Comments
 (0)