diff --git a/Projects/Server/Buffers/ValueStringBuilder.cs b/Projects/Server/Buffers/ValueStringBuilder.cs index 3196ca45e..b6d4ab2f1 100644 --- a/Projects/Server/Buffers/ValueStringBuilder.cs +++ b/Projects/Server/Buffers/ValueStringBuilder.cs @@ -309,7 +309,7 @@ public ref struct ValueStringBuilder } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Append(ReadOnlySpan value) + public void Append(scoped ReadOnlySpan value) { int pos = _length; if (pos > _chars.Length - value.Length) diff --git a/Projects/Server/Text/TextEncoding.cs b/Projects/Server/Text/TextEncoding.cs index bf8051d71..77cc707c1 100644 --- a/Projects/Server/Text/TextEncoding.cs +++ b/Projects/Server/Text/TextEncoding.cs @@ -1,6 +1,6 @@ /************************************************************************* * ModernUO * - * Copyright 2019-2023 - ModernUO Development Team * + * Copyright 2019-2025 - ModernUO Development Team * * Email: hi@modernuo.com * * File: TextEncoding.cs * * * @@ -16,6 +16,7 @@ using System; using System.Runtime.CompilerServices; using System.Text; +using Server.Buffers; namespace Server.Text; @@ -114,33 +115,55 @@ public static class TextEncoding _ => 1 }; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static bool IsSafeChar(ushort c) => c is >= 0x20 and < 0xFFFE; - public static string GetString(ReadOnlySpan span, Encoding encoding, bool safeString = false) { - string s = encoding.GetString(span); - if (!safeString) { - return s; + return encoding.GetString(span); } - ReadOnlySpan chars = s.AsSpan(); + var charCount = encoding.GetMaxCharCount(span.Length); - using var sb = new ValueStringBuilder(stackalloc char[256]); - var hasDoneAnyReplacements = false; + char[] rentedChars = null; + Span chars = charCount <= 256 + ? stackalloc char[charCount] + : rentedChars = STArrayPool.Shared.Rent(charCount); - for (int i = 0, last = 0; i < chars.Length; i++) + try { - if (!IsSafeChar(chars[i]) && i == chars.Length - 1) + var length = encoding.GetChars(span, chars); + chars = chars[..length]; + + var index = chars.IndexOfAnyExceptInRange((char)0x20, (char)0xFFFD); + if (index == -1) { - hasDoneAnyReplacements = true; - sb.Append(chars.Slice(last, i - last)); - last = i + 1; // Skip the unsafe char + return new string(chars); + } + + using var sb = charCount <= 256 + ? new ValueStringBuilder(stackalloc char[charCount]) + : ValueStringBuilder.Create(charCount); + + while (index != -1) + { + sb.Append(chars[..index]); + chars = chars[(index + 1)..]; + index = chars.IndexOfAnyExceptInRange((char)0x20, (char)0xFFFD); + } + + if (chars.Length > 0) + { + sb.Append(chars); + } + + return sb.ToString(); + } + finally + { + if (rentedChars != null) + { + STArrayPool.Shared.Return(rentedChars); } } - - return !hasDoneAnyReplacements ? s : sb.ToString(); } }