-
Notifications
You must be signed in to change notification settings - Fork 138
/
KeyBytes.cs
179 lines (157 loc) · 6.28 KB
/
KeyBytes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using System.Text;
namespace Libplanet.Store.Trie
{
/// <summary>
/// Wraps a byte array and provides equality comparison and hash code calculation. Designed
/// to be used as a key in dictionaries.
/// </summary>
public readonly struct KeyBytes
: IEquatable<KeyBytes>, IEquatable<ImmutableArray<byte>>, IEquatable<byte[]>
{
private readonly ImmutableArray<byte> _byteArray;
/// <summary>
/// Creates a new <see cref="KeyBytes"/> instance from the given byte array.
/// </summary>
/// <param name="bytes">A mutable byte array to wrap.</param>
public KeyBytes(params byte[] bytes)
: this(ImmutableArray.Create(bytes))
{
}
/// <summary>
/// Creates a new <see cref="KeyBytes"/> instance from the given byte array.
/// </summary>
/// <param name="bytes">An immutable byte array to wrap.</param>
public KeyBytes(in ImmutableArray<byte> bytes)
{
_byteArray = bytes;
}
/// <summary>
/// Creates a new <seealso cref="KeyBytes"/> instance from the given <paramref
/// name="string"/>.
/// </summary>
/// <param name="string">A key string. This is encoded to bytes.</param>
/// <param name="encoding">The text encoding used for the key string.</param>
public KeyBytes(string @string, Encoding encoding)
{
byte[] neverReusedBuffer = encoding.GetBytes(@string);
ImmutableArray<byte> movedImmutable =
Unsafe.As<byte[], ImmutableArray<byte>>(ref neverReusedBuffer);
_byteArray = movedImmutable;
}
/// <summary>
/// The length of the byte array.
/// </summary>
public int Length => _byteArray.IsDefault ? 0 : ByteArray.Length;
/// <summary>
/// The immutable array of bytes.
/// </summary>
public ImmutableArray<byte> ByteArray => _byteArray.IsDefault
? ImmutableArray<byte>.Empty
: _byteArray;
/// <summary>
/// The hexadecimal string representation of the byte array.
/// </summary>
public string Hex => _byteArray.IsDefaultOrEmpty
? string.Empty
: ByteUtil.Hex(_byteArray);
/// <summary>
/// Compares two <see cref="KeyBytes"/> values.
/// </summary>
/// <param name="left">An operand.</param>
/// <param name="right">Another operand.</param>
/// <returns><see langword="true"/> if two values equal; otherwise <see langword="false"/>.
/// </returns>
public static bool operator ==(KeyBytes left, KeyBytes right) => left.Equals(right);
/// <summary>
/// Compares two <see cref="KeyBytes"/> values.
/// </summary>
/// <param name="left">An operand.</param>
/// <param name="right">Another operand.</param>
/// <returns><see langword="false"/> if two values equal; otherwise <see langword="true"/>.
/// </returns>
public static bool operator !=(KeyBytes left, KeyBytes right) => !left.Equals(right);
/// <summary>
/// Parses the given hexadecimal string as bytes and returns a new <see cref="KeyBytes"/>
/// instance.
/// </summary>
/// <param name="hex">A hexadecimal string which encodes bytes.</param>
/// <returns>A new <see cref="KeyBytes"/> instance.</returns>
/// <exception cref="ArgumentNullException">Thrown when the given <paramref name="hex"/>
/// string is <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the length of the given
/// <paramref name="hex"/> string is an odd number.</exception>
/// <exception cref="FormatException">Thrown when the given <paramref name="hex"/> string is
/// not a valid hexadecimal string.</exception>
public static KeyBytes FromHex(string hex) =>
new KeyBytes(ByteUtil.ParseHex(hex));
/// <summary>
/// Converts to a mutable byte array.
/// </summary>
/// <returns>A new copy of mutable byte array.</returns>
public byte[] ToByteArray() => ByteArray.IsDefault
? Array.Empty<byte>()
: ByteArray.ToBuilder().ToArray();
/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
public bool Equals(ImmutableArray<byte> other)
{
if (other.IsDefaultOrEmpty)
{
return _byteArray.IsDefaultOrEmpty;
}
else if (Length != other.Length)
{
return false;
}
for (int i = 0; i < Length; i++)
{
if (_byteArray[i] != other[i])
{
return false;
}
}
return true;
}
/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
public bool Equals(KeyBytes other) => Equals(other._byteArray);
/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
public bool Equals(byte[]? other)
{
if (other is { } o && o.Length == Length)
{
for (int i = 0; i < Length; i++)
{
if (_byteArray[i] != o[i])
{
return false;
}
}
return true;
}
return false;
}
/// <inheritdoc cref="object.Equals(object?)"/>
public override bool Equals(object? obj) => obj is KeyBytes other && Equals(other);
/// <inheritdoc cref="object.GetHashCode()"/>
public override int GetHashCode()
{
int hash = 17;
if (!_byteArray.IsDefaultOrEmpty)
{
foreach (byte b in _byteArray)
{
hash = unchecked(hash * (31 + b));
}
}
return hash;
}
/// <inheritdoc cref="object.ToString()"/>
public override string ToString()
{
string hex = Length > 0 ? $" {ByteUtil.Hex(_byteArray)}" : string.Empty;
return $"{nameof(KeyBytes)} ({Length} B){hex}";
}
}
}