Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
davisjam committed Jun 2, 2018
1 parent 7e5a727 commit fb047b2
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -1355,36 +1355,35 @@ function splitCells(tableRow, count) {
return cells;
}

// Return str with all trailing {c | all but c} removed
// allButC: Default false
function rtrim(str, c, allButC) {
if (typeof allButC === 'undefined') {
allButC = false;
// Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
// invert: Remove suffix of non-c chars intead. Default false.
function rtrim(str, c, invert) {
if (typeof invert === 'undefined' || !invert) {
invert = false;
} else {
allButC = true;
invert = true;
}
var mustMatchC = !allButC;

if (str.length === 0) {
return '';
}

// ix+1 of leftmost that fits description
// i.e. the length of the string we should return
var curr = str.length;
// Length of suffix matching the invert condition.
var suffLen = 0;

while (curr > 0) {
var currChar = str.charAt(curr - 1);
if (mustMatchC && currChar === c) {
curr--;
} else if (!mustMatchC && currChar !== c) {
curr--;
// Step left until we fail to match the invert condition.
while (suffLen < str.length) {
var currChar = str.charAt(str.length - suffLen - 1);
if (currChar === c && !invert) {
suffLen++;
} else if (currChar !== c && invert) {
suffLen++;
} else {
break;
}
}

return str.substr(0, curr);
return str.substr(0, str.length - suffLen);
}

/**
Expand Down

0 comments on commit fb047b2

Please sign in to comment.