Skip to content

Commit 35f4da1

Browse files
committed
[Proto] Fixed regression & inference of EncodeString
1 parent 43f5ad8 commit 35f4da1

File tree

2 files changed

+7
-30
lines changed

2 files changed

+7
-30
lines changed

Lagrange.Proto/Primitives/ProtoWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void EncodeString(ReadOnlySpan<char> str)
4444
int utf16Max = ProtoConstants.MaxExpansionFactorWhileTranscoding * str.Length;
4545
if (_memory.Length < utf16Max) Grow(utf16Max);
4646

47-
if (str.Length > min && str.Length < max) // falls within the range
47+
if (str.Length > min && utf16Max < max) // falls within the range
4848
{
4949
BytesPending += count;
5050
var status = ProtoWriteHelper.ToUtf8(str, _memory.Span[BytesPending..], out int written);

Lagrange.Proto/Utility/ProtoHelper.cs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,19 @@ namespace Lagrange.Proto.Utility;
77

88
public static class ProtoHelper
99
{
10-
/*
11-
* private static final int[] VAR_INT_LENGTHS = new int[65];
10+
private static readonly int[] VarIntValues;
1211

13-
static {
14-
for (int i = 0; i <= 64; ++i) {
15-
VAR_INT_LENGTHS[i] = (63 - i) / 7;
16-
}
17-
}
18-
*/
19-
20-
private static readonly int[] VarIntLengths = new int[65];
21-
2212
static ProtoHelper()
2313
{
24-
for (int i = 0; i <= 64; ++i) VarIntLengths[i] = (63 - i) / 7;
14+
VarIntValues = new int[5];
15+
for (int i = 0; i < VarIntValues.Length; i++) VarIntValues[i] = 1 << (7 * i);
2516
}
26-
17+
2718
[MethodImpl(MethodImplOptions.AggressiveInlining)]
28-
internal static int GetVarIntMin(int length) => length switch
29-
{
30-
1 => (1 << 0),
31-
2 => (1 << 7),
32-
3 => (1 << 14),
33-
4 => (1 << 21),
34-
_ => throw new ArgumentOutOfRangeException(nameof(length), "Invalid length for VarInt.")
35-
};
19+
internal static int GetVarIntMin(int length) => VarIntValues[length - 1];
3620

3721
[MethodImpl(MethodImplOptions.AggressiveInlining)]
38-
internal static int GetVarIntMax(int length) => length switch
39-
{
40-
1 => (1 << 7) - 1,
41-
2 => (1 << 14) - 1,
42-
3 => (1 << 21) - 1,
43-
4 => (1 << 28) - 1,
44-
_ => throw new ArgumentOutOfRangeException(nameof(length), "Invalid length for VarInt.")
45-
};
22+
internal static int GetVarIntMax(int length) => VarIntValues[length] - 1;
4623

4724
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4825
public static unsafe int GetVarIntLength<T>(T value) where T : unmanaged, INumberBase<T>

0 commit comments

Comments
 (0)