lib/command/string.js
function bitpos(key, bit, start, end /* req */) {
  var req = typeof arguments[arguments.length - 1] === 'object'
    ? arguments[arguments.length - 1] : null
    , buf = this.getValueBuffer(key, req, true)
    , i
    , ind;

  if(buf === undefined) buf = new Buffer('');
  if(bit && !buf.length) return -1;

  // got start/end range
  if(arguments.length >= 3) {
    if(start < 0) start = buf.length + start;
    if(start < 0) start = 0;
    if(end < 0) end = buf.length + end;
    if(end < 0) end = 0;
    if(end > buf.length || end === undefined) end = buf.length;
  }else{
    start = 0;
    end = buf.length;
  }

  if(typeof start === 'object') start = 0;
  if(typeof end === 'object') end = buf.length;

  // get the range we are inspecting
  buf = buf.slice(start, end + 1);

  function getBitPos(bit, byte, index) {
    var mask = [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01]
      , i;
    for(i = 0;i < mask.length;i++) {
      if(bit && (byte & mask[i]) || !bit && !(byte & mask[i])) {
        return index + i;
      }
    }
    return -1;
  }

  for(i = 0;i < buf.length;i++) {
    ind = getBitPos(bit, buf[i], (start + i) * 8);
    if(~ind) return ind;
  }

  // looking for clear bit, but all bits set
  if(!bit) return i * 8;

  return -1;
}
function bitpos(key, bit, start, end /* req */) {
  var req = typeof arguments[arguments.length - 1] === 'object'
    ? arguments[arguments.length - 1] : null
    , buf = this.getValueBuffer(key, req, true)
    , i
    , ind;

  if(buf === undefined) buf = new Buffer('');
  if(bit && !buf.length) return -1;

  // got start/end range
  if(arguments.length >= 3) {
    if(start < 0) start = buf.length + start;
    if(start < 0) start = 0;
    if(end < 0) end = buf.length + end;
    if(end < 0) end = 0;
    if(end > buf.length || end === undefined) end = buf.length;
  }else{
    start = 0;
    end = buf.length;
  }

  if(typeof start === 'object') start = 0;
  if(typeof end === 'object') end = buf.length;

  // get the range we are inspecting
  buf = buf.slice(start, end + 1);

  function getBitPos(bit, byte, index) {
    var mask = [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01]
      , i;
    for(i = 0;i < mask.length;i++) {
      if(bit && (byte & mask[i]) || !bit && !(byte & mask[i])) {
        return index + i;
      }
    }
    return -1;
  }

  for(i = 0;i < buf.length;i++) {
    ind = getBitPos(bit, buf[i], (start + i) * 8);
    if(~ind) return ind;
  }

  // looking for clear bit, but all bits set
  if(!bit) return i * 8;

  return -1;
}
function bitpos ( key , bit , start , end /* req */ ) { var req = typeof arguments [ arguments . length - 1 ] === 'object' ? arguments [ arguments . length - 1 ] : null , buf = this . getValueBuffer ( key , req , true ) , i , ind ; if ( buf === undefined ) buf = new Buffer ( '' ) ; if ( bit && ! buf . length ) return - 1 ; // got start/end range if ( arguments . length >= 3 ) { if ( start < 0 ) start = buf . length + start ; if ( start < 0 ) start = 0 ; if ( end < 0 ) end = buf . length + end ; if ( end < 0 ) end = 0 ; if ( end > buf . length || end === undefined ) end = buf . length ; } else { start = 0 ; end = buf . length ; } if ( typeof start === 'object' ) start = 0 ; if ( typeof end === 'object' ) end = buf . length ; // get the range we are inspecting buf = buf . slice ( start , end + 1 ) ; function getBitPos ( bit , byte , index ) { var mask = [ 0x80 , 0x40 , 0x20 , 0x10 , 0x08 , 0x04 , 0x02 , 0x01 ] , i ; for ( i = 0 ; i < mask . length ; i ++ ) { if ( bit && ( byte & mask [ i ] ) || ! bit && ! ( byte & mask [ i ] ) ) { return index + i ; } } return - 1 ; } for ( i = 0 ; i < buf . length ; i ++ ) { ind = getBitPos ( bit , buf [ i ] , ( start + i ) * 8 ) ; if ( ~ ind ) return ind ; } // looking for clear bit, but all bits set if ( ! bit ) return i * 8 ; return - 1 ; }
Return the position of the first bit set to 1 or 0 in a string.
Return the position of the first bit set to 1 or 0 in a string .