bc: use '\0' insteads of 0xff (BC_PARSE_STREND) as name terminator
authorDenys Vlasenko <vda.linux@googlemail.com>
Wed, 26 Dec 2018 19:02:09 +0000 (20:02 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Wed, 26 Dec 2018 19:02:27 +0000 (20:02 +0100)
function                                             old     new   delta
zdc_program_printStream                                -     146    +146
zbc_program_exec                                    4003    4016     +13
zdc_parse_expr                                       473     470      -3
bc_parse_pushName                                     31      20     -11
bc_program_name                                       63      34     -29
zbc_program_pushArray                                147       -    -147
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 1/3 up/down: 159/-190)          Total: -31 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
miscutils/bc.c

index 5b79b2db1e0e4ba48a73ac9f6d99f5369d10dbf5..2569967d61bf99debc1e4efeeea6fe75474a7e49 100644 (file)
@@ -803,8 +803,6 @@ struct globals {
 #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b'))
 #define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b'))
 
-#define BC_PARSE_STREND         (0xff)
-
 #if ENABLE_BC
 # define BC_PARSE_REL           (1 << 0)
 # define BC_PARSE_PRINT         (1 << 1)
@@ -3434,16 +3432,16 @@ static BC_STATUS zdc_lex_token(void)
 
 static void bc_parse_push(char i)
 {
-       BcParse *p = &G.prs;
-       dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i);
-       bc_vec_pushByte(&p->func->code, i);
+       BcVec *code = &G.prs.func->code;
+       dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i);
+       bc_vec_pushByte(code, i);
 }
 
 static void bc_parse_pushName(char *name)
 {
-       while (*name)
-               bc_parse_push(*name++);
-       bc_parse_push(BC_PARSE_STREND);
+       do {
+               bc_parse_push(*name);
+       } while (*name++);
 }
 
 static void bc_parse_pushIndex(size_t idx)
@@ -4895,7 +4893,7 @@ static BC_STATUS zdc_parse_cond(uint8_t inst)
                if (s) RETURN_STATUS(s);
                s = zbc_lex_next();
        } else {
-               bc_parse_push(BC_PARSE_STREND);
+               bc_parse_push('\0');
        }
 
        RETURN_STATUS(s);
@@ -5271,22 +5269,10 @@ static size_t bc_program_index(char *code, size_t *bgn)
 
 static char *bc_program_name(char *code, size_t *bgn)
 {
-       size_t i;
-       char *s;
-
        code += *bgn;
-       s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1);
-       i = 0;
-       for (;;) {
-               char c = *code++;
-               if (c == BC_PARSE_STREND)
-                       break;
-               s[i++] = c;
-       }
-       s[i] = '\0';
-       *bgn += i + 1;
+       *bgn += strlen(code) + 1;
 
-       return s;
+       return xstrdup(code);
 }
 
 static void bc_program_printString(const char *str)
@@ -6305,7 +6291,7 @@ static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
                char *then_name = bc_program_name(code, bgn);
                char *else_name = NULL;
 
-               if (code[*bgn] == BC_PARSE_STREND)
+               if (code[*bgn] == '\0')
                        (*bgn) += 1;
                else
                        else_name = bc_program_name(code, bgn);