[Issue 12445] New: std.bitmanip.read should have overloads specifying the count of bytes to read

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Mar 23 09:05:28 PDT 2014


https://d.puremagic.com/issues/show_bug.cgi?id=12445

           Summary: std.bitmanip.read should have overloads specifying the
                    count of bytes to read
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: andrej.mitrovich at gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2014-03-23 09:05:24 PDT ---
This seems to be a common occurrence in D code:

-----
uint pop24bitsLE(R)(ref R input) if (isInputRange!R)
{
    ubyte b0 = popByte(input);
    ubyte b1 = popByte(input);
    ubyte b2 = popByte(input);
    return (b2 << 16) | (b1 << 8) | b0;
}
-----

std.bitmanip comes close with:

-----
import std.bitmanip;
uint u = range.read!(uint, Endian.littleEndian)();
-----

But this will actually read 4 bytes instead of 3. I propose we add an overload
or two via:

-----
// compile-time byte-count version
T read(T, Endian endianness = Endian.bigEndian, R, size_t bytes = T.sizeof)(ref
R range).

// run-time equivalent
T read(T, Endian endianness = Endian.bigEndian, R)(ref R range, size_t bytes =
T.sizeof).
-----

'read' would ensure 'bytes' does not exceed T.sizeof, but it would allow the
bytes to be smaller than T.sizeof. The usage would be:

-----
import std.bitmanip;

// run-time version
uint u = range.read!(uint, Endian.littleEndian)(3);

// or for the compile-time version:
alias pop24bitsLE = read!(uint, Endian.littleEndian, 3);
uint u = range.pop24bitsLE();
-----

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list