Skip to content

Commit 3816fed

Browse files
committed
refactor: Use explicit length when slicing constant destination
1 parent e13f310 commit 3816fed

12 files changed

+81
-81
lines changed

src/Darp.BinaryObjects.Generator/BinaryMemberInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ out int bytesWritten
7575
var methodName = BinaryObjectsGenerator.GetWriteMethodName(CollectionKind, TypeKind, isLittleEndian);
7676
var optionalCast = BinaryObjectsGenerator.GetOptionalCastToUnderlyingEnumValue(TypeSymbol);
7777
writeString =
78-
$"global::Darp.BinaryObjects.Generated.Utilities.{methodName}(destination[{currentByteIndex}..], {optionalCast}this.{MemberSymbol.Name});";
78+
$"global::Darp.BinaryObjects.Generated.Utilities.{methodName}(destination[{currentByteIndex}..{currentByteIndex + ConstantByteLength}], {optionalCast}this.{MemberSymbol.Name});";
7979
bytesWritten = ConstantByteLength;
8080
return true;
8181
}

test/Darp.BinaryObjects.Generator.Tests/Snapshots/IntegrationTest.AllPrimitives_DefaultAsync#BinaryObjectsGenerator.g.verified.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ public bool TryWriteLittleEndian(global::System.Span<byte> destination, out int
4040

4141
if (destination.Length < 77)
4242
return false;
43-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.ValueBool);
44-
global::Darp.BinaryObjects.Generated.Utilities.WriteInt8(destination[1..], this.ValueSByte);
45-
global::Darp.BinaryObjects.Generated.Utilities.WriteInt16LittleEndian(destination[2..], this.ValueShort);
46-
global::Darp.BinaryObjects.Generated.Utilities.WriteHalfLittleEndian(destination[4..], this.ValueHalf);
47-
global::Darp.BinaryObjects.Generated.Utilities.WriteInt32LittleEndian(destination[6..], this.ValueInt);
48-
global::Darp.BinaryObjects.Generated.Utilities.WriteSingleLittleEndian(destination[10..], this.ValueFloat);
49-
global::Darp.BinaryObjects.Generated.Utilities.WriteInt64LittleEndian(destination[14..], this.ValueLong);
50-
global::Darp.BinaryObjects.Generated.Utilities.WriteInt128LittleEndian(destination[22..], this.ValueInt128);
51-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt128LittleEndian(destination[38..], this.ValueUInt128);
52-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt64LittleEndian(destination[54..], this.ValueULong);
53-
global::Darp.BinaryObjects.Generated.Utilities.WriteDoubleLittleEndian(destination[62..], this.ValueDouble);
54-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt32LittleEndian(destination[70..], this.ValueUInt);
55-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt16LittleEndian(destination[74..], this.ValueUShort);
56-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[76..], this.ValueByte);
43+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.ValueBool);
44+
global::Darp.BinaryObjects.Generated.Utilities.WriteInt8(destination[1..2], this.ValueSByte);
45+
global::Darp.BinaryObjects.Generated.Utilities.WriteInt16LittleEndian(destination[2..4], this.ValueShort);
46+
global::Darp.BinaryObjects.Generated.Utilities.WriteHalfLittleEndian(destination[4..6], this.ValueHalf);
47+
global::Darp.BinaryObjects.Generated.Utilities.WriteInt32LittleEndian(destination[6..10], this.ValueInt);
48+
global::Darp.BinaryObjects.Generated.Utilities.WriteSingleLittleEndian(destination[10..14], this.ValueFloat);
49+
global::Darp.BinaryObjects.Generated.Utilities.WriteInt64LittleEndian(destination[14..22], this.ValueLong);
50+
global::Darp.BinaryObjects.Generated.Utilities.WriteInt128LittleEndian(destination[22..38], this.ValueInt128);
51+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt128LittleEndian(destination[38..54], this.ValueUInt128);
52+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt64LittleEndian(destination[54..62], this.ValueULong);
53+
global::Darp.BinaryObjects.Generated.Utilities.WriteDoubleLittleEndian(destination[62..70], this.ValueDouble);
54+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt32LittleEndian(destination[70..74], this.ValueUInt);
55+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt16LittleEndian(destination[74..76], this.ValueUShort);
56+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[76..77], this.ValueByte);
5757
bytesWritten += 77;
5858

