Browse Source

- Added `GetCharCountImpl` tests, fixed handling of empty strings

- Added ifdef to remove `Deconstruct` extension on everything except `NETSTANDARD2_0`
tags/v0.6.0
Martin Evans 2 years ago
parent
commit
b8f0eff080
3 changed files with 33 additions and 1 deletions
  1. +27
    -0
      LLama.Unittest/EncodingExtensionsTests.cs
  2. +3
    -0
      LLama/Extensions/EncodingExtensions.cs
  3. +3
    -1
      LLama/Extensions/KeyValuePairExtensions.cs

+ 27
- 0
LLama.Unittest/EncodingExtensionsTests.cs View File

@@ -16,6 +16,15 @@ namespace LLama.Unittest
Assert.True(chars[..count].SequenceEqual(str)); Assert.True(chars[..count].SequenceEqual(str));
} }


private static void GetCharCountTest(string str)
{
var bytes = Encoding.UTF8.GetBytes(str);

var count = EncodingExtensions.GetCharCountImpl(Encoding.UTF8, bytes);

Assert.Equal(str.Length, count);
}

[Fact] [Fact]
public void GetCharsEmptyString() public void GetCharsEmptyString()
{ {
@@ -33,5 +42,23 @@ namespace LLama.Unittest
{ {
GetCharsTest("猫坐在垫子上"); GetCharsTest("猫坐在垫子上");
} }

[Fact]
public void GetCharCountEmptyString()
{
GetCharCountTest("");
}

[Fact]
public void GetCharCountString()
{
GetCharCountTest("Hello World");
}

[Fact]
public void GetCharCountChineseString()
{
GetCharCountTest("猫坐在垫子上");
}
} }
} }

+ 3
- 0
LLama/Extensions/EncodingExtensions.cs View File

@@ -35,6 +35,9 @@ internal static class EncodingExtensions


internal static int GetCharCountImpl(Encoding encoding, ReadOnlySpan<byte> bytes) internal static int GetCharCountImpl(Encoding encoding, ReadOnlySpan<byte> bytes)
{ {
if (bytes.Length == 0)
return 0;

unsafe unsafe
{ {
fixed (byte* bytePtr = bytes) fixed (byte* bytePtr = bytes)


+ 3
- 1
LLama/Extensions/KeyValuePairExtensions.cs View File

@@ -5,8 +5,9 @@ namespace LLama.Extensions
/// <summary> /// <summary>
/// Extensions to the KeyValuePair struct /// Extensions to the KeyValuePair struct
/// </summary> /// </summary>
public static class KeyValuePairExtensions
internal static class KeyValuePairExtensions
{ {
#if NETSTANDARD2_0
/// <summary> /// <summary>
/// Deconstruct a KeyValuePair into it's constituent parts. /// Deconstruct a KeyValuePair into it's constituent parts.
/// </summary> /// </summary>
@@ -20,5 +21,6 @@ namespace LLama.Extensions
first = pair.Key; first = pair.Key;
second = pair.Value; second = pair.Value;
} }
#endif
} }
} }

Loading…
Cancel
Save