From: Szabolcs Nagy <nsz@port70.net>
To: musl@lists.openwall.com
Subject: Re: Non-stub gettext API functions committed, ready for testing
Date: Tue, 29 Jul 2014 15:49:46 +0200 [thread overview]
Message-ID: <20140729134946.GM10402@port70.net> (raw)
In-Reply-To: <20140728162731.GD1674@brightrain.aerifal.cx>
[-- Attachment #1: Type: text/plain, Size: 501 bytes --]
* Rich Felker <dalias@libc.org> [2014-07-28 12:27:32 -0400]:
> My leaning is to go with the version that's smaller and more flexible;
> I think the time spent in this function will usually be heavily
> dominated by the binary search for the message text. But it's cool to
> have both for possible future uses (independent of musl, even).
i have a bit smaller pleval.c version:
non-pic .o with -Os is 980 vs 826 bytes here,
has about the same speed,
binary op parsing is a bit magical otherwise clean
[-- Attachment #2: pleval.c --]
[-- Type: text/x-csrc, Size: 3284 bytes --]
#include <stdlib.h>
#include <ctype.h>
/*
grammar:
Start = Expr ';'
Expr = Or | Or '?' Expr ':' Expr
Or = And | Or '||' And
And = Eq | And '&&' Eq
Eq = Rel | Eq '==' Rel | Eq '!=' Rel
Rel = Add | Rel '<=' Add | Rel '>=' Add | Rel '<' Add | Rel '>' Add
Add = Mul | Add '+' Mul | Add '-' Mul
Mul = Term | Mul '*' Term | Mul '/' Term | Mul '%' Term
Term = '(' Expr ')' | '!' Term | decimal | 'n'
internals:
recursive descent expression evaluator with stack depth limit.
eval* functions return the value of the subexpression and set
the current string pointer to the next non-space char.
*/
struct st {
const char *s;
unsigned long n;
int err;
};
static const char *skipspace(const char *s)
{
while (isspace(*s)) s++;
return s;
}
static unsigned long fail(struct st *st)
{
st->err = 1;
return 0;
}
static unsigned long evalexpr(struct st *st, int d);
static unsigned long evalterm(struct st *st, int d)
{
unsigned long a;
char *e;
if (--d < 0) return fail(st);
st->s = skipspace(st->s);
if (*st->s == '!') {
st->s++;
return !evalterm(st, d);
}
if (*st->s == '(') {
st->s++;
a = evalexpr(st, d);
if (*st->s != ')') return fail(st);
st->s = skipspace(st->s + 1);
return a;
}
if (*st->s == 'n') {
st->s = skipspace(st->s + 1);
return st->n;
}
a = strtoul(st->s, &e, 10);
if (!isdigit(*st->s) || e == st->s || a == -1) return fail(st);
st->s = skipspace(e);
return a;
}
static unsigned long binop(struct st *st, int op, unsigned long a, unsigned long b)
{
switch (op&0xff) {
case 0: return a||b;
case 1: return a&&b;
case 2: return a==b;
case 3: return a!=b;
case 4: return a>=b;
case 5: return a<=b;
case 6: return a>b;
case 7: return a<b;
case 8: return a-b;
case 9: return a+b;
case 10: return b ? a%b : fail(st);
case 11: return b ? a/b : fail(st);
case 12: return a*b;
}
return fail(st);
}
static int parseop(struct st *st)
{
static const char opch[18] = "|&=!><-+%/*\0|&====";
static const char prec[] = {1,2,3,3,4,4,5,5,6,6,6};
int i, p;
for (i=0; opch[i]; i++)
if (*st->s == opch[i]) {
p = prec[i]<<8;
if (i<6 && st->s[1] == opch[i+12]) {
st->s+=2;
return i | p;
}
if (i>=4) {
st->s++;
return i+2 | p;
}
return 0;
}
return 0;
}
static unsigned long evalbinop2(struct st *st, int op, unsigned long a, int d)
{
unsigned long a2;
int op2, highprec;
d--;
for (;;) {
a2 = evalterm(st, d);
op2 = parseop(st);
highprec = op2>>8 > op>>8;
if (highprec)
a2 = evalbinop2(st, op2, a2, d);
a = binop(st, op, a, a2);
if (!op2 || highprec)
return a;
op = op2;
}
}
static unsigned long evalbinop(struct st *st, int d)
{
unsigned long a;
int op;
a = evalterm(st, d);
op = parseop(st);
if (!op) return a;
return evalbinop2(st, op, a, d);
}
static unsigned long evalexpr(struct st *st, int d)
{
unsigned long a1, a2, a3;
if (--d < 0) return fail(st);
a1 = evalbinop(st, d);
if (*st->s != '?')
return a1;
st->s++;
a2 = evalexpr(st, d);
if (*st->s != ':') return fail(st);
st->s++;
a3 = evalexpr(st, d);
return a1 ? a2 : a3;
}
unsigned long __pleval(const char *s, unsigned long n)
{
unsigned long a;
struct st st;
st.s = s;
st.n = n;
st.err = 0;
a = evalexpr(&st, 100);
if (st.err || *st.s != ';')
return -1;
return a;
}
next prev parent reply other threads:[~2014-07-29 13:49 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-27 8:46 Rich Felker
2014-07-27 10:06 ` Harald Becker
2014-07-27 14:14 ` Szabolcs Nagy
2014-07-27 16:49 ` Rich Felker
2014-07-27 17:23 ` Szabolcs Nagy
2014-07-27 17:36 ` Rich Felker
2014-07-27 17:51 ` Szabolcs Nagy
2014-07-27 18:00 ` Rich Felker
2014-07-28 10:18 ` Szabolcs Nagy
2014-07-28 13:00 ` Szabolcs Nagy
2014-07-28 14:01 ` Szabolcs Nagy
2014-07-28 16:27 ` Rich Felker
2014-07-29 13:49 ` Szabolcs Nagy [this message]
2014-07-27 10:19 ` Harald Becker
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140729134946.GM10402@port70.net \
--to=nsz@port70.net \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).