digitalmars.D.learn - Conversion to output ranges
- Mafi <mafi example.org> Feb 07 2012
- Timon Gehr <timon.gehr gmx.ch> Feb 07 2012
- Timon Gehr <timon.gehr gmx.ch> Feb 07 2012
- Timon Gehr <timon.gehr gmx.ch> Feb 07 2012
- Mafi <mafi example.org> Feb 08 2012
- Pedro Lacerda <pslacerda gmail.com> Feb 07 2012
- Pedro Lacerda <pslacerda gmail.com> Feb 07 2012
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
Feb 07 2012
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); }
Feb 07 2012
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); }
Feb 07 2012
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); }
Feb 07 2012
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
Feb 08 2012
--0015175cd3b4cfdabf04b86b05a1
Content-Type: text/plain; charset=UTF-8
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);
}
--0015175cd3b4cfdabf04b86b05a1
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Sorry, I didn't reused the buffer. Hope this helps:<div><br></div><div>=
<div style><font face=3D"'courier new', monospace">=C2=A0 =C2=A0 au=
to buffer =3D new OutBuffer();</font></div><div style><font face=3D"'co=
urier new', monospace">=C2=A0 =C2=A0=C2=A0</font></div>
<div style><font face=3D"'courier new', monospace">=C2=A0 =C2=A0 in=
t a =3D 42;</font></div><div style><font face=3D"'courier new', mon=
ospace">=C2=A0 =C2=A0 buffer.write(a);</font></div><div style><font face=3D=
"'courier new', monospace">=C2=A0 =C2=A0=C2=A0</font></div>
<div style><font face=3D"'courier new', monospace">=C2=A0 =C2=A0 by=
te[] bytes =3D cast(byte[]) buffer.toBytes();</font></div><div style><font =
face=3D"'courier new', monospace">=C2=A0 =C2=A0 ubyte[] ubytes =3D =
buffer.toBytes();</font></div>
</div><div style><font face=3D"'courier new', monospace">=C2=A0 =C2=
=A0=C2=A0</font></div><div style><font face=3D"'courier new', monos=
pace">=C2=A0 =C2=A0 buffer.offset =3D 0; // cleared</font></div><div style>=
<font face=3D"'courier new', monospace">=C2=A0 =C2=A0 buffer.write(=
cast(byte[]) [1,2,0x2]);</font></div>
<div><div>=C2=A0=C2=A0</div><div>Timon, std.outbuffer.OutBuffer looks like =
your Filler struct.</div><div><br></div>Pedro Lacerda<br><br>
<br><br><div class=3D"gmail_quote">2012/2/7 Timon Gehr <span dir=3D"ltr">&l=
t;<a href=3D"mailto:timon.gehr gmx.ch" target=3D"_blank">timon.gehr gmx.ch<=
/a>></span><br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .=
8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div>On 02/07/2012 04:50 PM, Timon Gehr wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
On 02/07/2012 04:49 PM, Timon Gehr wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
On 02/07/2012 02:35 PM, Mafi wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
Hi,<br>
does anybody know how to bring <a href=3D"http://std.conv.to" target=3D"_bl=
ank">std.conv.to</a> or something similar to<br>
output into an output range?<br>
<br>
int a =3D 42;<br>
char[25] buffer;<br>
to!typeof(buffer[])(a, buffer[]);<br>
<br>
I want to send these texts throw sockets. Therefore I'd like to reuse<b=
r>
the buffer.<br>
<br>
Mafi<br>
</blockquote>
<br>
You could use std.format.formattedWrite.<br>
<br>
import std.exception, std.format, std.stdio;<br>
<br>
// I don't know if this already exists somewhere:<br>
struct Filler(T:T[]){<br>
this(T[] pl){payload =3D pl;}<br>
size_t index=3D0;<br>
T[] payload;<br>
void put(const T[] s){<br>
enforce(payload.length>=3Dindex+<u></u>s.length);<br>
payload[index..index+s.length]<u></u>=3Ds;<br>
index+=3Ds.length;<br>
}<br>
void put(char s){<br>
</blockquote>
<br>
Should be 'void put(T s)'.<br>
</blockquote>
<br></div></div>
Just noticed that this overload is not even necessary.<div><div><br>
<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
enforce(payload.length>=3Dindex)<u></u>;<br>
payload[index++]=3Ds;<br>
}<br>
property auto data(){return payload[0..index];}<br>
}<br>
auto filler(T)(T pl){return Filler!T(pl);}<br>
<br>
void main(){<br>
int a =3D 42;<br>
char[25] buffer;<br>
auto f =3D filler(buffer[]);<br>
formattedWrite(&f,"%s",a);<br>
writeln(f.data);<br>
}<br>
</blockquote>
<br>
</blockquote>
<br>
</div></div></blockquote></div><br></div>
--0015175cd3b4cfdabf04b86b05a1--
Feb 07 2012
--0015175cab466feb7f04b8606766
Content-Type: text/plain; charset=UTF-8
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
--0015175cab466feb7f04b8606766
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Maybe=C2=A0std.outbuffer...<div><br></div><div><div><font face=3D"'cour=
ier new', monospace">=C2=A0 =C2=A0 auto buffer =3D new OutBuffer();</fo=
nt></div><div><font face=3D"'courier new', monospace">=C2=A0 =C2=A0=
=C2=A0</font></div><div><font face=3D"'courier new', monospace">=C2=
=A0 =C2=A0 int a =3D 42;</font></div>
<div><font face=3D"'courier new', monospace">=C2=A0 =C2=A0 buffer.w=
rite(a);</font></div><div><font face=3D"'courier new', monospace">=
=C2=A0 =C2=A0=C2=A0</font></div><div><font face=3D"'courier new', m=
onospace">=C2=A0 =C2=A0 byte[] bytes =3D cast(byte[]) buffer.toBytes();</fo=
nt></div>
<div><font face=3D"'courier new', monospace">=C2=A0 =C2=A0 ubyte[] =
ubytes =3D buffer.toBytes();</font></div><div><br></div><div><br></div>Pedr=
o Lacerda<br><br>
<br><br><div class=3D"gmail_quote">2012/2/7 Mafi <span dir=3D"ltr"><<a h=
ref=3D"mailto:mafi example.org">mafi example.org</a>></span><br><blockqu=
ote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc s=
olid;padding-left:1ex">
Hi,<br>
does anybody know how to bring <a href=3D"http://std.conv.to" target=3D"_bl=
ank">std.conv.to</a> or something similar to output into an output range?<b=
r>
<br>
int a =3D 42;<br>
char[25] buffer;<br>
to!typeof(buffer[])(a, buffer[]);<br>
<br>
I want to send these texts throw sockets. Therefore I'd like to reuse t=
he buffer.<span class=3D"HOEnZb"><font color=3D"#888888"><br>
<br>
Mafi<br>
</font></span></blockquote></div><br></div>
--0015175cab466feb7f04b8606766--
Feb 07 2012









Timon Gehr <timon.gehr gmx.ch> 