The items of an array, split at its own commas, not at those inside strings or inline tables.
(inner: string)
| 44 | } |
| 45 | /** The items of an array, split at its own commas, not at those inside strings or inline tables. */ |
| 46 | function arrayItems(inner: string): string[] { |
| 47 | const items: string[] = []; |
| 48 | let depth = 0, quote: string | undefined, start = 0; |
| 49 | for (let index = 0; index < inner.length; index++) { |
| 50 | const character = inner[index]; |
| 51 | if (quote) { if (character === '\\' && quote === '"') index++; else if (character === quote) quote = undefined; continue; } |
| 52 | if (character === '"' || character === "'") quote = character; |
| 53 | else if (character === '[' || character === '{') depth++; |
| 54 | else if (character === ']' || character === '}') depth--; |
| 55 | else if (character === ',' && depth === 0) { items.push(inner.slice(start, index)); start = index + 1; } |
| 56 | } |
| 57 | return [...items, inner.slice(start)].map(item => item.trim()).filter(Boolean); |
| 58 | } |
| 59 | |
| 60 | /** TOML: the keys before the first table, each `[table]` with its keys, each `[[table]]` as a list |
| 61 | * item. An array may run over several lines ("dependencies = [" with one item per line). */ |