Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 43 additions & 0 deletions Cesium.Runtime.Tests/StringFunctionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Text;

namespace Cesium.Runtime.Tests;

public unsafe class StringFunctionTests
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some Unicode tests, shall we?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do

{
[Theory]
[InlineData("Hello\0", 5)]
[InlineData("Goodbye\0", 7)]
[InlineData("Hello\0Goodbye\0", 5)]
[InlineData(" \0", 18)]
public void StrLen(string input, int expected)
{
// TODO: If you are rich enough to procure a 2-4+ GB RAM runner,
// please update this test to exercise the path where the string
// length exceeds int.MaxLength of bytes.
var bytes = Encoding.UTF8.GetBytes(input);
fixed (byte* str = bytes)
{
var actual = StringFunctions.StrLen(str);

Assert.Equal((nuint)expected, actual);
}
}

[Theory]
[InlineData("Hello\n", 5)]
[InlineData("Goodbye\n", 7)]
[InlineData("Hello\nGoodbye\n", 5)]
[InlineData(" \n", 18)]
public void StrChr(string input, int expectedOffset)
{
var needle = '\n';
var bytes = Encoding.UTF8.GetBytes(input);
fixed (byte* str = bytes)
{
var ptr = StringFunctions.StrChr(str, '\n');

Assert.Equal((byte)needle, *ptr);
Assert.Equal(expectedOffset, (int)(ptr - str));
}
}
}
4 changes: 4 additions & 0 deletions Cesium.Runtime/Cesium.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
<InternalsVisibleTo Include="Cesium.Runtime.Tests" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>

</Project>
42 changes: 14 additions & 28 deletions Cesium.Runtime/StringFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#if NETSTANDARD
using System;
using System.Text;
#else
using System.Collections.Specialized;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#endif

Expand All @@ -14,26 +16,8 @@ public unsafe static class StringFunctions
{
public static nuint StrLen(byte* str)
{
#if NETSTANDARD
if (str == null)
{
return 0;
}

Encoding encoding = Encoding.UTF8;
int byteLength = 0;
byte* search = str;
while (*search != '\0')
{
byteLength++;
search++;
}

int stringLength = encoding.GetCharCount(str, byteLength);
return (uint)stringLength;
#else
return (uint)(Marshal.PtrToStringUTF8((nint)str)?.Length ?? 0);
#endif
var offset = StrChr(str, 0);
return (nuint)(offset - str);
}
public static byte* StrCpy(byte* dest, byte* src)
{
Expand Down Expand Up @@ -186,19 +170,21 @@ public static int StrNCmp(byte* lhs, byte* rhs, nuint count)
}
public static byte* StrChr(byte* str, int ch)
{
if (str == null)
if (str != null)
{
return null;
}
int match;
nuint offset = 0;
byte c = (byte)ch;

while (*str != 0)
{
if (*str == ch)
// TODO: Copy IndexOfNullByte impl. from CoreLib in a distant future
var span = new Span<byte>(str, int.MaxValue);
while ((match = span.IndexOf(c)) < 0)
{
return str;
offset += int.MaxValue;
span = new Span<byte>(str + offset, int.MaxValue);
}

str++;
return str + ((uint)match + offset);
}

return null;
Expand Down