Skip to content

Commit

Permalink
Improve code quality (#825)
Browse files Browse the repository at this point in the history
* Avoid encoding ASCII in more cases

* Make Space a const

* Use WriteWhiteSpace extension to eliminate possible virtual call

* Use ASCII when encoding constrained character subset

* Simplify pragmas

* Revert Whitespace rename

* Fix using statement order

* Remove obsolete serialization support on .NET

* Remove obsolete serialization support on .NET (part 2)
  • Loading branch information
iamcarbon authored May 3, 2024
1 parent 7f42a8d commit da44e1a
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 127 deletions.
6 changes: 6 additions & 0 deletions src/UglyToad.PdfPig.Core/PdfDocumentFormatException.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
namespace UglyToad.PdfPig.Core
{
using System;
#if !NET
using System.Runtime.Serialization;
#endif

/// <inheritdoc />
/// <summary>
/// This exception will be thrown where the contents of the PDF document do not match the specification in such a way that it
/// renders the document unreadable.
/// </summary>
#if !NET
[Serializable]
#endif
public class PdfDocumentFormatException : Exception
{
/// <inheritdoc />
Expand All @@ -29,11 +33,13 @@ public PdfDocumentFormatException(string message, Exception inner) : base(messag
{
}

#if !NET
/// <inheritdoc />
protected PdfDocumentFormatException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
#endif
}
}
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Core/Polyfills/EncodingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if !NET

namespace System.Text;

Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Core/Polyfills/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if !NET

namespace System.IO;

Expand Down
6 changes: 6 additions & 0 deletions src/UglyToad.PdfPig.Fonts/CorruptCompressedDataException.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
namespace UglyToad.PdfPig.Fonts
{
using System;
#if !NET
using System.Runtime.Serialization;
#endif

/// <summary>
/// Thrown when a PDF contains an invalid compressed data stream.
/// </summary>
#if !NET
[Serializable]
#endif
public class CorruptCompressedDataException : Exception
{
/// <inheritdoc />
Expand All @@ -24,11 +28,13 @@ public CorruptCompressedDataException(string message, Exception inner) : base(me
{
}

#if !NET
/// <inheritdoc />
protected CorruptCompressedDataException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
#endif
}
}
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Fonts/Polyfills/EncodingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if !NET

namespace System.Text;

Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Fonts/Polyfills/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if !NET

namespace System.IO;

Expand Down
4 changes: 2 additions & 2 deletions src/UglyToad.PdfPig.Tokenization/NameTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
using Core;
using Tokens;

#if NET8_0_OR_GREATER
#if NET
using System.Text.Unicode;
#endif

internal sealed class NameTokenizer : ITokenizer
{
static NameTokenizer()
{
#if NET6_0_OR_GREATER
#if NET
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Content/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private static string GetText(PageContent content)
return string.Empty;
}

#if NET6_0_OR_GREATER
#if NET
int length = 0;

for (var i = 0; i < content.Letters.Count; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Content/Pages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ internal void AddPageFactory<TPage>(IPageFactory<TPage> pageFactory)
pageFactoryCache.Add(type, pageFactory);
}

#if NET6_0_OR_GREATER
#if NET
internal void AddPageFactory<TPage, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] TPageFactory>() where TPageFactory : IPageFactory<TPage>
#else
internal void AddPageFactory<TPage, TPageFactory>() where TPageFactory : IPageFactory<TPage>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
namespace UglyToad.PdfPig.Exceptions
{
using System;
#if !NET
using System.Runtime.Serialization;
#endif
using Encryption;

/// <inheritdoc />
/// <summary>
/// The document is encrypted and cannot be decrypted.
/// </summary>
#if !NET
[Serializable]
#endif
public class PdfDocumentEncryptedException : Exception
{
internal EncryptionDictionary? Dictionary { get; }
Expand Down Expand Up @@ -38,11 +42,13 @@ internal PdfDocumentEncryptedException(string message, EncryptionDictionary dict
Dictionary = dictionary;
}

#if !NET
/// <inheritdoc />
protected PdfDocumentEncryptedException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
#endif
}
}
4 changes: 2 additions & 2 deletions src/UglyToad.PdfPig/Graphics/Colors/ColorSpaceDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ internal override ReadOnlySpan<byte> Transform(ReadOnlySpan<byte> decoded)
}
}

#if NET8_0_OR_GREATER
#if NET
return CollectionsMarshal.AsSpan(transformed);
#else
return transformed.ToArray();
Expand Down Expand Up @@ -745,7 +745,7 @@ internal override ReadOnlySpan<byte> Transform(ReadOnlySpan<byte> values)
}
}

