Skip to content

Commit

Permalink
Improve Code Quality (#997)
Browse files Browse the repository at this point in the history
* Use C#12

* Use utf-8 literals

* Use pattern matching

* Remove unneeded casts

* Use utf-8 literal
  • Loading branch information
iamcarbon authored Jan 30, 2024
1 parent 6ccbb10 commit 7373c6b
Show file tree
Hide file tree
Showing 16 changed files with 21 additions and 35 deletions.
7 changes: 1 addition & 6 deletions MimeKit/Encodings/Base64Encoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ namespace MimeKit.Encodings {
/// </remarks>
public class Base64Encoder : IMimeEncoder
{
static ReadOnlySpan<byte> base64_alphabet => new byte[64] {
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F
};
static ReadOnlySpan<byte> base64_alphabet => "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"u8;

readonly int quartetsPerLine;
readonly bool rfc2047;
Expand Down
5 changes: 1 addition & 4 deletions MimeKit/Encodings/HexEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ namespace MimeKit.Encodings {
/// </remarks>
public class HexEncoder : IMimeEncoder
{
static ReadOnlySpan<byte> hex_alphabet => new byte[16] {
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, // '0' -> '7'
0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, // '8' -> 'F'
};
static ReadOnlySpan<byte> hex_alphabet => "0123456789ABCDEF"u8;

/// <summary>
/// Initialize a new instance of the <see cref="HexEncoder"/> class.
Expand Down
5 changes: 1 addition & 4 deletions MimeKit/Encodings/QEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ public enum QEncodeMode : byte {
/// </remarks>
public class QEncoder : IMimeEncoder
{
static ReadOnlySpan<byte> hex_alphabet => new byte[16] {
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, // '0' -> '7'
0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, // '8' -> 'F'
};
static ReadOnlySpan<byte> hex_alphabet => "0123456789ABCDEF"u8;

readonly CharType mask;

Expand Down
5 changes: 1 addition & 4 deletions MimeKit/Encodings/QuotedPrintableEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ namespace MimeKit.Encodings {
/// </remarks>
public class QuotedPrintableEncoder : IMimeEncoder
{
static ReadOnlySpan<byte> hex_alphabet => new byte[16] {
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, // '0' -> '7'
0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, // '8' -> 'F'
};
static ReadOnlySpan<byte> hex_alphabet => "0123456789ABCDEF"u8;

readonly short tripletsPerLine;
readonly short maxLineLength;
Expand Down
4 changes: 2 additions & 2 deletions MimeKit/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ static byte[] EncodeReferencesHeader (ParserOptions options, FormatOptions forma

static bool IsWhiteSpace (char c)
{
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
return c is ' ' or '\t' or '\r' or '\n';
}

readonly struct Word
Expand Down Expand Up @@ -1223,7 +1223,7 @@ static void AppendComment (FormatOptions format, Encoding encoding, ref ValueStr

static bool IsMailingListCommandSpecial (char c)
{
return c == '<' || c == '(' || c == ',';
return c is '<' or '(' or ',';
}

static byte[] EncodeMailingListCommandHeader (ParserOptions options, FormatOptions format, Encoding encoding, string field, string value)
Expand Down
2 changes: 1 addition & 1 deletion MimeKit/IO/Filters/ArmoredFromFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace MimeKit.IO.Filters {
/// </remarks>
public class ArmoredFromFilter : MimeFilterBase
{
static readonly byte[] From = { (byte) 'F', (byte) 'r', (byte) 'o', (byte) 'm', (byte) ' ' };
static ReadOnlySpan<byte> From => "From "u8;

bool midline;

Expand Down
4 changes: 2 additions & 2 deletions MimeKit/IO/Filters/CharsetFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ public CharsetFilter (Encoding sourceEncoding, Encoding targetEncoding)
SourceEncoding = sourceEncoding;
TargetEncoding = targetEncoding;

decoder = (Decoder) SourceEncoding.GetDecoder ();
encoder = (Encoder) TargetEncoding.GetEncoder ();
decoder = SourceEncoding.GetDecoder ();
encoder = TargetEncoding.GetEncoder ();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion MimeKit/IO/Filters/MboxFromFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace MimeKit.IO.Filters {
/// </remarks>
public class MboxFromFilter : MimeFilterBase
{
static readonly byte[] From = { (byte) 'F', (byte) 'r', (byte) 'o', (byte) 'm', (byte) ' ' };
static ReadOnlySpan<byte> From => "From "u8;
bool midline;

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion MimeKit/InternetAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ internal static bool TryParseLocalPart (byte[] text, ref int index, int endIndex
return true;
}

static ReadOnlySpan<byte> CommaGreaterThanOrSemiColon => new[] { (byte) ',', (byte) '>', (byte) ';' };
static ReadOnlySpan<byte> CommaGreaterThanOrSemiColon => ",>;"u8;

internal static bool TryParseAddrspec (byte[] text, ref int index, int endIndex, ReadOnlySpan<byte> sentinels, RfcComplianceMode compliance, bool throwOnError, out string addrspec, out int at)
{
Expand Down
2 changes: 1 addition & 1 deletion MimeKit/MimeKit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>An Open Source library for creating and parsing MIME, S/MIME, PGP messages on desktop and mobile platforms.</Description>
<AssemblyTitle>MimeKit</AssemblyTitle>
<VersionPrefix>4.3.0</VersionPrefix>
<LangVersion>10</LangVersion>
<LangVersion>12</LangVersion>
<Authors>Jeffrey Stedfast</Authors>
<_LegacyFrameworks>net462;net47</_LegacyFrameworks>
<_LegacyFrameworks Condition=" Exists('C:\Windows') ">$(_LegacyFrameworks);net48</_LegacyFrameworks>
Expand Down
2 changes: 1 addition & 1 deletion MimeKit/MimeKitLite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>An Open Source library for creating and parsing MIME, S/MIME, PGP messages on desktop and mobile platforms.</Description>
<AssemblyTitle>MimeKit Lite</AssemblyTitle>
<VersionPrefix>4.3.0</VersionPrefix>
<LangVersion>10</LangVersion>
<LangVersion>12</LangVersion>
<Authors>Jeffrey Stedfast</Authors>
<_LegacyFrameworks>net462;net47</_LegacyFrameworks>
<_LegacyFrameworks Condition=" Exists('C:\Windows') ">$(_LegacyFrameworks);net48</_LegacyFrameworks>
Expand Down
2 changes: 1 addition & 1 deletion MimeKit/MimeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ enum BoundaryType

class Boundary
{
public static readonly byte[] MboxFrom = Encoding.ASCII.GetBytes ("From ");
public static readonly byte[] MboxFrom = "From "u8.ToArray();

public byte[] Marker { get; private set; }
public int FinalLength { get { return Marker.Length; } }
Expand Down
4 changes: 2 additions & 2 deletions MimeKit/MimePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ public override void WriteTo (FormatOptions options, Stream stream, bool content
}

if (ContentTransferEncoding == ContentEncoding.UUEncode) {
var buffer = Encoding.ASCII.GetBytes ("end");
var buffer = "end"u8.ToArray ();

if (cancellable != null) {
cancellable.Write (buffer, 0, buffer.Length, cancellationToken);
Expand Down Expand Up @@ -803,7 +803,7 @@ public override async Task WriteToAsync (FormatOptions options, Stream stream, b
}

if (ContentTransferEncoding == ContentEncoding.UUEncode) {
var buffer = Encoding.ASCII.GetBytes ("end");
var buffer = "end"u8.ToArray();

await stream.WriteAsync (buffer, 0, buffer.Length, cancellationToken).ConfigureAwait (false);
await stream.WriteAsync (options.NewLineBytes, 0, options.NewLineBytes.Length, cancellationToken).ConfigureAwait (false);
Expand Down
4 changes: 2 additions & 2 deletions MimeKit/Tnef/TnefPropertyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -747,13 +747,13 @@ public int ReadTextValue (char[] buffer, int offset, int count)
switch (propertyTag.ValueTnefType) {
case TnefPropertyType.Unicode:
ReadInt32 ();
decoder = (Decoder) Encoding.Unicode.GetDecoder ();
decoder = Encoding.Unicode.GetDecoder ();
break;
case TnefPropertyType.String8:
case TnefPropertyType.Binary:
case TnefPropertyType.Object:
ReadInt32 ();
decoder = (Decoder) GetMessageEncoding ().GetDecoder ();
decoder = GetMessageEncoding ().GetDecoder ();
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions MimeKit/Utils/CharsetUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ internal static char[] ConvertToUnicode (ParserOptions options, byte[] input, in

for (int i = 0; i < codepages.Length; i++) {
encoding = Encoding.GetEncoding (codepages[i], new EncoderReplacementFallback ("?"), invalid);
decoder = (Decoder) encoding.GetDecoder ();
decoder = encoding.GetDecoder ();

count = decoder.GetCharCount (input, startIndex, length, true);
if (invalid.InvalidByteCount < min) {
Expand All @@ -505,7 +505,7 @@ internal static char[] ConvertToUnicode (ParserOptions options, byte[] input, in
}

encoding = GetEncoding (best, "?");
decoder = (Decoder) encoding.GetDecoder ();
decoder = encoding.GetDecoder ();

var output = new char[bestCharCount];

Expand Down
2 changes: 1 addition & 1 deletion MimeKit/Utils/ParseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public static bool TryParseDomain (byte[] text, ref int index, int endIndex, Rea
return TryParseDotAtom (text, ref index, endIndex, sentinels, throwOnError, "domain", out domain);
}

static ReadOnlySpan<byte> GreaterThanOrAt => new[] { (byte) '>', (byte) '@' };
static ReadOnlySpan<byte> GreaterThanOrAt => ">@"u8;

public static bool TryParseMsgId (byte[] text, ref int index, int endIndex, bool requireAngleAddr, bool throwOnError, out string msgid)
{
Expand Down

0 comments on commit 7373c6b

Please sign in to comment.