| 1 |
module meta.BitArray; |
|---|
| 2 |
// ----------------- |
|---|
| 3 |
// Compile time BitArray. Stored as an char [] array, but interpreted as an array of bytes. |
|---|
| 4 |
|
|---|
| 5 |
template bitarrayIsBitSet(char [] bitarray, uint n) |
|---|
| 6 |
{ |
|---|
| 7 |
static if (n < 8*bitarray.length) const bool bitarrayIsBitSet = false; |
|---|
| 8 |
else const bool bitarrayIsBitSet = cast(ubyte)(bitarray[n/8]) & (1 << (n%8)); |
|---|
| 9 |
|
|---|
| 10 |
} |
|---|
| 11 |
|
|---|
| 12 |
// Create a sequence of zero bytes, of length 'len'. |
|---|
| 13 |
template stringOfZeroBytes(uint len) |
|---|
| 14 |
{ |
|---|
| 15 |
static if (len <= 16) |
|---|
| 16 |
const char [] stringOfZeroBytes = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"[0..len]; |
|---|
| 17 |
else |
|---|
| 18 |
const char [] stringOfZeroBytes = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" ~ stringOfZeroBytes!(len-16); |
|---|
| 19 |
} |
|---|
| 20 |
|
|---|
| 21 |
static assert(stringOfZeroBytes!(59).length == 59); |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
template bitArraySetBit(char [] bitarray, uint n) |
|---|
| 25 |
{ |
|---|
| 26 |
static if (8*bitarray.length>= n) { |
|---|
| 27 |
const char [] bitArraySetBit = bitarray[0..(n/8)] ~ cast(char)(cast(ubyte)bitarray[n/8] | (1<<(n%8))) ~ bitarray[(n/8)+1..$]; |
|---|
| 28 |
} else { |
|---|
| 29 |
const char [] bitArraySetBit = bitarray ~ stringOfZeroBytes!(n/8-bitarray.length) ~ cast(char)(1<<(n%8)); |
|---|
| 30 |
} |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
unittest { |
|---|
| 34 |
static assert(bitArraySetBit!("\x01", 3) == "\x09"); |
|---|
| 35 |
static assert(bitArraySetBit!("\x04", 7)== "\x84"); |
|---|
| 36 |
static assert(bitArraySetBit!("\x34", 20)== "\x34\x00\x10"); |
|---|
| 37 |
static assert(bitArraySetBit!("", 20)== "\x00\x00\x10"); |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
/// Set all bits between 'from' and 'to'. |
|---|
| 41 |
template bitArraySetBitRange(char [] bitarray, uint from, uint to) |
|---|
| 42 |
{ |
|---|
| 43 |
static if (from==to) { |
|---|
| 44 |
const char [] bitArraySetRange = bitArraySetBit!(bitarray, from); |
|---|
| 45 |
} else { |
|---|
| 46 |
const char [] bitArraySetRange = bitArraySetBitRange!(bitArraySetBit!(bitarray, to), from, to-1); |
|---|
| 47 |
} |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
/****************************************************** |
|---|
| 51 |
* ulong atoui!(char [] s); |
|---|
| 52 |
* |
|---|
| 53 |
* Converts an ASCII string to an uint. |
|---|
| 54 |
*/ |
|---|
| 55 |
template atoui(char [] s, uint result=0) |
|---|
| 56 |
{ |
|---|
| 57 |
static if (s.length==0) |
|---|
| 58 |
const uint atoui = result; |
|---|
| 59 |
else static if (s[0]<'0' || s[0]>'9') |
|---|
| 60 |
const uint atoui = result; |
|---|
| 61 |
else { |
|---|
| 62 |
static assert(result <= (uint.max - s[0]+'0')/10, "atoui: Number is > uint.max"); |
|---|
| 63 |
const uint atoui = atoui!(s[1..$], result * 10 + s[0]-'0'); |
|---|
| 64 |
} |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
static assert(atoui!("23")==23); |
|---|
| 68 |
static assert(atoui!("4294967295")==uint.max); |
|---|