Skip to content

Commit 1236cb1

Browse files
committed
Oops, fixed a bug in reading strings that contain null terminators.
1 parent 10626dc commit 1236cb1

4 files changed

Lines changed: 24 additions & 5 deletions

File tree

Schema Tests/binary/reader/StringAsciiTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ public void TestString(string str) {
3535
Assert.AreEqual(str.Length, ms.Position);
3636
}
3737

38+
[Test]
39+
[TestCase("foobar\0e", ExpectedResult = "foobar")]
40+
public string TestStringWithNullTerminator(string str) {
41+
using var ms = new MemoryStream();
42+
using var sw = new StreamWriter(ms);
43+
sw.Write(str);
44+
sw.Flush();
45+
ms.Position = 0;
46+
47+
using var br = new SchemaBinaryReader(ms);
48+
return br.ReadString(str.Length);
49+
}
50+
3851
[Test]
3952
public void TestReadNT() {
4053
var str = "string 1\0string 2\0string 3";

Schema/Schema.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<Description>Library for converting classes to and from binary. Provides a C# Roslyn generator that automatically implements conversion logic for simple classes.</Description>
1515
<PackageId>schema</PackageId>
1616
<Title>schema</Title>
17-
<Version>0.6.18</Version>
17+
<Version>0.6.19</Version>
1818
<Authors>MeltyPlayer</Authors>
1919
</PropertyGroup>
2020

Schema/src/binary/reader/SchemaBinaryReader_Strings.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using schema.binary.attributes;
66
using schema.text.reader;
7+
using schema.util.strings;
78

89

910
namespace schema.binary;
@@ -263,7 +264,7 @@ public string ReadLine(Encoding encoding)
263264
[MethodImpl(MethodImplOptions.AggressiveInlining)]
264265
public void AssertString(string expectedValue)
265266
=> SchemaBinaryReader.AssertStrings_(
266-
expectedValue.AsSpan().TrimEnd('\0'),
267+
expectedValue.TrimNt(),
267268
this.ReadString(expectedValue.Length).AsSpan());
268269

269270
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -275,14 +276,14 @@ public void AssertString(StringEncodingType encodingType,
275276
[MethodImpl(MethodImplOptions.AggressiveInlining)]
276277
public void AssertString(Encoding encoding, string expectedValue)
277278
=> SchemaBinaryReader.AssertStrings_(
278-
expectedValue.AsSpan().TrimEnd('\0'),
279+
expectedValue.TrimNt(),
279280
this.ReadString(encoding, expectedValue.Length).AsSpan());
280281

281282
[MethodImpl(MethodImplOptions.AggressiveInlining)]
282283
public string ReadString(long count) {
283284
Span<char> buffer = stackalloc char[(int) count];
284285
this.ReadChars(buffer);
285-
return ((ReadOnlySpan<char>) buffer).TrimEnd('\0').ToString();
286+
return buffer.TrimNt().ToString();
286287
}
287288

288289
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -293,7 +294,7 @@ public string ReadString(StringEncodingType encodingType, long count)
293294
public string ReadString(Encoding encoding, long count) {
294295
Span<char> buffer = stackalloc char[(int) count];
295296
this.ReadChars(encoding, buffer);
296-
return ((ReadOnlySpan<char>) buffer).TrimEnd('\0').ToString();
297+
return buffer.TrimNt().ToString();
297298
}
298299

299300

Schema/src/util/strings/StringExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
namespace schema.util.strings;
88

99
public static class StringExtensions {
10+
public static ReadOnlySpan<char> TrimNt(this ReadOnlySpan<char> text) {
11+
var ntIndex = text.IndexOf('\0');
12+
return ntIndex != -1 ? text.Slice(0, ntIndex) : text;
13+
}
14+
1015
public static int IndexOfFirst(
1116
this string text,
1217
ReadOnlySpan<char> chars,

0 commit comments

Comments
 (0)