Changeset: Invert conditions to improve readability

This commit is contained in:
Richard Hansen 2021-09-27 17:25:31 -04:00
parent b29e59419e
commit 7fa9b07116

View file

@ -425,41 +425,39 @@ exports.mergingOpAssembler = () => {
let bufOpAdditionalCharsAfterNewline = 0; let bufOpAdditionalCharsAfterNewline = 0;
const flush = (isEndDocument) => { const flush = (isEndDocument) => {
if (bufOp.opcode) { if (!bufOp.opcode) return;
if (isEndDocument && bufOp.opcode === '=' && !bufOp.attribs) { if (isEndDocument && bufOp.opcode === '=' && !bufOp.attribs) {
// final merged keep, leave it implicit // final merged keep, leave it implicit
} else { } else {
assem.append(bufOp);
if (bufOpAdditionalCharsAfterNewline) {
bufOp.chars = bufOpAdditionalCharsAfterNewline;
bufOp.lines = 0;
assem.append(bufOp); assem.append(bufOp);
if (bufOpAdditionalCharsAfterNewline) { bufOpAdditionalCharsAfterNewline = 0;
bufOp.chars = bufOpAdditionalCharsAfterNewline;
bufOp.lines = 0;
assem.append(bufOp);
bufOpAdditionalCharsAfterNewline = 0;
}
} }
bufOp.opcode = '';
} }
bufOp.opcode = '';
}; };
const append = (op) => { const append = (op) => {
if (op.chars > 0) { if (op.chars <= 0) return;
if (bufOp.opcode === op.opcode && bufOp.attribs === op.attribs) { if (bufOp.opcode === op.opcode && bufOp.attribs === op.attribs) {
if (op.lines > 0) { if (op.lines > 0) {
// bufOp and additional chars are all mergeable into a multi-line op // bufOp and additional chars are all mergeable into a multi-line op
bufOp.chars += bufOpAdditionalCharsAfterNewline + op.chars; bufOp.chars += bufOpAdditionalCharsAfterNewline + op.chars;
bufOp.lines += op.lines; bufOp.lines += op.lines;
bufOpAdditionalCharsAfterNewline = 0; bufOpAdditionalCharsAfterNewline = 0;
} else if (bufOp.lines === 0) { } else if (bufOp.lines === 0) {
// both bufOp and op are in-line // both bufOp and op are in-line
bufOp.chars += op.chars; bufOp.chars += op.chars;
} else {
// append in-line text to multi-line bufOp
bufOpAdditionalCharsAfterNewline += op.chars;
}
} else { } else {
flush(); // append in-line text to multi-line bufOp
copyOp(op, bufOp); bufOpAdditionalCharsAfterNewline += op.chars;
} }
} else {
flush();
copyOp(op, bufOp);
} }
}; };
@ -762,31 +760,28 @@ const textLinesMutator = (lines) => {
* @param {boolean} includeInSplice - indicates if attributes are present * @param {boolean} includeInSplice - indicates if attributes are present
*/ */
const skipLines = (L, includeInSplice) => { const skipLines = (L, includeInSplice) => {
if (L) { if (!L) return;
if (includeInSplice) { if (includeInSplice) {
if (!inSplice) { if (!inSplice) enterSplice();
enterSplice(); // TODO(doc) should this count the number of characters that are skipped to check?
} for (let i = 0; i < L; i++) {
// TODO(doc) should this count the number of characters that are skipped to check?
for (let i = 0; i < L; i++) {
curCol = 0;
putCurLineInSplice();
curLine++;
}
} else {
if (inSplice) {
if (L > 1) {
// TODO(doc) figure out why single lines are incorporated into splice instead of ignored
leaveSplice();
} else {
putCurLineInSplice();
}
}
curLine += L;
curCol = 0; curCol = 0;
putCurLineInSplice();
curLine++;
} }
// tests case foo in remove(), which isn't otherwise covered in current impl } else {
if (inSplice) {
if (L > 1) {
// TODO(doc) figure out why single lines are incorporated into splice instead of ignored
leaveSplice();
} else {
putCurLineInSplice();
}
}
curLine += L;
curCol = 0;
} }
// tests case foo in remove(), which isn't otherwise covered in current impl
}; };
/** /**
@ -797,20 +792,17 @@ const textLinesMutator = (lines) => {
* @param {boolean} includeInSplice - indicates if attributes are present * @param {boolean} includeInSplice - indicates if attributes are present
*/ */
const skip = (N, L, includeInSplice) => { const skip = (N, L, includeInSplice) => {
if (N) { if (!N) return;
if (L) { if (L) {
skipLines(L, includeInSplice); skipLines(L, includeInSplice);
} else { } else {
if (includeInSplice && !inSplice) { if (includeInSplice && !inSplice) enterSplice();
enterSplice(); if (inSplice) {
} // although the line is put into splice curLine is not increased, because
if (inSplice) { // only some chars are skipped, not the whole line
// although the line is put into splice curLine is not increased, because putCurLineInSplice();
// only some chars are skipped, not the whole line
putCurLineInSplice();
}
curCol += N;
} }
curCol += N;
} }
}; };
@ -821,41 +813,39 @@ const textLinesMutator = (lines) => {
* @returns {string} * @returns {string}
*/ */
const removeLines = (L) => { const removeLines = (L) => {
let removed = ''; if (!L) return '';
if (L) { if (!inSplice) enterSplice();
if (!inSplice) {
enterSplice();
}
/** /**
* Gets a string of joined lines after the end of the splice. * Gets a string of joined lines after the end of the splice.
* *
* @param {number} k - number of lines * @param {number} k - number of lines
* @returns {string} joined lines * @returns {string} joined lines
*/ */
const nextKLinesText = (k) => { const nextKLinesText = (k) => {
const m = curSplice[0] + curSplice[1]; const m = curSplice[0] + curSplice[1];
return linesSlice(m, m + k).join(''); return linesSlice(m, m + k).join('');
}; };
if (isCurLineInSplice()) {
if (curCol === 0) { let removed = '';
removed = curSplice[curSplice.length - 1]; if (isCurLineInSplice()) {
curSplice.length--; if (curCol === 0) {
removed += nextKLinesText(L - 1); removed = curSplice[curSplice.length - 1];
curSplice[1] += L - 1; curSplice.length--;
} else { removed += nextKLinesText(L - 1);
removed = nextKLinesText(L - 1); curSplice[1] += L - 1;
curSplice[1] += L - 1;
const sline = curSplice.length - 1;
removed = curSplice[sline].substring(curCol) + removed;
curSplice[sline] = curSplice[sline].substring(0, curCol) +
linesGet(curSplice[0] + curSplice[1]);
curSplice[1] += 1;
}
} else { } else {
removed = nextKLinesText(L); removed = nextKLinesText(L - 1);
curSplice[1] += L; curSplice[1] += L - 1;
const sline = curSplice.length - 1;
removed = curSplice[sline].substring(curCol) + removed;
curSplice[sline] = curSplice[sline].substring(0, curCol) +
linesGet(curSplice[0] + curSplice[1]);
curSplice[1] += 1;
} }
} else {
removed = nextKLinesText(L);
curSplice[1] += L;
} }
return removed; return removed;
}; };
@ -868,22 +858,15 @@ const textLinesMutator = (lines) => {
* @returns {string} * @returns {string}
*/ */
const remove = (N, L) => { const remove = (N, L) => {
let removed = ''; if (!N) return '';
if (N) { if (L) return removeLines(L);
if (L) { if (!inSplice) enterSplice();
return removeLines(L); // although the line is put into splice, curLine is not increased, because
} else { // only some chars are removed not the whole line
if (!inSplice) { const sline = putCurLineInSplice();
enterSplice(); const removed = curSplice[sline].substring(curCol, curCol + N);
} curSplice[sline] = curSplice[sline].substring(0, curCol) +
// although the line is put into splice, curLine is not increased, because curSplice[sline].substring(curCol + N);
// only some chars are removed not the whole line
const sline = putCurLineInSplice();
removed = curSplice[sline].substring(curCol, curCol + N);
curSplice[sline] = curSplice[sline].substring(0, curCol) +
curSplice[sline].substring(curCol + N);
}
}
return removed; return removed;
}; };
@ -894,44 +877,41 @@ const textLinesMutator = (lines) => {
* @param {number} L - number of newlines in text * @param {number} L - number of newlines in text
*/ */
const insert = (text, L) => { const insert = (text, L) => {
if (text) { if (!text) return;
if (!inSplice) { if (!inSplice) enterSplice();
enterSplice(); if (L) {
} const newLines = exports.splitTextLines(text);
if (L) { if (isCurLineInSplice()) {
const newLines = exports.splitTextLines(text); const sline = curSplice.length - 1;
if (isCurLineInSplice()) { /** @type {string} */
const sline = curSplice.length - 1; const theLine = curSplice[sline];
/** @type {string} */ const lineCol = curCol;
const theLine = curSplice[sline]; // insert the first new line
const lineCol = curCol; curSplice[sline] = theLine.substring(0, lineCol) + newLines[0];
// insert the first new line curLine++;
curSplice[sline] = theLine.substring(0, lineCol) + newLines[0]; newLines.splice(0, 1);
curLine++; // insert the remaining new lines
newLines.splice(0, 1); Array.prototype.push.apply(curSplice, newLines);
// insert the remaining new lines curLine += newLines.length;
Array.prototype.push.apply(curSplice, newLines); // insert the remaining chars from the "old" line (e.g. the line we were in
curLine += newLines.length; // when we started to insert new lines)
// insert the remaining chars from the "old" line (e.g. the line we were in curSplice.push(theLine.substring(lineCol));
// when we started to insert new lines) curCol = 0; // TODO(doc) why is this not set to the length of last line?
curSplice.push(theLine.substring(lineCol));
curCol = 0; // TODO(doc) why is this not set to the length of last line?
} else {
Array.prototype.push.apply(curSplice, newLines);
curLine += newLines.length;
}
} else { } else {
// there are no additional lines Array.prototype.push.apply(curSplice, newLines);
// although the line is put into splice, curLine is not increased, because curLine += newLines.length;
// there may be more chars in the line (newline is not reached)
const sline = putCurLineInSplice();
if (!curSplice[sline]) {
console.error('curSplice[sline] not populated, actual curSplice contents is ', curSplice, '. Possibly related to https://github.com/ether/etherpad-lite/issues/2802');
}
curSplice[sline] = curSplice[sline].substring(0, curCol) + text +
curSplice[sline].substring(curCol);
curCol += text.length;
} }
} else {
// there are no additional lines
// although the line is put into splice, curLine is not increased, because
// there may be more chars in the line (newline is not reached)
const sline = putCurLineInSplice();
if (!curSplice[sline]) {
console.error('curSplice[sline] not populated, actual curSplice contents is ', curSplice, '. Possibly related to https://github.com/ether/etherpad-lite/issues/2802');
}
curSplice[sline] = curSplice[sline].substring(0, curCol) + text +
curSplice[sline].substring(curCol);
curCol += text.length;
} }
}; };
@ -1171,15 +1151,14 @@ exports.composeAttributes = (att1, att2, resultIsMutation, pool) => {
let found = false; let found = false;
for (let i = 0; i < atts.length; i++) { for (let i = 0; i < atts.length; i++) {
const oldPair = atts[i]; const oldPair = atts[i];
if (oldPair[0] === pair[0]) { if (oldPair[0] !== pair[0]) continue;
if (pair[1] || resultIsMutation) { if (pair[1] || resultIsMutation) {
oldPair[1] = pair[1]; oldPair[1] = pair[1];
} else { } else {
atts.splice(i, 1); atts.splice(i, 1);
}
found = true;
break;
} }
found = true;
break;
} }
if ((!found) && (pair[1] || resultIsMutation)) { if ((!found) && (pair[1] || resultIsMutation)) {
atts.push(pair); atts.push(pair);
@ -1340,12 +1319,11 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
lineAssem = exports.mergingOpAssembler(); lineAssem = exports.mergingOpAssembler();
} }
lineAssem.append(op); lineAssem.append(op);
if (op.lines > 0) { if (op.lines <= 0) return;
assert(op.lines === 1, `Can't have op.lines of ${op.lines} in attribution lines`); assert(op.lines === 1, `Can't have op.lines of ${op.lines} in attribution lines`);
// ship it to the mut // ship it to the mut
mut.insert(lineAssem.toString(), 1); mut.insert(lineAssem.toString(), 1);
lineAssem = null; lineAssem = null;
}
}; };
const csOp = exports.newOp(); const csOp = exports.newOp();
@ -1505,16 +1483,11 @@ exports.compose = (cs1, cs2, pool) => {
*/ */
exports.attributeTester = (attribPair, pool) => { exports.attributeTester = (attribPair, pool) => {
const never = (attribs) => false; const never = (attribs) => false;
if (!pool) { if (!pool) return never;
return never;
}
const attribNum = pool.putAttrib(attribPair, true); const attribNum = pool.putAttrib(attribPair, true);
if (attribNum < 0) { if (attribNum < 0) return never;
return never; const re = new RegExp(`\\*${exports.numToString(attribNum)}(?!\\w)`);
} else { return (attribs) => re.test(attribs);
const re = new RegExp(`\\*${exports.numToString(attribNum)}(?!\\w)`);
return (attribs) => re.test(attribs);
}
}; };
/** /**
@ -1796,14 +1769,11 @@ exports.applyToAText = (cs, atext, pool) => ({
* @returns {AText} * @returns {AText}
*/ */
exports.cloneAText = (atext) => { exports.cloneAText = (atext) => {
if (atext) { if (!atext) error('atext is null');
return { return {
text: atext.text, text: atext.text,
attribs: atext.attribs, attribs: atext.attribs,
}; };
} else {
error('atext is null');
}
}; };
/** /**
@ -1902,14 +1872,13 @@ exports.opAttributeValue = (op, key, pool) => exports.attribsAttributeValue(op.a
* @returns {string} * @returns {string}
*/ */
exports.attribsAttributeValue = (attribs, key, pool) => { exports.attribsAttributeValue = (attribs, key, pool) => {
if (!attribs) return '';
let value = ''; let value = '';
if (attribs) { exports.eachAttribNumber(attribs, (n) => {
exports.eachAttribNumber(attribs, (n) => { if (pool.getAttribKey(n) === key) {
if (pool.getAttribKey(n) === key) { value = pool.getAttribValue(n);
value = pool.getAttribValue(n); }
} });
});
}
return value; return value;
}; };
@ -2038,20 +2007,19 @@ exports.subattribution = (astr, start, optEnd) => {
const opOut = exports.newOp(); const opOut = exports.newOp();
const doCsOp = () => { const doCsOp = () => {
if (csOp.chars) { if (!csOp.chars) return;
while (csOp.opcode && (attOp.opcode || iter.hasNext())) { while (csOp.opcode && (attOp.opcode || iter.hasNext())) {
if (!attOp.opcode) iter.next(attOp); if (!attOp.opcode) iter.next(attOp);
if (csOp.opcode && attOp.opcode && csOp.chars >= attOp.chars && if (csOp.opcode && attOp.opcode && csOp.chars >= attOp.chars &&
attOp.lines > 0 && csOp.lines <= 0) { attOp.lines > 0 && csOp.lines <= 0) {
csOp.lines++; csOp.lines++;
} }
slicerZipperFunc(attOp, csOp, opOut, null); slicerZipperFunc(attOp, csOp, opOut, null);
if (opOut.opcode) { if (opOut.opcode) {
assem.append(opOut); assem.append(opOut);
opOut.opcode = ''; opOut.opcode = '';
}
} }
} }
}; };
@ -2399,13 +2367,12 @@ const followAttributes = (att1, att2, pool) => {
const pair1 = pool.getAttrib(exports.parseNum(a)); const pair1 = pool.getAttrib(exports.parseNum(a));
for (let i = 0; i < atts.length; i++) { for (let i = 0; i < atts.length; i++) {
const pair2 = atts[i]; const pair2 = atts[i];
if (pair1[0] === pair2[0]) { if (pair1[0] !== pair2[0]) continue;
if (pair1[1] <= pair2[1]) { if (pair1[1] <= pair2[1]) {
// winner of merge is pair1, delete this attribute // winner of merge is pair1, delete this attribute
atts.splice(i, 1); atts.splice(i, 1);
}
break;
} }
break;
} }
return ''; return '';
}); });