Skip to content

Commit 52daca7

Browse files
committed
Clean up some MVC code
1 parent 2458822 commit 52daca7

File tree

9 files changed

+31
-32
lines changed

9 files changed

+31
-32
lines changed

src/Mvc/Mvc.Core/src/Controllers/ControllerBinderDelegateProvider.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.Linq;
78
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Mvc.Abstractions;
810
using Microsoft.AspNetCore.Mvc.ModelBinding;
911

1012
namespace Microsoft.AspNetCore.Mvc.Controllers
@@ -55,6 +57,9 @@ internal static class ControllerBinderDelegateProvider
5557
return null;
5658
}
5759

60+
var parameters = actionDescriptor.Parameters as List<ParameterDescriptor> ?? actionDescriptor.Parameters.ToList();
61+
var properties = actionDescriptor.BoundProperties as List<ParameterDescriptor> ?? actionDescriptor.BoundProperties.ToList();
62+
5863
return Bind;
5964

6065
async Task Bind(ControllerContext controllerContext, object controller, Dictionary<string, object?> arguments)
@@ -67,9 +72,8 @@ async Task Bind(ControllerContext controllerContext, object controller, Dictiona
6772

6873
Debug.Assert(valueProvider is not null);
6974

70-
var parameters = actionDescriptor.Parameters;
71-
72-
for (var i = 0; i < parameters.Count; i++)
75+
var parameterCount = parameters.Count;
76+
for (var i = 0; i < parameterCount; i++)
7377
{
7478
var parameter = parameters[i];
7579
var bindingInfo = parameterBindingInfo![i];
@@ -95,8 +99,8 @@ async Task Bind(ControllerContext controllerContext, object controller, Dictiona
9599
}
96100
}
97101

98-
var properties = actionDescriptor.BoundProperties;
99-
for (var i = 0; i < properties.Count; i++)
102+
var propertyCount = properties.Count;
103+
for (var i = 0; i < propertyCount; i++)
100104
{
101105
var property = properties[i];
102106
var bindingInfo = propertyBindingInfo![i];

src/Mvc/Mvc.Razor/src/TagHelpers/UrlResolutionTagHelper.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,7 @@ private static string CreateTrimmedString(string input, int start)
343343

344344
private static bool IsCharWhitespace(char ch)
345345
{
346-
for (var i = 0; i < ValidAttributeWhitespaceChars.Length; i++)
347-
{
348-
if (ValidAttributeWhitespaceChars[i] == ch)
349-
{
350-
return true;
351-
}
352-
}
353-
// the character is not white space
354-
return false;
346+
return ValidAttributeWhitespaceChars.AsSpan().IndexOf(ch) != -1;
355347
}
356348

357349
private class EncodeFirstSegmentContent : IHtmlContent

src/Mvc/Mvc.TagHelpers/src/AttributeMatcher.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using Microsoft.AspNetCore.Razor.TagHelpers;
76

87
namespace Microsoft.AspNetCore.Mvc.TagHelpers
@@ -24,7 +23,7 @@ internal static class AttributeMatcher
2423
/// <returns><c>true</c> if a mode was determined, otherwise <c>false</c>.</returns>
2524
public static bool TryDetermineMode<TMode>(
2625
TagHelperContext context,
27-
IReadOnlyList<ModeAttributes<TMode>> modeInfos,
26+
ModeAttributes<TMode>[] modeInfos,
2827
Func<TMode, TMode, int> compare,
2928
out TMode result)
3029
{
@@ -47,13 +46,11 @@ public static bool TryDetermineMode<TMode>(
4746
result = default;
4847

4948
// Perf: Avoid allocating enumerator
50-
var modeInfosCount = modeInfos.Count;
5149
var allAttributes = context.AllAttributes;
5250
// Read interface .Count once rather than per iteration
5351
var allAttributesCount = allAttributes.Count;
54-
for (var i = 0; i < modeInfosCount; i++)
52+
foreach (var modeInfo in modeInfos.AsSpan())
5553
{
56-
var modeInfo = modeInfos[i];
5754
var requiredAttributes = modeInfo.Attributes;
5855
// If there are fewer attributes present than required, one or more of them must be missing.
5956
if (allAttributesCount >= requiredAttributes.Length &&

src/Mvc/Mvc.ViewFeatures/src/DefaultDisplayTemplates.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper)
212212
var viewBufferScope = serviceProvider.GetRequiredService<IViewBufferScope>();
213213

214214
var content = new HtmlContentBuilder(modelExplorer.Metadata.Properties.Count);
215-
foreach (var propertyExplorer in modelExplorer.Properties)
215+
foreach (var propertyExplorer in modelExplorer.PropertiesInternal.AsSpan())
216216
{
217217
var propertyMetadata = propertyExplorer.Metadata;
218218
if (!ShouldShow(propertyExplorer, templateInfo))

src/Mvc/Mvc.ViewFeatures/src/DefaultEditorTemplates.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper)
255255
var viewBufferScope = serviceProvider.GetRequiredService<IViewBufferScope>();
256256

257257
var content = new HtmlContentBuilder(modelExplorer.Metadata.Properties.Count);
258-
foreach (var propertyExplorer in modelExplorer.Properties)
258+
foreach (var propertyExplorer in modelExplorer.PropertiesInternal.AsSpan())
259259
{
260260
var propertyMetadata = propertyExplorer.Metadata;
261261
if (!ShouldShow(propertyExplorer, templateInfo))
@@ -482,6 +482,14 @@ public override void Write(char[] buffer, int index, int count)
482482
}
483483
}
484484

485+
public override void Write(ReadOnlySpan<char> buffer)
486+
{
487+
if (!buffer.IsEmpty)
488+
{
489+
HasContent = true;
490+
}
491+
}
492+
485493
public override void Write(string value)
486494
{
487495
if (!string.IsNullOrEmpty(value))
@@ -540,7 +548,7 @@ public override void Encode(TextWriter output, string value, int startIndex, int
540548
return;
541549
}
542550

543-
output.Write(value.Substring(startIndex, characterCount));
551+
output.Write(value.AsSpan(startIndex, characterCount));
544552
}
545553

546554
public override unsafe int FindFirstCharacterToEncode(char* text, int textLength)

src/Mvc/Mvc.ViewFeatures/src/ModelExplorer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public Type ModelType
199199
/// </remarks>
200200
public IEnumerable<ModelExplorer> Properties => PropertiesInternal;
201201

202-
private ModelExplorer[] PropertiesInternal
202+
internal ModelExplorer[] PropertiesInternal
203203
{
204204
get
205205
{
@@ -472,4 +472,4 @@ private ModelExplorer CreateExplorerForProperty(
472472
return new ModelExplorer(_metadataProvider, this, propertyMetadata, modelAccessor);
473473
}
474474
}
475-
}
475+
}

src/Mvc/Mvc.ViewFeatures/src/ModelExplorerExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Diagnostics;
66
using System.Globalization;
7-
using System.Linq;
87

98
namespace Microsoft.AspNetCore.Mvc.ViewFeatures
109
{
@@ -68,12 +67,13 @@ public static string GetSimpleDisplayText(this ModelExplorer modelExplorer)
6867
return stringResult;
6968
}
7069

71-
var firstProperty = modelExplorer.Properties.FirstOrDefault();
72-
if (firstProperty == null)
70+
if (modelExplorer.PropertiesInternal.Length == 0)
7371
{
7472
return string.Empty;
7573
}
7674

75+
var firstProperty = modelExplorer.PropertiesInternal[0];
76+
7777
if (firstProperty.Model == null)
7878
{
7979
return firstProperty.Metadata.NullDisplayText;
@@ -82,4 +82,4 @@ public static string GetSimpleDisplayText(this ModelExplorer modelExplorer)
8282
return Convert.ToString(firstProperty.Model, CultureInfo.CurrentCulture);
8383
}
8484
}
85-
}
85+
}

