Server IP : 85.214.239.14 / Your IP : 18.119.135.231 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/3/root/proc/self/root/usr/share/doc/re2c/examples/c/conditions/ |
Upload File : |
// re2c $INPUT -o $OUTPUT -i #include <stdint.h> #include <limits.h> #include <assert.h> static const uint64_t ERROR = UINT64_MAX; template<int BASE> static void add(uint64_t &u, char d) { u = u * BASE + d; if (u > UINT32_MAX) u = ERROR; } static uint64_t parse_u32(const char *s) { const char *YYCURSOR = s, *YYMARKER; uint64_t u = 0; /*!re2c re2c:yyfill:enable = 0; re2c:define:YYCTYPE = char; end = "\x00"; '0b' / [01] { goto bin; } "0" { goto oct; } "" / [1-9] { goto dec; } '0x' / [0-9a-fA-F] { goto hex; } * { return ERROR; } */ bin: /*!re2c end { return u; } [01] { add<2>(u, YYCURSOR[-1] - '0'); goto bin; } * { return ERROR; } */ oct: /*!re2c end { return u; } [0-7] { add<8>(u, YYCURSOR[-1] - '0'); goto oct; } * { return ERROR; } */ dec: /*!re2c end { return u; } [0-9] { add<10>(u, YYCURSOR[-1] - '0'); goto dec; } * { return ERROR; } */ hex: /*!re2c end { return u; } [0-9] { add<16>(u, YYCURSOR[-1] - '0'); goto hex; } [a-f] { add<16>(u, YYCURSOR[-1] - 'a' + 10); goto hex; } [A-F] { add<16>(u, YYCURSOR[-1] - 'A' + 10); goto hex; } * { return ERROR; } */ } int main() { assert(parse_u32("") == ERROR); assert(parse_u32("1234567890") == 1234567890); assert(parse_u32("0b1101") == 13); assert(parse_u32("0x7Fe") == 2046); assert(parse_u32("0644") == 420); assert(parse_u32("9999999999") == ERROR); return 0; }