Subject: [Bug & Patch] clumsy & wrong token parsing

From: [email-masked] (Albrecht Kadlec)
Date: Thu, 19 Sep 96 22:19:01 +0200

Hi all,

I nosed & hacked around on my windowlist, when I was bitten by a
sudden need to speed up some code ;^)

& here it is, my first routine for what could become a more powerful
libfvwm2:

Ta-daaa

It's called TokenCmp() and is a drop in bugfix/speedup/beautify replacement
for mystrncasecmp()

It fixes the annoying bug, that all token parsing was done by
mystrncasecmp(line,token,strlen(token)) which doesn't assert, that 
e.g: "ColorType" isn't matched to the "Color" token.

(I wanted to define a function "Execute" and all the time the builtin
"Exec" was used)

There might be some complaints, that AddToFunction isn't parsed as
AddToFunc any more, but what the heck, that was the bug!!!

It implicitly handles token-end and just returns boolean equal (1)/not
equal (0) which is all, that is needed and can be computed much faster.

I just wrote it, did the 
#define mystrncasecmp(a,b,n) (!TokenCmp(a,b))
make && make install
restart fvwm2
and it worked (well not the first time, but after I had fixed my
misinterpretation of the ispunct man page).

While I was there I inserted Andrew's trimleft & trimright macros to the
header file.

Now all I want to see the ugly mystrncasecmp constructs be replaced by
TokenCmp calls and all the 
        while(isspace(*restofline))restofline++;
replaced by 
	trimleft(restofline);
          
If no one else volunteers, I'll do it next week (at least for main fvwm2,
since some modules are heavily hacked upon by their respective authors).
This should make style.c readable again.

albrecht

save this to libs/TokenCmp.c and apply the diffs below

/* -*- C -*-
   File:		TokenCmp.c
   Date:		Thu Sep 19 19:25:16 1996
   Author:		Albrecht Kadlec (albrecht)

   Description:		replacement for mystrncasecmp abuse to parse tokens.
                        requires isspace(nextchar), otherwise it's another
			token!    

   Modifications:	$Log$
*/

#include <ctype.h>
#include <stdio.h>
#include <assert.h>

/* replacement for !mystrncasecmp

#if 0
   int mystrncasecmp(char *a, char *b,int n);
#else
  int TokenCmp(register char *s,register char *t)
# define mystrncasecmp(a,b,n) (!TokenCmp(a,b))
#endif

should be equivalent, except for one fact:
TokenCmp requires s to be delimited by isspace(), ispunct() or '\0'

compare string s to token t, until t finished or unequal
s may be NULL, t may NOT
n was dropped, since it is strlen(t) and isn't used by design 

the return code is just =0 or != 0 the sign isn't specified
*/
int TokenCmp(register char *s,register char *t)
{
    if (s==NULL) return 0;
#if 0
    if (t==NULL) return 0;
#else
    assert(t!=NULL);	/* the token may not be NULL -> design flaw */
#endif

    while (*t && (*s==*t || toupper(*s)==toupper(*t)))
	s++,t++;			/* loop as tight as possible */	

/* second toupper could be dropped, if tokens are required to be */
/* specified in uppercase -> would have to go through code */
/* isupper(*s) && _toupper(*s)==*t would be faster */

    if (*t=='\0')			/* end of token */
	return !*s || ispunct(*s) || my_isspace(*s);
/* \0 is punctuation ? 
my manual says: "neither control nor alphanumerical", 
but it didn't match whitespace either */
    else return 0;			/* no match */
}

*** ../fvwm-2.0.43/libs/fvwmlib.h       Fri Mar 22 21:32:32 1996
--- fvwmlib.h   Thu Sep 19 22:05:13 1996
***************
*** 4,10 ****
--- 4,30 ----
  #include <X11/Xutil.h>

  int mystrcasecmp(char *a, char *b);
+
+ #if 0
    int mystrncasecmp(char *a, char *b,int n);
+ #else
+   int TokenCmp(register char *s,register char *t);
+ #define mystrncasecmp(a,b,n) (!TokenCmp(a,b))
+
+ #if 1
+ #define isspace(c)    ((c)==' ' || (c)=='\t' || (c)=='\n')
+ #else
+ #include <ctype.h>
+ #endif
+
+ /* ----------- shamelessly stolen from FvwmButtons/parse.c  ------------ */
+ #define trimleft(s) {while(isspace(*s))(s)++;}
+ #define trimright(s) {                                      \
+   register char *t=(s);while(*t) t++;                       \
+   --t; while(t>=s && isspace(*t)) t--;                      \
+   t[1]=0;}
+ #endif
+
  char *CatString3(char *a, char *b, char *c);
  int mygethostname(char *client, int namelen);
  void SendText(int *fd,char *message,unsigned long window);

