Skip to content

Commit

Permalink
Fixed inline comments bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
irverbitskiy committed Dec 25, 2012
1 parent b7c7581 commit b8c804a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 35 deletions.
33 changes: 21 additions & 12 deletions lib/simple-ini.js
Expand Up @@ -112,7 +112,7 @@ var SimpleIni = (function () {
comment,
property,
value,
str, i, j;
str, i, j, position;

finalOptions = processOptions(options);

Expand All @@ -122,6 +122,24 @@ var SimpleIni = (function () {
line += this.quotedValues ? line += '"' + value + '"' : value;
return line;
}

finalOptions.cleanComments = function(str) {
var minPosition,
position,
i;

minPosition = -1;
for (i = 0; i < this.comments.length; ++i) {
position = str.indexOf(this.comments[i]);
if (position > -1 &&
(minPosition === -1 ||
(minPosition !== -1 && position < minPosition))) {
minPosition = position;
}
}

return minPosition > -1 ? str.substr(0, minPosition) : str;
}


// *******************************************************************
Expand Down Expand Up @@ -256,7 +274,7 @@ var SimpleIni = (function () {

next_line:
for (i = 0; i < content.length; ++i) {
baseLine = content[i];
baseLine = finalOptions.cleanComments(content[i]);
line = baseLine.trim();

// Check for blanks
Expand All @@ -265,16 +283,6 @@ var SimpleIni = (function () {
continue;
}

// Check for comments

for (j = 0; j < finalOptions.comments.length; ++j) {
comment = finalOptions.comments[j];
str = line.substr(0, comment.length);
if (str === comment) {
continue next_line;
}
}

// Check for sections

isSection = line.length > 2 &&
Expand Down Expand Up @@ -308,6 +316,7 @@ var SimpleIni = (function () {
while (i < content.length && value.charAt(value.length - 1) === '\\') {
value = value.substr(0, value.length - 1);
baseLine = content[++i].trimLeft();
baseLine = finalOptions.cleanComments(baseLine);
value += baseLine;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "simple-ini",
"version": "1.0.2",
"version": "1.0.3",
"description": "Simple INI-parser",
"main": "./lib/simple-ini.js",
"homepage": "https://github.com/ilich/simple-ini",
Expand Down
33 changes: 21 additions & 12 deletions tests/browser/simple-ini.js
Expand Up @@ -112,7 +112,7 @@ var SimpleIni = (function () {
comment,
property,
value,
str, i, j;
str, i, j, position;

finalOptions = processOptions(options);

Expand All @@ -122,6 +122,24 @@ var SimpleIni = (function () {
line += this.quotedValues ? line += '"' + value + '"' : value;
return line;
}

finalOptions.cleanComments = function(str) {
var minPosition,
position,
i;

minPosition = -1;
for (i = 0; i < this.comments.length; ++i) {
position = str.indexOf(this.comments[i]);
if (position > -1 &&
(minPosition === -1 ||
(minPosition !== -1 && position < minPosition))) {
minPosition = position;
}
}

return minPosition > -1 ? str.substr(0, minPosition) : str;
}


// *******************************************************************
Expand Down Expand Up @@ -256,7 +274,7 @@ var SimpleIni = (function () {

next_line:
for (i = 0; i < content.length; ++i) {
baseLine = content[i];
baseLine = finalOptions.cleanComments(content[i]);
line = baseLine.trim();

// Check for blanks
Expand All @@ -265,16 +283,6 @@ var SimpleIni = (function () {
continue;
}

// Check for comments

for (j = 0; j < finalOptions.comments.length; ++j) {
comment = finalOptions.comments[j];
str = line.substr(0, comment.length);
if (str === comment) {
continue next_line;
}
}

// Check for sections

isSection = line.length > 2 &&
Expand Down Expand Up @@ -308,6 +316,7 @@ var SimpleIni = (function () {
while (i < content.length && value.charAt(value.length - 1) === '\\') {
value = value.substr(0, value.length - 1);
baseLine = content[++i].trimLeft();
baseLine = finalOptions.cleanComments(baseLine);
value += baseLine;
}

Expand Down
10 changes: 5 additions & 5 deletions tests/browser/simple-ini.test.js
Expand Up @@ -76,7 +76,7 @@ var SimpleIniTests = (function() {
'[owner]',
'; date=20101010',
'name=John Doe',
'organization=Acme Widgets Inc.',
'organization=Acme Widgets Inc.; Company Name',
'',
' [database] ',
' ; use IP address in case network name resolution is not working',
Expand All @@ -95,7 +95,7 @@ var SimpleIniTests = (function() {
'[owner]\r',
'; date=20101010\r',
'name=John Doe\r',
'organization=Acme Widgets Inc.\r',
'organization=Acme Widgets Inc.; Company Name\r',
'\r',
' [database] \r',
' ; use IP address in case network name resolution is not working\r',
Expand All @@ -114,9 +114,9 @@ var SimpleIniTests = (function() {
'[owner]',
'name=John Doe',
'organization=Acme Widgets Inc.',
'definition=This is line 1, \\',
'definition=This is line 1, \\; Just a comment - the first one',
' line 2, \\',
' line 3, \\',
' line 3, \\; Just a comment',
' line 4.',
'',
'[database]',
Expand Down Expand Up @@ -455,7 +455,7 @@ var SimpleIniTests = (function() {
'; last modified 1 April 2001 by John Doe',
'[owner]',
'name=John Doe',
'organization=Acme Widgets Inc.',
'organization=Acme Widgets Inc.# Company name; just another comment',
'',
'[database]',
'# use IP address in case network name resolution is not working',
Expand Down
10 changes: 5 additions & 5 deletions tests/simple-ini.test.js
Expand Up @@ -78,7 +78,7 @@ var SimpleIniTests = (function() {
'[owner]',
'; date=20101010',
'name=John Doe',
'organization=Acme Widgets Inc.',
'organization=Acme Widgets Inc.; Company Name',
'',
' [database] ',
' ; use IP address in case network name resolution is not working',
Expand All @@ -97,7 +97,7 @@ var SimpleIniTests = (function() {
'[owner]\r',
'; date=20101010\r',
'name=John Doe\r',
'organization=Acme Widgets Inc.\r',
'organization=Acme Widgets Inc.; Company Name\r',
'\r',
' [database] \r',
' ; use IP address in case network name resolution is not working\r',
Expand All @@ -116,9 +116,9 @@ var SimpleIniTests = (function() {
'[owner]',
'name=John Doe',
'organization=Acme Widgets Inc.',
'definition=This is line 1, \\',
'definition=This is line 1, \\; Just a comment - the first one',
' line 2, \\',
' line 3, \\',
' line 3, \\; Just a comment',
' line 4.',
'',
'[database]',
Expand Down Expand Up @@ -457,7 +457,7 @@ var SimpleIniTests = (function() {
'; last modified 1 April 2001 by John Doe',
'[owner]',
'name=John Doe',
'organization=Acme Widgets Inc.',
'organization=Acme Widgets Inc.# Company name; just another comment',
'',
'[database]',
'# use IP address in case network name resolution is not working',
Expand Down

0 comments on commit b8c804a

Please sign in to comment.