Skip to content

Commit

Permalink
Fix List to work with empty matches.
Browse files Browse the repository at this point in the history
  • Loading branch information
gskinner committed Jul 4, 2018
1 parent a97ba75 commit ff3e935
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
15 changes: 8 additions & 7 deletions dev/src/helpers/BrowserSolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default class BrowserSolver {

let tool = this._req.tool;
result.tool = { id: tool.id };
if (!error && tool.input != null) {
if (!error || error.warning && tool.input != null) {
let str = Utils.unescSubstStr(tool.input);
result.tool.result = (tool.id === "replace") ? this._getReplace(str) : this._getList(str);
}
Expand All @@ -94,6 +94,7 @@ export default class BrowserSolver {
}

_getList(str) {
// TODO: should we move this into a worker?
let source = this._req.text, result = "", repl, ref, trimR = 0, regex;

// build a RegExp without the global flag:
Expand All @@ -107,14 +108,14 @@ export default class BrowserSolver {
trimR = str.length;
str = "$&"+str;
}
while (true) {
ref = source.replace(regex, "\b");
let index = ref.indexOf("\b");
if (index === -1 || ref.length > source.length) { break; }
do {
ref = source.replace(regex, "\b"); // bell char - just a placeholder to find
let index = ref.indexOf("\b"), empty = (ref.length > source.length);
if (index === -1) { break; }
repl = source.replace(regex, str);
result += repl.substr(index, repl.length-ref.length+1);
source = ref.substr(index+1);
}
source = ref.substr(index+(empty?2:1));
} while (source.length);
if (trimR) { result = result.substr(0,result.length-trimR); }
return result;
}
Expand Down
1 change: 1 addition & 0 deletions dev/src/utils/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Utils.shorten = function (str, length, htmlSafe, tag="") {
};

Utils.unescSubstStr = function(str) {
if (!str) { return ""; }
return str.replace(Utils.SUBST_ESC_RE, (a, b, c)=> Utils.SUBST_ESC_CHARS[b] || String.fromCharCode(parseInt(c, 16)) );
};

Expand Down

0 comments on commit ff3e935

Please sign in to comment.