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)



Re: [dwm] [patch] simplification to drawtext

2008-06-01 Thread Premysl Hruby
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/



Re: [dwm] [patch] simplification to drawtext

2008-06-01 Thread Enno "Gottox" Boland
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.

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));

I also prefer strncpy over memcpy because we're not copy plain data,
but strings.

regards
Gottox

2008/6/1, Anselm R. Garbe <[EMAIL PROTECTED]>:
> On Sun, Jun 01, 2008 at 01:21:03PM +0200, Premysl Hruby wrote:
>  > Not much readable (because it firstly looks like it's error (buf maybe
>  > not ended with \0)). I am against using strncpy in case that dest string
>  > is not C string, but char[] + length.
>  >
>  > Maybe:
>  >
>  > memcpy(&buf[MAX(0, len - 3)], "...", 3);
>  >
>  > would be somewhat better ;)
>  >
>  >
>  > > XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
>  > > if(dc.font.set)
>  > > XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, 
> buf, len);
>
>
> I though a while about this, and I think the memcpy() idea is
>  best.
>
>  Kind regards,
>
> --
>   Anselm R. Garbe >< http://www.suckless.org/ >< GPG key: 0D73F361
>
>


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



Re: [dwm] [patch] simplification to drawtext

2008-06-01 Thread Anselm R. Garbe
On Sun, Jun 01, 2008 at 01:21:03PM +0200, Premysl Hruby wrote:
> Not much readable (because it firstly looks like it's error (buf maybe
> not ended with \0)). I am against using strncpy in case that dest string
> is not C string, but char[] + length.
> 
> Maybe:
> 
> memcpy(&buf[MAX(0, len - 3)], "...", 3);
> 
> would be somewhat better ;)
> 
> 
> > XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
> > if(dc.font.set)
> > XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, 
> > len);

I though a while about this, and I think the memcpy() idea is
best.

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



Re: [dwm] [patch] simplification to drawtext

2008-06-01 Thread Premysl Hruby
On (01/06/08 12:38), Enno Gottox Boland wrote:
> To: dynamic window manager 
> From: Enno Gottox Boland <[EMAIL PROTECTED]>
> Subject: [dwm] [patch] simplification to drawtext
> Reply-To: dynamic window manager 
> List-Id: dynamic window manager 
> 
> Hi!
> 
> Here's a small simplification to drawtext.
> 
> regards
> Gottox
> 

> diff -r 2488c46e002c dwm.c
> --- a/dwm.c   Thu May 29 18:42:53 2008 +0100
> +++ b/dwm.c   Sun Jun 01 12:36:37 2008 +0200
> @@ -571,14 +571,8 @@
>   for(; len && (w = textnw(buf, len)) > dc.w - h; len--);
>   if(!len)
>   return;
> - if(len < olen) {
> - if(len > 1)
> - buf[len - 1] = '.';
> - if(len > 2)
> - buf[len - 2] = '.';
> - if(len > 3)
> - buf[len - 3] = '.';
> - }
> + if(len < olen)
> + strncpy(&buf[MAX(0, len - 3)], "...", len);

Not much readable (because it firstly looks like it's error (buf maybe
not ended with \0)). I am against using strncpy in case that dest string
is not C string, but char[] + length.

Maybe:

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

would be somewhat better ;)


>   XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
>   if(dc.font.set)
>   XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, 
> len);


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



[dwm] [patch] simplification to drawtext

2008-06-01 Thread Enno "Gottox" Boland
Hi!

Here's a small simplification to drawtext.

regards
Gottox

-- 
http://www.gnuffy.org - Real Community Distro
http://www.gnuffy.org/index.php/GnuEm - Gnuffy on Ipaq (Codename Peggy)
diff -r 2488c46e002c dwm.c
--- a/dwm.c Thu May 29 18:42:53 2008 +0100
+++ b/dwm.c Sun Jun 01 12:36:37 2008 +0200
@@ -571,14 +571,8 @@
for(; len && (w = textnw(buf, len)) > dc.w - h; len--);
if(!len)
return;
-   if(len < olen) {
-   if(len > 1)
-   buf[len - 1] = '.';
-   if(len > 2)
-   buf[len - 2] = '.';
-   if(len > 3)
-   buf[len - 3] = '.';
-   }
+   if(len < olen)
+   strncpy(&buf[MAX(0, len - 3)], "...", len);
XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
if(dc.font.set)
XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, 
len);