corrade-vassal – Diff between revs 13 and 14

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 13 Rev 14
Line 2... Line 2...
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 ///////////////////////////////////////////////////////////////////////////
Line -... Line 6...
-   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; -  
Line 10... Line 11...
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 /// </summary> 21 /// Converts a list of strings to a comma-separated values string.
22 /// <param name="l">a list of strings</param> 22 /// </summary>
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 Func<IEnumerable<string>, string> FromEnumerable =
25 public static string FromEnumerable(IEnumerable<string> l) 26 ((Expression<Func<IEnumerable<string>, string>>) (data => string.Join(",",
26 { 27 data
27 string[] csv = l.Select(o => o).ToArray(); -  
28 char[] escapeCharacters = {'"', ' ', ',', '\r', '\n'}; 28 .Select(o => o.Replace("\"", "\"\""))
29 Parallel.ForEach(csv.Select((v, i) => new {i, v}), o => -  
Line -... Line 29...
-   29 .Select(o => o.IndexOfAny(new[] {'"', ' ', ',', '\r', '\n'}).Equals(-1) ? o : "\"" + o + "\""))))
30 { 30 .Compile();
-   31  
31 string cell = o.v.Replace("\"", "\"\""); 32 ///////////////////////////////////////////////////////////////////////////
-   33 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
32   34 ///////////////////////////////////////////////////////////////////////////
33 switch (cell.IndexOfAny(escapeCharacters)) 35 /// <summary>
-   36 /// Converts successive comma-separated values to key-value pairs.
34 { 37 /// </summary>
35 case -1: 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 } -  
Line 42... Line 44...
42 }); 44 .TakeWhile(o => o.Length%2 == 0)
43 return string.Join(",", csv); 45 .Where(o => !string.IsNullOrEmpty(o[0]) || !string.IsNullOrEmpty(o[1]))
44 } 46 .ToDictionary(o => o[0], p => p[1]).Select(o => o))).Compile();
45   47