Skip to content

Commit b2e86a4

Browse files
committed
Switch from bits to byte for the argument
It doesn't make much sense to take the argument as a number of bits, if the utility itself cannot represent values which are not a multiple of 8. Instead, just accept a number of bytes and simplify the code. It also makes it much clearer that we're dealing with a glorified static array.
1 parent 5e0a2a4 commit b2e86a4

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

source/geod24/bitblob.d

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import std.utf;
2424
@nogc @safe pure nothrow unittest
2525
{
2626
/// Alias for a 256 bits / 32 byte hash type
27-
alias Hash = BitBlob!256;
27+
alias Hash = BitBlob!32;
2828

2929
import std.digest.sha;
3030
// Can be initialized from an `ubyte[32]`
@@ -46,26 +46,19 @@ import std.utf;
4646

4747
/*******************************************************************************
4848
49-
A value type representing a hash
49+
A value type representing a large binary value
5050
5151
Params:
52-
Bits = The size of the hash, in bits. Must be a multiple of 8.
52+
Size = The size of the hash, in bytes
5353
5454
*******************************************************************************/
5555

56-
public struct BitBlob (size_t Bits)
56+
public struct BitBlob (size_t Size)
5757
{
5858
@safe:
5959

60-
static assert (
61-
Bits % 8 == 0,
62-
"Argument to BitBlob must be a multiple of 8");
63-
64-
/// The width of this aggregate, in octets
65-
public static immutable Width = Bits / 8;
66-
6760
/// Convenience enum
68-
public enum StringBufferSize = (Width * 2 + 2);
61+
public enum StringBufferSize = (Size * 2 + 2);
6962

7063
/***************************************************************************
7164
@@ -125,22 +118,22 @@ public struct BitBlob (size_t Bits)
125118
Internally the data will be stored in little endian.
126119
127120
Throws:
128-
If `bin.length != typeof(this).Width`
121+
If `bin.length != typeof(this).sizeof`
129122
130123
***************************************************************************/
131124

132125
public this (scope const ubyte[] bin, bool isLE = true)
133126
{
134-
enum W = Width; // Make sure the value is shown, not the symbol
135-
if (bin.length != Width)
127+
enum W = Size; // Make sure the value is shown, not the symbol
128+
if (bin.length != Size)
136129
assert(0, "ubyte[] argument to " ~ typeof(this).stringof
137130
~ " constructor does not match the expected size of "
138131
~ W.stringof);
139132

140133
this.data[] = bin[];
141134
if (!isLE)
142135
{
143-
foreach (cnt; 0 .. Width / 2)
136+
foreach (cnt; 0 .. Size / 2)
144137
{
145138
// Not sure the frontend is clever enough to avoid bounds checks
146139
this.data[cnt] ^= this.data[$ - 1 - cnt];
@@ -160,23 +153,23 @@ public struct BitBlob (size_t Bits)
160153
Can be upper or lower case.
161154
162155
Throws:
163-
If `hexstr_without_prefix.length != (typeof(this).Width * 2)`.
156+
If `hexstr_without_prefix.length != (typeof(this).sizeof * 2)`.
164157
165158
***************************************************************************/
166159

167160
public this (scope const(char)[] hexstr)
168161
{
169-
enum W = Width; // Make sure the value is shown, not the symbol
162+
enum W = Size; // Make sure the value is shown, not the symbol
170163
enum ErrorMsg = "Length of string passed to " ~ typeof(this).stringof
171164
~ " constructor does not match the expected size of " ~ W.stringof;
172-
if (hexstr.length == (Width * 2) + "0x".length)
165+
if (hexstr.length == (Size * 2) + "0x".length)
173166
{
174167
assert(hexstr[0] == '0', ErrorMsg);
175168
assert(hexstr[1] == 'x' || hexstr[1] == 'X', ErrorMsg);
176169
hexstr = hexstr[2 .. $];
177170
}
178171
else
179-
assert(hexstr.length == (Width * 2), ErrorMsg);
172+
assert(hexstr.length == (Size * 2), ErrorMsg);
180173

181174
auto range = hexstr.byChar.map!(std.ascii.toLower!(char));
182175
size_t idx;
@@ -197,11 +190,11 @@ public struct BitBlob (size_t Bits)
197190

198191
static auto fromString (scope const(char)[] str)
199192
{
200-
return BitBlob!(Bits)(str);
193+
return BitBlob!(Size)(str);
201194
}
202195

203196
/// Store the internal data
204-
private ubyte[Width] data;
197+
private ubyte[Size] data;
205198

206199
/// Returns: If this BitBlob has any value
207200
public bool isNull () const
@@ -222,7 +215,7 @@ public struct BitBlob (size_t Bits)
222215
}
223216

224217
/// Ditto
225-
alias opDollar = Width;
218+
alias opDollar = Size;
226219

227220
/// Public because of a visibility bug
228221
public static ubyte fromHex (char c)
@@ -253,7 +246,7 @@ public struct BitBlob (size_t Bits)
253246

254247
pure @safe nothrow @nogc unittest
255248
{
256-
alias Hash = BitBlob!256;
249+
alias Hash = BitBlob!32;
257250

258251
Hash gen1 = GenesisBlockHashStr;
259252
Hash gen2 = GenesisBlockHash;
@@ -285,7 +278,7 @@ pure @safe nothrow @nogc unittest
285278
unittest
286279
{
287280
import std.format;
288-
alias Hash = BitBlob!256;
281+
alias Hash = BitBlob!32;
289282
Hash gen1 = GenesisBlockHashStr;
290283
assert(format("%s", gen1) == GenesisBlockHashStr);
291284
assert(gen1.toString() == GenesisBlockHashStr);
@@ -298,7 +291,7 @@ unittest
298291
{
299292
import core.memory;
300293
import std.format;
301-
alias Hash = BitBlob!256;
294+
alias Hash = BitBlob!32;
302295

303296
Hash gen1 = GenesisBlockHashStr;
304297
char[Hash.StringBufferSize] buffer;
@@ -315,7 +308,7 @@ unittest
315308
import std.algorithm.mutation : reverse;
316309
ubyte[32] genesis = GenesisBlockHash;
317310
genesis[].reverse;
318-
auto h = BitBlob!(256)(genesis, false);
311+
auto h = BitBlob!(32)(genesis, false);
319312
assert(h.toString() == GenesisBlockHashStr);
320313
}
321314

@@ -325,7 +318,7 @@ unittest
325318
import core.exception : AssertError;
326319
import std.algorithm.mutation : reverse;
327320
import std.exception;
328-
alias Hash = BitBlob!(256);
321+
alias Hash = BitBlob!(32);
329322
ubyte[32] genesis = GenesisBlockHash;
330323
genesis[].reverse;
331324
Hash result;
@@ -338,7 +331,7 @@ unittest
338331
import core.exception : AssertError;
339332
import std.algorithm.mutation : reverse;
340333
import std.exception;
341-
alias Hash = BitBlob!(256);
334+
alias Hash = BitBlob!(32);
342335
ubyte[32] genesis = GenesisBlockHash;
343336
genesis[].reverse;
344337
Hash h = Hash(genesis, false);
@@ -350,7 +343,7 @@ unittest
350343
// Ditto
351344
unittest
352345
{
353-
alias Hash = BitBlob!(256);
346+
alias Hash = BitBlob!(32);
354347
import core.exception : AssertError;
355348
import std.exception;
356349
char[GenesisBlockHashStr.length] buff = GenesisBlockHashStr;
@@ -362,14 +355,14 @@ unittest
362355
// Make sure the string parsing works at CTFE
363356
unittest
364357
{
365-
static immutable BitBlob!256 CTFEability = BitBlob!256(GenesisBlockHashStr);
358+
static immutable BitBlob!32 CTFEability = BitBlob!32(GenesisBlockHashStr);
366359
static assert(CTFEability[] == GenesisBlockHash);
367360
}
368361

369362
// Support for rvalue opCmp
370363
unittest
371364
{
372-
alias Hash = BitBlob!(256);
365+
alias Hash = BitBlob!(32);
373366
import std.algorithm.sorting : sort;
374367

375368
static Hash getLValue(int) { return Hash.init; }

0 commit comments

Comments
 (0)