5959
return true;
@@ -69,20 +69,20 @@ public bool TryWriteBigEndian(global::System.Span<byte> destination, out int byt
6969

7070
if (destination.Length < 77)
7171
return false;
72-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.ValueBool);
73-
global::Darp.BinaryObjects.Generated.Utilities.WriteInt8(destination[1..], this.ValueSByte);
74-
global::Darp.BinaryObjects.Generated.Utilities.WriteInt16BigEndian(destination[2..], this.ValueShort);
75-
global::Darp.BinaryObjects.Generated.Utilities.WriteHalfBigEndian(destination[4..], this.ValueHalf);
76-
global::Darp.BinaryObjects.Generated.Utilities.WriteInt32BigEndian(destination[6..], this.ValueInt);
77-
global::Darp.BinaryObjects.Generated.Utilities.WriteSingleBigEndian(destination[10..], this.ValueFloat);
78-
global::Darp.BinaryObjects.Generated.Utilities.WriteInt64BigEndian(destination[14..], this.ValueLong);
79-
global::Darp.BinaryObjects.Generated.Utilities.WriteInt128BigEndian(destination[22..], this.ValueInt128);
80-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt128BigEndian(destination[38..], this.ValueUInt128);
81-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt64BigEndian(destination[54..], this.ValueULong);
82-
global::Darp.BinaryObjects.Generated.Utilities.WriteDoubleBigEndian(destination[62..], this.ValueDouble);
83-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt32BigEndian(destination[70..], this.ValueUInt);
84-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt16BigEndian(destination[74..], this.ValueUShort);
85-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[76..], this.ValueByte);
72+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.ValueBool);
73+
global::Darp.BinaryObjects.Generated.Utilities.WriteInt8(destination[1..2], this.ValueSByte);
74+
global::Darp.BinaryObjects.Generated.Utilities.WriteInt16BigEndian(destination[2..4], this.ValueShort);
75+
global::Darp.BinaryObjects.Generated.Utilities.WriteHalfBigEndian(destination[4..6], this.ValueHalf);
76+
global::Darp.BinaryObjects.Generated.Utilities.WriteInt32BigEndian(destination[6..10], this.ValueInt);
77+
global::Darp.BinaryObjects.Generated.Utilities.WriteSingleBigEndian(destination[10..14], this.ValueFloat);
78+
global::Darp.BinaryObjects.Generated.Utilities.WriteInt64BigEndian(destination[14..22], this.ValueLong);
79+
global::Darp.BinaryObjects.Generated.Utilities.WriteInt128BigEndian(destination[22..38], this.ValueInt128);
80+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt128BigEndian(destination[38..54], this.ValueUInt128);
81+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt64BigEndian(destination[54..62], this.ValueULong);
82+
global::Darp.BinaryObjects.Generated.Utilities.WriteDoubleBigEndian(destination[62..70], this.ValueDouble);
83+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt32BigEndian(destination[70..74], this.ValueUInt);
84+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt16BigEndian(destination[74..76], this.ValueUShort);
85+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[76..77], this.ValueByte);
8686
bytesWritten += 77;
8787

8888
return true;

test/Darp.BinaryObjects.Generator.Tests/Snapshots/IntegrationTest.ArrayByteLength_DefaultAsync#BinaryObjectsGenerator.g.verified.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public bool TryWriteLittleEndian(global::System.Span<byte> destination, out int
2626

2727
if (destination.Length < 1)
2828
return false;
29-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[0..], this.Length);
29+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[0..1], this.Length);
3030
bytesWritten += 1;
3131

3232
if (destination.Length < bytesWritten + this.Length)
@@ -47,7 +47,7 @@ public bool TryWriteBigEndian(global::System.Span<byte> destination, out int byt
4747

4848
if (destination.Length < 1)
4949
return false;
50-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[0..], this.Length);
50+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[0..1], this.Length);
5151
bytesWritten += 1;
5252

