diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/CodeWriter.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/CodeWriter.cs index 1496438f940..7e647d2682a 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/CodeWriter.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/CodeWriter.cs @@ -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; @@ -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; diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultRazorProjectFileSystem.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultRazorProjectFileSystem.cs index 04b59fdd763..2c23011692c 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultRazorProjectFileSystem.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultRazorProjectFileSystem.cs @@ -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) => diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorProjectFileSystem.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorProjectFileSystem.cs index 67973d8e191..98c76b5427e 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorProjectFileSystem.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorProjectFileSystem.cs @@ -120,7 +120,7 @@ internal ImmutableArray 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) => diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Logging/LogMessageFormatter.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Logging/LogMessageFormatter.cs index bfa55365903..5881fbec322 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Logging/LogMessageFormatter.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Logging/LogMessageFormatter.cs @@ -5,7 +5,6 @@ using System.Diagnostics; using System.Runtime.CompilerServices; using Microsoft.AspNetCore.Razor; -using static System.StringExtensions; namespace Microsoft.CodeAnalysis.Razor.Logging; @@ -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); diff --git a/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/StringExtensions.cs b/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/StringExtensions.cs index 393a2626403..c51f0b3134f 100644 --- a/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/StringExtensions.cs +++ b/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/StringExtensions.cs @@ -555,6 +555,7 @@ public static bool EndsWith(this string text, char value) #endif } +#if !NET /// /// Encapsulates a method that receives a span of objects of type /// and a state object of type . @@ -573,49 +574,49 @@ public static bool EndsWith(this string text, char value) /// public delegate void SpanAction(Span span, TArg arg); - /// - /// Creates a new string with a specific length and initializes it after creation by using the specified callback. - /// - /// - /// The type of the element to pass to . - /// - /// - /// The length of the string to create. - /// - /// - /// The element to pass to . - /// - /// - /// A callback to initialize the string - /// - /// - /// The created string. - /// - /// - /// The initial content of the destination span passed to 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 - /// - public unsafe static string CreateString(int length, TState state, SpanAction 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) + /// + /// Creates a new string with a specific length and initializes it after creation by using the specified callback. + /// + /// + /// The type of the element to pass to . + /// + /// + /// The length of the string to create. + /// + /// + /// The element to pass to . + /// + /// + /// A callback to initialize the string + /// + /// + /// The created string. + /// + /// + /// The initial content of the destination span passed to 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. + /// + public unsafe static string Create(int length, TState state, SpanAction 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(ptr, length), state); - } + var result = new string('\0', length); - return result; -#endif + fixed (char* ptr = result) + { + action(new Span(ptr, length), state); + } + + return result; + } } +#endif }