@@ -15,14 +15,8 @@ namespace Libplanet.Store.Trie;
15
15
/// Creates a new <see cref="KeyBytes"/> instance from the given byte array.
16
16
/// </remarks>
17
17
/// <param name="bytes">An immutable byte array to wrap.</param>
18
- public readonly struct KeyBytes ( in ImmutableArray < byte > bytes ) : IEquatable < KeyBytes >
18
+ public readonly struct KeyBytes ( in ImmutableArray < byte > bytes ) : IEquatable < KeyBytes > , IFormattable
19
19
{
20
- /// <summary>
21
- /// The default <see cref="System.Text.Encoding"/>, which is <see cref="Encoding.UTF8"/>,ㅔ
22
- /// to use when creating an instance from a <see langword="string"/>.
23
- /// </summary>
24
- public static readonly Encoding Encoding = Encoding . UTF8 ;
25
-
26
20
public static readonly KeyBytes Empty = default ;
27
21
28
22
private readonly ImmutableArray < byte > _bytes = bytes ;
@@ -33,19 +27,7 @@ public readonly struct KeyBytes(in ImmutableArray<byte> bytes) : IEquatable<KeyB
33
27
/// </summary>
34
28
/// <param name="str">The key <see langword="string"/> to encode into bytes.</param>
35
29
public KeyBytes ( string str )
36
- : this ( str , Encoding )
37
- {
38
- }
39
-
40
- /// <summary>
41
- /// Creates a new <seealso cref="KeyBytes"/> instance from given <paramref name="str"/>
42
- /// with <paramref name="encoding"/>.
43
- /// </summary>
44
- /// <param name="str">The key <see langword="string"/> to encode into bytes.</param>
45
- /// <param name="encoding">The <see cref="System.Text.Encoding"/> to be used for
46
- /// <paramref name="str"/>.</param>
47
- private KeyBytes ( string str , Encoding encoding )
48
- : this ( CreateArray ( str , encoding ) )
30
+ : this ( CreateArray ( str ) )
49
31
{
50
32
}
51
33
@@ -59,11 +41,6 @@ private KeyBytes(string str, Encoding encoding)
59
41
/// </summary>
60
42
public ImmutableArray < byte > ByteArray => _bytes . IsDefault ? [ ] : _bytes;
61
43
62
- /// <summary>
63
- /// The hexadecimal string representation of the byte array.
64
- /// </summary>
65
- public string Hex => ByteUtil . Hex ( ByteArray ) ;
66
-
67
44
/// <summary>
68
45
/// Compares two <see cref="KeyBytes"/> values.
69
46
/// </summary>
@@ -138,9 +115,20 @@ public override string ToString()
138
115
return $ "{ nameof ( KeyBytes ) } ({ Length } B){ hex } ";
139
116
}
140
117
141
- private static ImmutableArray < byte > CreateArray ( string str , Encoding encoding )
118
+ /// <inheritdoc cref="IFormattable.ToString(string?, IFormatProvider?)"/>
119
+ public string ToString ( string ? format , IFormatProvider ? formatProvider )
120
+ {
121
+ return format switch
122
+ {
123
+ "h" => ByteUtil . Hex ( ByteArray ) ,
124
+ "H" => ByteUtil . Hex ( ByteArray ) . ToUpperInvariant ( ) ,
125
+ _ => ToString ( ) ,
126
+ } ;
127
+ }
128
+
129
+ private static ImmutableArray < byte > CreateArray ( string str )
142
130
{
143
- var bytes = encoding . GetBytes ( str ) ;
131
+ var bytes = Encoding . UTF8 . GetBytes ( str ) ;
144
132
return Unsafe . As < byte [ ] , ImmutableArray < byte > > ( ref bytes ) ;
145
133
}
146
134
}
0 commit comments