Skip to content

Commit 645d959

Browse files
Convert CreateString helper to static extension method (#11929)
To improve discoverability, transform Razor's StringExtensions.CreateString(...) helper to a static extension method on string.
2 parents 00e9d0e + 8daab97 commit 645d959

File tree

5 files changed

+44
-45
lines changed

5 files changed

+44
-45
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/CodeWriter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using System.Text;
1212
using Microsoft.AspNetCore.Razor.PooledObjects;
1313
using Microsoft.CodeAnalysis.Text;
14-
using static System.StringExtensions;
1514

1615
namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration;
1716

@@ -508,7 +507,7 @@ public override string ReadToEnd()
508507
return string.Empty;
509508
}
510509

511-
var result = CreateString(_remainingLength, (_page, _chunkIndex, _charIndex), static (destination, state) =>
510+
var result = string.Create(_remainingLength, (_page, _chunkIndex, _charIndex), static (destination, state) =>
512511
{
513512
var (page, chunkIndex, charIndex) = state;
514513

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultRazorProjectFileSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ protected override string NormalizeAndEnsureValidPath(string path)
127127
var needsSlash = Root[^1] is not '/' && normalizedPath[0] is not '/';
128128
var length = Root.Length + normalizedPath.Length + (needsSlash ? 1 : 0);
129129

130-
return StringExtensions.CreateString(
130+
return string.Create(
131131
length,
132132
state: (Root, normalizedPath, needsSlash),
133133
static (span, state) =>

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorProjectFileSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ internal ImmutableArray<RazorProjectItem> FindHierarchicalItems(string basePath,
120120
{
121121
pathMemory = pathMemory[..(index + 1)];
122122

123-
var itemPath = StringExtensions.CreateString(
123+
var itemPath = string.Create(
124124
length: pathMemory.Length + fileName.Length,
125125
state: (pathMemory, fileName),
126126
static (span, state) =>

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Logging/LogMessageFormatter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Diagnostics;
66
using System.Runtime.CompilerServices;
77
using Microsoft.AspNetCore.Razor;
8-
using static System.StringExtensions;
98

109
namespace Microsoft.CodeAnalysis.Razor.Logging;
1110

@@ -23,7 +22,7 @@ public static string FormatMessage(string message, string categoryName, Exceptio
2322
ref messageLineRangeBuilder, ref exceptionLineRangeBuilder);
2423

2524
// Create the final string.
26-
return CreateString(state.Length, state, static (span, state) =>
25+
return string.Create(state.Length, state, static (span, state) =>
2726
{
2827
Write(state.CategoryNamePart, ref span);
2928

src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/StringExtensions.cs

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ public static bool EndsWith(this string text, char value)
555555
#endif
556556
}
557557

558+
#if !NET
558559
/// <summary>
559560
/// Encapsulates a method that receives a span of objects of type <typeparamref name="T"/>
560561
/// and a state object of type <typeparamref name="TArg"/>.
@@ -573,49 +574,49 @@ public static bool EndsWith(this string text, char value)
573574
/// </param>
574575
public delegate void SpanAction<T, in TArg>(Span<T> span, TArg arg);
575576

576-
/// <summary>
577-
/// Creates a new string with a specific length and initializes it after creation by using the specified callback.
578-
/// </summary>
579-
/// <typeparam name="TState">
580-
/// The type of the element to pass to <paramref name="action"/>.
581-
/// </typeparam>
582-
/// <param name="length">
583-
/// The length of the string to create.
584-
/// </param>
585-
/// <param name="state">
586-
/// The element to pass to <paramref name="action"/>.
587-
/// </param>
588-
/// <param name="action">
589-
/// A callback to initialize the string
590-
/// </param>
591-
/// <returns>
592-
/// The created string.
593-
/// </returns>
594-
/// <remarks>
595-
/// The initial content of the destination span passed to <paramref name="action"/> is undefined.
596-
/// Therefore, it is the delegate's responsibility to ensure that every element of the span is assigned.
597-
/// Otherwise, the resulting string could contain random characters
598-
/// </remarks>
599-
public unsafe static string CreateString<TState>(int length, TState state, SpanAction<char, TState> action)
577+
extension(string)
600578
{
601-
#if NET
602-
return string.Create(length, (action, state), static (span, state) => state.action(span, state.state));
603-
#else
604-
ArgHelper.ThrowIfNegative(length);
605-
606-
if (length == 0)
579+
/// <summary>
580+
/// Creates a new string with a specific length and initializes it after creation by using the specified callback.
581+
/// </summary>
582+
/// <typeparam name="TState">
583+
/// The type of the element to pass to <paramref name="action"/>.
584+
/// </typeparam>
585+
/// <param name="length">
586+
/// The length of the string to create.
587+
/// </param>
588+
/// <param name="state">
589+
/// The element to pass to <paramref name="action"/>.
590+
/// </param>
591+
/// <param name="action">
592+
/// A callback to initialize the string
593+
/// </param>
594+
/// <returns>
595+
/// The created string.
596+
/// </returns>
597+
/// <remarks>
598+
/// The initial content of the destination span passed to <paramref name="action"/> is undefined.
599+
/// Therefore, it is the delegate's responsibility to ensure that every element of the span is assigned.
600+
/// Otherwise, the resulting string could contain random characters.
601+
/// </remarks>
602+
public unsafe static string Create<TState>(int length, TState state, SpanAction<char, TState> action)
607603
{
608-
return string.Empty;
609-
}
604+
ArgHelper.ThrowIfNegative(length);
610605

611-
var result = new string('\0', length);
606+
if (length == 0)
607+
{
608+
return string.Empty;
609+
}
612610

613-
fixed (char* ptr = result)
614-
{
615-
action(new Span<char>(ptr, length), state);
616-
}
611+
var result = new string('\0', length);
617612

618-
return result;
619-
#endif
613+
fixed (char* ptr = result)
614+
{
615+
action(new Span<char>(ptr, length), state);
616+
}
617+
618+
return result;
619+
}
620620
}
621+
#endif
621622
}

0 commit comments

Comments
 (0)