| |
| |
|
|
| using System.Collections.ObjectModel; |
|
|
| namespace Microsoft.PowerShell.Commands |
| { |
| |
| |
| |
| internal sealed class CSVHelper |
| { |
| internal CSVHelper(char delimiter) |
| { |
| Delimiter = delimiter; |
| } |
|
|
| |
| |
| |
| internal char Delimiter { get; } = ','; |
|
|
| |
| |
| |
| |
| |
| |
| internal Collection<string> ParseCsv(string csv) |
| { |
| Collection<string> result = new(); |
| string tempString = string.Empty; |
| csv = csv.Trim(); |
| if (csv.Length == 0 || csv[0] == '#') |
| { |
| return result; |
| } |
|
|
| bool inQuote = false; |
| for (int i = 0; i < csv.Length; i++) |
| { |
| char c = csv[i]; |
| if (c == Delimiter) |
| { |
| if (!inQuote) |
| { |
| result.Add(tempString); |
| tempString = string.Empty; |
| } |
| else |
| { |
| tempString += c; |
| } |
| } |
| else |
| { |
| switch (c) |
| { |
| case '"': |
| if (inQuote) |
| { |
| |
| |
| if (i == csv.Length - 1) |
| { |
| result.Add(tempString); |
| tempString = string.Empty; |
| inQuote = false; |
| break; |
| } |
|
|
| if (csv[i + 1] == Delimiter) |
| { |
| result.Add(tempString); |
| tempString = string.Empty; |
| inQuote = false; |
| i++; |
| } |
| else if (csv[i + 1] == '"') |
| { |
| tempString += '"'; |
| i++; |
| } |
| else |
| { |
| inQuote = false; |
| } |
| } |
| else |
| { |
| inQuote = true; |
| } |
|
|
| break; |
|
|
| default: |
| tempString += c; |
| break; |
| } |
| } |
| } |
|
|
| if (tempString.Length > 0) |
| { |
| result.Add(tempString); |
| } |
|
|
| return result; |
| } |
| } |
| } |
|
|