Skip to content

Commit 9536104

Browse files
authored
fix(shared-ini-file-loader): update ini parsing (#3682)
* fix(shared-ini-file-loader): update ini parsing * fix(shared-ini-file-loader): use indexOf instead of split * fix(shared-ini-file-loader): require non-empty for ini parse
1 parent 757f883 commit 9536104

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

packages/shared-ini-file-loader/src/parseIni.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@ export const parseIni = (iniData: string): ParsedIniData => {
77
let currentSection: string | undefined;
88

99
for (let line of iniData.split(/\r?\n/)) {
10-
line = line.split(/(^|\s)[;#]/)[0]; // remove comments
11-
const section = line.match(/^\s*\[([^\[\]]+)]\s*$/);
12-
if (section) {
13-
currentSection = section[1];
10+
line = line.split(/(^|\s)[;#]/)[0].trim(); // remove comments and trim
11+
const isSection: boolean = line[0] === "[" && line[line.length - 1] === "]";
12+
if (isSection) {
13+
currentSection = line.substring(1, line.length - 1);
1414
if (profileNameBlockList.includes(currentSection)) {
1515
throw new Error(`Found invalid profile name "${currentSection}"`);
1616
}
1717
} else if (currentSection) {
18-
const item = line.match(/^\s*(.+?)\s*=\s*(.+?)\s*$/);
19-
if (item) {
18+
const indexOfEqualsSign = line.indexOf("=");
19+
const start = 0;
20+
const end: number = line.length - 1;
21+
const isAssignment: boolean =
22+
indexOfEqualsSign !== -1 && indexOfEqualsSign !== start && indexOfEqualsSign !== end;
23+
if (isAssignment) {
24+
const [name, value]: [string, string] = [
25+
line.substring(0, indexOfEqualsSign).trim(),
26+
line.substring(indexOfEqualsSign + 1).trim(),
27+
];
2028
map[currentSection] = map[currentSection] || {};
21-
map[currentSection][item[1]] = item[2];
29+
map[currentSection][name] = value;
2230
}
2331
}
2432
}

0 commit comments

Comments
 (0)