*** ../fvwm-2.0.43/libs/Imakefile       Sat Feb 17 02:17:26 1996
--- Imakefile   Thu Sep 19 19:35:11 1996
***************
*** 5,10 ****
--- 5,11 ----
  #include "../Fvwm.tmpl"

  SRCS = \
+       TokenCmp.c \
        CatString3.c \
        CopyString.c \
        GetFdWidth.c \
***************
*** 26,31 ****
--- 27,33 ----
        wild.c

  OBJS = \
+       TokenCmp.o \
        CatString3.o \
        CopyString.o \
        GetFdWidth.o \

-- 
Did you hear about the termite who went into a bar and asked
"Where's the bar tender?"		-- Bob Woodfield
--
Visit the official FVWM web page at <URL:http://www.hpc.uh.edu/fvwm/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked].

---

From: Andrew Veliath <[email-masked]>
Date: Thu, 19 Sep 1996 17:05:23 -0400

>>>>> Regarding [Bug & Patch] clumsy & wrong token parsing;
>>>>> [email-masked] (Albrecht Kadlec) adds:

    Albrecht> While I was there I inserted Andrew's trimleft &
    Albrecht> trimright macros to the header file.

-- he means *Jarl's* trimleft & trimright :) (the author of FvwmButtons)

Nonetheless, the mystrncasecmp multimatching is annoying in
situations, a workaround might be appropriate.

later,
andy

-- 
Andrew Veliath <[email-masked]>        http://www.rpi.edu/~veliaa
Finger for PGP Public Key
Key fingerprint =  51 56 5D 3E 51 43 8B 74  7C B2 E4 B6 84 8E 8B 08
--
Visit the official FVWM web page at <URL:http://www.hpc.uh.edu/fvwm/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked].

---

From: [email-masked] (Albrecht Kadlec)
Date: Fri, 20 Sep 96 09:29:16 +0200

>>>>> Andrew Veliath writes:

A> -- he means *Jarl's* trimleft & trimright :) (the author of FvwmButtons)

A> Nonetheless, the mystrncasecmp multimatching is annoying in
A> situations, a workaround might be appropriate.

oh shoot, sorry, jarl. I used it for fvwmaudio some time ago, and thought
it was from andrew (while I remember, I fetched it from FvwmButtons)
it was late yesterday.

Today in the morning I thought SH^*@, I should have named it 
MatchToken and have it return the ENDPOINTER, or NULL if it couldn't match
the token.
MatchToken will be in the new lib, too.

BTW: I'm currently wading through the libs code, to look at awful things &
speed it up.
I'd like to do some alpha-checking on the workers list, before sending it
to chuck. (you know, the multi-platform test)

I'll do it as a drop-in replacement/enhancement for current function.
The hard test will be to recompile fvwm with the new lib & use for a week.

If chuck then says: "go on, I'll include it" I'll take the tedious task of
beautifying the code (style.c & maybe others), too.
But I'll be pretty much unavailable from Oct. 1st, though, so I'm sure I'll
finish the lib, & maybe fvwm, but I'm pretty sure I won't touch the
modules.

hello, chuck?
albrecht
-- 
Time keeps everything from happening at one time.
Space keeps everything from happening to YOU.	-- unknown
--
Visit the official FVWM web page at <URL:http://www.hpc.uh.edu/fvwm/>.
To unsubscribe from the list, send "unsubscribe fvwm-workers" in the
body of a message to [email-masked].
To report problems, send mail to [email-masked].

---

From: [email-masked] (Albrecht Kadlec)
Date: Fri, 20 Sep 96 11:16:19 +0200

oh shoot, sorry, jarl. I used it for fvwmaudio some time ago, and thought
it was from andrew (while I remember, I fetched it from FvwmButtons,
it was late yesterday).

I'll do it as a drop-in replacement/enhancement for current functions.
The hard test will be to recompile fvwm with the new lib & use for a week.

alright, chuck?
albrecht