Example #1
0
static void fmtint(struct fmtbuf *out, int value, int z, int base)
{
	static const char *digits = "0123456789abcdef";
	char buf[40];
	unsigned int a;
	int i;

	if (value < 0)
	{
		fmtputc(out, '-');
		a = -value;
	}
	else
		a = value;

	i = 0;
	while (a) {
		buf[i++] = digits[a % base];
		a /= base;
	}
	while (i < z)
		buf[i++] = '0';
	while (i > 0)
		fmtputc(out, buf[--i]);
}
Example #2
0
static void fmtuint32(struct fmtbuf *out, unsigned int a, int s, int z, int w, int base)
{
	char buf[40];
	int i;

	i = 0;
	if (a == 0)
		buf[i++] = '0';
	while (a) {
		buf[i++] = fz_hex_digits[a % base];
		a /= base;
	}
	if (z == '0')
		while (i < w - !!s)
			buf[i++] = z;
	if (s)
		buf[i++] = s;
	while (i < w)
		buf[i++] = z;
	if (z == ' ')
		while (i < w)
			buf[i++] = z;
	while (i > 0)
		fmtputc(out, buf[--i]);
}
Example #3
0
static void fmtfloat_f(struct fmtbuf *out, double f, int w, int p)
{
	char buf[100], *s = buf;
	snprintf(buf, sizeof buf, "%*.*f", w, p, f);
	while (*s)
		fmtputc(out, *s++);
}
Example #4
0
/*
 * Convert float to shortest possible string that won't lose precision, except:
 * NaN to 0, +Inf to FLT_MAX, -Inf to -FLT_MAX.
 */
static void fmtfloat(struct fmtbuf *out, float f)
{
	char digits[40], *s = digits;
	int exp, neg, ndigits, point;

	if (isnan(f)) f = 0;
	if (isinf(f)) f = f < 0 ? -FLT_MAX : FLT_MAX;

	fz_dtoa(f, digits, &exp, &neg, &ndigits);
	point = exp + ndigits;

	if (neg)
		fmtputc(out, '-');

	if (point <= 0)
	{
		fmtputc(out, '.');
		while (point++ < 0)
			fmtputc(out, '0');
		while (ndigits-- > 0)
			fmtputc(out, *s++);
	}

	else
	{
		while (ndigits-- > 0)
		{
			fmtputc(out, *s++);
			if (--point == 0 && ndigits > 0)
				fmtputc(out, '.');
		}
		while (point-- > 0)
			fmtputc(out, '0');
	}
}
Example #5
0
static bool Fconv(Format *f, int ignore) {	/* protect an exported name from brain-dead shells */
    int c;
    unsigned const char *s = va_arg(f->args, unsigned const char *);

    while ((c = *s++) != '\0')
        if (dnw[c] || c == '*' || (c == '_' && *s == '_'))
            fmtprint(f, "__%02x", c);
        else
            fmtputc(f, c);
    return FALSE;
}
Example #6
0
static void fmtint(struct fmtbuf *out, int value, int z, int base)
{
	unsigned int a;

	if (value < 0)
	{
		fmtputc(out, '-');
		a = -value;
	}
	else
		a = value;
	fmtuint(out, a, z, base);
}
Example #7
0
static void fmtquote(struct fmtbuf *out, const char *s, int sq, int eq)
{
	int c;
	fmtputc(out, sq);
	while ((c = *s++) != 0) {
		switch (c) {
		default:
			if (c < 32 || c > 127) {
				fmtputc(out, '\\');
				if (sq == '(')
				{
					fmtputc(out, '0' + ((c >> 6) & 7));
					fmtputc(out, '0' + ((c >> 3) & 7));
					fmtputc(out, '0' + ((c) & 7));
				}
				else
				{
					fmtputc(out, 'x');
					fmtputc(out, "0123456789ABCDEF"[(c>>4)&15]);
					fmtputc(out, "0123456789ABCDEF"[(c)&15]);
				}
			} else {
Example #8
0
static void fmtuint64(struct fmtbuf *out, uint64_t a, int z, int base)
{
	char buf[80];
	int i;

	i = 0;
	while (a) {
		buf[i++] = fz_hex_digits[a % base];
		a /= base;
	}
	while (i < z)
		buf[i++] = '0';
	while (i > 0)
		fmtputc(out, buf[--i]);
}
Example #9
0
/*
 * Convert float to shortest possible string that won't lose precision, except:
 * NaN to 0, +Inf to FLT_MAX, -Inf to -FLT_MAX.
 */
static void fmtfloat(struct fmtbuf *out, float f)
{
	char digits[40], *s = digits;
	int exp, ndigits, point;

	if (isnan(f)) f = 0;
	if (isinf(f)) f = f < 0 ? -FLT_MAX : FLT_MAX;

	if (signbit(f))
		fmtputc(out, '-');

	if (f == 0)
	{
		fmtputc(out, '0');
		return;
	}

	ndigits = fz_grisu(f, digits, &exp);
	point = exp + ndigits;

	if (point <= 0)
	{
		fmtputc(out, '.');
		while (point++ < 0)
			fmtputc(out, '0');
		while (ndigits-- > 0)
			fmtputc(out, *s++);
	}

	else
	{
		while (ndigits-- > 0)
		{
			fmtputc(out, *s++);
			if (--point == 0 && ndigits > 0)
				fmtputc(out, '.');
		}
		while (point-- > 0)
			fmtputc(out, '0');
	}
}
Example #10
0
/* strv -- print a formatted string into gc space */
extern char *strv(const char *fmt, va_list args) {
	Buffer *buf;
	Format format;

	gcdisable(0);
	buf = openbuffer(0);
	format.u.p	= buf;
	format.args	= args;
	format.buf	= buf->str;
	format.bufbegin	= buf->str;
	format.bufend	= buf->str + buf->len;
	format.grow	= str_grow;
	format.flushed	= 0;

	printfmt(&format, fmt);
	fmtputc(&format, '\0');
	gcenable();

	return sealbuffer(format.u.p);
}
Example #11
0
static void fmtquote(struct fmtbuf *out, const char *s, int sq, int eq)
{
	int c;
	fmtputc(out, sq);
	while ((c = *s++) != 0) {
		switch (c) {
		default:
			if (c < 32 || c > 127) {
				fmtputc(out, '\\');
				fmtputc(out, '0' + ((c >> 6) & 7));
				fmtputc(out, '0' + ((c >> 3) & 7));
				fmtputc(out, '0' + ((c) & 7));
			} else {
				if (c == sq || c == eq)
					fmtputc(out, '\\');
				fmtputc(out, c);
			}
			break;
		case '\\': fmtputc(out, '\\'); fmtputc(out, '\\'); break;
		case '\b': fmtputc(out, '\\'); fmtputc(out, 'b'); break;
		case '\f': fmtputc(out, '\\'); fmtputc(out, 'f'); break;
		case '\n': fmtputc(out, '\\'); fmtputc(out, 'n'); break;
		case '\r': fmtputc(out, '\\'); fmtputc(out, 'r'); break;
		case '\t': fmtputc(out, '\\'); fmtputc(out, 't'); break;
		}