Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
February 07, 2012 Conversion to output ranges | ||||
---|---|---|---|---|
| ||||
Hi, does anybody know how to bring std.conv.to or something similar to output into an output range? int a = 42; char[25] buffer; to!typeof(buffer[])(a, buffer[]); I want to send these texts throw sockets. Therefore I'd like to reuse the buffer. Mafi |
February 07, 2012 Re: Conversion to output ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mafi | On 02/07/2012 02:35 PM, Mafi wrote: > Hi, > does anybody know how to bring std.conv.to or something similar to > output into an output range? > > int a = 42; > char[25] buffer; > to!typeof(buffer[])(a, buffer[]); > > I want to send these texts throw sockets. Therefore I'd like to reuse > the buffer. > > Mafi You could use std.format.formattedWrite. import std.exception, std.format, std.stdio; // I don't know if this already exists somewhere: struct Filler(T:T[]){ this(T[] pl){payload = pl;} size_t index=0; T[] payload; void put(const T[] s){ enforce(payload.length>=index+s.length); payload[index..index+s.length]=s; index+=s.length; } void put(char s){ enforce(payload.length>=index); payload[index++]=s; } @property auto data(){return payload[0..index];} } auto filler(T)(T pl){return Filler!T(pl);} void main(){ int a = 42; char[25] buffer; auto f = filler(buffer[]); formattedWrite(&f,"%s",a); writeln(f.data); } |
February 07, 2012 Re: Conversion to output ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On 02/07/2012 04:49 PM, Timon Gehr wrote: > On 02/07/2012 02:35 PM, Mafi wrote: >> Hi, >> does anybody know how to bring std.conv.to or something similar to >> output into an output range? >> >> int a = 42; >> char[25] buffer; >> to!typeof(buffer[])(a, buffer[]); >> >> I want to send these texts throw sockets. Therefore I'd like to reuse >> the buffer. >> >> Mafi > > You could use std.format.formattedWrite. > > import std.exception, std.format, std.stdio; > > // I don't know if this already exists somewhere: > struct Filler(T:T[]){ > this(T[] pl){payload = pl;} > size_t index=0; > T[] payload; > void put(const T[] s){ > enforce(payload.length>=index+s.length); > payload[index..index+s.length]=s; > index+=s.length; > } > void put(char s){ Should be 'void put(T s)'. > enforce(payload.length>=index); > payload[index++]=s; > } > @property auto data(){return payload[0..index];} > } > auto filler(T)(T pl){return Filler!T(pl);} > > void main(){ > int a = 42; > char[25] buffer; > auto f = filler(buffer[]); > formattedWrite(&f,"%s",a); > writeln(f.data); > } |
February 07, 2012 Re: Conversion to output ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On 02/07/2012 04:50 PM, Timon Gehr wrote: > On 02/07/2012 04:49 PM, Timon Gehr wrote: >> On 02/07/2012 02:35 PM, Mafi wrote: >>> Hi, >>> does anybody know how to bring std.conv.to or something similar to >>> output into an output range? >>> >>> int a = 42; >>> char[25] buffer; >>> to!typeof(buffer[])(a, buffer[]); >>> >>> I want to send these texts throw sockets. Therefore I'd like to reuse >>> the buffer. >>> >>> Mafi >> >> You could use std.format.formattedWrite. >> >> import std.exception, std.format, std.stdio; >> >> // I don't know if this already exists somewhere: >> struct Filler(T:T[]){ >> this(T[] pl){payload = pl;} >> size_t index=0; >> T[] payload; >> void put(const T[] s){ >> enforce(payload.length>=index+s.length); >> payload[index..index+s.length]=s; >> index+=s.length; >> } >> void put(char s){ > > Should be 'void put(T s)'. Just noticed that this overload is not even necessary. > >> enforce(payload.length>=index); >> payload[index++]=s; >> } >> @property auto data(){return payload[0..index];} >> } >> auto filler(T)(T pl){return Filler!T(pl);} >> >> void main(){ >> int a = 42; >> char[25] buffer; >> auto f = filler(buffer[]); >> formattedWrite(&f,"%s",a); >> writeln(f.data); >> } > |
February 07, 2012 Re: Conversion to output ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mafi Attachments:
| Maybe std.outbuffer...
auto buffer = new OutBuffer();
int a = 42;
buffer.write(a);
byte[] bytes = cast(byte[]) buffer.toBytes();
ubyte[] ubytes = buffer.toBytes();
Pedro Lacerda
2012/2/7 Mafi <mafi@example.org>
> Hi,
> does anybody know how to bring std.conv.to or something similar to output
> into an output range?
>
> int a = 42;
> char[25] buffer;
> to!typeof(buffer[])(a, buffer[]);
>
> I want to send these texts throw sockets. Therefore I'd like to reuse the buffer.
>
> Mafi
>
|
February 08, 2012 Re: Conversion to output ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr Attachments:
| Sorry, I didn't reused the buffer. Hope this helps:
auto buffer = new OutBuffer();
int a = 42;
buffer.write(a);
byte[] bytes = cast(byte[]) buffer.toBytes();
ubyte[] ubytes = buffer.toBytes();
buffer.offset = 0; // cleared
buffer.write(cast(byte[]) [1,2,0x2]);
Timon, std.outbuffer.OutBuffer looks like your Filler struct.
Pedro Lacerda
2012/2/7 Timon Gehr <timon.gehr@gmx.ch>
> On 02/07/2012 04:50 PM, Timon Gehr wrote:
>
>> On 02/07/2012 04:49 PM, Timon Gehr wrote:
>>
>>> On 02/07/2012 02:35 PM, Mafi wrote:
>>>
>>>> Hi,
>>>> does anybody know how to bring std.conv.to or something similar to
>>>> output into an output range?
>>>>
>>>> int a = 42;
>>>> char[25] buffer;
>>>> to!typeof(buffer[])(a, buffer[]);
>>>>
>>>> I want to send these texts throw sockets. Therefore I'd like to reuse the buffer.
>>>>
>>>> Mafi
>>>>
>>>
>>> You could use std.format.formattedWrite.
>>>
>>> import std.exception, std.format, std.stdio;
>>>
>>> // I don't know if this already exists somewhere:
>>> struct Filler(T:T[]){
>>> this(T[] pl){payload = pl;}
>>> size_t index=0;
>>> T[] payload;
>>> void put(const T[] s){
>>> enforce(payload.length>=index+**s.length);
>>> payload[index..index+s.length]**=s;
>>> index+=s.length;
>>> }
>>> void put(char s){
>>>
>>
>> Should be 'void put(T s)'.
>>
>
> Just noticed that this overload is not even necessary.
>
>
>
>> enforce(payload.length>=index)**;
>>> payload[index++]=s;
>>> }
>>> @property auto data(){return payload[0..index];}
>>> }
>>> auto filler(T)(T pl){return Filler!T(pl);}
>>>
>>> void main(){
>>> int a = 42;
>>> char[25] buffer;
>>> auto f = filler(buffer[]);
>>> formattedWrite(&f,"%s",a);
>>> writeln(f.data);
>>> }
>>>
>>
>>
>
|
February 08, 2012 Re: Conversion to output ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | Am 07.02.2012 16:50, schrieb Timon Gehr:
> On 02/07/2012 04:49 PM, Timon Gehr wrote:
>> On 02/07/2012 02:35 PM, Mafi wrote:
>>> Hi,
>>> does anybody know how to bring std.conv.to or something similar to
>>> output into an output range?
>>>
>>> int a = 42;
>>> char[25] buffer;
>>> to!typeof(buffer[])(a, buffer[]);
>>>
>>> I want to send these texts throw sockets. Therefore I'd like to reuse
>>> the buffer.
>>>
>>> Mafi
>>
>> You could use std.format.formattedWrite.
>>
>> import std.exception, std.format, std.stdio;
>>
>> // I don't know if this already exists somewhere:
>> struct Filler(T:T[]){
>> this(T[] pl){payload = pl;}
>> size_t index=0;
>> T[] payload;
>> void put(const T[] s){
>> enforce(payload.length>=index+s.length);
>> payload[index..index+s.length]=s;
>> index+=s.length;
>> }
>> void put(char s){
>
> Should be 'void put(T s)'.
>
>> enforce(payload.length>=index);
>> payload[index++]=s;
>> }
>> @property auto data(){return payload[0..index];}
>> }
>> auto filler(T)(T pl){return Filler!T(pl);}
>>
>> void main(){
>> int a = 42;
>> char[25] buffer;
>> auto f = filler(buffer[]);
>> formattedWrite(&f,"%s",a);
>> writeln(f.data);
>> }
>
Thanks :) This solution seems to work.
I just wanted to point out that I forgot the ampersand and this was hard to track down. I seemed to work but index wasn't incremented so I always got an empty slice.
Mafi
|
Copyright © 1999-2021 by the D Language Foundation