Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite of CSV regex parsing to avoid exponential behavior #2513

Merged
Merged
Changes from 1 commit
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
41 changes: 20 additions & 21 deletions Assets/Scripts/Game/StringTableCSVParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,36 @@ public static KeyValuePair<string, string>[] Load(string filename)
/// <returns>KeyValuePair for each row.</returns>
static KeyValuePair<string, string>[] ParseCSVRows(string csvText)
{
// Regex pattern from https://gist.github.com/awwsmm/886ac0ce0cef517ad7092915f708175f
const string linePattern = "(?:,|\\n|^)(\"(?:(?:\"\")*[^\"]*)*\"|[^\",\\n]*|(?:\\n|$))";
// Regex pattern inspired by https://gist.github.com/awwsmm/886ac0ce0cef517ad7092915f708175f
// but without the exponential behavior
const string linePattern = "(?:\\n|^)([^\",\\n]*),((?:\"[^\"]*\")+|[^\",\\n]*)";

// Split source CSV based on regex matches
char[] trimChars = { '\r', '\n', '\"', ',' };
List<KeyValuePair<string, string>> rows = new List<KeyValuePair<string, string>>();
string[] matches = (from Match m in Regex.Matches(csvText, linePattern, RegexOptions.ExplicitCapture) select m.Groups[0].Value).ToArray();
int pos = 0;
while (pos < matches.Length)
{
if (pos + 1 == matches.Length)
{
// Exit if no valid pair at end of csv (likely an empty line at end of source data)
break;
}
string key = matches[pos++].Trim(trimChars);
string value = matches[pos++].Trim(trimChars);
value = value.Replace("\"\"", "\""); // Replace escaped quotes in value with single quote marks
KeyValuePair<string, string> kvp = new KeyValuePair<string, string>(key, value);
rows.Add(kvp);
}

char[] trimChars = { '\r', '\n' };
petchema marked this conversation as resolved.
Show resolved Hide resolved
List<KeyValuePair<string, string>> rows = (from Match m in
Regex.Matches(csvText, linePattern)
select new KeyValuePair<string, string>(
m.Groups[1].Value.Trim(trimChars),
UnescapeCSVvalue(m.Groups[2].Value).Trim(trimChars)
)
).ToList();
petchema marked this conversation as resolved.
Show resolved Hide resolved
// Remove first row if it contains "Key" as key and "Value" as value
// This is the expected header row but doesn't need to be present
// First row will be accepted if any other key/value pair is present instead
if (rows.Count > 0 && rows[0].Key == keyString && rows[0].Value == valueString)
rows.RemoveAt(0);

return rows.ToArray();
}

static string UnescapeCSVvalue(string value) {
petchema marked this conversation as resolved.
Show resolved Hide resolved
if (value.Length > 0 && value[0] == '"')
{
return value.Substring(1, value.Length - 2)
petchema marked this conversation as resolved.
Show resolved Hide resolved
.Replace("\"\"", "\""); // unescape quote marks
}
return value;
}

/// <summary>
/// Read text from a file in read-only access mode.
/// Allows modder to keep CSV open in Excel without throwing exception in game.
Expand Down