blob: fff80917b680b356deb65341054c53400b03c7e9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
'use strict';
// Preserve UTF-16 offsets and newlines, masking CIE quotes and // comments.
// Backslash escapes protect the next character; literals end at each newline.
function codeOnly(text) {
let quote = null;
let comment = false;
const result = [];
for (let i = 0; i < text.length; i++) {
const char = text[i];
if (char === '\n' || char === '\r') {
quote = null;
comment = false;
result.push(char);
} else if (comment) {
result.push(' ');
} else if (quote) {
result.push(' ');
if (char === '\\' && i + 1 < text.length && !/[\r\n]/.test(text[i + 1])) {
result.push(' ');
i++;
} else if (char === quote) quote = null;
} else if (char === '"' || char === "'") {
quote = char;
result.push(' ');
} else if (char === '/' && text[i + 1] === '/') {
comment = true;
result.push(' ');
} else {
result.push(char);
}
}
return result.join('');
}
module.exports = { codeOnly };
|