Skip to content

Commit

Permalink
Encode long strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Apr 16, 2024
1 parent 0572011 commit 4e2d5a3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ArtemisNetCoreClient/ArtemisBinaryConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ public static int WriteString(ref byte destination, string value)
}
else
{
offset += WriteInt32(ref destination.GetOffset(offset), value.Length << 1);
foreach (var c in value)
{
// Low byte
offset += WriteByte(ref destination.GetOffset(offset), (byte) (c & 0xFF));

// High byte
offset += WriteByte(ref destination.GetOffset(offset), (byte) ((c >> 8) & 0xFF));
}
}

return offset;
Expand Down
17 changes: 17 additions & 0 deletions test/ArtemisNetCoreClient.Tests/ArtemisBinaryConverterSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,21 @@ public void should_decode_medium_string()
Assert.Equal("abcdefghijkl", value);
Assert.Equal(byteBuffer.Length, readBytes);
}

[Fact]
public void should_encode_long_string()
{
// Arrange
var str = Enumerable.Repeat(0, 513).Aggregate("", (s, _) => s + "abcdefgh");
var byteCount = ArtemisBinaryConverter.GetStringByteCount(str);
var byteBuffer = new byte[byteCount];

// Act
var writtenBytes = ArtemisBinaryConverter.WriteString(ref byteBuffer.AsSpan().GetReference(), str);

// Assert
var expected = File.ReadAllText("long_encoded_string.txt").Split(",").Select(byte.Parse).ToArray();
Assert.Equal(expected, byteBuffer);
Assert.Equal(byteCount, writtenBytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@
<ProjectReference Include="..\..\src\ArtemisNetCoreClient\ArtemisNetCoreClient.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="long_encoded_string.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions test/ArtemisNetCoreClient.Tests/long_encoded_string.txt

Large diffs are not rendered by default.

0 comments on commit 4e2d5a3

Please sign in to comment.