5353
if (destination.Length < bytesWritten + this.Length)

test/Darp.BinaryObjects.Generator.Tests/Snapshots/IntegrationTest.ArrayReadRemaining_DefaultAsync#BinaryObjectsGenerator.g.verified.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public bool TryWriteLittleEndian(global::System.Span<byte> destination, out int
9999

100100
if (destination.Length < 1)
101101
return false;
102-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[0..], this.Offset);
102+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[0..1], this.Offset);
103103
bytesWritten += 1;
104104

105105
bytesWritten += global::Darp.BinaryObjects.Generated.Utilities.WriteUInt32SpanLittleEndian(destination[1..], this.Value);
@@ -117,7 +117,7 @@ public bool TryWriteBigEndian(global::System.Span<byte> destination, out int byt
117117

118118
if (destination.Length < 1)
119119
return false;
120-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[0..], this.Offset);
120+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[0..1], this.Offset);
121121
bytesWritten += 1;
122122

123123
bytesWritten += global::Darp.BinaryObjects.Generated.Utilities.WriteUInt32SpanBigEndian(destination[1..], this.Value);

test/Darp.BinaryObjects.Generator.Tests/Snapshots/IntegrationTest.Asd#BinaryObjectsGenerator.g.verified.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public bool TryWriteLittleEndian(global::System.Span<byte> destination, out int
2727

2828
if (destination.Length < 1)
2929
return false;
30-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.Value);
30+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.Value);
3131
bytesWritten += 1;
3232

3333
return true;
@@ -43,7 +43,7 @@ public bool TryWriteBigEndian(global::System.Span<byte> destination, out int byt
4343

4444
if (destination.Length < 1)
4545
return false;
46-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.Value);
46+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.Value);
4747
bytesWritten += 1;
4848

4949
return true;
@@ -113,8 +113,8 @@ public bool TryWriteLittleEndian(global::System.Span<byte> destination, out int
113113

114114
if (destination.Length < 2)
115115
return false;
116-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.Value);
117-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[1..], this.Value2);
116+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.Value);
117+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[1..2], this.Value2);
118118
bytesWritten += 2;
119119

120120
return true;
@@ -130,8 +130,8 @@ public bool TryWriteBigEndian(global::System.Span<byte> destination, out int byt
130130

131131
if (destination.Length < 2)
132132
return false;
133-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.Value);
134-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[1..], this.Value2);
133+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.Value);
134+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[1..2], this.Value2);
135135
bytesWritten += 2;
136136

137137
return true;
@@ -212,9 +212,9 @@ public bool TryWriteLittleEndian(global::System.Span<byte> destination, out int
212212

213213
if (destination.Length < 3)
214214
return false;
215-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.Value2);
216-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[1..], this.Value3);
217-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[2..], this.Value);
215+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.Value2);
216+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[1..2], this.Value3);
217+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[2..3], this.Value);
218218
bytesWritten += 3;
219219

220220
return true;
@@ -230,9 +230,9 @@ public bool TryWriteBigEndian(global::System.Span<byte> destination, out int byt
230230

231231
if (destination.Length < 3)
232232
return false;
233-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.Value2);
234-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[1..], this.Value3);
235-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[2..], this.Value);
233+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.Value2);
234+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[1..2], this.Value3);
235+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[2..3], this.Value);
236236
bytesWritten += 3;
237237

238238
return true;

test/Darp.BinaryObjects.Generator.Tests/Snapshots/IntegrationTest.BinaryObjectArray_DefaultAsync#BinaryObjectsGenerator.g.verified.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public bool TryWriteLittleEndian(global::System.Span<byte> destination, out int
2727

2828
if (destination.Length < 1)
2929
return false;
30-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.Value);
30+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.Value);
3131
bytesWritten += 1;
3232

3333
return true;
@@ -43,7 +43,7 @@ public bool TryWriteBigEndian(global::System.Span<byte> destination, out int byt
4343

4444
if (destination.Length < 1)
4545
return false;
46-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.Value);
46+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.Value);
4747
bytesWritten += 1;
4848

