Skip to content

Commit 0f2cc89

Browse files
committed
* Rename ByteCount to Length to match ReadOnlyMemory
* Assume UTF8 for `AmqpString` * Use "lazy" feature of `AmqpString` for incoming data like `ConsumerTag`, `ExchangeName` etc
1 parent 9c6e4dd commit 0f2cc89

17 files changed

+51
-117
lines changed

projects/RabbitMQ.Client/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ override RabbitMQ.Client.ShutdownReportEntry.ToString() -> string
7171
RabbitMQ.Client.AmqpString
7272
RabbitMQ.Client.AmqpString.AmqpString() -> void
7373
RabbitMQ.Client.AmqpString.AmqpString(System.ReadOnlyMemory<byte> stringBytes) -> void
74-
RabbitMQ.Client.AmqpString.ByteCount.get -> int
7574
RabbitMQ.Client.AmqpString.IsEmpty.get -> bool
75+
RabbitMQ.Client.AmqpString.Length.get -> int
7676
RabbitMQ.Client.AmqpTcpEndpoint
7777
RabbitMQ.Client.AmqpTcpEndpoint.AddressFamily.get -> System.Net.Sockets.AddressFamily
7878
RabbitMQ.Client.AmqpTcpEndpoint.AddressFamily.set -> void
@@ -859,8 +859,8 @@ virtual RabbitMQ.Client.TcpClientAdapter.ReceiveTimeout.set -> void
859859
~override RabbitMQ.Client.ExchangeType.Equals(object obj) -> bool
860860
~override RabbitMQ.Client.ExchangeType.ToString() -> string
861861
~override RabbitMQ.Client.QueueName.FixUp(string value) -> string
862-
~RabbitMQ.Client.AmqpString.AmqpString(string value, ushort maxLen, System.Text.Encoding encoding, bool strictValidation = false) -> void
863-
~RabbitMQ.Client.AmqpString.AmqpString(string value, ushort maxLen, System.Text.Encoding encoding, string validatorRegex, bool strictValidation = false) -> void
862+
~RabbitMQ.Client.AmqpString.AmqpString(string value, ushort maxLen, bool strictValidation = false) -> void
863+
~RabbitMQ.Client.AmqpString.AmqpString(string value, ushort maxLen, string validatorRegex, bool strictValidation = false) -> void
864864
~RabbitMQ.Client.AmqpString.CompareTo(RabbitMQ.Client.AmqpString other) -> int
865865
~RabbitMQ.Client.AmqpString.Contains(string value) -> bool
866866
~RabbitMQ.Client.AmqpString.Equals(RabbitMQ.Client.AmqpString other) -> bool

