@@ -23,9 +23,15 @@ public bool TryFormat(object? value, string format, CultureInfo cultureInfo, out
2323 return true ;
2424
2525 var section = sections [ index ] ;
26+
27+ // FIX: Use absolute value for negative numbers in negative section to prevent double negatives
28+ var valueToFormat = ( index == 1 && value != null && IsNumeric ( value ) && Convert . ToDouble ( value ) < 0 )
29+ ? Math . Abs ( Convert . ToDouble ( value ) )
30+ : value ;
31+
2632 result = IsQuotedLiteral ( section )
2733 ? UnquoteString ( section )
28- : FormatWithSection ( value , section , cultureInfo ) ;
34+ : FormatWithSection ( valueToFormat , section , cultureInfo , sections , index ) ;
2935
3036 return true ;
3137 }
@@ -98,6 +104,10 @@ private static string UnquoteString(string section)
98104 if ( string . IsNullOrEmpty ( section ) ) return string . Empty ;
99105 var trimmed = section . Trim ( ) ;
100106
107+ // FIX: Handle empty quoted strings like '' and ""
108+ if ( trimmed == "''" || trimmed == "\" \" " )
109+ return string . Empty ;
110+
101111 return IsQuoted ( trimmed ) && trimmed . Length > 2
102112 ? trimmed [ 1 ..^ 1 ]
103113 : trimmed ;
@@ -106,7 +116,7 @@ static bool IsQuoted(string s) =>
106116 ( s . StartsWith ( '\' ' ) && s . EndsWith ( '\' ' ) ) || ( s . StartsWith ( '"' ) && s . EndsWith ( '"' ) ) ;
107117 }
108118
109- private static string FormatWithSection ( object ? value , string section , IFormatProvider formatProvider )
119+ private static string FormatWithSection ( object ? value , string section , IFormatProvider formatProvider , string [ ] ? sections = null , int index = 0 )
110120 {
111121 if ( string . IsNullOrEmpty ( section ) ) return string . Empty ;
112122 if ( IsQuotedLiteral ( section ) ) return UnquoteString ( section ) ;
@@ -118,12 +128,28 @@ private static string FormatWithSection(object? value, string section, IFormatPr
118128 IFormattable formattable => formattable . ToString ( section , formatProvider ) ,
119129 not null when IsValidFormatString ( section ) =>
120130 string . Format ( formatProvider , "{0:" + section + "}" , value ) ,
121- _ => value ? . ToString ( ) ?? string . Empty
131+ not null when index > 0 && sections != null && sections . Length > 0 && IsValidFormatString ( sections [ 0 ] ) =>
132+ // FIX: For invalid formats in non-first sections, use first section format
133+ string . Format ( formatProvider , "{0:" + sections [ 0 ] + "}" , value ) ,
134+ not null => value . ToString ( ) ?? string . Empty ,
135+ _ => section // Only for null values without valid format
122136 } ;
123137 }
124138 catch ( FormatException )
125139 {
126- return section ;
140+ // FIX: On format exception, try first section format or return value string
141+ if ( index > 0 && sections != null && sections . Length > 0 && IsValidFormatString ( sections [ 0 ] ) )
142+ {
143+ try
144+ {
145+ return string . Format ( formatProvider , "{0:" + sections [ 0 ] + "}" , value ) ;
146+ }
147+ catch
148+ {
149+ return value ? . ToString ( ) ?? section ;
150+ }
151+ }
152+ return value ? . ToString ( ) ?? section ;
127153 }
128154 }
129155
0 commit comments