ext/iodine/fio.c
/**
 * A helper function that writes a signed int64_t to a string.
 *
 * No overflow guard is provided, make sure there's at least 68 bytes
 * available (for base 2).
 *
 * Offers special support for base 2 (binary), base 8 (octal), base 10 and base
 * 16 (hex). An unsupported base will silently default to base 10. Prefixes
 * are automatically added (i.e., "0x" for hex and "0b" for base 2).
 *
 * Returns the number of bytes actually written (excluding the NUL
 * terminator).
 */
A helper function that writes a signed int64_t to a string.
No overflow guard is provided, make sure there's at least 68 bytes
available (for base 2).

Offers special support for base 2 (binary), base 8 (octal), base 10 and base
16 (hex). An unsupported base will silently default to base 10. Prefixes
are automatically added .

Returns the number of bytes actually written (excluding the NUL
terminator).
A helper function that writes a signed int64_t to a string . No overflow guard is provided make sure there ' s at least 68 bytes available ( for base 2 ) . Offers special support for base 2 ( binary ) base 8 ( octal ) base 10 and base 16 ( hex ) . An unsupported base will silently default to base 10 . Prefixes are automatically added . Returns the number of bytes actually written ( excluding the NUL terminator ) .
size_t fio_ltoa(char *dest, int64_t num, uint8_t base) {
  const char notation[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                           '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  size_t len = 0;
  char buf[48]; 
  if (!num)
    goto zero;
  switch (base) {
  case 1: 
  case 2:
    {
      uint64_t n = num; 
      uint8_t i = 0;    
      dest[len++] = '0';
      dest[len++] = 'b';
      while ((i < 64) && (n & 0x8000000000000000) == 0) {
        n = n << 1;
        i++;
      }
      if (i) {
        dest[len++] = '0';
      }
      while (i < 64) {
        dest[len++] = ((n & 0x8000000000000000) ? '1' : '0');
        n = n << 1;
        i++;
      }
      dest[len] = 0;
      return len;
    }
  case 8:
    {
      uint64_t l = 0;
      if (num < 0) {
        dest[len++] = '-';
        num = 0 - num;
      }
      dest[len++] = '0';
      while (num) {
        buf[l++] = '0' + (num & 7);
        num = num >> 3;
      }
      while (l) {
        --l;
        dest[len++] = buf[l];
      }
      dest[len] = 0;
      return len;
    }
  case 16:
    {
      uint64_t n = num; 
      uint8_t i = 0;    
      dest[len++] = '0';
      dest[len++] = 'x';
      while (i < 8 && (n & 0xFF00000000000000) == 0) {
        n = n << 8;
        i++;
      }
      if (i && (n & 0x8000000000000000)) {
        dest[len++] = '0';
        dest[len++] = '0';
      }
      while (i < 8) {
        uint8_t tmp = (n & 0xF000000000000000) >> 60;
        dest[len++] = notation[tmp];
        tmp = (n & 0x0F00000000000000) >> 56;
        dest[len++] = notation[tmp];
        i++;
        n = n << 8;
      }
      dest[len] = 0;
      return len;
    }
  case 3: 
  case 4: 
  case 5: 
  case 6: 
  case 7: 
  case 9: 
    if (num < 0) {
      dest[len++] = '-';
      num = 0 - num;
    }
    uint64_t l = 0;
    while (num) {
      uint64_t t = num / base;
      buf[l++] = '0' + (num - (t * base));
      num = t;
    }
    while (l) {
      --l;
      dest[len++] = buf[l];
    }
    dest[len] = 0;
    return len;
  default:
    break;
  }
  if (num < 0) {
    dest[len++] = '-';
    num = 0 - num;
  }
  uint64_t l = 0;
  while (num) {
    uint64_t t = num / 10;
    buf[l++] = '0' + (num - (t * 10));
    num = t;
  }
  while (l) {
    --l;
    dest[len++] = buf[l];
  }
  dest[len] = 0;
  return len;
zero:
  switch (base) {
  case 1:
  case 2:
    dest[len++] = '0';
    dest[len++] = 'b';
    break;
  case 8:
    dest[len++] = '0';
    break;
  case 16:
    dest[len++] = '0';
    dest[len++] = 'x';
    dest[len++] = '0';
    break;
  }
  dest[len++] = '0';
  dest[len] = 0;
  return len;
}
size_t fio_ltoa ( char * dest , int64_t num , uint8_t base ) { const char notation [ ] = { ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' } ; size_t len = 0 ; char buf [ 48 ] ; if ( ! num ) goto zero ; switch ( base ) { case 1 : case 2 : { uint64_t n = num ; uint8_t i = 0 ; dest [ len ++ ] = ' ' ; dest [ len ++ ] = ' ' ; while ( ( i < 64 ) && ( n & 0x8000000000000000 ) == 0 ) { n = n << 1 ; i ++ ; } if ( i ) { dest [ len ++ ] = ' ' ; } while ( i < 64 ) { dest [ len ++ ] = ( ( n & 0x8000000000000000 ) ? ' ' : ' ' ) ; n = n << 1 ; i ++ ; } dest [ len ] = 0 ; return len ; } case 8 : { uint64_t l = 0 ; if ( num < 0 ) { dest [ len ++ ] = ' ' ; num = 0 - num ; } dest [ len ++ ] = ' ' ; while ( num ) { buf [ l ++ ] = ' ' + ( num & 7 ) ; num = num >> 3 ; } while ( l ) { -- l ; dest [ len ++ ] = buf [ l ] ; } dest [ len ] = 0 ; return len ; } case 16 : { uint64_t n = num ; uint8_t i = 0 ; dest [ len ++ ] = ' ' ; dest [ len ++ ] = ' ' ; while ( i < 8 && ( n & 0xFF00000000000000 ) == 0 ) { n = n << 8 ; i ++ ; } if ( i && ( n & 0x8000000000000000 ) ) { dest [ len ++ ] = ' ' ; dest [ len ++ ] = ' ' ; } while ( i < 8 ) { uint8_t tmp = ( n & 0xF000000000000000 ) >> 60 ; dest [ len ++ ] = notation [ tmp ] ; tmp = ( n & 0x0F00000000000000 ) >> 56 ; dest [ len ++ ] = notation [ tmp ] ; i ++ ; n = n << 8 ; } dest [ len ] = 0 ; return len ; } case 3 : case 4 : case 5 : case 6 : case 7 : case 9 : if ( num < 0 ) { dest [ len ++ ] = ' ' ; num = 0 - num ; } uint64_t l = 0 ; while ( num ) { uint64_t t = num / base ; buf [ l ++ ] = ' ' + ( num - ( t * base ) ) ; num = t ; } while ( l ) { -- l ; dest [ len ++ ] = buf [ l ] ; } dest [ len ] = 0 ; return len ; default : break ; } if ( num < 0 ) { dest [ len ++ ] = ' ' ; num = 0 - num ; } uint64_t l = 0 ; while ( num ) { uint64_t t = num / 10 ; buf [ l ++ ] = ' ' + ( num - ( t * 10 ) ) ; num = t ; } while ( l ) { -- l ; dest [ len ++ ] = buf [ l ] ; } dest [ len ] = 0 ; return len ; zero : switch ( base ) { case 1 : case 2 : dest [ len ++ ] = ' ' ; dest [ len ++ ] = ' ' ; break ; case 8 : dest [ len ++ ] = ' ' ; break ; case 16 : dest [ len ++ ] = ' ' ; dest [ len ++ ] = ' ' ; dest [ len ++ ] = ' ' ; break ; } dest [ len ++ ] = ' ' ; dest [ len ] = 0 ; return len ; }
A helper function that writes a signed int64_t to a string.
A helper function that writes a signed int64_t to a string .
/* we only need up to 20 for base 10, but base 3 needs 41... */ /* fallthrough */ /* Base 2 */ /* avoid bit shifting inconsistencies with signed bit */ /* counting bits */ /* make sure the Binary representation doesn't appear signed. */ /* write to dest. */ /* Base 8 */ /* Base 16 */ /* avoid bit shifting inconsistencies with signed bit */ /* counting bits */ /* make sure the Hex representation doesn't appear misleadingly signed. */ /* write the damn thing, high to low */ /* fallthrough */ /* fallthrough */ /* fallthrough */ /* fallthrough */ /* fallthrough */ /* fallthrough */ /* rare bases */ /* Base 10, the default base */