]> git.draconx.ca Git - cdecl99.git/commitdiff
libcdecl: Prefer memchr over strchr in the scanner.
authorNick Bowler <nbowler@draconx.ca>
Sun, 19 Nov 2023 01:05:23 +0000 (20:05 -0500)
committerNick Bowler <nbowler@draconx.ca>
Sun, 19 Nov 2023 01:07:10 +0000 (20:07 -0500)
Since nothing else in the library uses strchr, but we do use memchr
elsewhere, using memchr instead avoids a comparatively expensive PLT
entry.  We already know the string length so the only real difference
is the additional argument marshalling.

src/scan.l

index 7549e6f7baee99cafb40a46d72f2211f10d511ce..2b92846678a911fc2e5f92d5e46f0ce024623012 100644 (file)
@@ -157,9 +157,10 @@ INTEGER 0x[[:xdigit:]]+|0[0-7]+|[[:digit:]]+
 }
 
 {IDENT} {
-       unsigned x = cdecl__to_keyword(yytext, yyleng, yyextra);
-       int tok;
+       int len = yyleng, tok;
+       unsigned x;
 
+       x = cdecl__to_keyword(yytext, len, yyextra);
        yylval->spectype = UNPACK_SPEC(x & 0xff);
        if ((tok = (x >> 8)) == PACK_TOKEN(T_IDENT)) {
                /*
@@ -173,14 +174,14 @@ INTEGER 0x[[:xdigit:]]+|0[0-7]+|[[:digit:]]+
                 * downsides.
                 */
 #if 1
-               if ((c = strchr(yytext, '-')))
+               if ((c = memchr(yytext, '-', len)))
                        goto invalid_char;
 #else
                yyless(strcspn(yytext, "-"));
 #endif
-               if (!(yylval->item = cdecl__alloc_item(yyleng+1)))
+               if (!(yylval->item = cdecl__alloc_item(len+1)))
                        return T_LEX_ERROR; \
-               memcpy(yylval->item->s, yytext, yyleng+1);
+               memcpy(yylval->item->s, yytext, len+1);
        }
        return UNPACK_TOKEN(tok);
 }