mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-09-18 18:27:55 +08:00
Use ReferenceEquals to avoid infinite recursion
Fix InlineImageDataToken not checking Data length before comparing bytes Use a more straightforward compare Dictionary Content
This commit is contained in:
@@ -92,19 +92,27 @@
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IToken obj)
|
||||
{
|
||||
if (this == obj)
|
||||
if (ReferenceEquals(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;
|
||||
|
@@ -44,8 +44,10 @@
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IToken obj)
|
||||
{
|
||||
if (this == obj)
|
||||
if (ReferenceEquals(this, obj))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj is BooleanToken other))
|
||||
{
|
||||
|
@@ -29,6 +29,11 @@
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IToken obj)
|
||||
{
|
||||
if (ReferenceEquals(this, obj))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj is CommentToken other))
|
||||
{
|
||||
return false;
|
||||
|
@@ -118,8 +118,10 @@
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IToken obj)
|
||||
{
|
||||
if (this == obj)
|
||||
if (ReferenceEquals(this, obj))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj is DictionaryToken other))
|
||||
{
|
||||
@@ -127,11 +129,19 @@
|
||||
}
|
||||
|
||||
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));
|
||||
foreach (var kvp in other.Data)
|
||||
{
|
||||
if (!Data.TryGetValue(kvp.Key, out var val) || !val.Equals(kvp.Value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
@@ -17,15 +17,12 @@
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IToken obj)
|
||||
{
|
||||
if (this == obj)
|
||||
return true;
|
||||
|
||||
if (!(obj is EndOfLineToken other))
|
||||
if (ReferenceEquals(this, obj))
|
||||
{
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
return obj is EndOfLineToken;
|
||||
}
|
||||
}
|
||||
}
|
@@ -141,8 +141,10 @@ namespace UglyToad.PdfPig.Tokens
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IToken obj)
|
||||
{
|
||||
if (this == obj)
|
||||
if (ReferenceEquals(this, obj))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj is HexToken other))
|
||||
{
|
||||
|
@@ -24,8 +24,10 @@
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IToken obj)
|
||||
{
|
||||
if (this == obj)
|
||||
if (ReferenceEquals(this, obj))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj is IndirectReferenceToken other))
|
||||
{
|
||||
|
@@ -22,8 +22,10 @@
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IToken obj)
|
||||
{
|
||||
if (this == obj)
|
||||
if (ReferenceEquals(this, obj))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj is InlineImageDataToken other))
|
||||
{
|
||||
@@ -31,14 +33,17 @@
|
||||
}
|
||||
|
||||
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;
|
||||
|
@@ -109,8 +109,10 @@
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IToken obj)
|
||||
{
|
||||
if (this == obj)
|
||||
if (ReferenceEquals(this, obj))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj is NumericToken other))
|
||||
{
|
||||
|
@@ -40,34 +40,17 @@
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IToken obj)
|
||||
{
|
||||
if (this == obj)
|
||||
if (ReferenceEquals(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;
|
||||
return Number.Equals(other.Number) && Data.Equals(other.Data);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
@@ -198,8 +198,10 @@
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IToken obj)
|
||||
{
|
||||
if (this == obj)
|
||||
if (ReferenceEquals(this, obj))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj is OperatorToken other))
|
||||
{
|
||||
|
@@ -33,8 +33,10 @@
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IToken obj)
|
||||
{
|
||||
if (this == obj)
|
||||
if (ReferenceEquals(this, obj))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj is StreamToken other))
|
||||
{
|
||||
@@ -46,12 +48,17 @@
|
||||
return false;
|
||||
}
|
||||
|
||||
// Note: This maybe slow?
|
||||
// Maybe pre calculate some sort of hash and compare that?
|
||||
if (Data.Count != other.Data.Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var index = 0; index < Data.Count; ++index)
|
||||
{
|
||||
if (Data[index] != other.Data[index])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@@ -61,20 +61,17 @@ namespace UglyToad.PdfPig.Tokens
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IToken obj)
|
||||
{
|
||||
if (this == obj)
|
||||
if (ReferenceEquals(this, obj))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(obj is StringToken other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!EncodedWith.Equals(other.EncodedWith))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return Data.Equals(other.Data);
|
||||
return EncodedWith.Equals(other.EncodedWith) && Data.Equals(other.Data);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
Reference in New Issue
Block a user