Skip to content

Commit fb95921

Browse files
committed
follow the code analyzer rules
1 parent 8a06da5 commit fb95921

File tree

2 files changed

+30
-75
lines changed

2 files changed

+30
-75
lines changed

src/Smdn.Fundamental.PrintableEncoding.PercentEncoding/Smdn.Formats.PercentEncodings/FromPercentEncodedTransform.cs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,13 @@
33
using System;
44
using System.Security.Cryptography;
55

6-
using Smdn.Formats;
7-
using Smdn.Text;
8-
96
namespace Smdn.Formats.PercentEncodings {
107
[System.Runtime.CompilerServices.TypeForwardedFrom("Smdn, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null")]
118
public sealed class FromPercentEncodedTransform : ICryptoTransform {
12-
public bool CanTransformMultipleBlocks {
13-
get { return true; }
14-
}
15-
16-
public bool CanReuseTransform {
17-
get { return true; }
18-
}
19-
20-
public int InputBlockSize {
21-
get { return 1; }
22-
}
23-
24-
public int OutputBlockSize {
25-
get { return 1; }
26-
}
9+
public bool CanTransformMultipleBlocks => true;
10+
public bool CanReuseTransform => true;
11+
public int InputBlockSize => 1;
12+
public int OutputBlockSize => 1;
2713

2814
public FromPercentEncodedTransform()
2915
: this(false)
@@ -40,10 +26,7 @@ public void Clear()
4026
disposed = true;
4127
}
4228

43-
void IDisposable.Dispose()
44-
{
45-
Clear();
46-
}
29+
void IDisposable.Dispose() => Clear();
4730

4831
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
4932
{
@@ -128,7 +111,7 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input
128111
return outputBuffer;
129112
}
130113

131-
private byte[] buffer = new byte[3];
114+
private readonly byte[] buffer = new byte[3];
132115
private int bufferOffset = 0;
133116
private bool disposed = false;
134117
private readonly bool decodePlusToSpace;

src/Smdn.Fundamental.PrintableEncoding.PercentEncoding/Smdn.Formats.PercentEncodings/ToPercentEncodedTransform.cs

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
using System;
44
using System.Security.Cryptography;
55

6-
using Smdn.Formats;
7-
using Smdn.Text;
8-
96
namespace Smdn.Formats.PercentEncodings {
107
/*
118
* http://tools.ietf.org/html/rfc3986
129
* RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax
1310
* 2.1. Percent-Encoding
14-
*
11+
*
1512
* http://tools.ietf.org/html/rfc2396
1613
* RFC 2396 - Uniform Resource Identifiers (URI): Generic Syntax
1714
*/
@@ -53,48 +50,24 @@ private byte[] GetEscapeOctets(string str)
5350
return octets;
5451
}
5552

56-
public bool CanTransformMultipleBlocks {
57-
get { return true; }
58-
}
59-
60-
public bool CanReuseTransform {
61-
get { return true; }
62-
}
63-
64-
public int InputBlockSize {
65-
get { return 1; }
66-
}
67-
68-
public int OutputBlockSize {
69-
get { return 3; }
70-
}
53+
public bool CanTransformMultipleBlocks => true;
54+
public bool CanReuseTransform => true;
55+
public int InputBlockSize => 1;
56+
public int OutputBlockSize => 3;
7157

7258
public ToPercentEncodedTransform(ToPercentEncodedTransformMode mode)
7359
{
74-
switch (mode & ToPercentEncodedTransformMode.ModeMask) {
75-
case ToPercentEncodedTransformMode.Rfc2396Uri:
76-
escapeOctets = GetEscapeOctets(Rfc2396UriEscapeChars);
77-
break;
78-
case ToPercentEncodedTransformMode.Rfc2396Data:
79-
escapeOctets = GetEscapeOctets(Rfc2396DataEscapeChars);
80-
break;
81-
case ToPercentEncodedTransformMode.Rfc3986Uri:
82-
escapeOctets = GetEscapeOctets(Rfc3986UriEscapeChars);
83-
break;
84-
case ToPercentEncodedTransformMode.Rfc3986Data:
85-
escapeOctets = GetEscapeOctets(Rfc3986DataEscapeChars);
86-
break;
87-
case ToPercentEncodedTransformMode.Rfc5092Uri:
88-
escapeOctets = GetEscapeOctets(Rfc5092AChars);
89-
break;
90-
case ToPercentEncodedTransformMode.Rfc5092Path:
91-
escapeOctets = GetEscapeOctets(Rfc5092BChars);
92-
break;
93-
default:
94-
throw ExceptionUtils.CreateNotSupportedEnumValue(mode);
95-
}
96-
97-
escapeSpaceToPlus = (int)(mode & ToPercentEncodedTransformMode.EscapeSpaceToPlus) != 0;
60+
escapeOctets = (mode & ToPercentEncodedTransformMode.ModeMask) switch {
61+
ToPercentEncodedTransformMode.Rfc2396Uri => GetEscapeOctets(Rfc2396UriEscapeChars),
62+
ToPercentEncodedTransformMode.Rfc2396Data => GetEscapeOctets(Rfc2396DataEscapeChars),
63+
ToPercentEncodedTransformMode.Rfc3986Uri => GetEscapeOctets(Rfc3986UriEscapeChars),
64+
ToPercentEncodedTransformMode.Rfc3986Data => GetEscapeOctets(Rfc3986DataEscapeChars),
65+
ToPercentEncodedTransformMode.Rfc5092Uri => GetEscapeOctets(Rfc5092AChars),
66+
ToPercentEncodedTransformMode.Rfc5092Path => GetEscapeOctets(Rfc5092BChars),
67+
_ => throw ExceptionUtils.CreateNotSupportedEnumValue(mode),
68+
};
69+
70+
escapeSpaceToPlus = (mode & ToPercentEncodedTransformMode.EscapeSpaceToPlus) != 0;
9871
}
9972

10073
public void Clear()
@@ -133,14 +106,13 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
133106
for (var i = 0; i < inputCount; i++) {
134107
var octet = inputBuffer[inputOffset++];
135108

136-
var escape =
137-
!((0x30 <= octet && octet <= 0x39) || // DIGIT
138-
(0x41 <= octet && octet <= 0x5a) || // UPALPHA
139-
(0x61 <= octet && octet <= 0x7a) // LOWALPHA
140-
) &&
141-
(octet < 0x20 || 0x80 <= octet);
109+
var isPrintable = octet is >= 0x20 and < 0x80;
110+
var isDigit = isPrintable && octet is >= 0x30 and <= 0x39; // DIGIT
111+
var isUpAlpha = isPrintable && octet is >= 0x41 and <= 0x5a; // UPALPHA
112+
var isLowAlpha = isPrintable && octet is >= 0x61 and <= 0x7a; // UPALPHA
113+
var escape = !isPrintable && !(isDigit || isUpAlpha || isLowAlpha);
142114

143-
escape |= (0 <= Array.BinarySearch(escapeOctets, octet));
115+
escape |= 0 <= Array.BinarySearch(escapeOctets, octet);
144116

145117
if (escape) {
146118
if (octet == 0x20 && escapeSpaceToPlus) {
@@ -192,7 +164,7 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input
192164
}
193165

194166
private bool disposed = false;
195-
private byte[] escapeOctets;
196-
private bool escapeSpaceToPlus;
167+
private readonly byte[] escapeOctets;
168+
private readonly bool escapeSpaceToPlus;
197169
}
198170
}

0 commit comments

Comments
 (0)