Re: Formatting a string on a variadic parameter function without using GC

2016-03-02 Thread Mike Parker via Digitalmars-d-learn
On Wednesday, 2 March 2016 at 10:57:35 UTC, Luis wrote: Read https://dlang.org/spec/arrays.html#strings and try to use std.string.toStringz (http://dlang.org/phobos/std_string.html#.toStringz) Yes, but that's not what you use when you want to avoid allocation.

Re: Formatting a string on a variadic parameter function without using GC

2016-03-02 Thread Luis via Digitalmars-d-learn
On Wednesday, 2 March 2016 at 04:12:13 UTC, Mike Parker wrote: On Wednesday, 2 March 2016 at 01:39:13 UTC, David G. Maziero wrote: Consider the following function: void RenderText( FontBMP font, int x, int y, const char* text ) { for( int r=0; text[r]!='\0'; ++r ) {

Re: Formatting a string on a variadic parameter function without using GC

2016-03-02 Thread cym13 via Digitalmars-d-learn
On Wednesday, 2 March 2016 at 05:04:37 UTC, David G. Maziero wrote: I figured out what I wanted. Thanks for your answer Mike, sorry to bother you though. [...] Even null-terminated be sure to profile your loop, for isn't garanteed to be faster than foreach at all and foreach is definitely

Re: Formatting a string on a variadic parameter function without using GC

2016-03-02 Thread David G. Maziero via Digitalmars-d-learn
On Wednesday, 2 March 2016 at 05:04:37 UTC, David G. Maziero wrote: void RenderText( FontBMP font, int x, int y, const char* text, Just one more correction for future reference, RenderText should be extern(C) void RenderText... in order for it to work correctly with va_start/etc.

Re: Formatting a string on a variadic parameter function without using GC

2016-03-02 Thread David G. Maziero via Digitalmars-d-learn
On Wednesday, 2 March 2016 at 05:04:37 UTC, David G. Maziero wrote: char[256] buff; va_list ap; va_start( ap, text ); sprintf( buff.ptr, text, ap ); va_end( ap ); Sorry again, where it reads "sprintf" should be "vsprintf".

Re: Formatting a string on a variadic parameter function without using GC

2016-03-02 Thread David G. Maziero via Digitalmars-d-learn
I figured out what I wanted. Thanks for your answer Mike, sorry to bother you though. Here's the result: void RenderText( FontBMP font, int x, int y, const char* text, ... ) { SDL_Rect rect1, rect2; rect2.x = x; rect2.y = y; rect2.w = font.width;

Re: Formatting a string on a variadic parameter function without using GC

2016-03-02 Thread David G. Maziero via Digitalmars-d-learn
I forgot to add that the "RenderText(font,0,0,"FPS: "~to!string(fps));" was an older version where it wasn't const char *, but string. And I was using a foreach, so no null-termination. But that's beyond the point of not using GC.

Re: Formatting a string on a variadic parameter function without using GC

2016-03-02 Thread David G. Maziero via Digitalmars-d-learn
Yes, I'm aware of the null-termination thing. I might have pasted code that I already changed. But I already messed with sformat, and it seems that it does use the GC. I've put @nogc in RenderText, and the compiler says sformat uses GC, so I don't know. But the thing is, I don't want to

Re: Formatting a string on a variadic parameter function without using GC

2016-03-02 Thread Mike Parker via Digitalmars-d-learn
On Wednesday, 2 March 2016 at 04:12:13 UTC, Mike Parker wrote: char buf[1024]; Ugh. And the proper declaration in D: char[1024] buf;

Re: Formatting a string on a variadic parameter function without using GC

2016-03-02 Thread Mike Parker via Digitalmars-d-learn
On Wednesday, 2 March 2016 at 01:39:13 UTC, David G. Maziero wrote: Consider the following function: void RenderText( FontBMP font, int x, int y, const char* text ) { for( int r=0; text[r]!='\0'; ++r ) { You're asking for trouble here. There's no guarantee that any D