wasSharp – Diff between revs 6 and 7
?pathlinks?
Rev 6 | Rev 7 | |||
---|---|---|---|---|
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 | |
- | ||
7 | using System; |
6 | |
|
8 | using System.Collections.Generic; |
7 | using System.Collections.Generic; |
|
9 | using System.Linq; |
- | ||
10 | using System.Linq.Expressions; |
8 | using System.Linq; |
|
11 | using System.Text; |
9 | using System.Text; |
|
12 | |
10 | |
|
13 | namespace wasSharp |
11 | namespace wasSharp |
|
14 | { |
12 | { |
|
15 | public class CSV |
13 | public static class CSV |
|
16 | { |
14 | { |
|
17 | /////////////////////////////////////////////////////////////////////////// |
15 | /////////////////////////////////////////////////////////////////////////// |
|
18 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
16 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
|
19 | /////////////////////////////////////////////////////////////////////////// |
17 | /////////////////////////////////////////////////////////////////////////// |
|
20 | /// <summary> |
18 | /// <summary> |
|
21 | /// Converts a list of strings to a comma-separated values string. |
19 | /// Converts a list of strings to a comma-separated values string. |
|
22 | /// </summary> |
20 | /// </summary> |
|
23 | /// <returns>a commma-separated list of values</returns> |
21 | /// <returns>a commma-separated list of values</returns> |
|
24 | /// <remarks>compliant with RFC 4180</remarks> |
22 | /// <remarks>compliant with RFC 4180</remarks> |
|
25 | public static Func<IEnumerable<string>, string> FromEnumerable = |
23 | public static string FromEnumerable(IEnumerable<string> input) |
|
- | 24 | { |
||
26 | ((Expression<Func<IEnumerable<string>, string>>) (data => string.Join(",", |
25 | return string.Join(",", |
|
27 | data |
26 | input |
|
28 | .Select(o => o.Replace("\"", "\"\"")) |
27 | .Select(o => o.Replace("\"", "\"\"")) |
|
29 | .Select(o => o.IndexOfAny(new[] {'"', ' ', ',', '\r', '\n'}).Equals(-1) ? o : "\"" + o + "\"")))) |
28 | .Select(o => o.IndexOfAny(new[] {'"', ' ', ',', '\r', '\n'}).Equals(-1) ? o : "\"" + o + "\"")); |
|
- | 29 | } |
||
- | 30 | |
||
- | 31 | /////////////////////////////////////////////////////////////////////////// |
||
- | 32 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 33 | /////////////////////////////////////////////////////////////////////////// |
||
- | 34 | /// <summary> |
||
- | 35 | /// Converts a dictionary of strings to a comma-separated values string. |
||
- | 36 | /// </summary> |
||
- | 37 | /// <returns>a commma-separated list of values</returns> |
||
- | 38 | /// <remarks>compliant with RFC 4180</remarks> |
||
- | 39 | public static string FromDictionary<K, V>(Dictionary<K, V> input) |
||
- | 40 | { |
||
- | 41 | return string.Join(",", input.Keys.Select(o => o.ToString()).Zip(input.Values.Select(o => o.ToString()), |
||
30 | .Compile(); |
42 | (o, p) => |
|
- | 43 | string.Join(",", |
||
- | 44 | o.Replace("\"", "\"\"").IndexOfAny(new[] {'"', ' ', ',', '\r', '\n'}).Equals(-1) |
||
- | 45 | ? o |
||
- | 46 | : "\"" + o + "\"", |
||
- | 47 | p.Replace("\"", "\"\"").IndexOfAny(new[] {'"', ' ', ',', '\r', '\n'}).Equals(-1) |
||
- | 48 | ? p |
||
- | 49 | : "\"" + p + "\""))); |
||
- | 50 | } |
||
31 | |
51 | |
|
32 | /////////////////////////////////////////////////////////////////////////// |
52 | /////////////////////////////////////////////////////////////////////////// |
|
33 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
53 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
|
34 | /////////////////////////////////////////////////////////////////////////// |
54 | /////////////////////////////////////////////////////////////////////////// |
|
35 | /// <summary> |
55 | /// <summary> |
|
36 | /// Converts successive comma-separated values to key-value pairs. |
56 | /// Converts successive comma-separated values to key-value pairs. |
|
37 | /// </summary> |
57 | /// </summary> |
|
38 | /// <returns>key-value pairs of successive comma-separate values</returns> |
58 | /// <returns>key-value pairs of successive comma-separate values</returns> |
|
39 | public static Func<string, IEnumerable<KeyValuePair<string, string>>> ToKeyValue = |
59 | public static IEnumerable<KeyValuePair<string, string>> ToKeyValue(string input) |
|
40 | ((Expression<Func<string, IEnumerable<KeyValuePair<string, string>>>>) |
60 | { |
|
41 | (csv => ToEnumerable(csv).AsParallel().Select((o, p) => new {o, p}) |
61 | return ToEnumerable(input).AsParallel().Select((o, p) => new {o, p}) |
|
42 | .GroupBy(q => q.p/2, q => q.o) |
62 | .GroupBy(q => q.p/2, q => q.o) |
|
43 | .Select(o => o.ToArray()) |
63 | .Select(o => o.ToArray()) |
|
44 | .TakeWhile(o => o.Length%2 == 0) |
64 | .TakeWhile(o => o.Length%2 == 0) |
|
45 | .Where(o => !string.IsNullOrEmpty(o[0]) || !string.IsNullOrEmpty(o[1])) |
65 | .Where(o => !string.IsNullOrEmpty(o[0]) || !string.IsNullOrEmpty(o[1])) |
|
46 | .ToDictionary(o => o[0], p => p[1]).Select(o => o))).Compile(); |
66 | .ToDictionary(o => o[0], p => p[1]).Select(o => o); |
|
- | 67 | } |
||
- | 68 | |
||
- | 69 | /////////////////////////////////////////////////////////////////////////// |
||
- | 70 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 71 | /////////////////////////////////////////////////////////////////////////// |
||
- | 72 | /// <summary> |
||
- | 73 | /// Converts a generic key value pair to a CSV. |
||
- | 74 | /// </summary> |
||
- | 75 | /// <returns>a commma-separated list of values</returns> |
||
- | 76 | /// <remarks>compliant with RFC 4180</remarks> |
||
- | 77 | public static string FromKeyValue<K, V>(KeyValuePair<K, V> input) |
||
- | 78 | { |
||
- | 79 | var key = input.Key.ToString(); |
||
- | 80 | var value = input.Value.ToString(); |
||
- | 81 | |
||
- | 82 | return string.Join(",", key |
||
- | 83 | .Replace("\"", "\"\"").IndexOfAny(new[] {'"', ' ', ',', '\r', '\n'}).Equals(-1) |
||
- | 84 | ? key |
||
- | 85 | : "\"" + key + "\"", value |
||
- | 86 | .Replace("\"", "\"\"") |
||
- | 87 | .IndexOfAny(new[] {'"', ' ', ',', '\r', '\n'}) |
||
- | 88 | .Equals(-1) |
||
- | 89 | ? value |
||
- | 90 | : "\"" + value + "\""); |
||
- | 91 | } |
||
47 | |
92 | |
|
48 | /////////////////////////////////////////////////////////////////////////// |
93 | /////////////////////////////////////////////////////////////////////////// |
|
49 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
94 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
|
50 | /////////////////////////////////////////////////////////////////////////// |
95 | /////////////////////////////////////////////////////////////////////////// |
|
51 | /// <summary> |
96 | /// <summary> |
|
52 | /// Converts a comma-separated list of values to a list of strings. |
97 | /// Converts a comma-separated list of values to a list of strings. |
|
53 | /// </summary> |
98 | /// </summary> |
|
54 | /// <param name="csv">a comma-separated list of values</param> |
99 | /// <param name="csv">a comma-separated list of values</param> |
|
55 | /// <returns>a list of strings</returns> |
100 | /// <returns>a list of strings</returns> |
|
56 | /// <remarks>compliant with RFC 4180</remarks> |
101 | /// <remarks>compliant with RFC 4180</remarks> |
|
57 | public static IEnumerable<string> ToEnumerable(string csv) |
102 | public static IEnumerable<string> ToEnumerable(string csv) |
|
58 | { |
103 | { |
|
59 | Stack<char> s = new Stack<char>(); |
104 | var s = new Stack<char>(); |
|
60 | StringBuilder m = new StringBuilder(); |
105 | var m = new StringBuilder(); |
|
61 | for (int i = 0; i < csv.Length; ++i) |
106 | for (var i = 0; i < csv.Length; ++i) |
|
62 | { |
107 | { |
|
63 | switch (csv[i]) |
108 | switch (csv[i]) |
|
64 | { |
109 | { |
|
65 | case ',': |
110 | case ',': |
|
66 | if (!s.Any() || !s.Peek().Equals('"')) |
111 | if (!s.Any() || !s.Peek().Equals('"')) |
|
67 | { |
112 | { |
|
68 | yield return m.ToString(); |
113 | yield return m.ToString(); |
|
69 | m = new StringBuilder(); |
114 | m = new StringBuilder(); |
|
70 | continue; |
115 | continue; |
|
71 | } |
116 | } |
|
72 | m.Append(csv[i]); |
117 | m.Append(csv[i]); |
|
73 | continue; |
118 | continue; |
|
74 | case '"': |
119 | case '"': |
|
75 | if (i + 1 < csv.Length && csv[i].Equals(csv[i + 1])) |
120 | if (i + 1 < csv.Length && csv[i].Equals(csv[i + 1])) |
|
76 | { |
121 | { |
|
77 | m.Append(csv[i]); |
122 | m.Append(csv[i]); |
|
78 | ++i; |
123 | ++i; |
|
79 | continue; |
124 | continue; |
|
80 | } |
125 | } |
|
81 | if (!s.Any() || !s.Peek().Equals(csv[i])) |
126 | if (!s.Any() || !s.Peek().Equals(csv[i])) |
|
82 | { |
127 | { |
|
83 | s.Push(csv[i]); |
128 | s.Push(csv[i]); |
|
84 | continue; |
129 | continue; |
|
85 | } |
130 | } |
|
86 | s.Pop(); |
131 | s.Pop(); |
|
87 | continue; |
132 | continue; |
|
88 | } |
133 | } |
|
89 | m.Append(csv[i]); |
134 | m.Append(csv[i]); |
|
90 | } |
135 | } |
|
91 | |
136 | |
|
92 | yield return m.ToString(); |
137 | yield return m.ToString(); |
|
93 | } |
138 | } |
|
94 | } |
139 | } |
|
95 | } |
140 | } |
|
96 | |
141 | |
|
97 |
|
142 |
|
|
98 | |
143 | |
|
99 | |
144 | |
|
100 | |
145 | |