Re: [dwm] [patch] simplification to drawtext

2008-06-02 Thread Anselm R. Garbe
On Mon, Jun 02, 2008 at 10:16:34AM +0200, Premysl Hruby wrote:
> On (02/06/08 09:56), Enno Gottox Boland wrote:
> > To: dynamic window manager 
> > From: Enno Gottox Boland <[EMAIL PROTECTED]>
> > Subject: Re: [dwm] [patch] simplification to drawtext
> > Reply-To: dynamic window manager 
> > List-Id: dynamic window manager 
> > 
> > whoops...
> > 
> > memcpy(&buf[MAX(0, len - 3)], "...", MIN(3, len));
> > 
> 
> Still, this is not needed, as buf is ALWAYS greater than 3 bytes. So:
> 
> memcpy(&buf[MAX(0, len - 3)], "...", 3);
> 
> is sufficent, this was my thought when I wrote this memcpy variant.

Yes, I came to the same conclusion yesterday. Even if len < 3 it
doesn't matter, because MAX(0, len - 3) will always evaluate to
0 for len <= 3 and sizeof buf > 3 is a precondition.

However memcpy() might be slower than the following replacement:

if(len < olen)
for(i = len; i >= MAX(0, len - 3); buf[i--] = '.');

memcpy is an additional function call -- the for-solution might
not be the fastest because a sane memcpy will attempt to copy
4xlong resp. 1 long in a step for speed reasons, however for the
3 dots this speed hack won't be used anyways.

Kind regards,
-- 
 Anselm R. Garbe >< http://www.suckless.org/ >< GPG key: 0D73F361



Re: [dwm] [patch] simplification to drawtext

2008-06-02 Thread Premysl Hruby
On (02/06/08 09:56), Enno Gottox Boland wrote:
> To: dynamic window manager 
> From: Enno Gottox Boland <[EMAIL PROTECTED]>
> Subject: Re: [dwm] [patch] simplification to drawtext
> Reply-To: dynamic window manager 
> List-Id: dynamic window manager 
> 
> whoops...
> 
> memcpy(&buf[MAX(0, len - 3)], "...", MIN(3, len));
> 

Still, this is not needed, as buf is ALWAYS greater than 3 bytes. So:

memcpy(&buf[MAX(0, len - 3)], "...", 3);

is sufficent, this was my thought when I wrote this memcpy variant.

-Ph

-- 
Premysl "Anydot" Hruby, http://www.redrum.cz/



Re: [dwm] [patch] simplification to drawtext

2008-06-02 Thread Enno "Gottox" Boland
whoops...

memcpy(&buf[MAX(0, len - 3)], "...", MIN(3, len));

2008/6/1, Premysl Hruby <[EMAIL PROTECTED]>:
> On (01/06/08 19:30), Enno Gottox Boland wrote:
>  > To: dynamic window manager 
>  > From: Enno Gottox Boland <[EMAIL PROTECTED]>
>
> > Subject: Re: [dwm] [patch] simplification to drawtext
>
> > Reply-To: dynamic window manager 
>  > List-Id: dynamic window manager 
>  >
>
> > beware that it should be
>  >
>  > memcpy(&buf[MAX(0, len - 3)], "...", 4);
>  >
>  > not
>  >
>  > memcpy(&buf[MAX(0, len - 3)], "...", 3);
>  >
>  > otherwise it will overwrite the zero-terminator if len < 3.
>
>
> This is REALLY OK, as that Xlib calls doesn't use zero-terminated
>  string, but string with length argument!
>
>
>  >
>  > Furthermore it will change the length of the string if len < 3, which
>  > has no impact on the output, as XDrawString respects len, but it's
>  > still ugly.
>  >
>  > So I still prefer strncpy as the result doing the same with memcopy
>  > should look like this to work properly:
>  > memcpy(&buf[MAX(0, len - 3)], "...", MAX(len, 3));
>
>
> That MAX is baaad, as if len > 4 it will copy some random data after
>  string "..." which is really bad.
>
>
>  >
>  > I also prefer strncpy over memcpy because we're not copy plain data,
>  > but strings.
>
>
> No, at that point, we are copying plain data. Not strings[1]
>
>  >
>  > regards
>  > Gottox
>  >
>
>  [1]: meaned as C string == char[] which ends with zero-terminator
>
>
>  --
>  Premysl "Anydot" Hruby, http://www.redrum.cz/
>
>


-- 
http://www.gnuffy.org - Real Community Distro
http://www.gnuffy.org/index.php/GnuEm - Gnuffy on Ipaq (Codename Peggy)