mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-07-17 09:47:06 +08:00
Make IToken implement IEquatable<IToken>
This would allow us to deduplicate tokens, by comparing their content
This commit is contained in:
parent
9366aa2b37
commit
be7716eeea
@ -88,5 +88,26 @@
|
|||||||
|
|
||||||
return builder.ToString();
|
return builder.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!(obj is ArrayToken other))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (other.Length != Length)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (var index = 0; index < Length; ++index)
|
||||||
|
{
|
||||||
|
if (!Data[index].Equals(other[index]))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,17 +29,6 @@
|
|||||||
Data = data;
|
Data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override bool Equals(object obj)
|
|
||||||
{
|
|
||||||
if (!(obj is BooleanToken other))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return other.Data == Data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override int GetHashCode()
|
public override int GetHashCode()
|
||||||
{
|
{
|
||||||
@ -51,5 +40,19 @@
|
|||||||
{
|
{
|
||||||
return Data.ToString();
|
return Data.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!(obj is BooleanToken other))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return other.Data == Data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -25,5 +25,16 @@
|
|||||||
{
|
{
|
||||||
return Data;
|
return Data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
if (!(obj is CommentToken other))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return other.Data == Data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,6 +114,26 @@
|
|||||||
return new DictionaryToken(result);
|
return new DictionaryToken(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!(obj is DictionaryToken other))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Data.Count != other.Data.Count)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// TODO: Maybe consider using a sorted dictionary?
|
||||||
|
return Data.OrderBy(kvp => kvp.Key)
|
||||||
|
.SequenceEqual(other.Data.OrderBy(kvp => kvp.Key));
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
|
@ -13,5 +13,19 @@
|
|||||||
private EndOfLineToken()
|
private EndOfLineToken()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!(obj is EndOfLineToken other))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -138,6 +138,20 @@ namespace UglyToad.PdfPig.Tokens
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!(obj is HexToken other))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Data == other.Data;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Converts the binary data back to a hex string representation.
|
/// Converts the binary data back to a hex string representation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
namespace UglyToad.PdfPig.Tokens
|
using System;
|
||||||
|
|
||||||
|
namespace UglyToad.PdfPig.Tokens
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A marker interface for tokens from the PDF file contents.
|
/// A marker interface for tokens from the PDF file contents.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IToken
|
public interface IToken : IEquatable<IToken>
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -21,6 +21,20 @@
|
|||||||
Data = data;
|
Data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!(obj is IndirectReferenceToken other))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Data.Equals(other.Data);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
|
@ -18,5 +18,30 @@
|
|||||||
{
|
{
|
||||||
Data = data;
|
Data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!(obj is InlineImageDataToken other))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Data.Count != other.Data.Count)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Note: This maybe slow?
|
||||||
|
// Maybe pre calculate some sort of hash and compare that?
|
||||||
|
for (var index = 0; index < Data.Count; ++index)
|
||||||
|
{
|
||||||
|
if (Data[index] != other.Data[index])
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -58,6 +58,12 @@
|
|||||||
return string.Equals(Data, other?.Data);
|
return string.Equals(Data, other?.Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
return Equals(obj as NameToken);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override int GetHashCode()
|
public override int GetHashCode()
|
||||||
{
|
{
|
||||||
|
@ -34,6 +34,12 @@
|
|||||||
return Equals(Data, other.Data);
|
return Equals(Data, other.Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
return obj is NullToken;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override int GetHashCode()
|
public override int GetHashCode()
|
||||||
{
|
{
|
||||||
|
@ -106,6 +106,20 @@
|
|||||||
Data = value;
|
Data = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!(obj is NumericToken other))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Data == other.Data;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override int GetHashCode()
|
public override int GetHashCode()
|
||||||
{
|
{
|
||||||
|
@ -37,6 +37,39 @@
|
|||||||
Data = data ?? throw new ArgumentNullException(nameof(data));
|
Data = data ?? throw new ArgumentNullException(nameof(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!(obj is ObjectToken other))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// I don't think we should compare this value...
|
||||||
|
// If this values are the same but this object are not the same (reference)
|
||||||
|
// It means that one of the obj is not from stream or one of the object have been overwritten
|
||||||
|
// So maybe putting an assert instead, would be a good idea?
|
||||||
|
if (Position != other.Position)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Number.Equals(other.Number))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Data.Equals(other.Data))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
|
@ -195,6 +195,20 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!(obj is OperatorToken other))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Data == other.Data;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override int GetHashCode()
|
public override int GetHashCode()
|
||||||
{
|
{
|
||||||
|
@ -29,7 +29,34 @@
|
|||||||
StreamDictionary = streamDictionary ?? throw new ArgumentNullException(nameof(streamDictionary));
|
StreamDictionary = streamDictionary ?? throw new ArgumentNullException(nameof(streamDictionary));
|
||||||
Data = data ?? throw new ArgumentNullException(nameof(data));
|
Data = data ?? throw new ArgumentNullException(nameof(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!(obj is StreamToken other))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!StreamDictionary.Equals(other.StreamDictionary))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: This maybe slow?
|
||||||
|
// Maybe pre calculate some sort of hash and compare that?
|
||||||
|
for (var index = 0; index < Data.Count; ++index)
|
||||||
|
{
|
||||||
|
if (Data[index] != other.Data[index])
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
|
@ -58,6 +58,25 @@ namespace UglyToad.PdfPig.Tokens
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool Equals(IToken obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!(obj is StringToken other))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!EncodedWith.Equals(other.EncodedWith))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Data.Equals(other.Data);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user