BitBlob

A value type representing a large binary value

Constructors

this
this(ubyte[] bin, bool isLE)

Create a BitBlob from binary data, e.g. serialized data

this
this(const(char)[] hexstr)

Create a BitBlob from an hexadecimal string representation

Members

Aliases

opDollar
alias opDollar = Size

Convenience overload

Functions

isNull
bool isNull()
opCmp
int opCmp(typeof(this) s)

Support for comparison (rvalue overload)

opCmp
int opCmp(typeof(this) s)

Support for comparison

opIndex
inout(ubyte)[] opIndex()

Used for sha256Of

opSlice
inout(ubyte)[] opSlice(size_t from, size_t to)

Convenience overload

toString
void toString(void delegate(const(char)[]) @(safe) sink)
void toString(void delegate(const(char)[]) @(safe) sink, FormatSpec!char spec)

Format the hash as a lowercase hex string

toString
string toString()

Get the string representation of this hash

Manifest constants

StringBufferSize
enum StringBufferSize;

Convenience enum

Static functions

fromHex
ubyte fromHex(char c)

Public because of a visibility bug

fromString
auto fromString(const(char)[] str)

Support deserialization

Parameters

Size

The size of the hash, in bytes

Examples

Test toString

import std.string : toUpper;

alias Hash = BitBlob!32;
Hash gen1 = GenesisBlockHashStr;
assert(format("%s", gen1) == GenesisBlockHashStr);
assert(format("%x", gen1) == GenesisBlockHashStr[2 .. $]);
assert(format("%X", gen1) == GenesisBlockHashStr[2 .. $].toUpper());
assert(format("%w", gen1) == GenesisBlockHashStr);
assert(gen1.toString() == GenesisBlockHashStr);
assert(Hash(gen1.toString()) == gen1);
assert(Hash.fromString(gen1.toString()) == gen1);

Make sure toString does not allocate even if it's not @nogc

import core.memory;
alias Hash = BitBlob!32;

Hash gen1 = GenesisBlockHashStr;
char[Hash.StringBufferSize] buffer;
auto statsBefore = GC.stats();
formattedWrite(buffer[], "%s", gen1);
auto statsAfter = GC.stats();
assert(buffer == GenesisBlockHashStr);
assert(statsBefore.usedSize == statsAfter.usedSize);

Test initialization from big endian

import std.algorithm.mutation : reverse;
ubyte[32] genesis = GenesisBlockHash;
genesis[].reverse;
auto h = BitBlob!(32)(genesis, false);
assert(h.toString() == GenesisBlockHashStr);

Meta