Skip to content

Commit 77d62f9

Browse files
perf: use ContainsAnyExceptInRange to calculate width
1 parent b3b3acb commit 77d62f9

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Src/CSharpier.Core/Utilities/StringExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Runtime.CompilerServices;
2+
13
namespace CSharpier.Core.Utilities;
24

35
internal static class StringExtensions
@@ -35,7 +37,20 @@ public static int IndexOf(this string value, char otherValue)
3537
#endif
3638

3739
// some unicode characters should be considered size of 2 when calculating how big this string will be when printed
40+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3841
public static int GetPrintedWidth(this string value)
42+
{
43+
#if NET8_0_OR_GREATER
44+
if (! MemoryExtensions.ContainsAnyExceptInRange(value, (char)0, (char)0x10FF))
45+
{
46+
return value.Length;
47+
}
48+
#endif
49+
50+
return CalculateActualWidth(value);
51+
}
52+
53+
private static int CalculateActualWidth(string value)
3954
{
4055
var sum = 0;
4156
// ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator

0 commit comments

Comments
 (0)