Skip to content

Commit

Permalink
Use SearchValues<char>
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Nov 27, 2023
1 parent af28a3b commit d9bc7ad
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
dotnet-version: |
6.0.x
8.0.x
- name: Restore
run: dotnet restore src/RazorBlade.sln
Expand Down
15 changes: 13 additions & 2 deletions src/RazorBlade.Library/HtmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ namespace RazorBlade;
[SuppressMessage("ReSharper", "MemberCanBeMadeStatic.Global")]
public sealed class HtmlHelper
{
#if NET8_0_OR_GREATER
private static readonly System.Buffers.SearchValues<char> _charsToEscape = System.Buffers.SearchValues.Create("&<>\"\'");
#elif NET6_0_OR_GREATER
private const string _charsToEscape = "&<>\"\'";
#endif

internal static HtmlHelper Instance { get; } = new();

/// <summary>
Expand All @@ -34,14 +40,16 @@ public string Encode(object? value)

#if NET6_0_OR_GREATER
var valueSpan = valueString.AsSpan();
var sb = new StringBuilder();
var sb = default(StringBuilder);

while (true)
{
var idx = valueSpan.IndexOfAny("&<>\"\'");
var idx = valueSpan.IndexOfAny(_charsToEscape);
if (idx < 0)
break;

sb ??= new StringBuilder();

if (idx != 0)
sb.Append(valueSpan[..idx]);

Expand All @@ -58,6 +66,9 @@ public string Encode(object? value)
valueSpan = valueSpan[(idx + 1)..];
}

if (sb is null)
return valueString;

if (valueSpan.Length != 0)
sb.Append(valueSpan);

Expand Down
8 changes: 7 additions & 1 deletion src/RazorBlade.Library/HtmlTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ namespace RazorBlade;
/// </remarks>
public abstract class HtmlTemplate : RazorTemplate
{
#if NET8_0_OR_GREATER
private static readonly System.Buffers.SearchValues<char> _charsToEscape = System.Buffers.SearchValues.Create("&<>\"\'");
#elif NET6_0_OR_GREATER
private const string _charsToEscape = "&<>\"\'";
#endif

private AttributeInfo _currentAttribute;

// ReSharper disable once RedundantDisableWarningComment
Expand Down Expand Up @@ -47,7 +53,7 @@ protected internal override void Write(object? value)

while (true)
{
var idx = valueSpan.IndexOfAny("&<>\"\'");
var idx = valueSpan.IndexOfAny(_charsToEscape);
if (idx < 0)
break;

Expand Down
2 changes: 1 addition & 1 deletion src/RazorBlade.Library/RazorBlade.Library.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net8.0;net6.0;netstandard2.0</TargetFrameworks>
<RootNamespace>RazorBlade</RootNamespace>
<LangVersion>10.0</LangVersion>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/RazorBlade.Tests/RazorBlade.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0;net48</TargetFrameworks>
<TargetFrameworks>net8.0;net6.0;net48</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/RazorBlade/RazorBlade.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net8.0;net6.0;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DebugType>embedded</DebugType>
</PropertyGroup>
Expand All @@ -16,7 +16,7 @@
<ProjectReference Include="../RazorBlade.Library/RazorBlade.Library.csproj" ReferenceOutputAssembly="false" />
<Compile Include="../RazorBlade.Library/**/*.cs" Exclude="../RazorBlade.Library/obj/**/*.cs" Visible="false" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../RazorBlade.Analyzers/RazorBlade.Analyzers.csproj" ReferenceOutputAssembly="false" />
<None Include="../RazorBlade.Analyzers/bin/$(Configuration)/netstandard2.0/RazorBlade.Analyzers.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
Expand Down

0 comments on commit d9bc7ad

Please sign in to comment.