Skip to content

Commit

Permalink
new undocumented function str_locate
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed Mar 15, 2020
1 parent ad6f703 commit d993343
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ else a Buffer for Buffer data. The callback is invoked when the 'end' event is
Changelog
---------

- 1.6.0 - new function `entries`
- 1.6.0 - new function `entries`, new undocumented function str_locate
- 1.5.1 - fix getProperty, do not prevent multiple callbacks from readBody
- 1.5.0 - new functions `derive`, `varargsRenamed`, `isMethodContext`, `readBody`;
make varargs attach the instance `this` if no `self` given,
Expand Down
19 changes: 19 additions & 0 deletions qibl.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var qibl = module.exports = {
str_truncate: str_truncate,
strtok: strtok,
str_random: str_random,
str_locate: str_locate,
newBuf: saneBuf().new,
allocBuf: saneBuf().alloc,
fromBuf: saneBuf().from,
Expand Down Expand Up @@ -363,6 +364,15 @@ function str_random( len ) {
}
}

// locate all substrings patt in string str, and call handler with their offsets
function str_locate( str, patt, handler, arg ) {
var pos = 0;
for (var pos = 0; pos < str.length; pos += patt.length) {
if ((pos = str.indexOf(patt, pos)) >= 0) handler(pos, arg);
else break;
}
}

// similar to strtok() and strsep() but empty strings are allowed
// NOTE: this function is not reentrant
// On first call the string is remembered, on subsequent calls it should be null.
Expand Down Expand Up @@ -699,6 +709,15 @@ function vinterpolate( str, patt, args, addslashes ) {
return ret;
}

/**
// return array with all locations of patt in str
function offsetsOf( str, patt ) {
var offsets = new Array();
str_locate(str, patt, function gather(ix, arr) { arr.push(ix) }, offsets);
return offsets;
}
**/

function addslashes( str, patt ) {
// TODO: default to escaping only \' \" \\ \0, pass them in for escapeshellcmd()
patt = patt || /([\'\"\\\x00])/g;
Expand Down
33 changes: 33 additions & 0 deletions test-qibl.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,39 @@ module.exports = {
},
},

'str_locate': {
'passes the argument on every call to the handler': function(t) {
var args = [];
var arg = {};
qibl.str_locate('foo', 'o', function(offset, arg) { args.push(arg) }, arg);
t.deepEqual(args, [arg, arg]);
t.done();
},

'invokes the handler on every pattern found': function(t) {
var offsets = [];
var handler = function(offs, arr) { arr.push(offs) };

offsets = [];
qibl.str_locate('foobar boofar', 'foof', handler, offsets);
t.deepEqual(offsets, []);

offsets = [];
qibl.str_locate('foobar boofar', 'boo', handler, offsets);
t.deepEqual(offsets, [7]);

offsets = [];
qibl.str_locate('foobar boofar', 'oo', handler, offsets);
t.deepEqual(offsets, [1, 8]);

offsets = [];
qibl.str_locate('foobar boofar', 'o', handler, offsets);
t.deepEqual(offsets, [1, 2, 8, 9]);

t.done();
},
},

'saneBuf': {
'newBuf should emulate legacy constructor': function(t) {
var buf = qibl.newBuf("foo");
Expand Down

0 comments on commit d993343

Please sign in to comment.