|
| 1 | +// Copyright (C) 2015-2024 The Neo Project. |
| 2 | +// |
| 3 | +// OldUInt160.cs file belongs to the neo project and is free |
| 4 | +// software distributed under the MIT software license, see the |
| 5 | +// accompanying file LICENSE in the main directory of the |
| 6 | +// repository or http://www.opensource.org/licenses/mit-license.php |
| 7 | +// for more details. |
| 8 | +// |
| 9 | +// Redistribution and use in source and binary forms with or without |
| 10 | +// modifications are permitted. |
| 11 | + |
| 12 | +using Neo.Extensions; |
| 13 | +using Neo.IO; |
| 14 | +using System.Globalization; |
| 15 | +using System.Runtime.InteropServices; |
| 16 | + |
| 17 | +namespace Neo |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Represents a 160-bit unsigned integer. |
| 21 | + /// </summary> |
| 22 | + [StructLayout(LayoutKind.Explicit, Size = 20)] |
| 23 | + public class OldUInt160 : IComparable<OldUInt160>, IEquatable<OldUInt160>, ISerializable |
| 24 | + { |
| 25 | + /// <summary> |
| 26 | + /// The length of <see cref="OldUInt160"/> values. |
| 27 | + /// </summary> |
| 28 | + public const int Length = 20; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Represents 0. |
| 32 | + /// </summary> |
| 33 | + public static readonly OldUInt160 Zero = new(); |
| 34 | + |
| 35 | + [FieldOffset(0)] private ulong value1; |
| 36 | + [FieldOffset(8)] private ulong value2; |
| 37 | + [FieldOffset(16)] private uint value3; |
| 38 | + |
| 39 | + public int Size => Length; |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Initializes a new instance of the <see cref="OldUInt160"/> class. |
| 43 | + /// </summary> |
| 44 | + public OldUInt160() |
| 45 | + { |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Initializes a new instance of the <see cref="OldUInt160"/> class. |
| 50 | + /// </summary> |
| 51 | + /// <param name="value">The value of the <see cref="OldUInt160"/>.</param> |
| 52 | + public unsafe OldUInt160(ReadOnlySpan<byte> value) |
| 53 | + { |
| 54 | + if (value.Length != Length) throw new FormatException(); |
| 55 | + fixed (ulong* p = &value1) |
| 56 | + { |
| 57 | + Span<byte> dst = new(p, Length); |
| 58 | + value[..Length].CopyTo(dst); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public int CompareTo(OldUInt160 other) |
| 63 | + { |
| 64 | + int result = value3.CompareTo(other.value3); |
| 65 | + if (result != 0) return result; |
| 66 | + result = value2.CompareTo(other.value2); |
| 67 | + if (result != 0) return result; |
| 68 | + return value1.CompareTo(other.value1); |
| 69 | + } |
| 70 | + |
| 71 | + public void Deserialize(ref MemoryReader reader) |
| 72 | + { |
| 73 | + value1 = reader.ReadUInt64(); |
| 74 | + value2 = reader.ReadUInt64(); |
| 75 | + value3 = reader.ReadUInt32(); |
| 76 | + } |
| 77 | + |
| 78 | + public override bool Equals(object obj) |
| 79 | + { |
| 80 | + if (ReferenceEquals(obj, this)) return true; |
| 81 | + return Equals(obj as OldUInt160); |
| 82 | + } |
| 83 | + |
| 84 | + public bool Equals(OldUInt160 other) |
| 85 | + { |
| 86 | + if (other is null) return false; |
| 87 | + return value1 == other.value1 |
| 88 | + && value2 == other.value2 |
| 89 | + && value3 == other.value3; |
| 90 | + } |
| 91 | + |
| 92 | + public override int GetHashCode() |
| 93 | + { |
| 94 | + return (int)value1; |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Parses an <see cref="OldUInt160"/> from the specified <see cref="string"/>. |
| 99 | + /// </summary> |
| 100 | + /// <param name="value">An <see cref="OldUInt160"/> represented by a <see cref="string"/>.</param> |
| 101 | + /// <returns>The parsed <see cref="OldUInt160"/>.</returns> |
| 102 | + /// <exception cref="FormatException"><paramref name="value"/> is not in the correct format.</exception> |
| 103 | + public static OldUInt160 Parse(string value) |
| 104 | + { |
| 105 | + if (!TryParse(value, out var result)) throw new FormatException(); |
| 106 | + return result; |
| 107 | + } |
| 108 | + |
| 109 | + public void Serialize(BinaryWriter writer) |
| 110 | + { |
| 111 | + writer.Write(value1); |
| 112 | + writer.Write(value2); |
| 113 | + writer.Write(value3); |
| 114 | + } |
| 115 | + |
| 116 | + public override string ToString() |
| 117 | + { |
| 118 | + return "0x" + this.ToArray().ToHexString(reverse: true); |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Parses an <see cref="OldUInt160"/> from the specified <see cref="string"/>. |
| 123 | + /// </summary> |
| 124 | + /// <param name="s">An <see cref="OldUInt160"/> represented by a <see cref="string"/>.</param> |
| 125 | + /// <param name="result">The parsed <see cref="OldUInt160"/>.</param> |
| 126 | + /// <returns><see langword="true"/> if an <see cref="OldUInt160"/> is successfully parsed; otherwise, <see langword="false"/>.</returns> |
| 127 | + public static bool TryParse(string s, out OldUInt160 result) |
| 128 | + { |
| 129 | + if (s == null) |
| 130 | + { |
| 131 | + result = null; |
| 132 | + return false; |
| 133 | + } |
| 134 | + if (s.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase)) |
| 135 | + s = s[2..]; |
| 136 | + if (s.Length != Length * 2) |
| 137 | + { |
| 138 | + result = null; |
| 139 | + return false; |
| 140 | + } |
| 141 | + byte[] data = new byte[Length]; |
| 142 | + for (int i = 0; i < Length; i++) |
| 143 | + if (!byte.TryParse(s.Substring(i * 2, 2), NumberStyles.AllowHexSpecifier, null, out data[Length - i - 1])) |
| 144 | + { |
| 145 | + result = null; |
| 146 | + return false; |
| 147 | + } |
| 148 | + result = new OldUInt160(data); |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + public static bool operator ==(OldUInt160 left, OldUInt160 right) |
| 153 | + { |
| 154 | + if (ReferenceEquals(left, right)) return true; |
| 155 | + if (left is null || right is null) return false; |
| 156 | + return left.Equals(right); |
| 157 | + } |
| 158 | + |
| 159 | + public static bool operator !=(OldUInt160 left, OldUInt160 right) |
| 160 | + { |
| 161 | + return !(left == right); |
| 162 | + } |
| 163 | + |
| 164 | + public static bool operator >(OldUInt160 left, OldUInt160 right) |
| 165 | + { |
| 166 | + return left.CompareTo(right) > 0; |
| 167 | + } |
| 168 | + |
| 169 | + public static bool operator >=(OldUInt160 left, OldUInt160 right) |
| 170 | + { |
| 171 | + return left.CompareTo(right) >= 0; |
| 172 | + } |
| 173 | + |
| 174 | + public static bool operator <(OldUInt160 left, OldUInt160 right) |
| 175 | + { |
| 176 | + return left.CompareTo(right) < 0; |
| 177 | + } |
| 178 | + |
| 179 | + public static bool operator <=(OldUInt160 left, OldUInt160 right) |
| 180 | + { |
| 181 | + return left.CompareTo(right) <= 0; |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments