Skip to content

Commit c204a86

Browse files
Improve string formatting
1 parent 59ff9e6 commit c204a86

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Diff for: src/AspNetCore/WebApi/src/Asp.Versioning.Mvc.ApiExplorer/ApiDescriptionExtensions.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,14 @@ public static bool TryUpdateRelativePathAndRemoveApiVersionParameter( this ApiDe
106106
return false;
107107
}
108108

109-
var token = '{' + parameter.Name + '}';
109+
Span<char> token = stackalloc char[parameter.Name.Length + 2];
110+
111+
token[0] = '{';
112+
token[^1] = '}';
113+
parameter.Name.AsSpan().CopyTo( token.Slice( 1, parameter.Name.Length ) );
114+
110115
var value = apiVersion.ToString( options.SubstitutionFormat, CultureInfo.InvariantCulture );
111-
var newRelativePath = relativePath.Replace( token, value, StringComparison.Ordinal );
116+
var newRelativePath = relativePath.Replace( token.ToString(), value, StringComparison.Ordinal );
112117

113118
if ( relativePath == newRelativePath )
114119
{

Diff for: src/AspNetCore/WebApi/src/Asp.Versioning.Mvc.ApiExplorer/ApiVersionParameterDescriptionContext.cs

+17-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Asp.Versioning.ApiExplorer;
99
using Microsoft.AspNetCore.Routing;
1010
using Microsoft.AspNetCore.Routing.Patterns;
1111
using Microsoft.AspNetCore.Routing.Template;
12+
using System.Runtime.CompilerServices;
1213
using static Asp.Versioning.ApiVersionParameterLocation;
1314
using static System.Linq.Enumerable;
1415
using static System.StringComparison;
@@ -304,7 +305,7 @@ routeInfo.Constraints is IEnumerable<IRouteConstraint> constraints &&
304305
continue;
305306
}
306307

307-
var token = $"{parameter.Name}:{constraintName}";
308+
var token = FormatToken( parameter.Name, constraintName );
308309

309310
parameterDescription.Name = parameter.Name;
310311
description.RelativePath = relativePath.Replace( token, parameter.Name, Ordinal );
@@ -375,7 +376,7 @@ routeInfo.Constraints is IEnumerable<IRouteConstraint> constraints &&
375376
},
376377
Source = BindingSource.Path,
377378
};
378-
var token = $"{parameter.Name}:{constraintName}";
379+
var token = FormatToken( parameter.Name!, constraintName! );
379380

380381
description.RelativePath = relativePath.Replace( token, parameter.Name, Ordinal );
381382
description.ParameterDescriptions.Insert( 0, result );
@@ -457,4 +458,18 @@ private static bool FirstParameterIsOptional(
457458

458459
return apiVersion == defaultApiVersion;
459460
}
461+
462+
[MethodImpl( MethodImplOptions.AggressiveInlining )]
463+
private static string FormatToken( ReadOnlySpan<char> parameterName, ReadOnlySpan<char> constraintName )
464+
{
465+
var left = parameterName.Length;
466+
var right = constraintName.Length;
467+
Span<char> token = stackalloc char[left + right + 1];
468+
469+
parameterName.CopyTo( token[..left] );
470+
token[left] = ':';
471+
constraintName.CopyTo( token.Slice( left + 1, right ) );
472+
473+
return token.ToString();
474+
}
460475
}

0 commit comments

Comments
 (0)