#if NET8_0_OR_GREATER
#if NET
return CollectionsMarshal.AsSpan(transformed);
#else
return transformed.ToArray();
Expand Down
19 changes: 12 additions & 7 deletions src/UglyToad.PdfPig/Graphics/Operations/OperationWriteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

internal static class OperationWriteHelper
{
private const byte WhiteSpace = (byte)' ';
private const byte Whitespace = (byte)' ';
private const byte NewLine = (byte)'\n';

public static void WriteText(this Stream stream, string text, bool appendWhitespace = false)
Expand All @@ -36,13 +36,18 @@ public static void WriteText(this Stream stream, string text, bool appendWhitesp
#endif
if (appendWhitespace)
{
stream.WriteByte(WhiteSpace);
stream.WriteByte(Whitespace);
}
}

public static void WriteText(this Stream stream, ReadOnlySpan<byte> asciiBytes)
public static void WriteText(this Stream stream, ReadOnlySpan<byte> asciiBytes, bool appendWhitespace = false)
{
stream.Write(asciiBytes);

if (appendWhitespace)
{
stream.WriteByte(Whitespace);
}
}

public static void WriteHex(this Stream stream, ReadOnlySpan<byte> bytes)
Expand All @@ -60,7 +65,7 @@ public static void WriteHex(this Stream stream, ReadOnlySpan<byte> bytes)

public static void WriteWhiteSpace(this Stream stream)
{
stream.WriteByte(WhiteSpace);
stream.WriteByte(Whitespace);
}

public static void WriteNewLine(this Stream stream)
Expand All @@ -80,23 +85,23 @@ public static void WriteDouble(this Stream stream, double value)
public static void WriteNumberText(this Stream stream, int number, string text)
{
stream.WriteDouble(number);
stream.WriteWhiteSpace();
stream.WriteByte(Whitespace);
stream.WriteText(text);
stream.WriteNewLine();
}

public static void WriteNumberText(this Stream stream, int number, ReadOnlySpan<byte> asciiBytes)
{
stream.WriteDouble(number);
stream.WriteWhiteSpace();
stream.WriteByte(Whitespace);
stream.WriteText(asciiBytes);
stream.WriteNewLine();
}

public static void WriteNumberText(this Stream stream, double number, string text)
{
stream.WriteDouble(number);
stream.WriteWhiteSpace();
stream.WriteByte(Whitespace);
stream.WriteText(text);
stream.WriteNewLine();
}
Expand Down
4 changes: 2 additions & 2 deletions src/UglyToad.PdfPig/IO/StreamWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override void SetLength(long value)
Stream.SetLength(value);
}

#if NET6_0_OR_GREATER
#if NET
public override int Read(Span<byte> buffer)
{
return Stream.Read(buffer);
Expand All @@ -44,7 +44,7 @@ public override void Write(byte[] buffer, int offset, int count)
Stream.Write(buffer, offset, count);
}

#if NET6_0_OR_GREATER
#if NET
public override void Write(ReadOnlySpan<byte> buffer)
{
Stream.Write(buffer);
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Images/Png/PngStreamWriteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override void Write(byte[] buffer, int offset, int count)
inner.Write(buffer, offset, count);
}

#if NET6_0_OR_GREATER
#if NET
public override void Write(ReadOnlySpan<byte> buffer)
{
crc.Append(buffer);
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void AddPageFactory<TPage>(IPageFactory<TPage> pageFactory)
/// </summary>
/// <typeparam name="TPage"></typeparam>
/// <typeparam name="TPageFactory"></typeparam>
#if NET6_0_OR_GREATER
#if NET
public void AddPageFactory<TPage, [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] TPageFactory>() where TPageFactory : IPageFactory<TPage>
#else
public void AddPageFactory<TPage, TPageFactory>() where TPageFactory : IPageFactory<TPage>
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Polyfills/NullableAnnotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace System.Diagnostics.CodeAnalysis;

#if NETSTANDARD2_0 || NETFRAMEWORK
#if !NET
internal sealed class DoesNotReturnAttribute : Attribute { }

[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Polyfills/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if !NET

namespace System.IO;

Expand Down
Loading

0 comments on commit da44e1a

Please sign in to comment.