@@ -257,6 +257,7 @@ in matches
257257 /// <param name="value">The value.</param>
258258 /// <param name="replacements">The replacements.</param>
259259 /// <param name="useDoubleBracket">Use double bracket if <see langword="true" />;otherwise <see langword="false" />.</param>
260+ /// <param name="comparison">Use comparison with default <see langword="StringComparison.Ordinal" /> for key matching.</param>
260261 /// <exception cref="ArgumentNullException">
261262 /// value
262263 /// or
@@ -265,7 +266,8 @@ in matches
265266 public static string SetStringFormatParameterTemplatePlaceholders (
266267 this string value ,
267268 IDictionary < string , string > replacements ,
268- bool useDoubleBracket = true )
269+ bool useDoubleBracket = true ,
270+ StringComparison comparison = StringComparison . Ordinal )
269271 {
270272 if ( value is null )
271273 {
@@ -284,8 +286,8 @@ public static string SetStringFormatParameterTemplatePlaceholders(
284286 {
285287 var s = ( pair . Key . StartsWith ( "{{" , StringComparison . Ordinal ) &&
286288 pair . Key . EndsWith ( "}}" , StringComparison . Ordinal )
287- ? placeholders . Find ( x => string . Equals ( x , pair . Key , StringComparison . Ordinal ) )
288- : placeholders . Find ( x => string . Equals ( x , "{{" + pair . Key + "}}" , StringComparison . Ordinal ) ) ) ! ;
289+ ? placeholders . Find ( x => string . Equals ( x , pair . Key , comparison ) )
290+ : placeholders . Find ( x => string . Equals ( x , "{{" + pair . Key + "}}" , comparison ) ) ) ! ;
289291
290292 if ( ! string . IsNullOrEmpty ( s ) )
291293 {
@@ -297,10 +299,10 @@ public static string SetStringFormatParameterTemplatePlaceholders(
297299 {
298300 foreach ( var pair in replacements )
299301 {
300- var s = ( pair . Key . StartsWith ( "{" , StringComparison . Ordinal ) &&
301- pair . Key . EndsWith ( "}" , StringComparison . Ordinal )
302- ? placeholders . Find ( x => string . Equals ( x , pair . Key , StringComparison . Ordinal ) )
303- : placeholders . Find ( x => string . Equals ( x , "{" + pair . Key + "}" , StringComparison . Ordinal ) ) ) ! ;
302+ var s = ( pair . Key . StartsWith ( '{' ) &&
303+ pair . Key . EndsWith ( '}' )
304+ ? placeholders . Find ( x => string . Equals ( x , pair . Key , comparison ) )
305+ : placeholders . Find ( x => string . Equals ( x , '{' + pair . Key + '}' , comparison ) ) ) ! ;
304306
305307 if ( ! string . IsNullOrEmpty ( s ) )
306308 {
@@ -2022,6 +2024,7 @@ public static bool TryParseToHttpStatusCode(
20222024 /// <param name="arg7">Optional argument for placeholder replacement.</param>
20232025 /// <param name="arg8">Optional argument for placeholder replacement.</param>
20242026 /// <param name="arg9">Optional argument for placeholder replacement.</param>
2027+ /// <param name="comparison">Use comparison with default <see langword="StringComparison.OrdinalIgnoreCase" /> for key matching.</param>
20252028 /// <param name="arg0Name">The name of <paramref name="arg0"/>, provided via <see cref="CallerArgumentExpressionAttribute"/>.</param>
20262029 /// <param name="arg1Name">The name of <paramref name="arg1"/> (if provided), automatically inferred.</param>
20272030 /// <param name="arg2Name">The name of <paramref name="arg2"/> (if provided), automatically inferred.</param>
@@ -2047,6 +2050,8 @@ public static bool TryParseToHttpStatusCode(
20472050 /// - Named placeholders (e.g., '{argName}') and indexed placeholders (e.g., '{0}') are supported.<br/>
20482051 /// - Argument names are inferred using the <see cref="CallerArgumentExpressionAttribute"/> for better debugging.
20492052 /// </remarks>
2053+ [ SuppressMessage ( "Critical Code Smell" , "S3776:Cognitive Complexity of methods should not be too high" , Justification = "OK." ) ]
2054+ [ SuppressMessage ( "Design" , "MA0051:Method is too long" , Justification = "OK." ) ]
20502055 public static string FormatWith(
20512056 this string template ,
20522057 string arg0 ,
@@ -2059,6 +2064,7 @@ public static string FormatWith(
20592064 string ? arg7 = null ,
20602065 string ? arg8 = null ,
20612066 string ? arg9 = null ,
2067+ StringComparison comparison = StringComparison . OrdinalIgnoreCase ,
20622068 [ CallerArgumentExpression ( "arg0" ) ] string arg0Name = null ! ,
20632069 [ CallerArgumentExpression ( "arg1" ) ] string ? arg1Name = null ,
20642070 [ CallerArgumentExpression ( "arg2" ) ] string ? arg2Name = null ,
@@ -2092,6 +2098,23 @@ public static string FormatWith(
20922098 var key = placeholder . Trim ( '{' , '}' ) ;
20932099
20942100 var index = Array . IndexOf ( argNames , key ) ;
2101+ if ( index == - 1 )
2102+ {
2103+ for ( var i = 0 ; i < argNames . Length ; i ++ )
2104+ {
2105+ if ( argNames [ i ] == null )
2106+ {
2107+ break ;
2108+ }
2109+
2110+ if ( argNames [ i ] ! . Contains ( '.' , StringComparison . Ordinal ) &&
2111+ argNames [ i ] ! . EndsWith ( key , comparison ) )
2112+ {
2113+ index = i ;
2114+ }
2115+ }
2116+ }
2117+
20952118 if ( index != - 1 && index < usedArgsCount )
20962119 {
20972120 dictionary . Add ( key , argValues [ index ] ! ) ;
@@ -2111,7 +2134,10 @@ public static string FormatWith(
21112134 }
21122135 }
21132136
2114- return template. SetStringFormatParameterTemplatePlaceholders ( dictionary , useDoubleBracket : false ) ;
2137+ return template . SetStringFormatParameterTemplatePlaceholders (
2138+ dictionary ,
2139+ useDoubleBracket : false ,
2140+ comparison : StringComparison . OrdinalIgnoreCase ) ;
21152141 }
21162142#endif
21172143
0 commit comments