diff --git a/Src/ConfigurationReader.cs b/Src/ConfigurationReader.cs index 369b207..b5b2647 100644 --- a/Src/ConfigurationReader.cs +++ b/Src/ConfigurationReader.cs @@ -4,6 +4,7 @@ using System; using System.IO; using System.Text; +using System.Collections.Generic; namespace SharpConfig { @@ -25,6 +26,7 @@ private static void Parse(StringReader reader, Configuration config) { var currentSection = new Section(Section.DefaultSectionName); var preCommentBuilder = new StringBuilder(); + HashSet validCommentChars = new HashSet(Configuration.ValidCommentChars); // Initialize once for each reader to optimize performance int lineNumber = 0; @@ -44,7 +46,7 @@ private static void Parse(StringReader reader, Configuration config) continue; } - var comment = ParseComment(line, out int commentIndex); + var comment = ParseComment(line, validCommentChars, out int commentIndex); if (commentIndex == 0) { @@ -108,7 +110,7 @@ private static void Parse(StringReader reader, Configuration config) } } - private static string ParseComment(string line, out int commentCharIndex) + private static string ParseComment(string line, HashSet validCommentChars, out int commentCharIndex) { // A comment starts with a valid comment character that: // 1. is not within a quote (eg. "this is # not a comment"), and @@ -122,11 +124,13 @@ private static string ParseComment(string line, out int commentCharIndex) int index = 0; int quoteCount = 0; - while (line.Length > index) // traverse line from left to right + int length = line.Length; + while (index < length) // traverse line from left to right { - bool isValidCommentChar = Array.IndexOf(Configuration.ValidCommentChars, line[index]) > -1; - bool isQuotationMark = line[index] == '\"'; - bool isCharWithinQuotes = quoteCount % 2 == 1; + char currentChar = line[index]; + bool isValidCommentChar = validCommentChars.Contains(currentChar); + bool isQuotationMark = currentChar == '\"'; + bool isCharWithinQuotes = (quoteCount & 1) == 1; // bitwise AND is slightly faster bool isCharEscaped = index > 0 && line[index - 1] == '\\'; if (isValidCommentChar && !isCharWithinQuotes && !isCharEscaped)