Skip to content

Commit

Permalink
More efficient Utf8String filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 committed Oct 6, 2024
1 parent c5a5ec0 commit b44f421
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
48 changes: 45 additions & 3 deletions Il2CppInterop.Generator/Extensions/StringEx.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using AsmResolver;
Expand Down Expand Up @@ -52,7 +53,7 @@ public static string MakeValidInSource(this string str)
for (var i = 0; i < str.Length; i++)
{
var it = str[i];
if (char.IsDigit(it) || (it is >= 'a' and <= 'z') || (it is >= 'A' and <= 'Z') || it == '_' || it == '`')
if (IsValidInSource(it))
continue;

chars ??= str.ToCharArray();
Expand All @@ -63,12 +64,53 @@ public static string MakeValidInSource(this string str)
return char.IsDigit(result[0]) ? "_" + result : result;
}

private static bool IsValidInSource(char c) => char.IsDigit(c) || (c is >= 'a' and <= 'z') || (c is >= 'A' and <= 'Z') || c == '_' || c == '`';

public static Utf8String MakeValidInSource(this Utf8String? str)
{
if (Utf8String.IsNullOrEmpty(str))
return Utf8String.Empty;
var result = str.Value.MakeValidInSource();
return result == str ? str : result;

byte[]? rentedArray = null;
var data = str.GetBytesUnsafe();
for (var i = 0; i < data.Length; i++)
{
var it = data[i];
if (IsValidInSource((char)it))
continue;

if (rentedArray is null)
{
rentedArray ??= ArrayPool<byte>.Shared.Rent(data.Length + 1);
Array.Copy(data, 0, rentedArray, 1, data.Length);
rentedArray[0] = (byte)'_';
}
rentedArray[i + 1] = (byte)'_';
}

if (char.IsDigit((char)data[0]))
{
if (rentedArray is null)
{
rentedArray = ArrayPool<byte>.Shared.Rent(data.Length + 1);
Array.Copy(data, 0, rentedArray, 1, data.Length);
rentedArray[0] = (byte)'_';
}

var result = new Utf8String(rentedArray, 0, data.Length + 1);
ArrayPool<byte>.Shared.Return(rentedArray);
return result;
}
else if (rentedArray is not null)
{
var result = new Utf8String(rentedArray, 1, data.Length);
ArrayPool<byte>.Shared.Return(rentedArray);
return result;
}
else
{
return str;
}
}

public static bool IsInvalidInSource([NotNullWhen(true)] this string? str)
Expand Down
3 changes: 3 additions & 0 deletions Il2CppInterop.Generator/Il2CppInterop.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

<ItemGroup>
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.1" />
<PackageReference Include="MonoMod.Backports" Version="1.1.2">
<Aliases>MonoModBackports</Aliases><!-- Transitive dependency from AsmResolver. Extern alias prevents it from affecting us. -->
</PackageReference>
<PackageReference Include="PolySharp" Version="1.14.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit b44f421

Please sign in to comment.