Skip to content
Merged
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
6 changes: 3 additions & 3 deletions Src/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static Configuration()
// but without modifying the real invariant culture instance.
s_cultureInfo = (CultureInfo)CultureInfo.InvariantCulture.Clone();

ValidCommentChars = new[] { '#', ';' };
ValidCommentChars = new HashSet<char> { '#', ';' };
s_preferredCommentChar = '#';
s_arrayElementSeparator = ',';

Expand Down Expand Up @@ -585,7 +585,7 @@ public static CultureInfo CultureInfo
/// Gets the array that contains all valid comment delimiting characters.
/// The current value is { '#', ';' }.
/// </summary>
public static char[] ValidCommentChars { get; private set; }
public static HashSet<char> ValidCommentChars { get; private set; }

/// <summary>
/// Gets or sets the preferred comment char when saving configurations.
Expand All @@ -597,7 +597,7 @@ public static char PreferredCommentChar
{
get => s_preferredCommentChar;
set {
if (!Array.Exists(ValidCommentChars, c => c == value))
if (!ValidCommentChars.Contains(value))
{
throw new ArgumentException("The specified char '" + value + "' is not allowed as a comment char.");
}
Expand Down
10 changes: 6 additions & 4 deletions Src/ConfigurationReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ private static string ParseComment(string line, out int commentCharIndex)
var index = 0;
var quoteCount = 0;

while (line.Length > index) // traverse line from left to right
var length = line.Length;
while (index < length) // traverse line from left to right
{
var isValidCommentChar = Array.IndexOf(Configuration.ValidCommentChars, line[index]) > -1;
var isQuotationMark = line[index] == '\"';
var isCharWithinQuotes = quoteCount % 2 == 1;
var currentChar = line[index];
var isValidCommentChar = Configuration.ValidCommentChars.Contains(currentChar);
var isQuotationMark = currentChar == '\"';
var isCharWithinQuotes = (quoteCount & 1) == 1; // bitwise AND is slightly faster
var isCharEscaped = index > 0 && line[index - 1] == '\\';

if (isValidCommentChar && !isCharWithinQuotes && !isCharEscaped)
Expand Down
5 changes: 4 additions & 1 deletion Src/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// https://sharpconfig.org

using System;
using System.Linq;
using System.Text;

namespace SharpConfig
Expand Down Expand Up @@ -612,8 +613,10 @@ private static string GetValueForOutput(string rawValue)
return rawValue;
}

bool isAnyCommentCharInRawValue() => rawValue.Any(c => Configuration.ValidCommentChars.Contains(c));

if (rawValue.IndexOf(" ", StringComparison.Ordinal) >= 0 ||
(rawValue.IndexOfAny(Configuration.ValidCommentChars) >= 0 && !Configuration.IgnoreInlineComments))
(isAnyCommentCharInRawValue() && !Configuration.IgnoreInlineComments))
{
rawValue = "\"" + rawValue + "\"";
}
Expand Down
Loading