Skip to content

Commit ca5db3a

Browse files
committed
Change ValidCommentChars to HashSet
1 parent 3ea9a8b commit ca5db3a

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.IO;
66
using System.Text;
7-
using System.Collections.Generic;
87

98
namespace SharpConfig
109
{
@@ -26,7 +25,6 @@ private static void Parse(StringReader reader, Configuration config)
2625
{
2726
var currentSection = new Section(Section.DefaultSectionName);
2827
var preCommentBuilder = new StringBuilder();
29-
HashSet<char> validCommentChars = new HashSet<char>(Configuration.ValidCommentChars); // Initialize once for each reader to optimize performance
3028
var lineNumber = 0;
3129
string line;
3230

@@ -44,7 +42,7 @@ private static void Parse(StringReader reader, Configuration config)
4442
continue;
4543
}
4644

47-
var comment = ParseComment(line, validCommentChars, out int commentIndex);
45+
var comment = ParseComment(line, out int commentIndex);
4846

4947
if (commentIndex == 0)
5048
{
@@ -111,7 +109,7 @@ private static void Parse(StringReader reader, Configuration config)
111109
}
112110
}
113111

114-
private static string ParseComment(string line, HashSet<char> validCommentChars, out int commentCharIndex)
112+
private static string ParseComment(string line, out int commentCharIndex)
115113
{
116114
// A comment starts with a valid comment character that:
117115
// 1. is not within a quote (eg. "this is # not a comment"), and
@@ -130,7 +128,7 @@ private static string ParseComment(string line, HashSet<char> validCommentChars,
130128
while (index < length) // traverse line from left to right
131129
{
132130
var currentChar = line[index];
133-
var isValidCommentChar = validCommentChars.Contains(currentChar);
131+
var isValidCommentChar = Configuration.ValidCommentChars.Contains(currentChar);
134132
var isQuotationMark = currentChar == '\"';
135133
var isCharWithinQuotes = (quoteCount & 1) == 1; // bitwise AND is slightly faster
136134
var isCharEscaped = index > 0 && line[index - 1] == '\\';

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)