File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11using System ;
2- using System . Linq ;
32using System . Text ;
43
54namespace SysadminsLV . Asn1Parser . Universal ;
@@ -20,7 +19,7 @@ public sealed class Asn1BitString : Asn1Universal {
2019 /// </exception>
2120 public Asn1BitString ( Asn1Reader asn ) : base ( asn , TYPE ) {
2221 UnusedBits = asn [ asn . PayloadStartOffset ] ;
23- Value = asn . GetPayload ( ) . Skip ( 1 ) . ToArray ( ) ;
22+ Value = asn . GetPayloadAsMemory ( ) . Slice ( 1 ) . ToArray ( ) ;
2423 }
2524 /// <summary>
2625 /// Initializes a new instance of <strong>Asn1BitString</strong> from an ASN.1-encoded memory buffer.
Original file line number Diff line number Diff line change @@ -57,9 +57,12 @@ void m_encode(String inputString) {
5757 Initialize ( Asn1Utils . EncodeAsReader ( Encoding . ASCII . GetBytes ( inputString ) . AsSpan ( ) , TYPE ) ) ;
5858 }
5959 void m_decode ( Asn1Reader asn ) {
60- if ( asn . GetPayload ( ) . Any ( b => b > 127 ) ) {
61- throw new InvalidDataException ( String . Format ( InvalidType , TYPE . ToString ( ) ) ) ;
60+ ReadOnlySpan < Byte > payload = asn . GetPayloadAsMemory ( ) . Span ;
61+ foreach ( Byte b in payload ) {
62+ if ( b > 127 ) {
63+ throw new InvalidDataException ( String . Format ( InvalidType , TYPE . ToString ( ) ) ) ;
64+ }
6265 }
63- Value = Encoding . ASCII . GetString ( asn . GetPayloadAsMemory ( ) ) ;
66+ Value = Encoding . ASCII . GetString ( payload ) ;
6467 }
6568}
Original file line number Diff line number Diff line change @@ -57,9 +57,12 @@ void m_encode(String inputString) {
5757 Initialize ( Asn1Utils . EncodeAsReader ( Encoding . ASCII . GetBytes ( inputString ) . AsSpan ( ) , TYPE ) ) ;
5858 }
5959 void m_decode ( Asn1Reader asn ) {
60- if ( asn . GetPayload ( ) . Any ( b => b is < 48 or > 57 && b != 32 ) ) {
61- throw new InvalidDataException ( String . Format ( InvalidType , TYPE . ToString ( ) ) ) ;
60+ ReadOnlySpan < Byte > payload = asn . GetPayloadAsMemory ( ) . Span ;
61+ foreach ( Byte b in payload ) {
62+ if ( b is < 48 or > 57 && b != 32 ) {
63+ throw new InvalidDataException ( String . Format ( InvalidType , TYPE . ToString ( ) ) ) ;
64+ }
6265 }
63- Value = Encoding . ASCII . GetString ( asn . GetPayloadAsMemory ( ) ) ;
66+ Value = Encoding . ASCII . GetString ( payload ) ;
6467 }
6568}
Original file line number Diff line number Diff line change @@ -59,19 +59,25 @@ void m_encode(String inputString) {
5959 Initialize ( Asn1Utils . EncodeAsReader ( Encoding . ASCII . GetBytes ( inputString ) . AsSpan ( ) , TYPE ) ) ;
6060 }
6161 void m_decode ( Asn1Reader asn ) {
62- if ( ! testValue ( asn . GetPayload ( ) ) ) {
62+ ReadOnlySpan < Byte > payload = asn . GetPayloadAsMemory ( ) . Span ;
63+ if ( ! testValue ( payload ) ) {
6364 throw new InvalidDataException ( String . Format ( InvalidType , TYPE . ToString ( ) ) ) ;
6465 }
65- Value = Encoding . ASCII . GetString ( asn . GetPayloadAsMemory ( ) ) ;
66+ Value = Encoding . ASCII . GetString ( payload ) ;
6667 }
6768 static Boolean testValue ( String str ) {
6869 List < Byte > alphabet = StringUtils . GetAlphabet ( TYPE ) ;
6970 try {
7071 return str . All ( c => alphabet . Contains ( Convert . ToByte ( c ) ) ) ;
7172 } catch { return false ; }
7273 }
73- static Boolean testValue ( IEnumerable < Byte > rawData ) {
74+ static Boolean testValue ( ReadOnlySpan < Byte > rawData ) {
7475 List < Byte > alphabet = StringUtils . GetAlphabet ( TYPE ) ;
75- return rawData . All ( alphabet . Contains ) ;
76+ foreach ( Byte b in rawData ) {
77+ if ( ! alphabet . Contains ( b ) ) {
78+ return false ;
79+ }
80+ }
81+ return true ;
7682 }
7783}
Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ void m_encode(String inputString) {
5757 Initialize ( Asn1Utils . EncodeAsReader ( Encoding . UTF8 . GetBytes ( inputString ) . AsMemory ( ) . Span , TYPE ) ) ;
5858 }
5959 void m_decode ( Asn1Reader asn ) {
60- Value = Encoding . UTF8 . GetString ( asn . GetPayloadAsMemory ( ) ) ;
60+ Value = Encoding . UTF8 . GetString ( asn . GetPayloadAsMemory ( ) . Span ) ;
6161 }
6262 static Boolean testValue ( String str ) {
6363 return str . All ( x => Convert . ToUInt32 ( x ) <= 255 ) ;
Original file line number Diff line number Diff line change @@ -14,9 +14,9 @@ static class EncodingExtensions {
1414 /// <param name="bytes">The byte sequence to decode, represented as a <see cref="ReadOnlyMemory{T}"/> of <see cref="Byte"/>.</param>
1515 /// <returns>A <see cref="String"/> that contains the decoded characters from the byte sequence.</returns>
1616 /// <exception cref="ArgumentNullException">Thrown if <paramref name="encoding"/> is <c>null</c>.</exception>
17- public static String GetString ( this Encoding encoding , ReadOnlyMemory < Byte > bytes ) {
17+ public static String GetString ( this Encoding encoding , ReadOnlySpan < Byte > bytes ) {
1818#if NET8_0_OR_GREATER
19- return encoding . GetString ( bytes . Span ) ;
19+ return encoding . GetString ( bytes ) ;
2020#else
2121 return encoding . GetString ( bytes . ToArray ( ) ) ;
2222#endif
You can’t perform that action at this time.
0 commit comments