Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions lib/ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,41 @@ const encode = (obj, opt = {}) => {
}

function splitSections (str, separator) {
var lastMatchIndex = 0
var lastSeparatorIndex = 0
var nextIndex = 0
var sections = []
var quote = null
var esc = false

do {
nextIndex = str.indexOf(separator, lastMatchIndex)
for (let i = 0; i < str.length; i++) {
const char = str[i]
if (esc) {
esc = false
continue
}

if (nextIndex !== -1) {
lastMatchIndex = nextIndex + separator.length
if (char === '\\') {
esc = true
continue
}

if (nextIndex > 0 && str[nextIndex - 1] === '\\') {
continue
if (quote) {
if (char === quote) {
quote = null
}
continue
}

if (char === '"' || char === "'") {
quote = char
continue
}

sections.push(str.slice(lastSeparatorIndex, nextIndex))
lastSeparatorIndex = nextIndex + separator.length
if (str.slice(i, i + separator.length) === separator) {
sections.push(str.slice(lastSeparatorIndex, i))
i += separator.length - 1
lastSeparatorIndex = i + 1
}
} while (nextIndex !== -1)
}

sections.push(str.slice(lastSeparatorIndex))

Expand Down
18 changes: 18 additions & 0 deletions test/quoted-sections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const ini = require('../')
const t = require('tap')

t.test('does not split section dots inside quotes', t => {
const data = ini.parse(`
[submodule "public/mks/parts/mks-Grove125KHzRFIDReader-V1.0"]
path = public/mks/parts/mks-Grove125KHzRFIDReader-V1.0
url = https://github.com/Make-Your-School/mks-Grove125KHzRFIDReader-V1.0.git
`)

t.same(data, {
'submodule "public/mks/parts/mks-Grove125KHzRFIDReader-V1.0"': {
path: 'public/mks/parts/mks-Grove125KHzRFIDReader-V1.0',
url: 'https://github.com/Make-Your-School/mks-Grove125KHzRFIDReader-V1.0.git',
},
})
t.end()
})