Files
wg_cpso/CppLibrary/fast.h

45 lines
930 B
C
Raw Permalink Normal View History

2026-03-25 18:20:24 +08:00
#pragma once
#define isspace_fast(x) ((x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == '\v' || (x) == '\f' || (x) == '\r')
int fast_atoi(const char *b)
{
int res = 0; // Initialize result
// Skip whitespace
for (; isspace_fast(*b); ++b);
if (*b == '-')
{
++b;
// Iterate through all characters of input string and update result
for (; ; ++b)
{
unsigned d = static_cast<unsigned>(*b) - '0';
if (d > 9)
{
return res;
}
res = res * 10 - d;
}
}
else if (*b == '+')
{
++b;
}
// Iterate through all characters of input string and update result
for (; ; ++b)
{
unsigned d = static_cast<unsigned>(*b) - '0';
if (d > 9)
{
return res;
}
res = res * 10 + d;
}
// unreachable
return res;
}