Skip to content

Commit b32120d

Browse files
authored
Update crunch.js
1 parent d28ae22 commit b32120d

1 file changed

Lines changed: 53 additions & 19 deletions

File tree

crunch.js

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,13 @@ function parseInput(input) {
9595
let pos = input.indexOf("\"");
9696

9797
while (input.length > 0) {
98-
99-
let pos2 = input.indexOf("#");
100-
98+
let pos2 = input.indexOf("#");
99+
if(pos != -1) {
100+
let dbl_chk = input.indexOf("\"", pos+1);
101+
if(dbl_chk == -1) {
102+
return arr;
103+
}
104+
}
101105
if (pos === -1) {
102106
let lines = input.split('\n');
103107
for (let line of lines) {
@@ -110,16 +114,34 @@ function parseInput(input) {
110114
}
111115
}
112116
break;
113-
} else if(pos2 < pos) {
114-
if(input.trim().startsWith("#")) {
115-
let pos = input.indexOf("\n");
116-
if(pos != -1) {
117-
let left = input.substring(0, pos);
117+
} else if(pos2 != -1 && pos2 < pos) {
118+
if(input.trim().startsWith("#")) {
119+
let posx = input.indexOf("\n");
120+
if(posx != -1) {
121+
let left = input.substring(0, posx);
118122
arr.push(left);
119-
let right = input.substring(pos + 1);
123+
let right = input.substring(posx + 1);
124+
input = right;
125+
pos = input.indexOf("\"");
126+
continue;
127+
} else {
128+
arr.push(input);
129+
return arr;
130+
}
131+
}
132+
else {
133+
134+
let nl = input.indexOf("\n");
135+
if(nl < pos2) {
136+
let left = input.substring(0, nl);
137+
let right = input.substring(nl+1);
138+
let words = left.split('\n').filter(word => word.trim() !== '');
139+
arr.push(...words);
120140
input = right;
121-
pos = input.indexOf("\n");
141+
pos = input.indexOf("\"");
122142
continue;
143+
} else {
144+
123145
}
124146
}
125147
}
@@ -133,7 +155,7 @@ function parseInput(input) {
133155
let lines = left.split('\n');
134156
for (let line of lines) {
135157
if (line.trim() === '') continue;
136-
if (line.startsWith('#')) {
158+
if (line.trim().startsWith('#')) {
137159
arr.push(line);
138160
} else {
139161
let words = line.split('\n').filter(word => word.trim() !== '');
@@ -153,39 +175,51 @@ function parseInput(input) {
153175
return arr;
154176
}
155177

156-
function procLine(line, last = false) {
178+
let regular = false;
179+
180+
function procLine(line) {
157181
let output = '';
158182
if (line.trim() === '') {
183+
regular = false;
159184
return '';
160185
}
161186
if (line.trim().startsWith("\"")) {
162187
output += line;
188+
regular = false;
163189
} else if (line.trim().startsWith('#')) {
164-
if (last === true)
190+
if(regular == true) {
165191
output += "\n";
166-
192+
}
193+
regular = false;
167194
output += line + "\n";
168195
} else {
169196
let linez = crunchLine(line) + ' ';
170197
linez = linez.replace(/\s\s+/g, ' ');
171198
linez = removeSpaces(linez);
172199
output += linez;
200+
if(linez.length > 0)
201+
regular = true;
173202
}
174203
return output;
175204
}
176205

177206
function crunch(input) {
178207
let data = input;
208+
209+
if(data.indexOf("\n") == -1) {
210+
return procLine(data);
211+
}
212+
179213
data = removeMlComment(data);
180214
const lines = parseInput(data);
181-
console.log
215+
216+
console.log(lines);
182217
let output = '';
183-
for (let i = 0; i < lines.length - 1; ++i) {
218+
for (let i = 0; i < lines.length; ++i) {
184219
let line = lines[i];
220+
if(line.trim() == '') continue;
185221
output += procLine(line);
186222
}
187-
if (lines.length - 1 > 0)
188-
output += procLine(lines[lines.length - 1], true);
189223
return output;
190224
}
191225

@@ -263,4 +297,4 @@ function crunchText() {
263297
const inputText = document.getElementById('inputText').value;
264298
const outputText = crunch(inputText);
265299
document.getElementById('output').innerText = outputText;
266-
}
300+
}

0 commit comments

Comments
 (0)