wasSharp – Diff between revs 55 and 56

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