Re: problems with std.bitmanip.append (bug?)

2015-03-26 Thread Hugo via Digitalmars-d-learn
On Thursday, 26 March 2015 at 02:39:56 UTC, Steven Schveighoffer 
wrote:


An array as an output range writes to the front. You can use 
std.array.Appender to get appending behavior. I know, it's 
weird.


Alternatively, you can add more bytes to the array, and append 
to the slice, but that may be ugly/hard to do.


-Steve


Hmm... isnt that't what the std.bitmanip.write function is for? 
It even provides an index.


I could make an 8 byte buffer and then make a slice with the last 
4 bytes and use append there, but it would be rather a hack 
around something that should have worked.


Perhaps I have found a bug. Actually I am not sure because I am 
not yet familiar with the way to use templates, so there is the 
possibility that I am using incorrect arguments.


If only the documentation and/or test units were more clear...





Re: problems with std.bitmanip.append (bug?)

2015-03-26 Thread Hugo via Digitalmars-d-learn

On Thursday, 26 March 2015 at 10:07:07 UTC, Hugo wrote:


If only the documentation and/or test units were more clear...


OK, I made a simpler test, using an example from the 
documentation:



void main() {
   import std.stdio, std.array, std.bitmanip;
   auto buffer = appender!(const ubyte[])();
   buffer.append!ushort(261);
   assert(buffer.data == [1, 5]);
   writefln("%s", buffer.data);
}

It seems to work, so apparently one has to explicitly create a 
buffer with the appender template. Not terribly useful IMHO.


Wouldn't it be possible for the append function to automaticaly 
change the mode of an already existing buffer?


Also, can anyone provide a similar example but using little 
endian order? If only to contrast differences between modes of 
invocation...


Re: problems with std.bitmanip.append (bug?)

2015-03-26 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/26/15 6:07 AM, Hugo wrote:

On Thursday, 26 March 2015 at 02:39:56 UTC, Steven Schveighoffer wrote:


An array as an output range writes to the front. You can use
std.array.Appender to get appending behavior. I know, it's weird.

Alternatively, you can add more bytes to the array, and append to the
slice, but that may be ugly/hard to do.



Hmm... isnt that't what the std.bitmanip.write function is for? It even
provides an index.


Quite possibly write and append do the same thing for arrays because of 
the way slices support the output range idiom.



Perhaps I have found a bug. Actually I am not sure because I am not yet
familiar with the way to use templates, so there is the possibility that
I am using incorrect arguments.


No, it's not a bug. A slice does not support appending in the way you 
expect as an output range.


Think of a slice/array as a buffer in which to put information, maybe 
it's a stack buffer. If you output to this buffer, you wouldn't expect 
it to allocate more memory and append to the end would you? Instead, 
you'd expect to write data starting at the beginning.


If you want append behavior, use std.array.Appender, as is described in 
the example of std.bitmanip.append.


I do, however, think that the term "append" is very misleading. If it 
were named "putInto", that might have been a less confusing term.


-Steve


Re: problems with std.bitmanip.append (bug?)

2015-03-26 Thread John Colvin via Digitalmars-d-learn

On Thursday, 26 March 2015 at 12:21:23 UTC, Hugo wrote:

On Thursday, 26 March 2015 at 10:07:07 UTC, Hugo wrote:


If only the documentation and/or test units were more clear...


OK, I made a simpler test, using an example from the 
documentation:



void main() {
   import std.stdio, std.array, std.bitmanip;
   auto buffer = appender!(const ubyte[])();
   buffer.append!ushort(261);
   assert(buffer.data == [1, 5]);
   writefln("%s", buffer.data);
}

It seems to work, so apparently one has to explicitly create a 
buffer with the appender template. Not terribly useful IMHO.


Wouldn't it be possible for the append function to automaticaly 
change the mode of an already existing buffer?


Also, can anyone provide a similar example but using little 
endian order? If only to contrast differences between modes of 
invocation...


void main() {
   import std.stdio, std.array, std.bitmanip, std.system;
   auto buffer = appender!(const ubyte[])();
   buffer.append!(ushort, Endian.littleEndian)(261);
   assert(buffer.data == [5, 1]);
   writefln("%s", buffer.data);
}


Re: problems with std.bitmanip.append (bug?)

2015-03-26 Thread Hugo via Digitalmars-d-learn

On Thursday, 26 March 2015 at 12:29:03 UTC, John Colvin wrote:


On Thursday, 26 March 2015 at 12:21:23 UTC, Hugo wrote:


Also, can anyone provide a similar example but using little 
endian order? If only to contrast differences between modes of 
invocation...


void main() {
   import std.stdio, std.array, std.bitmanip, std.system;
   auto buffer = appender!(const ubyte[])();
   buffer.append!(ushort, Endian.littleEndian)(261);
   assert(buffer.data == [5, 1]);
   writefln("%s", buffer.data);
}



Thanks, although it puzzles me that one has to move the type 
inside the parenthesis and the value after them, otherwise it 
doesn't compile.


It looks quite irregular, at least to someone like me, more used 
to function than templates. :(


I wish one could simply append a buffer using the concatenation 
operator, which would be the obvious choice, but it doesn't seem 
to work for ubytes...


Re: problems with std.bitmanip.append (bug?)

2015-03-27 Thread John Colvin via Digitalmars-d-learn

On Friday, 27 March 2015 at 00:50:34 UTC, Hugo wrote:

On Thursday, 26 March 2015 at 12:29:03 UTC, John Colvin wrote:


On Thursday, 26 March 2015 at 12:21:23 UTC, Hugo wrote:


Also, can anyone provide a similar example but using little 
endian order? If only to contrast differences between modes 
of invocation...


void main() {
  import std.stdio, std.array, std.bitmanip, std.system;
  auto buffer = appender!(const ubyte[])();
  buffer.append!(ushort, Endian.littleEndian)(261);
  assert(buffer.data == [5, 1]);
  writefln("%s", buffer.data);
}



Thanks, although it puzzles me that one has to move the type 
inside the parenthesis and the value after them, otherwise it 
doesn't compile.


It looks quite irregular, at least to someone like me, more 
used to function than templates. :(


Think of it as compile-time arguments and run-time arguments. 
First set of parenthesis are compile-time, second are run-time. 
The parenthesis are optional for compile-time arguments iff 
there's only one of them.


I wish one could simply append a buffer using the concatenation 
operator, which would be the obvious choice, but it doesn't 
seem to work for ubytes...


I agree that std.bitmanip often doesn't have the most intuitive 
interface.


Re: problems with std.bitmanip.append (bug?)

2015-03-27 Thread Hugo via Digitalmars-d-learn

On Friday, 27 March 2015 at 08:43:56 UTC, John Colvin wrote:


Think of it as compile-time arguments and run-time arguments. 
First set of parenthesis are compile-time, second are run-time. 
The parenthesis are optional for compile-time arguments iff 
there's only one of them.




I really appreciate this little explanation, now it makes some 
sense.