Permalink
Browse files

Fix #9 Allow escaped ; chars in values

  • Loading branch information...
1 parent 08b8f0a commit 34389870c29a3e9247f0fb95e4aa62f7d71d322f @isaacs isaacs committed Oct 4, 2012
Showing with 34 additions and 7 deletions.
  1. +28 −6 ini.js
  2. +3 −0 test/fixtures/foo.ini
  3. +3 −1 test/foo.js
View
34 ini.js
@@ -56,10 +56,7 @@ function decode (str) {
, section = null
lines.forEach(function (line, _, __) {
- //line = line
- var rem = line.indexOf(";")
- if (rem !== -1) line = line.substr(0, rem)//.trim()
- if (!line) return
+ if (!line || line.match(/^\s*;/)) return
var match = line.match(re)
if (!match) return
if (match[1] !== undefined) {
@@ -108,13 +105,38 @@ function safe (val) {
|| (val.length > 1
&& val.charAt(0) === "\""
&& val.slice(-1) === "\"")
- || val !== val.trim() ) ? JSON.stringify(val) : val
+ || val !== val.trim() )
+ ? JSON.stringify(val)
+ : val.replace(/;/g, '\\;')
}
-function unsafe (val) {
+function unsafe (val, doUnesc) {
val = (val || "").trim()
if (val.charAt(0) === "\"" && val.slice(-1) === "\"") {
try { val = JSON.parse(val) } catch (_) {}
+ } else {
+ // walk the val to find the first not-escaped ; character
+ var esc = false
+ var unesc = "";
+ for (var i = 0, l = val.length; i < l; i++) {
+ var c = val.charAt(i)
+ if (esc) {
+ if (c === "\\" || c === ";")
+ unesc += c
+ else
+ unesc += "\\" + c
+ esc = false
+ } else if (c === ";") {
+ break
+ } else if (c === "\\") {
+ esc = true
+ } else {
+ unesc += c
+ }
+ }
+ if (esc)
+ unesc += "\\"
+ return unesc
}
return val
}
View
@@ -27,3 +27,6 @@ x.y.z = xyz
[x\.y\.z.a\.b\.c]
a.b.c = abc
+
+; this next one is not a comment! it's escaped!
+nocomment = this\; this is not a comment
View
@@ -21,6 +21,7 @@ var i = require("../")
+ 'j = 2\n\n[x\\.y\\.z]\nx.y.z = xyz\n\n'
+ '[x\\.y\\.z.a\\.b\\.c]\n'
+ 'a.b.c = abc\n'
+ + 'nocomment = this\\; this is not a comment\n'
, expectD =
{ o: 'p',
'a with spaces': 'b c',
@@ -35,7 +36,8 @@ var i = require("../")
'x.y.z': {
'x.y.z': 'xyz',
'a.b.c': {
- 'a.b.c': 'abc'
+ 'a.b.c': 'abc',
+ 'nocomment': 'this\; this is not a comment'
}
}
}

0 comments on commit 3438987

Please sign in to comment.