1// Copyright 2016 The Fuchsia Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include <stdlib.h> 6 7#include <ctype.h> 8#include <stdbool.h> 9#include <stdint.h> 10 11#define ATOx(T, fn) \ 12T fn(const char* nptr) { \ 13 while (nptr && isspace(*nptr)) { \ 14 nptr++; \ 15 } \ 16 \ 17 bool neg = false; \ 18 if (*nptr == '-') { \ 19 neg = true; \ 20 nptr++; \ 21 } \ 22 \ 23 T ret = 0; \ 24 for (; nptr; nptr++) { \ 25 if (!isdigit(*nptr)) break; \ 26 ret = 10*ret + (*nptr - '0'); \ 27 } \ 28 \ 29 if (neg) ret = -ret; \ 30 return ret; \ 31} 32 33 34ATOx(int, atoi) 35ATOx(long, atol) 36ATOx(long long, atoll) 37