diff --git a/src/WCharT.Net.Tests/Tests.cs b/src/WCharT.Net.Tests/Tests.cs index ed4b3d0..5ddfd92 100644 --- a/src/WCharT.Net.Tests/Tests.cs +++ b/src/WCharT.Net.Tests/Tests.cs @@ -1,5 +1,4 @@ using System.Runtime.InteropServices; -using System.Text; using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -17,6 +16,17 @@ public void TestEncoding() str.GetString().Should().Be(text); } + [TestMethod] + public void BufferSizeIsRetained() + { + var bufferSize = 3; + var str = new WCharTString(bufferSize); + + var result = str.GetString(); + result.Should().Be(new string(new[] { char.MinValue, char.MinValue, char.MinValue })); + result.Length.Should().Be(bufferSize); + } + [TestMethod] public void TestUnicode() { diff --git a/src/WCharT.Net/Platforms/Unix.cs b/src/WCharT.Net/Platforms/Unix.cs index 587c8f6..7fbd577 100644 --- a/src/WCharT.Net/Platforms/Unix.cs +++ b/src/WCharT.Net/Platforms/Unix.cs @@ -7,7 +7,7 @@ internal static class Unix public static ReadOnlySpan CreateData(int chars) { var length = chars * sizeof(uint); - var data = new byte[length + sizeof(uint)]; //Null terminated + var data = new byte[length]; return new ReadOnlySpan(data, 0, length); } diff --git a/src/WCharT.Net/Platforms/Windows.cs b/src/WCharT.Net/Platforms/Windows.cs index df615c7..efbbdc2 100644 --- a/src/WCharT.Net/Platforms/Windows.cs +++ b/src/WCharT.Net/Platforms/Windows.cs @@ -7,7 +7,7 @@ internal static class Windows public static ReadOnlySpan CreateData(int chars) { var length = chars * sizeof(ushort); - var data = new byte[length + sizeof(ushort)]; //Null terminated + var data = new byte[length]; return new ReadOnlySpan(data, 0, length); } diff --git a/src/WCharT.Net/WCharTString.cs b/src/WCharT.Net/WCharTString.cs index 23a2b49..b292b25 100644 --- a/src/WCharT.Net/WCharTString.cs +++ b/src/WCharT.Net/WCharTString.cs @@ -28,6 +28,7 @@ public unsafe WCharTString(byte* p) /// Creates an instance from a given string to be able to pass on the data later. /// /// A string which should be encoded into a wchar_t string. + /// The internal representation of the string is null terminated. Meaning it is possible to simply pass the pointer of the to native code without any length information. At the same time the length parameter of the does not include the null termination character. Meaning if an API requires the size of the data in bytes without the null termination character the length value of the can be used. public WCharTString(string str) { data = Platform.CreateData(str); @@ -37,6 +38,7 @@ public WCharTString(string str) /// Creates an instance for a given amount of wchar_t characters. /// /// The number of wchar_t characters which should be able to put into the instance. + /// This does not automatically add a character in case a null termination character is needed. public WCharTString(int length) { data = Platform.CreateData(length); @@ -46,6 +48,7 @@ public WCharTString(int length) /// Reads the current data and returns the contained string. /// /// The decoded wchar_t string from the data. + /// The string has the size of the internal data block which can include null termination characters. If not needed those can be trimmed. public string GetString() { return Platform.GetString(data);