wasSharp – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | zed | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System.Collections.Generic; |
||
8 | using System.Linq; |
||
9 | using System.Text; |
||
10 | using System.Threading.Tasks; |
||
11 | |||
12 | namespace wasSharp |
||
13 | { |
||
14 | public class CSV |
||
15 | { |
||
16 | /////////////////////////////////////////////////////////////////////////// |
||
17 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
18 | /////////////////////////////////////////////////////////////////////////// |
||
19 | /// <summary> |
||
20 | /// Converts a list of string to a comma-separated values string. |
||
21 | /// </summary> |
||
22 | /// <param name="l">a list of strings</param> |
||
23 | /// <returns>a commma-separated list of values</returns> |
||
24 | /// <remarks>compliant with RFC 4180</remarks> |
||
25 | public static string wasEnumerableToCSV(IEnumerable<string> l) |
||
26 | { |
||
27 | string[] csv = l.Select(o => o).ToArray(); |
||
28 | Parallel.ForEach(csv.Select((v, i) => new {i, v}), o => |
||
29 | { |
||
30 | string cell = o.v.Replace("\"", "\"\""); |
||
31 | |||
32 | switch (cell.IndexOfAny(new[] {'"', ' ', ',', '\r', '\n'})) |
||
33 | { |
||
34 | case -1: |
||
35 | csv[o.i] = cell; |
||
36 | break; |
||
37 | default: |
||
38 | csv[o.i] = "\"" + cell + "\""; |
||
39 | break; |
||
40 | } |
||
41 | }); |
||
42 | return string.Join(",", csv); |
||
43 | } |
||
44 | |||
45 | /////////////////////////////////////////////////////////////////////////// |
||
46 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
47 | /////////////////////////////////////////////////////////////////////////// |
||
48 | /// <summary> |
||
49 | /// Converts a comma-separated list of values to a list of strings. |
||
50 | /// </summary> |
||
51 | /// <param name="csv">a comma-separated list of values</param> |
||
52 | /// <returns>a list of strings</returns> |
||
53 | /// <remarks>compliant with RFC 4180</remarks> |
||
54 | public static IEnumerable<string> wasCSVToEnumerable(string csv) |
||
55 | { |
||
56 | Stack<char> s = new Stack<char>(); |
||
57 | StringBuilder m = new StringBuilder(); |
||
58 | for (int i = 0; i < csv.Length; ++i) |
||
59 | { |
||
60 | switch (csv[i]) |
||
61 | { |
||
62 | case ',': |
||
63 | if (!s.Any() || !s.Peek().Equals('"')) |
||
64 | { |
||
65 | yield return m.ToString(); |
||
66 | m = new StringBuilder(); |
||
67 | continue; |
||
68 | } |
||
69 | m.Append(csv[i]); |
||
70 | continue; |
||
71 | case '"': |
||
72 | if (i + 1 < csv.Length && csv[i].Equals(csv[i + 1])) |
||
73 | { |
||
74 | m.Append(csv[i]); |
||
75 | ++i; |
||
76 | continue; |
||
77 | } |
||
78 | if (!s.Any() || !s.Peek().Equals(csv[i])) |
||
79 | { |
||
80 | s.Push(csv[i]); |
||
81 | continue; |
||
82 | } |
||
83 | s.Pop(); |
||
84 | continue; |
||
85 | } |
||
86 | m.Append(csv[i]); |
||
87 | } |
||
88 | |||
89 | yield return m.ToString(); |
||
90 | } |
||
91 | } |
||
92 | } |