Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Whitehouse112 committed Nov 6, 2023
1 parent b85d15c commit bf0fc10
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
23 changes: 11 additions & 12 deletions DbcParserLib/ExtensionsAndHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ internal static bool InitialValue(this Signal signal, out double initialValue)
internal class StringToDictionaryParser
{
private IDictionary<int, string> m_dictionary;
private int m_currentIndex;

private StringToDictionaryParser(IDictionary<int, string> dict)
{
Expand All @@ -134,36 +133,36 @@ public static bool ParseString(string text, out IReadOnlyDictionary<int, string>
dictionary = null;
var internalDictionary = new Dictionary<int, string>();
var parser = new StringToDictionaryParser(internalDictionary);
if (parser.ParseKey(text))
if (parser.ParseKey(text, 0))
{
dictionary = internalDictionary;
return true;
}
return false;
}

private bool ParseKey(string text)
private bool ParseKey(string text, int offset)
{
var index = text.IndexOf("\"", m_currentIndex, StringComparison.InvariantCulture);
var index = text.IndexOf("\"", offset, StringComparison.InvariantCulture);
if(index == -1)
return true;

var key = text.Substring(m_currentIndex, index - m_currentIndex);
m_currentIndex = index + 1;
return int.TryParse(key, out var intKey) ? ParseValue(text, intKey) : false;
var key = text.Substring(offset, index - offset);
offset = index + 1;
return int.TryParse(key, out var intKey) ? ParseValue(text, offset, intKey) : false;
}

private bool ParseValue(string text, int key)
private bool ParseValue(string text, int offset, int key)
{
var index = text.IndexOf("\"", m_currentIndex, StringComparison.InvariantCulture);
var index = text.IndexOf("\"", offset, StringComparison.InvariantCulture);
if (index == -1)
return false;

var value = text.Substring(m_currentIndex, index - m_currentIndex);
var value = text.Substring(offset, index - offset);

m_dictionary[key] = value;
m_currentIndex = index +1;
return ParseKey(text);
offset = index +1;
return ParseKey(text, offset);
}
}
}
2 changes: 0 additions & 2 deletions DbcParserLib/Parsers/ValueTableDefinitionLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ internal class ValueTableDefinitionLineParser : ILineParser
{
private const string ValueTableDefinitionLineStarter = "VAL_TABLE_ ";
private const string ValueTableDefinitionParsingRegex = @"VAL_TABLE_\s+([a-zA-Z_][\w]*)\s+((?:\d+\s+(?:""[^""]*"")\s+)*)\s*;";
private const string ValueTableNewLineRegex = @"(""[^""]*""\s+)";
private const string ValueTableNewLineRegexReplace = "$1\n";

private readonly IParseFailureObserver m_observer;

Expand Down
2 changes: 0 additions & 2 deletions DbcParserLib/Parsers/ValueTableLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ internal class ValueTableLineParser : ILineParser
private const string ValueTableLineStarter = "VAL_ ";
private const string ValueTableLinkParsingRegex = @"VAL_\s+(\d+)\s+([a-zA-Z_][\w]*)\s+([a-zA-Z_][\w]*)\s*;";
private const string ValueTableParsingRegex = @"VAL_\s+(?:(?:(\d+)\s+([a-zA-Z_][\w]*))|([a-zA-Z_][\w]*))\s+((?:(?:-?\d+)\s+(?:""[^""]*"")\s+)*)\s*;";
private const string ValueTableNewLineRegex = @"(""[^""]*""\s+)";
private const string ValueTableNewLineRegexReplace = "$1\n";

private readonly IParseFailureObserver m_observer;

Expand Down

0 comments on commit bf0fc10

Please sign in to comment.