4949
return true;

test/Darp.BinaryObjects.Generator.Tests/Snapshots/IntegrationTest.BinaryObject_DefaultAsync#BinaryObjectsGenerator.g.verified.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public bool TryWriteLittleEndian(global::System.Span<byte> destination, out int
2727

2828
if (destination.Length < 1)
2929
return false;
30-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.Value);
30+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.Value);
3131
bytesWritten += 1;
3232

3333
return true;
@@ -43,7 +43,7 @@ public bool TryWriteBigEndian(global::System.Span<byte> destination, out int byt
4343

4444
if (destination.Length < 1)
4545
return false;
46-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.Value);
46+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.Value);
4747
bytesWritten += 1;
4848

4949
return true;
@@ -198,8 +198,8 @@ public bool TryWriteLittleEndian(global::System.Span<byte> destination, out int
198198

199199
if (destination.Length < 3)
200200
return false;
201-
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectLittleEndian(destination[0..], this.Value);
202-
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectLittleEndian(destination[1..], this.Array);
201+
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectLittleEndian(destination[0..1], this.Value);
202+
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectLittleEndian(destination[1..3], this.Array);
203203
bytesWritten += 3;
204204

205205
return true;
@@ -215,8 +215,8 @@ public bool TryWriteBigEndian(global::System.Span<byte> destination, out int byt
215215

216216
if (destination.Length < 3)
217217
return false;
218-
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectBigEndian(destination[0..], this.Value);
219-
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectBigEndian(destination[1..], this.Array);
218+
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectBigEndian(destination[0..1], this.Value);
219+
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectBigEndian(destination[1..3], this.Array);
220220
bytesWritten += 3;
221221

222222
return true;

test/Darp.BinaryObjects.Generator.Tests/Snapshots/IntegrationTest.ManualBinaryObject_ConstantAsync#BinaryObjectsGenerator.g.verified.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public bool TryWriteLittleEndian(global::System.Span<byte> destination, out int
2929

3030
if (destination.Length < 4)
3131
return false;
32-
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectLittleEndian(destination[0..], this.Value);
32+
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectLittleEndian(destination[0..1], this.Value);
3333
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectSpanLittleEndian<ManualConstantObject>(destination.Slice(1, 2), this.Values);
34-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[3..], this.Length);
34+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[3..4], this.Length);
3535
bytesWritten += 4;
3636

3737
if (destination.Length < bytesWritten + this.Length)
@@ -54,9 +54,9 @@ public bool TryWriteBigEndian(global::System.Span<byte> destination, out int byt
5454

5555
if (destination.Length < 4)
5656
return false;
57-
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectBigEndian(destination[0..], this.Value);
57+
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectBigEndian(destination[0..1], this.Value);
5858
global::Darp.BinaryObjects.Generated.Utilities.WriteBinaryObjectSpanBigEndian<ManualConstantObject>(destination.Slice(1, 2), this.Values);
59-
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[3..], this.Length);
59+
global::Darp.BinaryObjects.Generated.Utilities.WriteUInt8(destination[3..4], this.Length);
6060
bytesWritten += 4;
6161

6262
if (destination.Length < bytesWritten + this.Length)

test/Darp.BinaryObjects.Generator.Tests/Snapshots/IntegrationTest.OneBool_DefaultAsync#BinaryObjectsGenerator.g.verified.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public bool TryWriteLittleEndian(global::System.Span<byte> destination, out int
2727

2828
if (destination.Length < 1)
2929
return false;
30-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.Value);
30+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.Value);
3131
bytesWritten += 1;
3232

3333
return true;
@@ -43,7 +43,7 @@ public bool TryWriteBigEndian(global::System.Span<byte> destination, out int byt
4343

4444
if (destination.Length < 1)
4545
return false;
46-
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..], this.Value);
46+
global::Darp.BinaryObjects.Generated.Utilities.WriteBool(destination[0..1], this.Value);
4747
bytesWritten += 1;
4848

4949
return true;

0 commit comments

Comments
 (0)