summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2015-01-10 22:14:40 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2015-01-10 22:14:40 -0800
commitd33cdfb18c4c3436d941cf96e9b7dd6775b0e3e9 (patch)
treeff2374816b3f3227e2bf3990828ec9479e5926d5
parent3bb150e5e689a6e58d485f2b87c15e38ccf50ae7 (diff)
More efficient detab.
-rw-r--r--js/lib/blocks.js33
1 files changed, 20 insertions, 13 deletions
diff --git a/js/lib/blocks.js b/js/lib/blocks.js
index b1f69eb..bd00b1a 100644
--- 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);