cmark

My personal build of CMark ✏️

Commit
d33cdfb18c4c3436d941cf96e9b7dd6775b0e3e9
Parent
3bb150e5e689a6e58d485f2b87c15e38ccf50ae7
Author
John MacFarlane <jgm@berkeley.edu>
Date

More efficient detab.

Diffstat

1 file changed, 20 insertions, 13 deletions

Status File Name N° Changes Insertions Deletions
Modified js/lib/blocks.js 33 20 13
diff --git a/js/lib/blocks.js b/js/lib/blocks.js
@@ -42,23 +42,25 @@ var isBlank = function(s) {
     return !(reNonSpace.test(s));
 };
 
+var tabSpaces = ['    ', '   ', '  ', ' '];
+
 // Convert tabs to spaces on each line using a 4-space tab stop.
 var detabLine = function(text) {
     "use strict";
-    if (text.indexOf('\u0000') !== -1) {
-        // replace NUL for security
-        text = text.replace(/\0/g, '\uFFFD');
-    }
-    if (text.indexOf('\t') === -1) {
-        return text;
-    } else {
-        var lastStop = 0;
-        return text.replace(/\t/g, function(match, offset) {
-            var result = '    '.slice((offset - lastStop) % 4);
-            lastStop = offset + 1;
-            return result;
-        });
+
+    var start = 0;
+    var offset;
+    var lastStop = 0;
+
+    while ((offset = text.indexOf('\t', start)) !== -1) {
+        var numspaces = (offset - lastStop) % 4;
+        var spaces = tabSpaces[numspaces];
+        text = text.slice(0, offset) + spaces + text.slice(offset + 1);
+        lastStop = offset + numspaces;
+        start = lastStop;
     }
+
+    return text;
 };
 
 // Attempt to match a regex in string s at offset offset.
@@ -245,6 +247,11 @@ var incorporateLine = function(ln, line_number) {
     var container = this.doc;
     var oldtip = this.tip;
 
+    // replace NUL characters for security
+    if (ln.indexOf('\u0000') !== -1) {
+        ln = ln.replace(/\0/g, '\uFFFD');
+    }
+
     // Convert tabs to spaces:
     ln = detabLine(ln);