|
| 1 | +namespace PacketGen.Generators; |
| 2 | + |
| 3 | +internal sealed class PacketDeepObjectHelperBuilder |
| 4 | +{ |
| 5 | + public string BuildDeepEqualsHelper(bool include) |
| 6 | + { |
| 7 | + if (!include) |
| 8 | + return string.Empty; |
| 9 | + |
| 10 | + return """ |
| 11 | +
|
| 12 | + private static bool DeepEquals(object? left, object? right) |
| 13 | + { |
| 14 | + if (object.ReferenceEquals(left, right)) |
| 15 | + return true; |
| 16 | +
|
| 17 | + if (left is null || right is null) |
| 18 | + return false; |
| 19 | +
|
| 20 | + if (left is string && right is string) |
| 21 | + return string.Equals((string)left, (string)right); |
| 22 | +
|
| 23 | + if (left is string || right is string) |
| 24 | + return false; |
| 25 | +
|
| 26 | + if (left is IDictionary leftDict && right is IDictionary rightDict) |
| 27 | + return DictionaryEquals(leftDict, rightDict); |
| 28 | +
|
| 29 | + if (left is Array leftArray && right is Array rightArray) |
| 30 | + return ArrayEquals(leftArray, rightArray); |
| 31 | +
|
| 32 | + if (left is IEnumerable leftEnumerable && right is IEnumerable rightEnumerable) |
| 33 | + return SequenceEquals(leftEnumerable, rightEnumerable); |
| 34 | +
|
| 35 | + return left.Equals(right); |
| 36 | + } |
| 37 | +
|
| 38 | + private static bool ArrayEquals(Array left, Array right) |
| 39 | + { |
| 40 | + if (left.Length != right.Length) |
| 41 | + return false; |
| 42 | +
|
| 43 | + Type? elementType = left.GetType().GetElementType(); |
| 44 | + if (elementType != null && elementType != typeof(object) && !typeof(IEnumerable).IsAssignableFrom(elementType)) |
| 45 | + return StructuralEqualityComparer.Equals(left, right); |
| 46 | +
|
| 47 | + return SequenceEquals(left, right); |
| 48 | + } |
| 49 | +
|
| 50 | + private static bool DictionaryEquals(IDictionary left, IDictionary right) |
| 51 | + { |
| 52 | + if (left.Count != right.Count) |
| 53 | + return false; |
| 54 | +
|
| 55 | + foreach (DictionaryEntry entry in left) |
| 56 | + { |
| 57 | + if (!right.Contains(entry.Key)) |
| 58 | + return false; |
| 59 | +
|
| 60 | + if (!DeepEquals(entry.Value, right[entry.Key])) |
| 61 | + return false; |
| 62 | + } |
| 63 | +
|
| 64 | + return true; |
| 65 | + } |
| 66 | +
|
| 67 | + private static bool SequenceEquals(IEnumerable left, IEnumerable right) |
| 68 | + { |
| 69 | + if (left is ICollection leftCollection && right is ICollection rightCollection && leftCollection.Count != rightCollection.Count) |
| 70 | + return false; |
| 71 | +
|
| 72 | + IEnumerator leftEnumerator = left.GetEnumerator(); |
| 73 | + IEnumerator rightEnumerator = right.GetEnumerator(); |
| 74 | +
|
| 75 | + while (true) |
| 76 | + { |
| 77 | + bool leftNext = leftEnumerator.MoveNext(); |
| 78 | + bool rightNext = rightEnumerator.MoveNext(); |
| 79 | +
|
| 80 | + if (leftNext != rightNext) |
| 81 | + return false; |
| 82 | +
|
| 83 | + if (!leftNext) |
| 84 | + return true; |
| 85 | +
|
| 86 | + if (!DeepEquals(leftEnumerator.Current, rightEnumerator.Current)) |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | +"""; |
| 91 | + } |
| 92 | + |
| 93 | + public string BuildDeepHashHelper(bool include, int hashSeedValue, int hashMultiplierSecondary) |
| 94 | + { |
| 95 | + if (!include) |
| 96 | + return string.Empty; |
| 97 | + |
| 98 | + return $$""" |
| 99 | +
|
| 100 | + private static int DeepHash(object? value) |
| 101 | + { |
| 102 | + if (value is null) |
| 103 | + return 0; |
| 104 | +
|
| 105 | + if (value is string) |
| 106 | + return value.GetHashCode(); |
| 107 | +
|
| 108 | + if (value is IDictionary dict) |
| 109 | + return DictionaryHash(dict); |
| 110 | +
|
| 111 | + if (value is Array array) |
| 112 | + return ArrayHash(array); |
| 113 | +
|
| 114 | + if (value is IEnumerable enumerable) |
| 115 | + return SequenceHash(enumerable); |
| 116 | +
|
| 117 | + return value.GetHashCode(); |
| 118 | + } |
| 119 | +
|
| 120 | + private static int ArrayHash(Array array) |
| 121 | + { |
| 122 | + Type? elementType = array.GetType().GetElementType(); |
| 123 | + if (elementType != null && elementType != typeof(object) && !typeof(IEnumerable).IsAssignableFrom(elementType)) |
| 124 | + return StructuralEqualityComparer.GetHashCode(array); |
| 125 | +
|
| 126 | + return SequenceHash(array); |
| 127 | + } |
| 128 | +
|
| 129 | + private static int DictionaryHash(IDictionary dict) |
| 130 | + { |
| 131 | + int hash = dict.Count; |
| 132 | +
|
| 133 | + foreach (DictionaryEntry entry in dict) |
| 134 | + { |
| 135 | + int entryHash = {{hashSeedValue}}; |
| 136 | + entryHash = (entryHash * {{hashMultiplierSecondary}}) ^ DeepHash(entry.Key); |
| 137 | + entryHash = (entryHash * {{hashMultiplierSecondary}}) ^ DeepHash(entry.Value); |
| 138 | + hash ^= entryHash; |
| 139 | + } |
| 140 | +
|
| 141 | + return hash; |
| 142 | + } |
| 143 | +
|
| 144 | + private static int SequenceHash(IEnumerable sequence) |
| 145 | + { |
| 146 | + int hash = {{hashSeedValue}}; |
| 147 | +
|
| 148 | + foreach (object item in sequence) |
| 149 | + { |
| 150 | + hash = (hash * {{hashMultiplierSecondary}}) ^ DeepHash(item); |
| 151 | + } |
| 152 | +
|
| 153 | + return hash; |
| 154 | + } |
| 155 | +"""; |
| 156 | + } |
| 157 | +} |
0 commit comments