@@ -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
0 commit comments