File size: 3,469 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.ObjectModel;

namespace Microsoft.PowerShell.Commands
{
    /// <summary>
    /// This class is used to parse CSV text.
    /// </summary>
    internal sealed class CSVHelper
    {
        internal CSVHelper(char delimiter)
        {
            Delimiter = delimiter;
        }

        /// <summary>
        /// Gets or sets the delimiter that separates the values.
        /// </summary>
        internal char Delimiter { get; } = ',';

        /// <summary>
        /// Parse a CSV string.
        /// </summary>
        /// <param name="csv">
        /// String to be parsed.
        /// </param>
        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 we are at the end of the string or the end of the segment, create a new value
                                // Otherwise we have an error
                                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;
        }
    }
}