Sha256: 9b87a1b2b8bc402a124b2a69f040e2e084de4020c6e32c1cc3ffddfbaf6f6010

Contents?: true

Size: 1.5 KB

Versions: 33

Compression:

Stored size: 1.5 KB

Contents

module.exports = function bufferIndexOf(buff, search, offset, encoding){
  if (!Buffer.isBuffer(buff)) {
    throw TypeError('buffer is not a buffer');
  }

  // allow optional offset when providing an encoding
  if (encoding === undefined && typeof offset === 'string') {
    encoding = offset;
    offset = undefined;
  }

  if (typeof search === 'string') {
    search = new Buffer(search, encoding || 'utf8');
  } else if (typeof search === 'number' && !isNaN(search)) {
    search = new Buffer([search])
  } else if (!Buffer.isBuffer(search)) {
    throw TypeError('search is not a bufferable object');
  }

  if (search.length === 0) {
    return -1;
  }

  if (offset === undefined || (typeof offset === 'number' && isNaN(offset))) {
    offset = 0;
  } else if (typeof offset !== 'number') {
    throw TypeError('offset is not a number');
  }

  if (offset < 0) {
    offset = buff.length + offset
  }

  if (offset < 0) {
    offset = 0;
  }

  var m = 0;
  var s = -1;

  for (var i = offset; i < buff.length ; ++i) {
    if(buff[i] != search[m]){
      s = -1;
      // <-- go back
      // match abc to aabc
      // 'aabc'
      // 'aab'
      //    ^ no match
      // a'abc'
      //   ^ set index here now and look at these again.
      //   'abc' yay!
      i -= m-1
      m = 0;
    }

    if(buff[i] == search[m]) {
      if(s == -1) {
        s = i;
      }
      ++m;
      if(m == search.length) {
        break;
      }
    }
  }

  if (s > -1 && buff.length - s < search.length) {
    return -1;
  }
  return s;
}


Version data entries

33 entries across 32 versions & 11 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/buffer-indexof/index.js
disco_app-0.18.0 test/dummy/node_modules/buffer-indexof/index.js
disco_app-0.18.2 test/dummy/node_modules/buffer-indexof/index.js
disco_app-0.16.1 test/dummy/node_modules/buffer-indexof/index.js
disco_app-0.15.2 test/dummy/node_modules/buffer-indexof/index.js
disco_app-0.18.4 test/dummy/node_modules/buffer-indexof/index.js
disco_app-0.18.1 test/dummy/node_modules/buffer-indexof/index.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/buffer-indexof/index.js
disco_app-0.14.0 test/dummy/node_modules/buffer-indexof/index.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/buffer-indexof/index.js
tang-0.2.1 spec/tang_app/node_modules/buffer-indexof/index.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/buffer-indexof/index.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/buffer-indexof/index.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/buffer-indexof/index.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/buffer-indexof/index.js
tang-0.2.0 spec/tang_app/node_modules/buffer-indexof/index.js
tang-0.1.0 spec/tang_app/node_modules/buffer-indexof/index.js
tang-0.0.9 spec/tang_app/node_modules/buffer-indexof/index.js
enju_library-0.3.8 spec/dummy/node_modules/buffer-indexof/index.js
ilog-0.4.1 node_modules/buffer-indexof/index.js