| 394 | * @returns An array of the split tokens. |
| 395 | */ |
| 396 | const split = (line = '') => { |
| 397 | const words = [] |
| 398 | let field = '' |
| 399 | scan(line, /\s*(?:([^\s\\'"]+)|'((?:[^'\\]|\\.)*)'|"((?:[^"\\]|\\.)*)"|(\\.?)|(\S))(\s|$)?/, (match) => { |
| 400 | const [, word, sq, dq, escape, garbage, separator] = match |
| 401 | |
| 402 | if (garbage != null) { |
| 403 | throw new Error(`Unmatched quote: ${line}`) |
| 404 | } |
| 405 | |
| 406 | if (word) { |
| 407 | field += word |
| 408 | } else { |
| 409 | let addition |
| 410 | |
| 411 | if (sq) { |
| 412 | addition = sq |
| 413 | } else if (dq) { |
| 414 | addition = dq |
| 415 | } else if (escape) { |
| 416 | addition = escape |
| 417 | } |
| 418 | |
| 419 | if (addition) { |
| 420 | field += addition.replace(/\\(?=.)/, '') |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | if (separator != null) { |
| 425 | words.push(field) |
| 426 | field = '' |
| 427 | } |
| 428 | }) |
| 429 | |
| 430 | if (field) { |
| 431 | words.push(field) |
| 432 | } |
| 433 | |
| 434 | return words |
| 435 | } |