corrade-vassal – Blame information for rev 16

Subversion Repositories:
Rev:
Rev Author Line No. Line
13 eva 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
12 {
16 eva 13 public static class CSV
13 eva 14 {
15 ///////////////////////////////////////////////////////////////////////////
16 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
17 ///////////////////////////////////////////////////////////////////////////
18 /// <summary>
14 zed 19 /// Converts a list of strings to a comma-separated values string.
13 eva 20 /// </summary>
21 /// <returns>a commma-separated list of values</returns>
22 /// <remarks>compliant with RFC 4180</remarks>
16 eva 23 public static string FromEnumerable(IEnumerable<string> input)
24 {
25 return string.Join(",",
26 input
14 zed 27 .Select(o => o.Replace("\"", "\"\""))
16 eva 28 .Select(o => o.IndexOfAny(new[] {'"', ' ', ',', '\r', '\n'}).Equals(-1) ? o : "\"" + o + "\""));
29 }
13 eva 30  
14 zed 31 ///////////////////////////////////////////////////////////////////////////
16 eva 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(Dictionary<string, string> input)
40 {
41 return string.Join(",", input.Keys.Zip(input.Values,
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 }
51  
52 ///////////////////////////////////////////////////////////////////////////
14 zed 53 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
54 ///////////////////////////////////////////////////////////////////////////
55 /// <summary>
56 /// Converts successive comma-separated values to key-value pairs.
57 /// </summary>
58 /// <returns>key-value pairs of successive comma-separate values</returns>
16 eva 59 public static IEnumerable<KeyValuePair<string, string>> ToKeyValue(string input)
60 {
61 return ToEnumerable(input).AsParallel().Select((o, p) => new {o, p})
62 .GroupBy(q => q.p/2, q => q.o)
63 .Select(o => o.ToArray())
64 .TakeWhile(o => o.Length%2 == 0)
65 .Where(o => !string.IsNullOrEmpty(o[0]) || !string.IsNullOrEmpty(o[1]))
66 .ToDictionary(o => o[0], p => p[1]).Select(o => o);
67 }
13 eva 68  
69 ///////////////////////////////////////////////////////////////////////////
70 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
71 ///////////////////////////////////////////////////////////////////////////
72 /// <summary>
73 /// Converts a comma-separated list of values to a list of strings.
74 /// </summary>
75 /// <param name="csv">a comma-separated list of values</param>
76 /// <returns>a list of strings</returns>
77 /// <remarks>compliant with RFC 4180</remarks>
78 public static IEnumerable<string> ToEnumerable(string csv)
79 {
16 eva 80 var s = new Stack<char>();
81 var m = new StringBuilder();
82 for (var i = 0; i < csv.Length; ++i)
13 eva 83 {
84 switch (csv[i])
85 {
86 case ',':
87 if (!s.Any() || !s.Peek().Equals('"'))
88 {
89 yield return m.ToString();
90 m = new StringBuilder();
91 continue;
92 }
93 m.Append(csv[i]);
94 continue;
95 case '"':
96 if (i + 1 < csv.Length && csv[i].Equals(csv[i + 1]))
97 {
98 m.Append(csv[i]);
99 ++i;
100 continue;
101 }
102 if (!s.Any() || !s.Peek().Equals(csv[i]))
103 {
104 s.Push(csv[i]);
105 continue;
106 }
107 s.Pop();
108 continue;
109 }
110 m.Append(csv[i]);
111 }
112  
113 yield return m.ToString();
114 }
115 }
116 }