@@ -12,46 +12,27 @@ public static object Format(object val)
1212 {
1313 switch ( val )
1414 {
15- case IDictionary < string , object > dict :
16- return string . Join ( ", " , dict . Values . Select ( Format ) ) ;
17- case IDictionary < char , int > dict :
18- return $ "new Dictionary<char, int> {{ { string . Join ( ", " , dict . Keys . Select ( key => $ "[{ Format ( key ) } ] = { Format ( dict [ key ] ) } ") ) } }}";
19- case IDictionary < string , int > dict :
20- return $ "new Dictionary<string, int> {{ { string . Join ( ", " , dict . Keys . Select ( key => $ "[{ Format ( key ) } ] = { Format ( dict [ key ] ) } ") ) } }}";
21- case Enum enumeration :
22- return $ "{ enumeration . GetType ( ) . Name } .{ enumeration } ";
23- case JArray jArray :
24- return $ "new[] {{ { string . Join ( ", " , jArray . Select ( Format ) ) } }}";
25- case int [ , ] multidimensionalArray :
26- return multidimensionalArray . GetLength ( 0 ) > 1
27- ? $ "new[,]\r \n {{\r \n { string . Join ( ",\r \n " , Enumerable . Range ( 0 , multidimensionalArray . GetUpperBound ( 0 ) + 1 ) . Select ( x => multidimensionalArray . SliceRow ( x ) . ToNestedArray ( ) ) ) } \r \n }}"
28- : "new int[,] { }" ;
29- case IEnumerable < Tuple < string , object > > tuples :
30- return $ "new[] {{ { string . Join ( ", " , tuples . Select ( Format ) ) } }}";
31- case Tuple < string , object > tuple :
32- return $ "Tuple.Create({ tuple . Item1 } , { tuple . Item2 } )";
33- case string str :
34- return str . FormatString ( ) ;
35- case IEnumerable < int > ints :
36- return ints . Any ( ) ? $ "new[] {{ { string . Join ( ", " , ints ) } }}" : "new int[0]" ;
37- case IEnumerable < string > strings :
38- return strings . Any ( ) ? $ "new[] {{ { string . Join ( ", " , strings . Select ( Format ) ) } }}" : "new string[0]" ;
39- case IEnumerable < UnescapedValue > unescapedValues when unescapedValues . Any ( ) :
40- return $ "new[] {{ { string . Join ( ", " , unescapedValues . Select ( Format ) ) } }}";
41- case double dbl :
42- return dbl . ToString ( CultureInfo . InvariantCulture ) ;
43- case float flt :
44- return flt . ToString ( CultureInfo . InvariantCulture ) ;
45- case ulong ulng :
46- return $ "{ ulng } UL";
47- case char c :
48- return $ "'{ c } '";
49- default :
50- return val ;
15+ case string str : return str . Format ( ) ;
16+ case double dbl : return dbl . Format ( ) ;
17+ case float flt : return flt . Format ( ) ;
18+ case ulong ulng : return ulng . Format ( ) ;
19+ case char c : return c . Format ( ) ;
20+ case Enum enumeration : return enumeration . Format ( ) ;
21+ case Tuple < string , object > tuple : return tuple . Format ( ) ;
22+ case IEnumerable < int > ints : return ints . Format ( ) ;
23+ case IEnumerable < string > strings : return strings . Format ( ) ;
24+ case IEnumerable < UnescapedValue > unescapedValues when unescapedValues . Any ( ) : return unescapedValues . Format ( ) ;
25+ case IDictionary < string , object > dict : return dict . Format ( ) ;
26+ case IDictionary < char , int > dict : return dict . Format ( ) ;
27+ case IDictionary < string , int > dict : return dict . Format ( ) ;
28+ case JArray jArray : return jArray . Format ( ) ;
29+ case int [ , ] multidimensionalArray : return multidimensionalArray . Format ( ) ;
30+ case IEnumerable < Tuple < string , object > > tuples : return tuples . Format ( ) ;
31+ default : return val ;
5132 }
5233 }
5334
54- public static string [ ] FormatVariables ( IReadOnlyDictionary < string , object > dict )
35+ public static string [ ] FormatVariables ( IReadOnlyDictionary < string , object > dict )
5536 => dict . Keys . SelectMany ( key => FormatVariable ( dict [ key ] , key . ToVariableName ( ) ) ) . ToArray ( ) ;
5637
5738 public static string [ ] FormatVariable ( object val , string name )
@@ -65,11 +46,59 @@ public static string[] FormatVariable(object val, string name)
6546 case IDictionary < string , int > dict :
6647 return FormatMultiLineEnumerable ( dict . Keys . Select ( ( key , i ) => $ "[{ Format ( key ) } ] = { Format ( dict [ key ] ) } " + ( i < dict . Keys . Count - 1 ? "," : "" ) ) , name , "new Dictionary<string, int>" ) ;
6748 default :
68- return new [ ] { $ "var { name } = { Format ( val ) } ;"} ;
49+ return new [ ] { $ "var { name } = { Format ( val ) } ;" } ;
6950 }
7051 }
7152
72- private static string FormatString ( this string s ) => s . EscapeSpecialCharacters ( ) . Quote ( ) ;
53+ private static string Format ( this string s ) => s . EscapeSpecialCharacters ( ) . Quote ( ) ;
54+
55+ private static string Format ( this char c ) => $ "'{ c } '";
56+
57+ private static string Format ( this double dbl ) => dbl . ToString ( CultureInfo . InvariantCulture ) ;
58+
59+ private static string Format ( this float flt ) => flt . ToString ( CultureInfo . InvariantCulture ) ;
60+
61+ private static string Format ( this ulong ulng ) => $ "{ ulng } UL";
62+
63+ private static string Format ( this Enum @enumeration ) =>
64+ $ "{ enumeration . GetType ( ) . Name } .{ enumeration } ";
65+
66+ private static string Format ( this Tuple < string , object > tuple ) =>
67+ $ "Tuple.Create({ tuple . Item1 } , { tuple . Item2 } )";
68+
69+ private static string Format ( this IEnumerable < int > ints ) => ints . Any ( ) ?
70+ $ "new[] {{ { string . Join ( ", " , ints ) } }}" : "new int[0]" ;
71+
72+ private static string Format ( this IEnumerable < string > strings ) =>
73+ strings . Any ( ) ? $ "new[] {{ { string . Join ( ", " , strings . Select ( Format ) ) } }}" : "new string[0]" ;
74+
75+ private static string Format ( this IEnumerable < UnescapedValue > unescapedValues ) =>
76+ $ "new[] {{ { string . Join ( ", " , unescapedValues . Select ( Format ) ) } }}";
77+
78+ private static string Format ( this IDictionary < string , object > dict ) =>
79+ string . Join ( ", " , dict . Values . Select ( Format ) ) ;
80+
81+ private static string Format ( this IDictionary < char , int > dict ) =>
82+ $ "new Dictionary<char, int> {{ { string . Join ( ", " , dict . Keys . Select ( key => $ "[{ Format ( key ) } ] = { Format ( dict [ key ] ) } ") ) } }}";
83+
84+ private static string Format ( this IDictionary < string , int > dict ) =>
85+ $ "new Dictionary<string, int> {{ { string . Join ( ", " , dict . Keys . Select ( key => $ "[{ Format ( key ) } ] = { Format ( dict [ key ] ) } ") ) } }}";
86+
87+ private static string Format ( this JArray jArray ) =>
88+ $ "new[] {{ { string . Join ( ", " , jArray . Select ( Format ) ) } }}";
89+
90+ private static string Format ( this int [ , ] multidimensionalArray )
91+ {
92+ return multidimensionalArray . GetLength ( 0 ) > 1
93+ ? $ "new[,]\r \n {{\r \n { string . Join ( ",\r \n " , Enumerable . Range ( 0 , multidimensionalArray . GetUpperBound ( 0 ) + 1 ) . Select ( x => multidimensionalArray . SliceRow ( x ) . ToNestedArray ( ) ) ) } \r \n }}"
94+ : "new int[,] { }" ;
95+ }
96+
97+ private static string Format ( this IEnumerable < Tuple < string , object > > tuples ) =>
98+ $ "new[] {{ { string . Join ( ", " , tuples . Select ( Format ) ) } }}";
99+
100+ private static string ToNestedArray < T > ( this IEnumerable < T > enumerable ) =>
101+ enumerable . Any ( ) ? $ "{{ { string . Join ( ", " , enumerable ) } }}" : string . Empty ;
73102
74103 private static string [ ] FormatMultiLineString ( string name , string str )
75104 {
@@ -82,41 +111,13 @@ private static string[] FormatMultiLineString(string name, string str)
82111 return FormatMultiLineVariable ( formattedStrings , name ) ;
83112 }
84113
85- private static string [ ] FormatMultiLineEnumerable ( IEnumerable < string > enumerable , string name , string constructor = null )
114+ private static string [ ] FormatMultiLineEnumerable ( IEnumerable < string > enumerable , string name , string constructor = null )
86115 => FormatMultiLineVariable ( enumerable . Prepend ( "{" ) . Append ( "}" ) , name , constructor ) ;
87116
88- private static string [ ] FormatMultiLineVariable ( IEnumerable < string > enumerable , string name , string constructor = null )
117+ private static string [ ] FormatMultiLineVariable ( IEnumerable < string > enumerable , string name , string constructor = null )
89118 => enumerable . Select ( line => line == "{" || line == "}" ? line : line . Indent ( ) )
90119 . AddTrailingSemicolon ( )
91120 . Prepend ( $ "var { name } = { constructor } ")
92121 . ToArray ( ) ;
93-
94- private static IEnumerable < string > AddTrailingSemicolon ( this IEnumerable < string > enumerable )
95- {
96- var array = enumerable . ToArray ( ) ;
97- array [ array . Length - 1 ] += ";" ;
98- return array ;
99- }
100-
101- private static IEnumerable < T > SliceRow < T > ( this T [ , ] array , int row )
102- {
103- for ( var i = array . GetLowerBound ( 1 ) ; i <= array . GetUpperBound ( 1 ) ; i ++ )
104- {
105- yield return array [ row , i ] ;
106- }
107- }
108-
109- private static IEnumerable < T > SliceColumn < T > ( this T [ , ] array , int column )
110- {
111- for ( var i = array . GetLowerBound ( 0 ) ; i <= array . GetUpperBound ( 0 ) ; i ++ )
112- {
113- yield return array [ i , column ] ;
114- }
115- }
116-
117- private static string ToNestedArray < T > ( this IEnumerable < T > enumerable )
118- {
119- return enumerable . Any ( ) ? $ "{{ { string . Join ( ", " , enumerable ) } }}" : string . Empty ;
120- }
121122 }
122123}
0 commit comments