Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Text;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using static System.StringExtensions;

namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration;

Expand Down Expand Up @@ -508,7 +507,7 @@ public override string ReadToEnd()
return string.Empty;
}

var result = CreateString(_remainingLength, (_page, _chunkIndex, _charIndex), static (destination, state) =>
var result = string.Create(_remainingLength, (_page, _chunkIndex, _charIndex), static (destination, state) =>
{
var (page, chunkIndex, charIndex) = state;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected override string NormalizeAndEnsureValidPath(string path)
var needsSlash = Root[^1] is not '/' && normalizedPath[0] is not '/';
var length = Root.Length + normalizedPath.Length + (needsSlash ? 1 : 0);

return StringExtensions.CreateString(
return string.Create(
length,
state: (Root, normalizedPath, needsSlash),
static (span, state) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ internal ImmutableArray<RazorProjectItem> FindHierarchicalItems(string basePath,
{
pathMemory = pathMemory[..(index + 1)];

var itemPath = StringExtensions.CreateString(
var itemPath = string.Create(
length: pathMemory.Length + fileName.Length,
state: (pathMemory, fileName),
static (span, state) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Razor;
using static System.StringExtensions;

namespace Microsoft.CodeAnalysis.Razor.Logging;

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

// Create the final string.
return CreateString(state.Length, state, static (span, state) =>
return string.Create(state.Length, state, static (span, state) =>
{
Write(state.CategoryNamePart, ref span);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ public static bool EndsWith(this string text, char value)
#endif
}

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

/// <summary>
/// Creates a new string with a specific length and initializes it after creation by using the specified callback.
/// </summary>
/// <typeparam name="TState">
/// The type of the element to pass to <paramref name="action"/>.
/// </typeparam>
/// <param name="length">
/// The length of the string to create.
/// </param>
/// <param name="state">
/// The element to pass to <paramref name="action"/>.
/// </param>
/// <param name="action">
/// A callback to initialize the string
/// </param>
/// <returns>
/// The created string.
/// </returns>
/// <remarks>
/// The initial content of the destination span passed to <paramref name="action"/> is undefined.
/// Therefore, it is the delegate's responsibility to ensure that every element of the span is assigned.
/// Otherwise, the resulting string could contain random characters
/// </remarks>
public unsafe static string CreateString<TState>(int length, TState state, SpanAction<char, TState> action)
extension(string)
{
#if NET
return string.Create(length, (action, state), static (span, state) => state.action(span, state.state));
#else
ArgHelper.ThrowIfNegative(length);

if (length == 0)
/// <summary>
/// Creates a new string with a specific length and initializes it after creation by using the specified callback.
/// </summary>
/// <typeparam name="TState">
/// The type of the element to pass to <paramref name="action"/>.
/// </typeparam>
/// <param name="length">
/// The length of the string to create.
/// </param>
/// <param name="state">
/// The element to pass to <paramref name="action"/>.
/// </param>
/// <param name="action">
/// A callback to initialize the string
/// </param>
/// <returns>
/// The created string.
/// </returns>
/// <remarks>
/// The initial content of the destination span passed to <paramref name="action"/> is undefined.
/// Therefore, it is the delegate's responsibility to ensure that every element of the span is assigned.
/// Otherwise, the resulting string could contain random characters.
/// </remarks>
public unsafe static string Create<TState>(int length, TState state, SpanAction<char, TState> action)
{
return string.Empty;
}
ArgHelper.ThrowIfNegative(length);

var result = new string('\0', length);
if (length == 0)
{
return string.Empty;
}

fixed (char* ptr = result)
{
action(new Span<char>(ptr, length), state);
}
var result = new string('\0', length);

return result;
#endif
fixed (char* ptr = result)
{
action(new Span<char>(ptr, length), state);
}

return result;
}
}
#endif
}