cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

atoi.c (625B)


      1#include <stdint.h>
      2#include <stdlib.h>
      3#include <types.h>
      4#include <ctype.h>
      5
      6inline bool _isdigit(char c) {
      7    return ((uint8_t)((uint8_t)c - '0') < 10u) ? true : false;
      8}
      9
     10int atoi(const char *s) NONBANKED
     11{
     12    bool sign = false;
     13    int n;
     14
     15    const uint8_t * pc = (const uint8_t *)s;
     16
     17    for(; ((*pc == ' ') || (*pc == '\t') || (*pc == '\n')); ++pc);
     18    
     19    switch(*pc) {
     20        case '-':
     21            sign = true;
     22            /* and fall through */
     23        case '+':
     24            ++pc;
     25            break;
     26    }
     27    
     28    for(n = 0; _isdigit(*pc); ++pc) n = 10 * n + (*pc - '0');
     29
     30    return (sign == 0 ? n : -n);
     31}