-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0722-remove-comments.js
More file actions
53 lines (50 loc) · 1.61 KB
/
0722-remove-comments.js
File metadata and controls
53 lines (50 loc) · 1.61 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Remove Comments
* Time Complexity: O(TotalChars)
* Space Complexity: O(TotalChars)
*/
var removeComments = function (source) {
const processedCodeLines = [];
let segmentBuilder = [];
let blockCommentInProgress = false;
for (const processingLine of source) {
let currentLineScanIndex = 0;
while (currentLineScanIndex < processingLine.length) {
if (blockCommentInProgress) {
if (
currentLineScanIndex + 1 < processingLine.length &&
processingLine[currentLineScanIndex] === "*" &&
processingLine[currentLineScanIndex + 1] === "/"
) {
blockCommentInProgress = false;
currentLineScanIndex += 2;
} else {
currentLineScanIndex++;
}
} else {
if (
currentLineScanIndex + 1 < processingLine.length &&
processingLine[currentLineScanIndex] === "/" &&
processingLine[currentLineScanIndex + 1] === "/"
) {
currentLineScanIndex = processingLine.length;
} else if (
currentLineScanIndex + 1 < processingLine.length &&
processingLine[currentLineScanIndex] === "/" &&
processingLine[currentLineScanIndex + 1] === "*"
) {
blockCommentInProgress = true;
currentLineScanIndex += 2;
} else {
segmentBuilder.push(processingLine[currentLineScanIndex]);
currentLineScanIndex++;
}
}
}
if (!blockCommentInProgress && segmentBuilder.length > 0) {
processedCodeLines.push(segmentBuilder.join(""));
segmentBuilder = [];
}
}
return processedCodeLines;
};