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