summaryrefslogtreecommitdiffabout
path: root/src/builtin/sprintf.bi
Side-by-side diff
Diffstat (limited to 'src/builtin/sprintf.bi') (more/less context) (ignore whitespace changes)
-rw-r--r--src/builtin/sprintf.bi96
1 files changed, 49 insertions, 47 deletions
diff --git a/src/builtin/sprintf.bi b/src/builtin/sprintf.bi
index 11effd5..0a927dd 100644
--- a/src/builtin/sprintf.bi
+++ b/src/builtin/sprintf.bi
@@ -34,14 +34,15 @@ typedef enum {
fmts_conv /* Expect conversion specifier */
} printf_format_state;
-char *
-get_num(char *p, unsigned *pn)
+static int
+get_num(const char *p, int i, unsigned *pn)
{
unsigned n = 0;
- for (; *p && mu_isdigit(*p); p++)
- n = n * 10 + *p - '0';
+
+ for (; p[i] && mu_isdigit(p[i]); i++)
+ n = n * 10 + p[i] - '0';
*pn = n;
- return p;
+ return i;
}
#define __MF_MAX(a,b) ((a)>(b) ? (a) : (b))
@@ -50,8 +51,8 @@ get_num(char *p, unsigned *pn)
MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
{
int i = 0;
- char *p = format;
- char *start;
+ int cur = 0;
+ int start;
char buf[SPRINTF_BUF_SIZE];
printf_format_state state = fmts_copy;
int flags = 0;
@@ -61,7 +62,7 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
MF_OBSTACK_BEGIN();
MF_VA_START();
- while (*p) {
+ while (format[cur]) {
unsigned n;
char *str;
long num;
@@ -71,31 +72,31 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
switch (state) {
case fmts_copy:
/* Expect `%', and copy all the rest verbatim */
- if (*p == '%') {
- start = p;
+ if (format[cur] == '%') {
+ start = cur;
state = fmts_pos;
flags = 0;
width = 0;
prec = 0;
} else
- MF_OBSTACK_1GROW(*p);
- p++;
+ MF_OBSTACK_1GROW(format[cur]);
+ cur++;
break;
case fmts_pos:
/* Expect '%' or an argument position -- %_% or %_2$ */
- if (*p == '%') {
+ if (format[cur] == '%') {
MF_OBSTACK_1GROW('%');
- p++;
+ cur++;
state = fmts_copy;
break;
}
- if (mu_isdigit(*p)) {
- char *q = get_num(p, &n);
- if (*q == '$') {
+ if (mu_isdigit(format[cur])) {
+ int pos = get_num(format, cur, &n);
+ if (format[pos] == '$') {
argnum = n - 1;
flags |= FMT_ALTPOS;
- p = q + 1;
+ cur = pos + 1;
}
}
state = fmts_flags;
@@ -103,30 +104,30 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
case fmts_flags:
/* Expect flags -- %2$_# */
- switch (*p) {
+ switch (format[cur]) {
case '#':
flags |= FMT_ALTERNATE;
- p++;
+ cur++;
break;
case '0':
flags |= FMT_PADZERO;
- p++;
+ cur++;
break;
case '-':
flags |= FMT_ADJUST_LEFT;
- p++;
+ cur++;
break;
case ' ':
flags |= FMT_SPACEPFX;
- p++;
+ cur++;
break;
case '+':
flags |= FMT_SIGNPFX;
- p++;
+ cur++;
break;
default:
@@ -136,11 +137,11 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
case fmts_width:
/* Expect width -- %2$#_8 or %2$#_* */
- if (mu_isdigit(*p)) {
- p = get_num(p, &width);
+ if (mu_isdigit(format[cur])) {
+ cur = get_num(format, cur, &width);
state = fmts_prec;
- } else if (*p == '*') {
- p++;
+ } else if (format[cur] == '*') {
+ cur++;
state = fmts_width_arg;
} else
state = fmts_prec;
@@ -149,11 +150,11 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
case fmts_width_arg:
/* Expect width argument position -- %2$#*_1$ */
state = fmts_prec;
- if (mu_isdigit(*p)) {
- char *q = get_num(p, &n);
- if (*q == '$') {
+ if (mu_isdigit(format[cur])) {
+ int pos = get_num(format, cur, &n);
+ if (format[pos] == '$') {
MF_VA_ARG(n-1, NUMBER, num);
- p = q + 1;
+ cur = pos + 1;
if (num < 0) {
flags |= FMT_SPACEPFX;
num = - num;
@@ -176,12 +177,12 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
case fmts_prec:
/* Expect precision -- %2$#*1$_. */
state = fmts_conv;
- if (*p == '.') {
- p++;
- if (mu_isdigit(*p)) {
- p = get_num(p, &prec);
- } else if (*p == '*') {
- p++;
+ if (format[cur] == '.') {
+ cur++;
+ if (mu_isdigit(format[cur])) {
+ cur = get_num(format, cur, &prec);
+ } else if (format[cur] == '*') {
+ cur++;
state = fmts_prec_arg;
}
}
@@ -191,13 +192,13 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
/* Expect precision argument position --
%2$#*1$.*_3$ */
state = fmts_conv;
- if (mu_isdigit(*p)) {
- char *q = get_num(p, &n);
- if (*q == '$') {
+ if (mu_isdigit(format[cur])) {
+ int pos = get_num(format, cur, &n);
+ if (format[pos] == '$') {
MF_VA_ARG(n-1, NUMBER, num);
if (num > 0)
prec = (unsigned) num;
- p = q + 1;
+ cur = pos + 1;
break;
}
}
@@ -210,7 +211,7 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
case fmts_conv: /* Expect conversion specifier */
if (!(flags & FMT_ALTPOS))
argnum = i++;
- switch (*p) {
+ switch (format[cur]) {
case 's':
MF_VA_ARG(argnum, STRING, str);
n = strlen(str);
@@ -331,7 +332,7 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
/* A - overrides a 0 if both are given.*/
if (prec || (flags & FMT_ADJUST_LEFT))
flags &= ~FMT_PADZERO;
- fmtbuf[1] = *p;
+ fmtbuf[1] = format[cur];
snprintf(buf+2, sizeof(buf)-2, fmtbuf, num);
str = buf + 2;
n = strlen(str);
@@ -342,7 +343,7 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
}
if (flags & FMT_ALTERNATE) {
- *--str = *p;
+ *--str = format[cur];
*--str = '0';
n += 2;
}
@@ -406,10 +407,11 @@ MF_DEFUN_VARARGS_NO_PROM(sprintf, STRING, STRING format)
break;
default:
- MF_OBSTACK_GROW(start, p - start + 1);
+ MF_OBSTACK_GROW(&format[start],
+ cur - start + 1);
}
- p++;
+ cur++;
state = fmts_copy;
}
}

Return to:

Send suggestions and report system problems to the System administrator.