corrade-vassal – Diff between revs 13 and 14

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 13 Rev 14
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;
7 using System.Collections.Generic; 8 using System.Collections.Generic;
-   9 using System.Linq;
8 using System.Linq; 10 using System.Linq.Expressions;
9 using System.Text; -  
10 using System.Threading.Tasks; 11 using System.Text;
11   12  
12 namespace wasSharp 13 namespace wasSharp
13 { 14 {
14 public class CSV 15 public class CSV
15 { 16 {
16 /////////////////////////////////////////////////////////////////////////// 17 ///////////////////////////////////////////////////////////////////////////
17 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 18 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
18 /////////////////////////////////////////////////////////////////////////// 19 ///////////////////////////////////////////////////////////////////////////
19 /// <summary> 20 /// <summary>
20 /// Converts a list of string to a comma-separated values string. 21 /// Converts a list of strings to a comma-separated values string.
21 /// </summary> 22 /// </summary>
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 FromEnumerable(IEnumerable<string> l) 25 public static Func<IEnumerable<string>, string> FromEnumerable =
-   26 ((Expression<Func<IEnumerable<string>, string>>) (data => string.Join(",",
26 { 27 data
27 string[] csv = l.Select(o => o).ToArray(); 28 .Select(o => o.Replace("\"", "\"\""))
28 char[] escapeCharacters = {'"', ' ', ',', '\r', '\n'}; 29 .Select(o => o.IndexOfAny(new[] {'"', ' ', ',', '\r', '\n'}).Equals(-1) ? o : "\"" + o + "\""))))
29 Parallel.ForEach(csv.Select((v, i) => new {i, v}), o => -  
30 { 30 .Compile();
31 string cell = o.v.Replace("\"", "\"\""); -  
-   31  
32   32 ///////////////////////////////////////////////////////////////////////////
-   33 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
33 switch (cell.IndexOfAny(escapeCharacters)) 34 ///////////////////////////////////////////////////////////////////////////
-   35 /// <summary>
34 { 36 /// Converts successive comma-separated values to key-value pairs.
35 case -1: 37 /// </summary>
-   38 /// <returns>key-value pairs of successive comma-separate values</returns>
36 csv[o.i] = cell; 39 public static Func<string, IEnumerable<KeyValuePair<string, string>>> ToKeyValue =
37 break; 40 ((Expression<Func<string, IEnumerable<KeyValuePair<string, string>>>>)
38 default: 41 (csv => ToEnumerable(csv).AsParallel().Select((o, p) => new {o, p})
39 csv[o.i] = "\"" + cell + "\""; 42 .GroupBy(q => q.p/2, q => q.o)
40 break; 43 .Select(o => o.ToArray())
41 } 44 .TakeWhile(o => o.Length%2 == 0)
42 }); 45 .Where(o => !string.IsNullOrEmpty(o[0]) || !string.IsNullOrEmpty(o[1]))
43 return string.Join(",", csv); -  
44 } 46 .ToDictionary(o => o[0], p => p[1]).Select(o => o))).Compile();
45   47  
46 /////////////////////////////////////////////////////////////////////////// 48 ///////////////////////////////////////////////////////////////////////////
47 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 49 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
48 /////////////////////////////////////////////////////////////////////////// 50 ///////////////////////////////////////////////////////////////////////////
49 /// <summary> 51 /// <summary>
50 /// Converts a comma-separated list of values to a list of strings. 52 /// Converts a comma-separated list of values to a list of strings.
51 /// </summary> 53 /// </summary>
52 /// <param name="csv">a comma-separated list of values</param> 54 /// <param name="csv">a comma-separated list of values</param>
53 /// <returns>a list of strings</returns> 55 /// <returns>a list of strings</returns>
54 /// <remarks>compliant with RFC 4180</remarks> 56 /// <remarks>compliant with RFC 4180</remarks>
55 public static IEnumerable<string> ToEnumerable(string csv) 57 public static IEnumerable<string> ToEnumerable(string csv)
56 { 58 {
57 Stack<char> s = new Stack<char>(); 59 Stack<char> s = new Stack<char>();
58 StringBuilder m = new StringBuilder(); 60 StringBuilder m = new StringBuilder();
59 for (int i = 0; i < csv.Length; ++i) 61 for (int i = 0; i < csv.Length; ++i)
60 { 62 {
61 switch (csv[i]) 63 switch (csv[i])
62 { 64 {
63 case ',': 65 case ',':
64 if (!s.Any() || !s.Peek().Equals('"')) 66 if (!s.Any() || !s.Peek().Equals('"'))
65 { 67 {
66 yield return m.ToString(); 68 yield return m.ToString();
67 m = new StringBuilder(); 69 m = new StringBuilder();
68 continue; 70 continue;
69 } 71 }
70 m.Append(csv[i]); 72 m.Append(csv[i]);
71 continue; 73 continue;
72 case '"': 74 case '"':
73 if (i + 1 < csv.Length && csv[i].Equals(csv[i + 1])) 75 if (i + 1 < csv.Length && csv[i].Equals(csv[i + 1]))
74 { 76 {
75 m.Append(csv[i]); 77 m.Append(csv[i]);
76 ++i; 78 ++i;
77 continue; 79 continue;
78 } 80 }
79 if (!s.Any() || !s.Peek().Equals(csv[i])) 81 if (!s.Any() || !s.Peek().Equals(csv[i]))
80 { 82 {
81 s.Push(csv[i]); 83 s.Push(csv[i]);
82 continue; 84 continue;
83 } 85 }
84 s.Pop(); 86 s.Pop();
85 continue; 87 continue;
86 } 88 }
87 m.Append(csv[i]); 89 m.Append(csv[i]);
88 } 90 }
89   91  
90 yield return m.ToString(); 92 yield return m.ToString();
91 } 93 }
92 } 94 }
93 } 95 }
94   96  
95
Generated by GNU Enscript 1.6.5.90.
97
Generated by GNU Enscript 1.6.5.90.
96   98  
97   99  
98   100