Skip to content

Commit

Permalink
while loop optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Casheeew committed Dec 18, 2023
1 parent d706221 commit 08aae70
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions ext/js/dom/document-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,20 @@ export class DocumentUtil {
// Move backward
let quoteStack = [];
for (; cursorStart > 0; --cursorStart) {
// Check if the previous character should be included.
let c = text[cursorStart - 1];
if (c === '\n' && terminateAtNewlines) { break; }

if (quoteStack.length === 0) {
let terminatorInfo = terminatorMap.get(c);
if (typeof terminatorInfo !== 'undefined') {
// Include the previous character while it is a terminator character and is included at start.
while (typeof terminatorInfo !== 'undefined' && terminatorInfo[0] && cursorStart > 0) {
while (terminatorInfo[0] && cursorStart > 0) {
--cursorStart;
if (cursorStart === 0) { break; }
c = text[cursorStart - 1];
terminatorInfo = terminatorMap.get(c);
if (typeof terminatorInfo === 'undefined') { break; }
}
break;
}
Expand All @@ -143,11 +145,12 @@ export class DocumentUtil {
if (typeof quoteInfo !== 'undefined') {
if (quoteStack.length === 0) {
// Include the previous character while it is a quote character and is included at start.
while (typeof quoteInfo !== 'undefined' && quoteInfo[1] && cursorStart > 0) {
while (quoteInfo[1] && cursorStart > 0) {
--cursorStart;
if (cursorStart === 0) { break; }
c = text[cursorStart - 1];
quoteInfo = forwardQuoteMap.get(c);
if (typeof quoteInfo === 'undefined') { break; }
}
break;
} else if (quoteStack[0] === c) {
Expand All @@ -173,11 +176,12 @@ export class DocumentUtil {
let terminatorInfo = terminatorMap.get(c);
if (typeof terminatorInfo !== 'undefined') {
// Include the following character while it is a terminator character and is included at end.
while (typeof terminatorInfo !== 'undefined' && terminatorInfo[1] && cursorEnd < textLength) {
while (terminatorInfo[1] && cursorEnd < textLength) {
++cursorEnd;
if (cursorEnd === textLength) { break; }
c = text[cursorEnd];
terminatorInfo = terminatorMap.get(c);
if (typeof terminatorInfo === 'undefined') { break; }
}
break;
}
Expand All @@ -187,11 +191,12 @@ export class DocumentUtil {
if (typeof quoteInfo !== 'undefined') {
if (quoteStack.length === 0) {
// Include the following character while it is a quote character and is included at end.
while (typeof quoteInfo !== 'undefined' && quoteInfo[1] && cursorEnd < textLength) {
while (quoteInfo[1] && cursorEnd < textLength) {
++cursorEnd;
if (cursorEnd === textLength) { break; }
c = text[cursorEnd];
quoteInfo = forwardQuoteMap.get(c);
if (typeof quoteInfo === 'undefined') { break; }
}
break;
} else if (quoteStack[0] === c) {
Expand Down

0 comments on commit 08aae70

Please sign in to comment.