Browse Source

Change CSVParser to use Regex.Split (way faster)

master
Christopher 8 years ago
parent
commit
0504eb3651
  1. 21
      Assets/Fungus/Thirdparty/CSVParser/CsvParser.cs

21
Assets/Fungus/Thirdparty/CSVParser/CsvParser.cs vendored

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace Ideafixxxer.CsvParser namespace Ideafixxxer.CsvParser
{ {
@ -190,15 +191,22 @@ namespace Ideafixxxer.CsvParser
var context = new ParserContext(); var context = new ParserContext();
// Handle both Windows and Mac line endings // Handle both Windows and Mac line endings
string[] lines = csvData.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); var lines = Regex.Split(csvData, "\n|\r\n");
ParserState currentState = ParserState.LineStartState; ParserState currentState = ParserState.LineStartState;
foreach (string next in lines) for (int i = 0; i < lines.Length; i++) {
{ var next = lines [i];
foreach (char ch in next)
{ // Skip empty entries
switch (ch) if (next.Length == 0)
{ {
continue;
}
for (int j = 0; j < next.Length; j++) {
var ch = next [j];
switch (ch) {
case CommaCharacter: case CommaCharacter:
currentState = currentState.Comma (context); currentState = currentState.Comma (context);
break; break;
@ -212,6 +220,7 @@ namespace Ideafixxxer.CsvParser
} }
currentState = currentState.EndOfLine (context); currentState = currentState.EndOfLine (context);
} }
List<string[]> allLines = context.GetAllLines(); List<string[]> allLines = context.GetAllLines();
return allLines.ToArray(); return allLines.ToArray();
} }

Loading…
Cancel
Save