projects/RabbitMQ.Client/client/api/AmqpString.cs

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,31 @@ namespace RabbitMQ.Client
3737
{
3838
public abstract class AmqpString : IEquatable<AmqpString>, IComparable<AmqpString>
3939
{
40+
private static readonly Encoding s_encoding = Encoding.UTF8;
4041
private string _value;
41-
private readonly Encoding _encoding;
4242
private readonly ReadOnlyMemory<byte> _stringBytes;
43-
private readonly int _byteCount;
4443

4544
protected AmqpString()
4645
{
4746
_value = string.Empty;
48-
_encoding = Encoding.UTF8;
4947
_stringBytes = ReadOnlyMemory<byte>.Empty;
50-
_byteCount = 0;
5148
}
5249

5350
public AmqpString(ReadOnlyMemory<byte> stringBytes)
5451
{
5552
_value = null;
56-
_encoding = Encoding.UTF8;
5753
_stringBytes = stringBytes;
58-
_byteCount = _stringBytes.Length;
5954
}
6055

61-
public AmqpString(string value, ushort maxLen, Encoding encoding,
56+
public AmqpString(string value, ushort maxLen,
6257
bool strictValidation = false)
63-
: this(value, maxLen, encoding, null, strictValidation)
58+
: this(value, maxLen, null, strictValidation)
6459
{
6560
}
6661

67-
public AmqpString(string value, ushort maxLen, Encoding encoding, string validatorRegex,
62+
public AmqpString(string value, ushort maxLen, string validatorRegex,
6863
bool strictValidation = false)
6964
{
70-
_encoding = encoding;
71-
7265
/*
7366
* Note:
7467
* RabbitMQ does hardly any validation for names, only stripping off CR/LF
@@ -89,22 +82,13 @@ public AmqpString(string value, ushort maxLen, Encoding encoding, string validat
8982
throw new ArgumentOutOfRangeException(nameof(value));
9083
}
9184
}
92-
93-
if (encoding == Encoding.ASCII)
94-
{
95-
if (false == isAscii(value))
96-
{
97-
throw new ArgumentOutOfRangeException(nameof(value));
98-
}
99-
}
10085
}
10186

10287
_value = FixUp(value);
103-
_stringBytes = new ReadOnlyMemory<byte>(encoding.GetBytes(value));
104-
_byteCount = _stringBytes.Length;
88+
_stringBytes = new ReadOnlyMemory<byte>(s_encoding.GetBytes(value));
10589
}
10690

107-
public int ByteCount => _byteCount;
91+
public int Length => _stringBytes.Length;
10892

10993
internal bool HasString => _value != null;
11094

@@ -223,19 +207,13 @@ protected virtual string FixUp(string value)
223207
return value;
224208
}
225209

226-
// TODO remove
227-
private static bool isAscii(string value)
228-
{
229-
return Encoding.UTF8.GetByteCount(value) == value.Length;
230-
}
231-
232210
private string Value
233211
{
234212
get
235213
{
236214
if (_value == null)
237215
{
238-
_value = _encoding.GetString(_stringBytes.ToArray());
216+
_value = s_encoding.GetString(_stringBytes.ToArray());
239217
}
240218
return _value;
241219
}
@@ -270,7 +248,7 @@ public ExchangeName(string exchangeName)
270248
}
271249

272250
public ExchangeName(string exchangeName, bool strictValidation)
273-
: base(exchangeName, 127, Encoding.ASCII, "^[a-zA-Z0-9-_.:]*$", strictValidation)
251+
: base(exchangeName, 127, "^[a-zA-Z0-9-_.:]*$", strictValidation)
274252
{
275253
}
276254

@@ -319,7 +297,7 @@ public QueueName(string queueName)
319297
}
320298

321299
public QueueName(string queueName, bool strictValidation)
322-
: base(queueName, 127, Encoding.ASCII, "^[a-zA-Z0-9-_.:]*$", strictValidation)
300+
: base(queueName, 127, "^[a-zA-Z0-9-_.:]*$", strictValidation)
323301
{
324302
}
325303

@@ -367,7 +345,7 @@ public RoutingKey(string routingKey)
367345
}
368346

369347
public RoutingKey(string routingKey, bool strictValidation)
370-
: base(routingKey, 256, Encoding.ASCII, strictValidation)
348+
: base(routingKey, 256, strictValidation)
371349
{
372350
}
373351

@@ -403,7 +381,7 @@ public ConsumerTag(string consumerTag)
403381
}
404382

405383
public ConsumerTag(string consumerTag, bool strictValidation)
406-
: base(consumerTag, 256, Encoding.UTF8, strictValidation)
384+
: base(consumerTag, 256, strictValidation)
407385
{
408386
}
409387

projects/RabbitMQ.Client/client/framing/BasicCancel.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,8 @@ public int WriteTo(Span<byte> span)
5858
public int GetRequiredBufferSize()
5959
{
6060
int bufferSize = 1 + 1; // bytes for length of _consumerTag, bit fields
61-
bufferSize += _consumerTag.ByteCount; // _consumerTag in bytes
61+
bufferSize += _consumerTag.Length; // _consumerTag in bytes
6262
return bufferSize;
6363
}
64-
65-
public static ConsumerTag GetConsumerTag(ReadOnlySpan<byte> span)
66-
{
67-
_ = WireFormatting.ReadShortstr(span, out string consumerTagStr);
68-
return new ConsumerTag(consumerTagStr);
69-
}
7064
}
7165
}

projects/RabbitMQ.Client/client/framing/BasicConsume.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public int WriteTo(Span<byte> span)
7373
public int GetRequiredBufferSize()
7474
{
7575
int bufferSize = 2 + 1 + 1 + 1; // bytes for _reserved1, length of _queue, length of _consumerTag, bit fields
76-
bufferSize += _queue.ByteCount; // _queue in bytes
77-
bufferSize += _consumerTag.ByteCount; // _consumerTag in bytes
76+
bufferSize += _queue.Length; // _queue in bytes
77+
bufferSize += _consumerTag.Length; // _consumerTag in bytes
7878
bufferSize += WireFormatting.GetTableByteCount(_arguments); // _arguments in bytes
7979
return bufferSize;
8080
}

projects/RabbitMQ.Client/client/framing/BasicGet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public int WriteTo(Span<byte> span)
5959
public int GetRequiredBufferSize()
6060
{
6161
int bufferSize = 2 + 1 + 1; // bytes for _reserved1, length of _queue, bit fields
62-
bufferSize += _queue.ByteCount; // _queue in bytes
62+
bufferSize += _queue.Length; // _queue in bytes
6363
return bufferSize;
6464
}
6565
}

projects/RabbitMQ.Client/client/framing/ExchangeBind.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ public int WriteTo(Span<byte> span)
7070
public int GetRequiredBufferSize()
7171
{
7272
int bufferSize = 2 + 1 + 1 + 1 + 1; // bytes for _reserved1, length of _destination, length of _source, length of _routingKey, bit fields
73-
bufferSize += _destination.ByteCount; // _destination in bytes
74-
bufferSize += _source.ByteCount; // _source in bytes
75-
bufferSize += _routingKey.ByteCount; // _routingKey in bytes
73+
bufferSize += _destination.Length; // _destination in bytes
74+
bufferSize += _source.Length; // _source in bytes
75+
bufferSize += _routingKey.Length; // _routingKey in bytes
7676
bufferSize += WireFormatting.GetTableByteCount(_arguments); // _arguments in bytes
7777
return bufferSize;
7878
}

projects/RabbitMQ.Client/client/framing/ExchangeDeclare.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public int WriteTo(Span<byte> span)
7676
public int GetRequiredBufferSize()
7777
{
7878
int bufferSize = 2 + 1 + 1 + 1; // bytes for _reserved1, length of _exchange, length of _type, bit fields
79-
bufferSize += _exchange.ByteCount; // _exchange in bytes
79+
bufferSize += _exchange.Length; // _exchange in bytes
8080
bufferSize += _type.ByteCount; // _type in bytes
8181
bufferSize += WireFormatting.GetTableByteCount(_arguments); // _arguments in bytes
8282
return bufferSize;

projects/RabbitMQ.Client/client/framing/ExchangeDelete.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public int WriteTo(Span<byte> span)
6161
public int GetRequiredBufferSize()
6262
{
6363
int bufferSize = 2 + 1 + 1; // bytes for _reserved1, length of _exchange, bit fields
64-
bufferSize += _exchange.ByteCount; // _exchange in bytes
64+
bufferSize += _exchange.Length; // _exchange in bytes
6565
return bufferSize;
6666
}
6767
}

projects/RabbitMQ.Client/client/framing/ExchangeUnbind.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ public int WriteTo(Span<byte> span)
7070
public int GetRequiredBufferSize()
7171
{
7272
int bufferSize = 2 + 1 + 1 + 1 + 1; // bytes for _reserved1, length of _destination, length of _source, length of _routingKey, bit fields
73-
bufferSize += _destination.ByteCount; // _destination in bytes
74-
bufferSize += _source.ByteCount; // _source in bytes
75-
bufferSize += _routingKey.ByteCount; // _routingKey in bytes
73+
bufferSize += _destination.Length; // _destination in bytes
74+
bufferSize += _source.Length; // _source in bytes
75+
bufferSize += _routingKey.Length; // _routingKey in bytes
7676
bufferSize += WireFormatting.GetTableByteCount(_arguments); // _arguments in bytes
7777
return bufferSize;
7878
}

projects/RabbitMQ.Client/client/framing/QueueBind.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public int WriteTo(Span<byte> span)
7070
public int GetRequiredBufferSize()
7171
{
7272
int bufferSize = 2 + 1 + 1 + 1 + 1; // bytes for _reserved1, length of _queue, length of _exchange, length of _routingKey, bit fields
73-
bufferSize += _queue.ByteCount; // _queue in bytes
73+
bufferSize += _queue.Length; // _queue in bytes
7474
bufferSize += WireFormatting.GetByteCount(_exchange); // _exchange in bytes
7575
bufferSize += WireFormatting.GetByteCount(_routingKey); // _routingKey in bytes
7676
bufferSize += WireFormatting.GetTableByteCount(_arguments); // _arguments in bytes

0 commit comments

Comments
 (0)