11using System ;
22using System . Collections . Generic ;
3+ using System . Globalization ;
34using System . Text . RegularExpressions ;
45
56namespace Open . Text . CSV
67{
7- public static partial class CsvUtility
8+ public static class CsvUtility
89 {
910 public const string LINE_PATTERN = "((?:\" )([^\" ]+)(?:\" )|([^,\" ]+))(?:\\ s*)(?:,|$)" ;
1011 public static readonly Regex LinePattern = new Regex ( LINE_PATTERN ) ;
@@ -33,6 +34,7 @@ public static string[][] GetArray(string csv, out int maxColumns)
3334 var lines = csv == null ? new string [ 0 ] : csv . Split ( '\n ' ) ;
3435
3536 var result = new List < string [ ] > ( lines . Length ) ;
37+ // ReSharper disable once LoopCanBeConvertedToQuery
3638 foreach ( var line in lines )
3739 result . Add ( GetLine ( line , ref maxColumns ) ) ;
3840
@@ -41,12 +43,12 @@ public static string[][] GetArray(string csv, out int maxColumns)
4143
4244 public static string [ ] [ ] GetArray ( string csv )
4345 {
44- return GetArray ( csv , out var maxColumns ) ;
46+ return GetArray ( csv , out _ ) ;
4547 }
4648
4749
4850 public const string NEWLINE = "\r \n " ;
49- public readonly static Regex QUOTESNEEDED = new Regex ( "^\\ s+|[,\n ]|\\ s+$" ) ;
51+ public static readonly Regex QUOTESNEEDED = new Regex ( "^\\ s+|[,\n ]|\\ s+$" ) ;
5052
5153 public static string WrapQuotes ( string value )
5254 {
@@ -70,17 +72,20 @@ public static string FormatValue(string value, bool forceQuotes = false)
7072 public static string ExportValue ( object value , bool forceQuotes = false )
7173 {
7274 var result = string . Empty ;
73- if ( value != null ) // && value != DBNull.Value )
75+ switch ( value )
7476 {
75- if ( value is DateTime datetime )
76- {
77+ case null :
78+ return FormatValue ( result , forceQuotes ) + "," ;
79+ case DateTime datetime :
7780 result = datetime . TimeOfDay == TimeSpan . Zero ?
7881 datetime . ToString ( "d" ) : // Use short date.
79- datetime . ToString ( ) ;
80- }
81- else
82+ datetime . ToString ( CultureInfo . InvariantCulture ) ;
83+ break ;
84+ default :
8285 result = value . ToString ( ) ;
86+ break ;
8387 }
88+
8489 return FormatValue ( result , forceQuotes ) + "," ;
8590 }
8691
0 commit comments