wasSharp – Diff between revs 1 and 4

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