src/Mvc/MvcNoDeps.slnf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"src\\Mvc\\test\\WebSites\\SimpleWebSite\\SimpleWebSite.csproj",
6868
"src\\Mvc\\test\\WebSites\\TagHelpersWebSite\\TagHelpersWebSite.csproj",
6969
"src\\Mvc\\test\\WebSites\\VersioningWebSite\\VersioningWebSite.csproj",
70-
"src\\Mvc\\test\\WebSites\\XmlFormattersWebSite\\XmlFormattersWebSite.csproj",
70+
"src\\Mvc\\test\\WebSites\\XmlFormattersWebSite\\XmlFormattersWebSite.csproj"
7171
]
7272
}
7373
}

src/Shared/ChunkingCookieManager/ChunkingCookieManager.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ private static int ParseChunksCount(string? value)
7070
{
7171
if (value != null && value.StartsWith(ChunkCountPrefix, StringComparison.Ordinal))
7272
{
73-
var chunksCountString = value.Substring(ChunkCountPrefix.Length);
74-
int chunksCount;
75-
if (int.TryParse(chunksCountString, NumberStyles.None, CultureInfo.InvariantCulture, out chunksCount))
73+
if (int.TryParse(value.AsSpan(ChunkCountPrefix.Length), NumberStyles.None, CultureInfo.InvariantCulture, out var chunksCount))
7674
{
7775
return chunksCount;
7876
}

0 commit comments

Comments
 (0)