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