wasSharp – Blame information for rev 6

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