Skip to content

Commit 64eb16f

Browse files
authored
fix(util): update ini parsing (#4125)
* fix(util): update ini parsing * fix(util): use indexOf instead of split * fix(util): require non-empty for ini parse * fix(util): update section parse to match v3
1 parent ef34124 commit 64eb16f

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

lib/util.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,28 @@ var util = {
215215
parse: function string(ini) {
216216
var currentSection, map = {};
217217
util.arrayEach(ini.split(/\r?\n/), function(line) {
218-
line = line.split(/(^|\s)[;#]/)[0]; // remove comments
219-
var section = line.match(/^\s*\[([^\[\]]+)\]\s*$/);
220-
if (section) {
221-
currentSection = section[1];
218+
line = line.split(/(^|\s)[;#]/)[0].trim(); // remove comments and trim
219+
var isSection = line[0] === '[' && line[line.length - 1] === ']';
220+
if (isSection) {
221+
currentSection = line.substring(1, line.length - 1);
222222
if (currentSection === '__proto__' || currentSection.split(/\s/)[1] === '__proto__') {
223223
throw util.error(
224224
new Error('Cannot load profile name \'' + currentSection + '\' from shared ini file.')
225225
);
226226
}
227227
} else if (currentSection) {
228-
var item = line.match(/^\s*(.+?)\s*=\s*(.+?)\s*$/);
229-
if (item) {
228+
var indexOfEqualsSign = line.indexOf('=');
229+
var start = 0;
230+
var end = line.length - 1;
231+
var isAssignment =
232+
indexOfEqualsSign !== -1 && indexOfEqualsSign !== start && indexOfEqualsSign !== end;
233+
234+
if (isAssignment) {
235+
var name = line.substring(0, indexOfEqualsSign).trim();
236+
var value = line.substring(indexOfEqualsSign + 1).trim();
237+
230238
map[currentSection] = map[currentSection] || {};
231-
map[currentSection][item[1]] = item[2];
239+
map[currentSection][name] = value;
232240
}
233241
}
234242
});

0 commit comments

Comments
 (0)