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