Skip to content

Commit f0afee9

Browse files
authored
Merge pull request #3 from rboy1/patch-4
Improve performance
2 parents bb71d8a + ca5db3a commit f0afee9

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

Src/Configuration.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static Configuration()
3535
// but without modifying the real invariant culture instance.
3636
s_cultureInfo = (CultureInfo)CultureInfo.InvariantCulture.Clone();
3737

38-
ValidCommentChars = new[] { '#', ';' };
38+
ValidCommentChars = new HashSet<char> { '#', ';' };
3939
s_preferredCommentChar = '#';
4040
s_arrayElementSeparator = ',';
4141

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

590590
/// <summary>
591591
/// Gets or sets the preferred comment char when saving configurations.
@@ -597,7 +597,7 @@ public static char PreferredCommentChar
597597
{
598598
get => s_preferredCommentChar;
599599
set {
600-
if (!Array.Exists(ValidCommentChars, c => c == value))
600+
if (!ValidCommentChars.Contains(value))
601601
{
602602
throw new ArgumentException("The specified char '" + value + "' is not allowed as a comment char.");
603603
}

Src/ConfigurationReader.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,13 @@ private static string ParseComment(string line, out int commentCharIndex)
124124
var index = 0;
125125
var quoteCount = 0;
126126

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

134136
if (isValidCommentChar && !isCharWithinQuotes && !isCharEscaped)

Src/Setting.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// https://sharpconfig.org
33

44
using System;
5+
using System.Linq;
56
using System.Text;
67

78
namespace SharpConfig
@@ -612,8 +613,10 @@ private static string GetValueForOutput(string rawValue)
612613
return rawValue;
613614
}
614615

616+
bool isAnyCommentCharInRawValue() => rawValue.Any(c => Configuration.ValidCommentChars.Contains(c));
617+
615618
if (rawValue.IndexOf(" ", StringComparison.Ordinal) >= 0 ||
616-
(rawValue.IndexOfAny(Configuration.ValidCommentChars) >= 0 && !Configuration.IgnoreInlineComments))
619+
(isAnyCommentCharInRawValue() && !Configuration.IgnoreInlineComments))
617620
{
618621
rawValue = "\"" + rawValue + "\"";
619622
}

0 commit comments

Comments
 (0)