ext/iodine/fio.c
/**
 * A helper function that converts between String data to a signed int64_t.
 *
 * Numbers are assumed to be in base 10. Octal (`0###`), Hex (`0x##`/`x##`) and
 * binary (`0b##`/ `b##`) are recognized as well. For binary Most Significant
 * Bit must come first.
 *
 * The most significant difference between this function and `strtol` (aside of
 * API design), is the added support for binary representations.
 */
A helper function that converts between String data to a signed int64_t.
Numbers are assumed to be in base 10.

The most significant difference between this function and `strtol` (aside of
API design), is the added support for binary representations.
A helper function that converts between String data to a signed int64_t . Numbers are assumed to be in base 10 . The most significant difference between this function and ` strtol ` ( aside of API design ) is the added support for binary representations .
int64_t fio_atol(char **pstr) {
  char *str = *pstr;
  uint64_t result = 0;
  uint8_t invert = 0;
  while (isspace(*str))
    ++(str);
  if (str[0] == '-') {
    invert ^= 1;
    ++str;
  } else if (*str == '+') {
    ++(str);
  }
  if (str[0] == 'B' || str[0] == 'b' ||
      (str[0] == '0' && (str[1] == 'b' || str[1] == 'B'))) {
    if (str[0] == '0')
      str++;
    str++;
    fio_atol_skip_zero(&str);
    while (str[0] == '0' || str[0] == '1') {
      result = (result << 1) | (str[0] - '0');
      str++;
    }
    goto sign; 
  } else if (str[0] == 'x' || str[0] == 'X' ||
             (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))) {
    if (str[0] == '0')
      str++;
    str++;
    fio_atol_skip_zero(&str);
    result = fio_atol_consume_hex(&str);
    if (fio_atol_skip_hex_test(&str)) 
      return 0;
    goto sign; 
  } else if (str[0] == '0') {
    fio_atol_skip_zero(&str);
    result = fio_atol_consume(&str, 8);
    if (fio_atol_skip_test(&str, 8)) 
      return 0;
  } else {
    result = fio_atol_consume(&str, 10);
    if (fio_atol_skip_test(&str, 10)) 
      return 0;
  }
  if (result & ((uint64_t)1 << 63))
    result = INT64_MAX; 
sign:
  if (invert)
    result = 0 - result;
  *pstr = str;
  return (int64_t)result;
}
int64_t fio_atol ( char * * pstr ) { char * str = * pstr ; uint64_t result = 0 ; uint8_t invert = 0 ; while ( isspace ( * str ) ) ++ ( str ) ; if ( str [ 0 ] == ' ' ) { invert ^= 1 ; ++ str ; } else if ( * str == ' ' ) { ++ ( str ) ; } if ( str [ 0 ] == ' ' || str [ 0 ] == ' ' || ( str [ 0 ] == ' ' && ( str [ 1 ] == ' ' || str [ 1 ] == ' ' ) ) ) { if ( str [ 0 ] == ' ' ) str ++ ; str ++ ; fio_atol_skip_zero ( & str ) ; while ( str [ 0 ] == ' ' || str [ 0 ] == ' ' ) { result = ( result << 1 ) | ( str [ 0 ] - ' ' ) ; str ++ ; } goto sign ; } else if ( str [ 0 ] == ' ' || str [ 0 ] == ' ' || ( str [ 0 ] == ' ' && ( str [ 1 ] == ' ' || str [ 1 ] == ' ' ) ) ) { if ( str [ 0 ] == ' ' ) str ++ ; str ++ ; fio_atol_skip_zero ( & str ) ; result = fio_atol_consume_hex ( & str ) ; if ( fio_atol_skip_hex_test ( & str ) ) return 0 ; goto sign ; } else if ( str [ 0 ] == ' ' ) { fio_atol_skip_zero ( & str ) ; result = fio_atol_consume ( & str , 8 ) ; if ( fio_atol_skip_test ( & str , 8 ) ) return 0 ; } else { result = fio_atol_consume ( & str , 10 ) ; if ( fio_atol_skip_test ( & str , 10 ) ) return 0 ; } if ( result & ( ( uint64_t ) 1 << 63 ) ) result = INT64_MAX ; sign : if ( invert ) result = 0 - result ; * pstr = str ; return ( int64_t ) result ; }
A helper function that converts between String data to a signed int64_t.
A helper function that converts between String data to a signed int64_t .
/* No binary representation in strtol */ /* base 2 */ /* no overlow protection, since sign might be embedded */ /* base 16 */ /* too large for a number */ /* no overlow protection, since sign might be embedded */ /* base 8 */ /* too large for a number */ /* base 10 */ /* too large for a number */ /* signed overflow protection */