2001-08-27 01:21:02 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
/*
|
|
|
|
* Portable safe sprintf code.
|
|
|
|
*
|
|
|
|
* Code based on mozilla/nsprpub/src/io/prprf.c rev 3.7
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Kipp E.B. Hickman <kipp@netscape.com> (original author)
|
|
|
|
* Frank Yung-Fong Tang <ftang@netscape.com>
|
|
|
|
* Daniele Nicolodi <daniele@grinta.net>
|
|
|
|
*/
|
|
|
|
|
2007-09-21 03:14:25 +04:00
|
|
|
/*
|
|
|
|
* Copied from xpcom/ds/nsTextFormatter.cpp r1.22
|
|
|
|
* Changed to use nsMemory and Frozen linkage
|
|
|
|
* -- Prasad <prasad@medhas.org>
|
|
|
|
*/
|
|
|
|
|
2000-01-17 09:33:59 +03:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2001-12-07 04:50:31 +03:00
|
|
|
#include "prdtoa.h"
|
2000-01-17 09:33:59 +03:00
|
|
|
#include "prlog.h"
|
2001-12-07 04:50:31 +03:00
|
|
|
#include "prprf.h"
|
2007-09-21 03:14:25 +04:00
|
|
|
#include "prmem.h"
|
|
|
|
#include "nsCRTGlue.h"
|
2000-01-17 09:33:59 +03:00
|
|
|
#include "nsTextFormatter.h"
|
2007-09-21 03:14:25 +04:00
|
|
|
#include "nsMemory.h"
|
2000-01-17 09:33:59 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Note: on some platforms va_list is defined as an array,
|
|
|
|
** and requires array notation.
|
|
|
|
*/
|
2001-12-07 04:50:31 +03:00
|
|
|
|
2003-02-23 09:59:39 +03:00
|
|
|
#ifdef HAVE_VA_COPY
|
|
|
|
#define VARARGS_ASSIGN(foo, bar) VA_COPY(foo,bar)
|
|
|
|
#elif defined(HAVE_VA_LIST_AS_ARRAY)
|
2000-01-17 09:33:59 +03:00
|
|
|
#define VARARGS_ASSIGN(foo, bar) foo[0] = bar[0]
|
|
|
|
#else
|
|
|
|
#define VARARGS_ASSIGN(foo, bar) (foo) = (bar)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct SprintfStateStr SprintfState;
|
|
|
|
|
|
|
|
struct SprintfStateStr {
|
2014-01-04 19:02:17 +04:00
|
|
|
int (*stuff)(SprintfState *ss, const char16_t *sp, uint32_t len);
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t *base;
|
|
|
|
char16_t *cur;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t maxlen;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2002-08-30 19:44:35 +04:00
|
|
|
void *stuffclosure;
|
2000-01-17 09:33:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Numbered Arguement State
|
|
|
|
*/
|
|
|
|
struct NumArgState{
|
|
|
|
int type; /* type of the current ap */
|
|
|
|
va_list ap; /* point to the corresponding position on ap */
|
2013-12-04 00:41:25 +04:00
|
|
|
|
|
|
|
enum Type {
|
|
|
|
INT16,
|
|
|
|
UINT16,
|
|
|
|
INTN,
|
|
|
|
UINTN,
|
|
|
|
INT32,
|
|
|
|
UINT32,
|
|
|
|
INT64,
|
|
|
|
UINT64,
|
|
|
|
STRING,
|
|
|
|
DOUBLE,
|
|
|
|
INTSTR,
|
|
|
|
UNISTRING,
|
|
|
|
UNKNOWN
|
|
|
|
};
|
2000-01-17 09:33:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NAS_DEFAULT_NUM 20 /* default number of NumberedArgumentState array */
|
|
|
|
|
|
|
|
#define _LEFT 0x1
|
|
|
|
#define _SIGNED 0x2
|
|
|
|
#define _SPACED 0x4
|
|
|
|
#define _ZEROS 0x8
|
|
|
|
#define _NEG 0x10
|
|
|
|
|
2001-08-27 01:21:02 +04:00
|
|
|
#define ELEMENTS_OF(array_) (sizeof(array_) / sizeof(array_[0]))
|
|
|
|
|
2000-01-17 09:33:59 +03:00
|
|
|
/*
|
|
|
|
** Fill into the buffer using the data in src
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static int fill2(SprintfState *ss, const char16_t *src, int srclen,
|
2001-12-07 04:50:31 +03:00
|
|
|
int width, int flags)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t space = ' ';
|
2000-01-17 09:33:59 +03:00
|
|
|
int rv;
|
2001-12-07 04:50:31 +03:00
|
|
|
|
2000-01-17 09:33:59 +03:00
|
|
|
width -= srclen;
|
2001-12-07 04:50:31 +03:00
|
|
|
/* Right adjusting */
|
|
|
|
if ((width > 0) && ((flags & _LEFT) == 0)) {
|
2000-01-17 09:33:59 +03:00
|
|
|
if (flags & _ZEROS) {
|
|
|
|
space = '0';
|
|
|
|
}
|
|
|
|
while (--width >= 0) {
|
|
|
|
rv = (*ss->stuff)(ss, &space, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy out the source data */
|
|
|
|
rv = (*ss->stuff)(ss, src, srclen);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-12-07 04:50:31 +03:00
|
|
|
|
|
|
|
/* Left adjusting */
|
|
|
|
if ((width > 0) && ((flags & _LEFT) != 0)) {
|
2000-01-17 09:33:59 +03:00
|
|
|
while (--width >= 0) {
|
|
|
|
rv = (*ss->stuff)(ss, &space, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Fill a number. The order is: optional-sign zero-filling conversion-digits
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static int fill_n(SprintfState *ss, const char16_t *src, int srclen,
|
2001-12-07 04:50:31 +03:00
|
|
|
int width, int prec, int type, int flags)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
2001-12-07 04:50:31 +03:00
|
|
|
int zerowidth = 0;
|
|
|
|
int precwidth = 0;
|
|
|
|
int signwidth = 0;
|
|
|
|
int leftspaces = 0;
|
2000-01-17 09:33:59 +03:00
|
|
|
int rightspaces = 0;
|
|
|
|
int cvtwidth;
|
|
|
|
int rv;
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t sign;
|
|
|
|
char16_t space = ' ';
|
|
|
|
char16_t zero = '0';
|
2000-01-17 09:33:59 +03:00
|
|
|
|
|
|
|
if ((type & 1) == 0) {
|
|
|
|
if (flags & _NEG) {
|
|
|
|
sign = '-';
|
|
|
|
signwidth = 1;
|
|
|
|
} else if (flags & _SIGNED) {
|
|
|
|
sign = '+';
|
|
|
|
signwidth = 1;
|
|
|
|
} else if (flags & _SPACED) {
|
|
|
|
sign = ' ';
|
|
|
|
signwidth = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cvtwidth = signwidth + srclen;
|
|
|
|
|
|
|
|
if (prec > 0) {
|
|
|
|
if (prec > srclen) {
|
2001-12-07 04:50:31 +03:00
|
|
|
/* Need zero filling */
|
|
|
|
precwidth = prec - srclen;
|
2000-01-17 09:33:59 +03:00
|
|
|
cvtwidth += precwidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & _ZEROS) && (prec < 0)) {
|
|
|
|
if (width > cvtwidth) {
|
2001-12-07 04:50:31 +03:00
|
|
|
/* Zero filling */
|
|
|
|
zerowidth = width - cvtwidth;
|
2000-01-17 09:33:59 +03:00
|
|
|
cvtwidth += zerowidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & _LEFT) {
|
|
|
|
if (width > cvtwidth) {
|
|
|
|
/* Space filling on the right (i.e. left adjusting) */
|
|
|
|
rightspaces = width - cvtwidth;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (width > cvtwidth) {
|
|
|
|
/* Space filling on the left (i.e. right adjusting) */
|
|
|
|
leftspaces = width - cvtwidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (--leftspaces >= 0) {
|
|
|
|
rv = (*ss->stuff)(ss, &space, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (signwidth) {
|
|
|
|
rv = (*ss->stuff)(ss, &sign, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (--precwidth >= 0) {
|
|
|
|
rv = (*ss->stuff)(ss, &space, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (--zerowidth >= 0) {
|
|
|
|
rv = (*ss->stuff)(ss, &zero, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rv = (*ss->stuff)(ss, src, srclen);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
while (--rightspaces >= 0) {
|
|
|
|
rv = (*ss->stuff)(ss, &space, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Convert a long into its printable form
|
|
|
|
*/
|
2001-12-07 04:50:31 +03:00
|
|
|
static int cvt_l(SprintfState *ss, long num, int width, int prec,
|
2014-01-04 19:02:17 +04:00
|
|
|
int radix, int type, int flags, const char16_t *hexp)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t cvtbuf[100];
|
|
|
|
char16_t *cvt;
|
2000-01-17 09:33:59 +03:00
|
|
|
int digits;
|
|
|
|
|
|
|
|
/* according to the man page this needs to happen */
|
|
|
|
if ((prec == 0) && (num == 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Converting decimal is a little tricky. In the unsigned case we
|
|
|
|
** need to stop when we hit 10 digits. In the signed case, we can
|
|
|
|
** stop when the number is zero.
|
|
|
|
*/
|
2001-08-27 01:21:02 +04:00
|
|
|
cvt = &cvtbuf[0] + ELEMENTS_OF(cvtbuf);
|
2000-01-17 09:33:59 +03:00
|
|
|
digits = 0;
|
|
|
|
while (num) {
|
|
|
|
int digit = (((unsigned long)num) % radix) & 0xF;
|
|
|
|
*--cvt = hexp[digit];
|
|
|
|
digits++;
|
|
|
|
num = (long)(((unsigned long)num) / radix);
|
|
|
|
}
|
|
|
|
if (digits == 0) {
|
|
|
|
*--cvt = '0';
|
|
|
|
digits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Now that we have the number converted without its sign, deal with
|
|
|
|
** the sign and zero padding.
|
|
|
|
*/
|
|
|
|
return fill_n(ss, cvt, digits, width, prec, type, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Convert a 64-bit integer into its printable form
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
static int cvt_ll(SprintfState *ss, int64_t num, int width, int prec,
|
2014-01-04 19:02:17 +04:00
|
|
|
int radix, int type, int flags, const char16_t *hexp)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t cvtbuf[100];
|
|
|
|
char16_t *cvt;
|
2000-01-17 09:33:59 +03:00
|
|
|
int digits;
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t rad;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
|
|
|
/* according to the man page this needs to happen */
|
2012-10-03 18:13:18 +04:00
|
|
|
if (prec == 0 && num == 0) {
|
2000-01-17 09:33:59 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Converting decimal is a little tricky. In the unsigned case we
|
|
|
|
** need to stop when we hit 10 digits. In the signed case, we can
|
|
|
|
** stop when the number is zero.
|
|
|
|
*/
|
2012-10-12 21:29:11 +04:00
|
|
|
rad = radix;
|
2001-08-27 01:21:02 +04:00
|
|
|
cvt = &cvtbuf[0] + ELEMENTS_OF(cvtbuf);
|
2000-01-17 09:33:59 +03:00
|
|
|
digits = 0;
|
2012-10-03 18:13:18 +04:00
|
|
|
while (num != 0) {
|
2012-11-02 04:57:25 +04:00
|
|
|
*--cvt = hexp[int32_t(num % rad) & 0xf];
|
2000-01-17 09:33:59 +03:00
|
|
|
digits++;
|
2012-11-02 04:57:25 +04:00
|
|
|
num /= rad;
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
if (digits == 0) {
|
|
|
|
*--cvt = '0';
|
|
|
|
digits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Now that we have the number converted without its sign, deal with
|
|
|
|
** the sign and zero padding.
|
|
|
|
*/
|
|
|
|
return fill_n(ss, cvt, digits, width, prec, type, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Convert a double precision floating point number into its printable
|
|
|
|
** form.
|
|
|
|
*/
|
2001-12-07 04:50:31 +03:00
|
|
|
static int cvt_f(SprintfState *ss, double d, int width, int prec,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t type, int flags)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
2001-12-07 04:50:31 +03:00
|
|
|
int mode = 2;
|
|
|
|
int decpt;
|
|
|
|
int sign;
|
|
|
|
char buf[256];
|
|
|
|
char * bufp = buf;
|
|
|
|
int bufsz = 256;
|
|
|
|
char num[256];
|
|
|
|
char * nump;
|
|
|
|
char * endnum;
|
|
|
|
int numdigits = 0;
|
|
|
|
char exp = 'e';
|
|
|
|
|
|
|
|
if (prec == -1) {
|
|
|
|
prec = 6;
|
|
|
|
} else if (prec > 50) {
|
|
|
|
// limit precision to avoid PR_dtoa bug 108335
|
|
|
|
// and to prevent buffers overflows
|
|
|
|
prec = 50;
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
switch (type) {
|
|
|
|
case 'f':
|
|
|
|
numdigits = prec;
|
|
|
|
mode = 3;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
exp = 'E';
|
|
|
|
// no break
|
|
|
|
case 'e':
|
|
|
|
numdigits = prec + 1;
|
|
|
|
mode = 2;
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
exp = 'E';
|
|
|
|
// no break
|
|
|
|
case 'g':
|
|
|
|
if (prec == 0) {
|
|
|
|
prec = 1;
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
2001-12-07 04:50:31 +03:00
|
|
|
numdigits = prec;
|
|
|
|
mode = 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("invalid type passed to cvt_f");
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
2001-12-07 04:50:31 +03:00
|
|
|
|
|
|
|
if (PR_dtoa(d, mode, numdigits, &decpt, &sign, &endnum, num, bufsz) == PR_FAILURE) {
|
|
|
|
buf[0] = '\0';
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
numdigits = endnum - num;
|
|
|
|
nump = num;
|
2000-04-12 02:47:18 +04:00
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
if (sign) {
|
|
|
|
*bufp++ = '-';
|
|
|
|
} else if (flags & _SIGNED) {
|
|
|
|
*bufp++ = '+';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decpt == 9999) {
|
|
|
|
while ((*bufp++ = *nump++)) { }
|
|
|
|
} else {
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case 'E':
|
|
|
|
case 'e':
|
|
|
|
|
|
|
|
*bufp++ = *nump++;
|
|
|
|
if (prec > 0) {
|
|
|
|
*bufp++ = '.';
|
|
|
|
while (*nump) {
|
|
|
|
*bufp++ = *nump++;
|
|
|
|
prec--;
|
|
|
|
}
|
|
|
|
while (prec-- > 0) {
|
|
|
|
*bufp++ = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*bufp++ = exp;
|
|
|
|
PR_snprintf(bufp, bufsz - (bufp - buf), "%+03d", decpt-1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
|
|
|
|
if (decpt < 1) {
|
|
|
|
*bufp++ = '0';
|
|
|
|
if (prec > 0) {
|
|
|
|
*bufp++ = '.';
|
|
|
|
while (decpt++ && prec-- > 0) {
|
|
|
|
*bufp++ = '0';
|
|
|
|
}
|
|
|
|
while (*nump && prec-- > 0) {
|
|
|
|
*bufp++ = *nump++;
|
|
|
|
}
|
|
|
|
while (prec-- > 0) {
|
|
|
|
*bufp++ = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (*nump && decpt-- > 0) {
|
|
|
|
*bufp++ = *nump++;
|
|
|
|
}
|
|
|
|
while (decpt-- > 0) {
|
|
|
|
*bufp++ = '0';
|
|
|
|
}
|
|
|
|
if (prec > 0) {
|
|
|
|
*bufp++ = '.';
|
|
|
|
while (*nump && prec-- > 0) {
|
|
|
|
*bufp++ = *nump++;
|
|
|
|
}
|
|
|
|
while (prec-- > 0) {
|
|
|
|
*bufp++ = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*bufp = '\0';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'G':
|
|
|
|
case 'g':
|
|
|
|
|
|
|
|
if ((decpt < -3) || ((decpt - 1) >= prec)) {
|
|
|
|
*bufp++ = *nump++;
|
|
|
|
numdigits--;
|
|
|
|
if (numdigits > 0) {
|
|
|
|
*bufp++ = '.';
|
|
|
|
while (*nump) {
|
|
|
|
*bufp++ = *nump++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*bufp++ = exp;
|
|
|
|
PR_snprintf(bufp, bufsz - (bufp - buf), "%+03d", decpt-1);
|
|
|
|
} else {
|
|
|
|
if (decpt < 1) {
|
|
|
|
*bufp++ = '0';
|
|
|
|
if (prec > 0) {
|
|
|
|
*bufp++ = '.';
|
|
|
|
while (decpt++) {
|
|
|
|
*bufp++ = '0';
|
|
|
|
}
|
|
|
|
while (*nump) {
|
|
|
|
*bufp++ = *nump++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (*nump && decpt-- > 0) {
|
|
|
|
*bufp++ = *nump++;
|
|
|
|
numdigits--;
|
|
|
|
}
|
|
|
|
while (decpt-- > 0) {
|
|
|
|
*bufp++ = '0';
|
|
|
|
}
|
|
|
|
if (numdigits > 0) {
|
|
|
|
*bufp++ = '.';
|
|
|
|
while (*nump) {
|
|
|
|
*bufp++ = *nump++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*bufp = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t rbuf[256];
|
|
|
|
char16_t *rbufp = rbuf;
|
2001-12-07 04:50:31 +03:00
|
|
|
bufp = buf;
|
2014-01-04 19:02:17 +04:00
|
|
|
// cast to char16_t
|
2001-12-07 04:50:31 +03:00
|
|
|
while ((*rbufp++ = *bufp++)) { }
|
|
|
|
*rbufp = '\0';
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2007-09-21 03:14:25 +04:00
|
|
|
return fill2(ss, rbuf, NS_strlen(rbuf), width, flags);
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-12-07 04:50:31 +03:00
|
|
|
** Convert a string into its printable form. "width" is the output
|
2000-01-17 09:33:59 +03:00
|
|
|
** width. "prec" is the maximum number of characters of "s" to output,
|
|
|
|
** where -1 means until NUL.
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static int cvt_S(SprintfState *ss, const char16_t *s, int width,
|
2001-12-07 04:50:31 +03:00
|
|
|
int prec, int flags)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
|
|
|
int slen;
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
if (prec == 0) {
|
2000-01-17 09:33:59 +03:00
|
|
|
return 0;
|
2001-12-07 04:50:31 +03:00
|
|
|
}
|
2000-01-17 09:33:59 +03:00
|
|
|
|
|
|
|
/* Limit string length by precision value */
|
2007-09-21 03:14:25 +04:00
|
|
|
slen = s ? NS_strlen(s) : 6;
|
2000-01-17 09:33:59 +03:00
|
|
|
if (prec > 0) {
|
|
|
|
if (prec < slen) {
|
|
|
|
slen = prec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and away we go */
|
2014-02-06 20:42:58 +04:00
|
|
|
return fill2(ss, s ? s : MOZ_UTF16("(null)"), slen, width, flags);
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Convert a string into its printable form. "width" is the output
|
|
|
|
** width. "prec" is the maximum number of characters of "s" to output,
|
|
|
|
** where -1 means until NUL.
|
|
|
|
*/
|
2001-12-07 04:50:31 +03:00
|
|
|
static int cvt_s(SprintfState *ss, const char *s, int width,
|
|
|
|
int prec, int flags)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
2005-02-20 20:09:10 +03:00
|
|
|
NS_ConvertUTF8toUTF16 utf16Val(s);
|
|
|
|
return cvt_S(ss, utf16Val.get(), width, prec, flags);
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-11-26 00:57:13 +03:00
|
|
|
** BuildArgArray stands for Numbered Argument list Sprintf
|
2000-01-17 09:33:59 +03:00
|
|
|
** for example,
|
|
|
|
** fmp = "%4$i, %2$d, %3s, %1d";
|
|
|
|
** the number must start from 1, and no gap among them
|
|
|
|
*/
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
static struct NumArgState* BuildArgArray(const char16_t *fmt,
|
2001-12-07 04:50:31 +03:00
|
|
|
va_list ap, int * rv,
|
|
|
|
struct NumArgState * nasArray)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
|
|
|
int number = 0, cn = 0, i;
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* p;
|
|
|
|
char16_t c;
|
2000-01-17 09:33:59 +03:00
|
|
|
struct NumArgState* nas;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** first pass:
|
|
|
|
** detemine how many legal % I have got, then allocate space
|
|
|
|
*/
|
|
|
|
p = fmt;
|
|
|
|
*rv = 0;
|
|
|
|
i = 0;
|
2001-12-07 04:50:31 +03:00
|
|
|
while ((c = *p++) != 0) {
|
|
|
|
if (c != '%') {
|
2000-01-17 09:33:59 +03:00
|
|
|
continue;
|
2001-12-07 04:50:31 +03:00
|
|
|
}
|
|
|
|
/* skip %% case */
|
|
|
|
if ((c = *p++) == '%') {
|
2000-01-17 09:33:59 +03:00
|
|
|
continue;
|
2001-12-07 04:50:31 +03:00
|
|
|
}
|
2000-01-17 09:33:59 +03:00
|
|
|
|
|
|
|
while( c != 0 ){
|
2001-12-07 04:50:31 +03:00
|
|
|
if (c > '9' || c < '0') {
|
|
|
|
/* numbered argument csae */
|
|
|
|
if (c == '$') {
|
|
|
|
if (i > 0) {
|
2000-01-17 09:33:59 +03:00
|
|
|
*rv = -1;
|
2013-10-11 00:41:39 +04:00
|
|
|
return nullptr;
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
number++;
|
|
|
|
break;
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
} else {
|
|
|
|
/* non-numbered argument case */
|
|
|
|
if (number > 0) {
|
2000-01-17 09:33:59 +03:00
|
|
|
*rv = -1;
|
2013-10-11 00:41:39 +04:00
|
|
|
return nullptr;
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
i = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c = *p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
if (number == 0) {
|
2013-10-11 00:41:39 +04:00
|
|
|
return nullptr;
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
if (number > NAS_DEFAULT_NUM) {
|
2007-09-21 03:14:25 +04:00
|
|
|
nas = (struct NumArgState*)nsMemory::Alloc(number * sizeof(struct NumArgState));
|
2001-12-07 04:50:31 +03:00
|
|
|
if (!nas) {
|
2000-01-17 09:33:59 +03:00
|
|
|
*rv = -1;
|
2013-10-11 00:41:39 +04:00
|
|
|
return nullptr;
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nas = nasArray;
|
|
|
|
}
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
for (i = 0; i < number; i++) {
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[i].type = NumArgState::UNKNOWN;
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** second pass:
|
|
|
|
** set nas[].type
|
|
|
|
*/
|
|
|
|
p = fmt;
|
2001-12-07 04:50:31 +03:00
|
|
|
while ((c = *p++) != 0) {
|
|
|
|
if (c != '%') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
c = *p++;
|
|
|
|
if (c == '%') {
|
|
|
|
continue;
|
|
|
|
}
|
2000-01-17 09:33:59 +03:00
|
|
|
cn = 0;
|
2001-12-07 04:50:31 +03:00
|
|
|
/* should imporve error check later */
|
|
|
|
while (c && c != '$') {
|
2000-01-17 09:33:59 +03:00
|
|
|
cn = cn*10 + c - '0';
|
|
|
|
c = *p++;
|
|
|
|
}
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
if (!c || cn < 1 || cn > number) {
|
2000-01-17 09:33:59 +03:00
|
|
|
*rv = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
/* nas[cn] starts from 0, and make sure
|
|
|
|
nas[cn].type is not assigned */
|
2000-01-17 09:33:59 +03:00
|
|
|
cn--;
|
2013-12-04 00:41:25 +04:00
|
|
|
if (nas[cn].type != NumArgState::UNKNOWN) {
|
2000-01-17 09:33:59 +03:00
|
|
|
continue;
|
2001-12-07 04:50:31 +03:00
|
|
|
}
|
2000-01-17 09:33:59 +03:00
|
|
|
|
|
|
|
c = *p++;
|
|
|
|
|
|
|
|
/* width */
|
|
|
|
if (c == '*') {
|
|
|
|
/* not supported feature, for the argument is not numbered */
|
|
|
|
*rv = -1;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
while ((c >= '0') && (c <= '9')) {
|
|
|
|
c = *p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* precision */
|
|
|
|
if (c == '.') {
|
|
|
|
c = *p++;
|
|
|
|
if (c == '*') {
|
|
|
|
/* not supported feature, for the argument is not numbered */
|
|
|
|
*rv = -1;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
while ((c >= '0') && (c <= '9')) {
|
|
|
|
c = *p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* size */
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::INTN;
|
2000-01-17 09:33:59 +03:00
|
|
|
if (c == 'h') {
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::INT16;
|
2000-01-17 09:33:59 +03:00
|
|
|
c = *p++;
|
|
|
|
} else if (c == 'L') {
|
|
|
|
/* XXX not quite sure here */
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::INT64;
|
2000-01-17 09:33:59 +03:00
|
|
|
c = *p++;
|
|
|
|
} else if (c == 'l') {
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::INT32;
|
2000-01-17 09:33:59 +03:00
|
|
|
c = *p++;
|
|
|
|
if (c == 'l') {
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::INT64;
|
2000-01-17 09:33:59 +03:00
|
|
|
c = *p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* format */
|
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
|
|
|
case 'c':
|
|
|
|
case 'i':
|
|
|
|
case 'o':
|
|
|
|
case 'u':
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e':
|
|
|
|
case 'f':
|
|
|
|
case 'g':
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::DOUBLE;
|
2000-01-17 09:33:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
/* XXX should use cpp */
|
2012-08-22 19:56:38 +04:00
|
|
|
if (sizeof(void *) == sizeof(int32_t)) {
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::UINT32;
|
2012-08-22 19:56:38 +04:00
|
|
|
} else if (sizeof(void *) == sizeof(int64_t)) {
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::UINT64;
|
2012-08-09 11:09:40 +04:00
|
|
|
} else if (sizeof(void *) == sizeof(int)) {
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::UINTN;
|
2000-01-17 09:33:59 +03:00
|
|
|
} else {
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::UNKNOWN;
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
/* XXX not supported I suppose */
|
|
|
|
PR_ASSERT(0);
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::UNKNOWN;
|
2000-01-17 09:33:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S':
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::UNISTRING;
|
2000-01-17 09:33:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::STRING;
|
2000-01-17 09:33:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::INTSTR;
|
2000-01-17 09:33:59 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
PR_ASSERT(0);
|
2013-12-04 00:41:25 +04:00
|
|
|
nas[cn].type = NumArgState::UNKNOWN;
|
2000-01-17 09:33:59 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get a legal para. */
|
2013-12-04 00:41:25 +04:00
|
|
|
if (nas[cn].type == NumArgState::UNKNOWN) {
|
2000-01-17 09:33:59 +03:00
|
|
|
*rv = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** third pass
|
|
|
|
** fill the nas[cn].ap
|
|
|
|
*/
|
2001-12-07 04:50:31 +03:00
|
|
|
if (*rv < 0) {
|
|
|
|
if( nas != nasArray ) {
|
|
|
|
PR_DELETE(nas);
|
|
|
|
}
|
2013-10-11 00:41:39 +04:00
|
|
|
return nullptr;
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cn = 0;
|
2001-12-07 04:50:31 +03:00
|
|
|
while (cn < number) {
|
2013-12-04 00:41:25 +04:00
|
|
|
if (nas[cn].type == NumArgState::UNKNOWN) {
|
2000-01-17 09:33:59 +03:00
|
|
|
cn++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
VARARGS_ASSIGN(nas[cn].ap, ap);
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
switch (nas[cn].type) {
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::INT16:
|
|
|
|
case NumArgState::UINT16:
|
|
|
|
case NumArgState::INTN:
|
|
|
|
case NumArgState::UINTN: (void)va_arg(ap, int); break;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::INT32: (void)va_arg(ap, int32_t); break;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::UINT32: (void)va_arg(ap, uint32_t); break;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::INT64: (void)va_arg(ap, int64_t); break;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::UINT64: (void)va_arg(ap, uint64_t); break;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::STRING: (void)va_arg(ap, char*); break;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::INTSTR: (void)va_arg(ap, int*); break;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::DOUBLE: (void)va_arg(ap, double); break;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
case NumArgState::UNISTRING: (void)va_arg(ap, char16_t*); break;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
|
|
|
default:
|
2001-12-07 04:50:31 +03:00
|
|
|
if( nas != nasArray ) {
|
2000-01-17 09:33:59 +03:00
|
|
|
PR_DELETE( nas );
|
2001-12-07 04:50:31 +03:00
|
|
|
}
|
2000-01-17 09:33:59 +03:00
|
|
|
*rv = -1;
|
2013-10-11 00:41:39 +04:00
|
|
|
return nullptr;
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
cn++;
|
|
|
|
}
|
|
|
|
return nas;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The workhorse sprintf code.
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static int dosprintf(SprintfState *ss, const char16_t *fmt, va_list ap)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t c;
|
2000-01-17 09:33:59 +03:00
|
|
|
int flags, width, prec, radix, type;
|
|
|
|
union {
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t ch;
|
2000-01-17 09:33:59 +03:00
|
|
|
int i;
|
|
|
|
long l;
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t ll;
|
2000-01-17 09:33:59 +03:00
|
|
|
double d;
|
|
|
|
const char *s;
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *S;
|
2000-01-17 09:33:59 +03:00
|
|
|
int *ip;
|
|
|
|
} u;
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t space = ' ';
|
2000-04-01 04:39:02 +04:00
|
|
|
|
|
|
|
nsAutoString hex;
|
2004-06-17 04:13:25 +04:00
|
|
|
hex.AssignLiteral("0123456789abcdef");
|
2000-04-01 04:39:02 +04:00
|
|
|
|
|
|
|
nsAutoString HEX;
|
2004-06-17 04:13:25 +04:00
|
|
|
HEX.AssignLiteral("0123456789ABCDEF");
|
2000-04-01 04:39:02 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *hexp;
|
2000-01-17 09:33:59 +03:00
|
|
|
int rv, i;
|
2013-10-11 00:41:39 +04:00
|
|
|
struct NumArgState* nas = nullptr;
|
2001-12-07 04:50:31 +03:00
|
|
|
struct NumArgState nasArray[NAS_DEFAULT_NUM];
|
2000-01-17 09:33:59 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** build an argument array, IF the fmt is numbered argument
|
|
|
|
** list style, to contain the Numbered Argument list pointers
|
|
|
|
*/
|
2001-12-07 04:50:31 +03:00
|
|
|
nas = BuildArgArray (fmt, ap, &rv, nasArray);
|
|
|
|
if (rv < 0) {
|
2000-01-17 09:33:59 +03:00
|
|
|
/* the fmt contains error Numbered Argument format, jliu@netscape.com */
|
|
|
|
PR_ASSERT(0);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((c = *fmt++) != 0) {
|
|
|
|
if (c != '%') {
|
|
|
|
rv = (*ss->stuff)(ss, fmt - 1, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Gobble up the % format string. Hopefully we have handled all
|
|
|
|
** of the strange cases!
|
|
|
|
*/
|
|
|
|
flags = 0;
|
|
|
|
c = *fmt++;
|
|
|
|
if (c == '%') {
|
|
|
|
/* quoting a % with %% */
|
|
|
|
rv = (*ss->stuff)(ss, fmt - 1, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-10-11 00:41:39 +04:00
|
|
|
if (nas != nullptr) {
|
2000-01-17 09:33:59 +03:00
|
|
|
/* the fmt contains the Numbered Arguments feature */
|
|
|
|
i = 0;
|
2001-12-07 04:50:31 +03:00
|
|
|
/* should imporve error check later */
|
|
|
|
while (c && c != '$') {
|
|
|
|
i = (i * 10) + (c - '0');
|
2000-01-17 09:33:59 +03:00
|
|
|
c = *fmt++;
|
|
|
|
}
|
|
|
|
|
2013-12-04 00:41:25 +04:00
|
|
|
if (nas[i-1].type == NumArgState::UNKNOWN) {
|
2001-12-07 04:50:31 +03:00
|
|
|
if (nas && (nas != nasArray)) {
|
|
|
|
PR_DELETE(nas);
|
|
|
|
}
|
2000-01-17 09:33:59 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-14 14:11:25 +04:00
|
|
|
VARARGS_ASSIGN(ap, nas[i-1].ap);
|
2000-01-17 09:33:59 +03:00
|
|
|
c = *fmt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Examine optional flags. Note that we do not implement the
|
|
|
|
* '#' flag of sprintf(). The ANSI C spec. of the '#' flag is
|
|
|
|
* somewhat ambiguous and not ideal, which is perhaps why
|
|
|
|
* the various sprintf() implementations are inconsistent
|
|
|
|
* on this feature.
|
|
|
|
*/
|
|
|
|
while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) {
|
|
|
|
if (c == '-') flags |= _LEFT;
|
|
|
|
if (c == '+') flags |= _SIGNED;
|
|
|
|
if (c == ' ') flags |= _SPACED;
|
|
|
|
if (c == '0') flags |= _ZEROS;
|
|
|
|
c = *fmt++;
|
|
|
|
}
|
|
|
|
if (flags & _SIGNED) flags &= ~_SPACED;
|
|
|
|
if (flags & _LEFT) flags &= ~_ZEROS;
|
|
|
|
|
|
|
|
/* width */
|
|
|
|
if (c == '*') {
|
|
|
|
c = *fmt++;
|
|
|
|
width = va_arg(ap, int);
|
|
|
|
} else {
|
|
|
|
width = 0;
|
|
|
|
while ((c >= '0') && (c <= '9')) {
|
|
|
|
width = (width * 10) + (c - '0');
|
|
|
|
c = *fmt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* precision */
|
|
|
|
prec = -1;
|
|
|
|
if (c == '.') {
|
|
|
|
c = *fmt++;
|
|
|
|
if (c == '*') {
|
|
|
|
c = *fmt++;
|
|
|
|
prec = va_arg(ap, int);
|
|
|
|
} else {
|
|
|
|
prec = 0;
|
|
|
|
while ((c >= '0') && (c <= '9')) {
|
|
|
|
prec = (prec * 10) + (c - '0');
|
|
|
|
c = *fmt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* size */
|
2013-12-04 00:41:25 +04:00
|
|
|
type = NumArgState::INTN;
|
2000-01-17 09:33:59 +03:00
|
|
|
if (c == 'h') {
|
2013-12-04 00:41:25 +04:00
|
|
|
type = NumArgState::INT16;
|
2000-01-17 09:33:59 +03:00
|
|
|
c = *fmt++;
|
|
|
|
} else if (c == 'L') {
|
|
|
|
/* XXX not quite sure here */
|
2013-12-04 00:41:25 +04:00
|
|
|
type = NumArgState::INT64;
|
2000-01-17 09:33:59 +03:00
|
|
|
c = *fmt++;
|
|
|
|
} else if (c == 'l') {
|
2013-12-04 00:41:25 +04:00
|
|
|
type = NumArgState::INT32;
|
2000-01-17 09:33:59 +03:00
|
|
|
c = *fmt++;
|
|
|
|
if (c == 'l') {
|
2013-12-04 00:41:25 +04:00
|
|
|
type = NumArgState::INT64;
|
2000-01-17 09:33:59 +03:00
|
|
|
c = *fmt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* format */
|
2001-06-30 15:02:25 +04:00
|
|
|
hexp = hex.get();
|
2000-01-17 09:33:59 +03:00
|
|
|
switch (c) {
|
2001-12-07 04:50:31 +03:00
|
|
|
case 'd':
|
|
|
|
case 'i': /* decimal/integer */
|
2000-01-17 09:33:59 +03:00
|
|
|
radix = 10;
|
|
|
|
goto fetch_and_convert;
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
case 'o': /* octal */
|
2000-01-17 09:33:59 +03:00
|
|
|
radix = 8;
|
|
|
|
type |= 1;
|
|
|
|
goto fetch_and_convert;
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
case 'u': /* unsigned decimal */
|
2000-01-17 09:33:59 +03:00
|
|
|
radix = 10;
|
|
|
|
type |= 1;
|
|
|
|
goto fetch_and_convert;
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
case 'x': /* unsigned hex */
|
2000-01-17 09:33:59 +03:00
|
|
|
radix = 16;
|
|
|
|
type |= 1;
|
|
|
|
goto fetch_and_convert;
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
case 'X': /* unsigned HEX */
|
2000-01-17 09:33:59 +03:00
|
|
|
radix = 16;
|
2001-06-30 15:02:25 +04:00
|
|
|
hexp = HEX.get();
|
2000-01-17 09:33:59 +03:00
|
|
|
type |= 1;
|
|
|
|
goto fetch_and_convert;
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
fetch_and_convert:
|
2000-01-17 09:33:59 +03:00
|
|
|
switch (type) {
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::INT16:
|
2000-01-17 09:33:59 +03:00
|
|
|
u.l = va_arg(ap, int);
|
|
|
|
if (u.l < 0) {
|
|
|
|
u.l = -u.l;
|
|
|
|
flags |= _NEG;
|
|
|
|
}
|
|
|
|
goto do_long;
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::UINT16:
|
2000-01-17 09:33:59 +03:00
|
|
|
u.l = va_arg(ap, int) & 0xffff;
|
|
|
|
goto do_long;
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::INTN:
|
2000-01-17 09:33:59 +03:00
|
|
|
u.l = va_arg(ap, int);
|
|
|
|
if (u.l < 0) {
|
|
|
|
u.l = -u.l;
|
|
|
|
flags |= _NEG;
|
|
|
|
}
|
|
|
|
goto do_long;
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::UINTN:
|
2000-01-17 09:33:59 +03:00
|
|
|
u.l = (long)va_arg(ap, unsigned int);
|
|
|
|
goto do_long;
|
|
|
|
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::INT32:
|
2012-08-22 19:56:38 +04:00
|
|
|
u.l = va_arg(ap, int32_t);
|
2000-01-17 09:33:59 +03:00
|
|
|
if (u.l < 0) {
|
|
|
|
u.l = -u.l;
|
|
|
|
flags |= _NEG;
|
|
|
|
}
|
|
|
|
goto do_long;
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::UINT32:
|
2012-08-22 19:56:38 +04:00
|
|
|
u.l = (long)va_arg(ap, uint32_t);
|
2001-12-07 04:50:31 +03:00
|
|
|
do_long:
|
2000-01-17 09:33:59 +03:00
|
|
|
rv = cvt_l(ss, u.l, width, prec, radix, type, flags, hexp);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::INT64:
|
2012-08-22 19:56:38 +04:00
|
|
|
u.ll = va_arg(ap, int64_t);
|
2012-10-03 18:13:19 +04:00
|
|
|
if (u.ll < 0) {
|
2012-10-05 09:05:27 +04:00
|
|
|
u.ll = -u.ll;
|
2000-01-17 09:33:59 +03:00
|
|
|
flags |= _NEG;
|
|
|
|
}
|
|
|
|
goto do_longlong;
|
2013-12-04 00:41:25 +04:00
|
|
|
case NumArgState::UINT64:
|
2012-08-22 19:56:38 +04:00
|
|
|
u.ll = va_arg(ap, uint64_t);
|
2001-12-07 04:50:31 +03:00
|
|
|
do_longlong:
|
2000-01-17 09:33:59 +03:00
|
|
|
rv = cvt_ll(ss, u.ll, width, prec, radix, type, flags, hexp);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
case 'f':
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
2000-01-17 09:33:59 +03:00
|
|
|
u.d = va_arg(ap, double);
|
2001-12-07 04:50:31 +03:00
|
|
|
rv = cvt_f(ss, u.d, width, prec, c, flags);
|
2000-01-17 09:33:59 +03:00
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
case 'c':
|
2000-01-17 09:33:59 +03:00
|
|
|
u.ch = va_arg(ap, int);
|
|
|
|
if ((flags & _LEFT) == 0) {
|
|
|
|
while (width-- > 1) {
|
|
|
|
rv = (*ss->stuff)(ss, &space, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rv = (*ss->stuff)(ss, &u.ch, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (flags & _LEFT) {
|
|
|
|
while (width-- > 1) {
|
|
|
|
rv = (*ss->stuff)(ss, &space, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
case 'p':
|
2012-08-22 19:56:38 +04:00
|
|
|
if (sizeof(void *) == sizeof(int32_t)) {
|
2013-12-04 00:41:25 +04:00
|
|
|
type = NumArgState::UINT32;
|
2012-08-22 19:56:38 +04:00
|
|
|
} else if (sizeof(void *) == sizeof(int64_t)) {
|
2013-12-04 00:41:25 +04:00
|
|
|
type = NumArgState::UINT64;
|
2000-01-17 09:33:59 +03:00
|
|
|
} else if (sizeof(void *) == sizeof(int)) {
|
2013-12-04 00:41:25 +04:00
|
|
|
type = NumArgState::UINTN;
|
2000-01-17 09:33:59 +03:00
|
|
|
} else {
|
|
|
|
PR_ASSERT(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
radix = 16;
|
|
|
|
goto fetch_and_convert;
|
|
|
|
|
|
|
|
#if 0
|
2001-12-07 04:50:31 +03:00
|
|
|
case 'C':
|
2000-01-17 09:33:59 +03:00
|
|
|
/* XXX not supported I suppose */
|
|
|
|
PR_ASSERT(0);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
case 'S':
|
2014-01-04 19:02:17 +04:00
|
|
|
u.S = va_arg(ap, const char16_t*);
|
2000-01-17 09:33:59 +03:00
|
|
|
rv = cvt_S(ss, u.S, width, prec, flags);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
case 's':
|
2000-01-17 09:33:59 +03:00
|
|
|
u.s = va_arg(ap, const char*);
|
|
|
|
rv = cvt_s(ss, u.s, width, prec, flags);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
case 'n':
|
2000-01-17 09:33:59 +03:00
|
|
|
u.ip = va_arg(ap, int*);
|
|
|
|
if (u.ip) {
|
|
|
|
*u.ip = ss->cur - ss->base;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-12-07 04:50:31 +03:00
|
|
|
default:
|
2000-01-17 09:33:59 +03:00
|
|
|
/* Not a % token after all... skip it */
|
|
|
|
#if 0
|
|
|
|
PR_ASSERT(0);
|
|
|
|
#endif
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t perct = '%';
|
2000-01-17 09:33:59 +03:00
|
|
|
rv = (*ss->stuff)(ss, &perct, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = (*ss->stuff)(ss, fmt - 1, 1);
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stuff trailing NUL */
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t null = '\0';
|
2000-01-17 09:33:59 +03:00
|
|
|
|
|
|
|
rv = (*ss->stuff)(ss, &null, 1);
|
|
|
|
|
|
|
|
if( nas && ( nas != nasArray ) ){
|
|
|
|
PR_DELETE( nas );
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
|
2002-08-30 19:44:35 +04:00
|
|
|
static int
|
2014-01-04 19:02:17 +04:00
|
|
|
StringStuff(SprintfState* ss, const char16_t* sp, uint32_t len)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
2010-10-21 14:56:33 +04:00
|
|
|
if (*sp == '\0')
|
|
|
|
return 0;
|
|
|
|
|
2002-08-30 19:44:35 +04:00
|
|
|
ptrdiff_t off = ss->cur - ss->base;
|
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
nsAString* str = static_cast<nsAString*>(ss->stuffclosure);
|
2002-08-30 19:44:35 +04:00
|
|
|
str->Append(sp, len);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2007-09-21 03:14:25 +04:00
|
|
|
ss->base = str->BeginWriting();
|
2002-08-30 19:44:35 +04:00
|
|
|
ss->cur = ss->base + off;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Stuff routine that automatically grows the malloc'd output buffer
|
|
|
|
** before it overflows.
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static int GrowStuff(SprintfState *ss, const char16_t *sp, uint32_t len)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
|
|
|
ptrdiff_t off;
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t *newbase;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t newlen;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
|
|
|
off = ss->cur - ss->base;
|
|
|
|
if (off + len >= ss->maxlen) {
|
|
|
|
/* Grow the buffer */
|
|
|
|
newlen = ss->maxlen + ((len > 32) ? len : 32);
|
|
|
|
if (ss->base) {
|
2014-01-04 19:02:17 +04:00
|
|
|
newbase = (char16_t*) nsMemory::Realloc(ss->base, newlen*sizeof(char16_t));
|
2000-01-17 09:33:59 +03:00
|
|
|
} else {
|
2014-01-04 19:02:17 +04:00
|
|
|
newbase = (char16_t*) nsMemory::Alloc(newlen*sizeof(char16_t));
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
if (!newbase) {
|
|
|
|
/* Ran out of memory */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ss->base = newbase;
|
|
|
|
ss->maxlen = newlen;
|
|
|
|
ss->cur = ss->base + off;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy data */
|
|
|
|
while (len) {
|
|
|
|
--len;
|
|
|
|
*ss->cur++ = *sp++;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
PR_ASSERT((uint32_t)(ss->cur - ss->base) <= ss->maxlen);
|
2000-01-17 09:33:59 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** sprintf into a malloc'd buffer
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t * nsTextFormatter::smprintf(const char16_t *fmt, ...)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
|
|
|
va_list ap;
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t *rv;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
rv = nsTextFormatter::vsmprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
uint32_t nsTextFormatter::ssprintf(nsAString& out, const char16_t* fmt, ...)
|
2002-08-30 19:44:35 +04:00
|
|
|
{
|
|
|
|
va_list ap;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t rv;
|
2002-08-30 19:44:35 +04:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
rv = nsTextFormatter::vssprintf(out, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
uint32_t nsTextFormatter::vssprintf(nsAString& out, const char16_t* fmt, va_list ap)
|
2002-08-30 19:44:35 +04:00
|
|
|
{
|
|
|
|
SprintfState ss;
|
|
|
|
ss.stuff = StringStuff;
|
|
|
|
ss.base = 0;
|
|
|
|
ss.cur = 0;
|
|
|
|
ss.maxlen = 0;
|
|
|
|
ss.stuffclosure = &out;
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
out.Truncate();
|
|
|
|
int n = dosprintf(&ss, fmt, ap);
|
2002-08-30 19:44:35 +04:00
|
|
|
return n ? n - 1 : n;
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t * nsTextFormatter::vsmprintf(const char16_t *fmt, va_list ap)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
|
|
|
SprintfState ss;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
ss.stuff = GrowStuff;
|
|
|
|
ss.base = 0;
|
|
|
|
ss.cur = 0;
|
|
|
|
ss.maxlen = 0;
|
|
|
|
rv = dosprintf(&ss, fmt, ap);
|
|
|
|
if (rv < 0) {
|
|
|
|
if (ss.base) {
|
|
|
|
PR_DELETE(ss.base);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ss.base;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Stuff routine that discards overflow data
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static int LimitStuff(SprintfState *ss, const char16_t *sp, uint32_t len)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t limit = ss->maxlen - (ss->cur - ss->base);
|
2000-01-17 09:33:59 +03:00
|
|
|
|
|
|
|
if (len > limit) {
|
|
|
|
len = limit;
|
|
|
|
}
|
|
|
|
while (len) {
|
|
|
|
--len;
|
|
|
|
*ss->cur++ = *sp++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** sprintf into a fixed size buffer. Make sure there is a NUL at the end
|
|
|
|
** when finished.
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
uint32_t nsTextFormatter::snprintf(char16_t *out, uint32_t outlen, const char16_t *fmt, ...)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
|
|
|
va_list ap;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t rv;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
PR_ASSERT((int32_t)outlen > 0);
|
|
|
|
if ((int32_t)outlen <= 0) {
|
2000-01-17 09:33:59 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
rv = nsTextFormatter::vsnprintf(out, outlen, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
uint32_t nsTextFormatter::vsnprintf(char16_t *out, uint32_t outlen,const char16_t *fmt,
|
2001-12-07 04:50:31 +03:00
|
|
|
va_list ap)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
|
|
|
SprintfState ss;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t n;
|
2000-01-17 09:33:59 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
PR_ASSERT((int32_t)outlen > 0);
|
|
|
|
if ((int32_t)outlen <= 0) {
|
2000-01-17 09:33:59 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ss.stuff = LimitStuff;
|
|
|
|
ss.base = out;
|
|
|
|
ss.cur = out;
|
|
|
|
ss.maxlen = outlen;
|
|
|
|
(void) dosprintf(&ss, fmt, ap);
|
|
|
|
|
|
|
|
/* If we added chars, and we didn't append a null, do it now. */
|
|
|
|
if( (ss.cur != ss.base) && (*(ss.cur - 1) != '\0') )
|
|
|
|
*(--ss.cur) = '\0';
|
|
|
|
|
|
|
|
n = ss.cur - ss.base;
|
|
|
|
return n ? n - 1 : n;
|
|
|
|
}
|
|
|
|
|
2007-09-21 03:14:25 +04:00
|
|
|
/*
|
|
|
|
* Free memory allocated, for the caller, by smprintf
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
void nsTextFormatter::smprintf_free(char16_t *mem)
|
2000-01-17 09:33:59 +03:00
|
|
|
{
|
2007-09-21 03:14:25 +04:00
|
|
|
nsMemory::Free(mem);
|
2000-01-17 09:33:59 +03:00
|
|
|
}
|
|
|
|
|