nsTextFormatter.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org Code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Portable safe sprintf code.
*
*
* Contributor(s):
* Kipp E.B. Hickman <kipp@netscape.com> (original author)
* Frank Yung-Fong Tang <ftang@netscape.com>
* Daniele Nicolodi <daniele@grinta.net>
*/
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "prdtoa.h"
#include "prlong.h"
#include "prlog.h"
#include "prmem.h"
#include "prprf.h"
#include "nsCRT.h"
#include "nsTextFormatter.h"
#include "nsString.h"
#include "nsReadableUtils.h"
/*
** Note: on some platforms va_list is defined as an array,
** and requires array notation.
*/
#ifdef HAVE_VA_COPY
#elif defined(HAVE_VA_LIST_AS_ARRAY)
#else
#endif
typedef struct SprintfStateStr SprintfState;
struct SprintfStateStr {
void *stuffclosure;
};
/*
** Numbered Arguement State
*/
struct NumArgState{
int type; /* type of the current ap */
};
#define TYPE_INT16 0
#define TYPE_UINT16 1
#define TYPE_INTN 2
#define TYPE_UINTN 3
#define TYPE_INT32 4
#define TYPE_UINT32 5
#define TYPE_INT64 6
#define TYPE_UINT64 7
#define TYPE_STRING 8
#define TYPE_DOUBLE 9
#define TYPE_INTSTR 10
#define TYPE_UNISTRING 11
#define TYPE_UNKNOWN 20
#define _LEFT 0x1
#define _SIGNED 0x2
#define _SPACED 0x4
#define _ZEROS 0x8
#define _NEG 0x10
// Warning: if aDest isn't big enough this function returns the converted
// string in allocated memory which must be freed using PR_FREE().
// May return nsnull if memory couldn't be allocated.
{
// decide the length of the UCS2 first.
if (0 == state) {
if (0 == (0x80 & (*in))) {
needLen++;
needLen++;
state = 1;
needLen++;
state = 2;
needLen+=2;
state = 3;
needLen+=2;
state = 4;
needLen+=2;
state = 5;
} else {
needLen++;
state = 0;
}
} else {
state--;
} else {
state = 0;
}
}
}
needLen++; // add null termination.
// allocates sufficient memory if aDest is not big enough.
}
return nsnull;
}
if (0 == state) {
if (0 == (0x80 & (*in))) {
// ASCII
// 2 bytes UTF8
state=1;
state=2;
state=3;
state=4;
state=5;
} else {
NS_ASSERTION(0, "The input string is not in utf8");
state=0;
ucs4=0;
}
} else {
if (0 == state) {
if (ucs4 >= 0x00010000) {
if (ucs4 >= 0x00110000) {
*out++ = 0xFFFD;
} else {
ucs4 -= 0x00010000;
}
} else {
}
ucs4 = 0;
}
} else {
state = 0;
ucs4 = 0;
}
}
}
*out = 0x0000;
return aDest;
}
/*
** Fill into the buffer using the data in src
*/
{
int rv;
/* Right adjusting */
space = '0';
}
while (--width >= 0) {
if (rv < 0) {
return rv;
}
}
}
/* Copy out the source data */
if (rv < 0) {
return rv;
}
/* Left adjusting */
while (--width >= 0) {
if (rv < 0) {
return rv;
}
}
}
return 0;
}
/*
** Fill a number. The order is: optional-sign zero-filling conversion-digits
*/
{
int zerowidth = 0;
int precwidth = 0;
int signwidth = 0;
int leftspaces = 0;
int rightspaces = 0;
int cvtwidth;
int rv;
if ((type & 1) == 0) {
sign = '-';
signwidth = 1;
sign = '+';
signwidth = 1;
sign = ' ';
signwidth = 1;
}
}
if (prec > 0) {
/* Need zero filling */
}
}
/* Zero filling */
}
}
/* Space filling on the right (i.e. left adjusting) */
}
} else {
/* Space filling on the left (i.e. right adjusting) */
}
}
while (--leftspaces >= 0) {
if (rv < 0) {
return rv;
}
}
if (signwidth) {
if (rv < 0) {
return rv;
}
}
while (--precwidth >= 0) {
if (rv < 0) {
return rv;
}
}
while (--zerowidth >= 0) {
if (rv < 0) {
return rv;
}
}
if (rv < 0) {
return rv;
}
while (--rightspaces >= 0) {
if (rv < 0) {
return rv;
}
}
return 0;
}
/*
** Convert a long into its printable form
*/
{
int digits;
/* according to the man page this needs to happen */
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.
*/
digits = 0;
while (num) {
digits++;
}
if (digits == 0) {
*--cvt = '0';
digits++;
}
/*
** Now that we have the number converted without its sign, deal with
** the sign and zero padding.
*/
}
/*
** Convert a 64-bit integer into its printable form
*/
{
int digits;
/* according to the man page this needs to happen */
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.
*/
digits = 0;
while (!LL_IS_ZERO(num)) {
digits++;
}
if (digits == 0) {
*--cvt = '0';
digits++;
}
/*
** Now that we have the number converted without its sign, deal with
** the sign and zero padding.
*/
}
/*
** Convert a double precision floating point number into its printable
** form.
*/
{
int mode = 2;
int decpt;
int sign;
char buf[256];
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;
}
switch (type) {
case 'f':
mode = 3;
break;
case 'E':
exp = 'E';
// no break
case 'e':
mode = 2;
break;
case 'G':
exp = 'E';
// no break
case 'g':
if (prec == 0) {
prec = 1;
}
mode = 2;
break;
default:
NS_ERROR("invalid type passed to cvt_f");
}
buf[0] = '\0';
return -1;
}
if (sign) {
*bufp++ = '-';
*bufp++ = '+';
}
if (decpt == 9999) {
} else {
switch (type) {
case 'E':
case 'e':
if (prec > 0) {
*bufp++ = '.';
while (*nump) {
prec--;
}
while (prec-- > 0) {
*bufp++ = '0';
}
}
break;
case 'f':
if (decpt < 1) {
*bufp++ = '0';
if (prec > 0) {
*bufp++ = '.';
*bufp++ = '0';
}
}
while (prec-- > 0) {
*bufp++ = '0';
}
}
} else {
}
while (decpt-- > 0) {
*bufp++ = '0';
}
if (prec > 0) {
*bufp++ = '.';
}
while (prec-- > 0) {
*bufp++ = '0';
}
}
}
*bufp = '\0';
break;
case 'G':
case 'g':
numdigits--;
if (numdigits > 0) {
*bufp++ = '.';
while (*nump) {
}
}
} else {
if (decpt < 1) {
*bufp++ = '0';
if (prec > 0) {
*bufp++ = '.';
while (decpt++) {
*bufp++ = '0';
}
while (*nump) {
}
}
} else {
numdigits--;
}
while (decpt-- > 0) {
*bufp++ = '0';
}
if (numdigits > 0) {
*bufp++ = '.';
while (*nump) {
}
}
}
*bufp = '\0';
}
}
}
// cast to PRUnichar
*rbufp = '\0';
}
/*
** 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.
*/
{
int slen;
if (prec == 0) {
return 0;
}
/* Limit string length by precision value */
if (prec > 0) {
}
}
/* and away we go */
}
/*
** 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.
*/
{
// convert s from UTF8 to PRUnichar*
// Fix me !!!
if (s) {
return -1;
}
}
}
return ret;
}
/*
** BiuldArgArray stands for Numbered Argument list Sprintf
** for example,
** fmp = "%4$i, %2$d, %3s, %1d";
** the number must start from 1, and no gap among them
*/
struct NumArgState * nasArray)
{
const PRUnichar* p;
PRUnichar c;
struct NumArgState* nas;
/*
** first pass:
** detemine how many legal % I have got, then allocate space
*/
p = fmt;
*rv = 0;
i = 0;
while ((c = *p++) != 0) {
if (c != '%') {
continue;
}
/* skip %% case */
if ((c = *p++) == '%') {
continue;
}
while( c != 0 ){
if (c > '9' || c < '0') {
/* numbered argument csae */
if (c == '$') {
if (i > 0) {
*rv = -1;
return NULL;
}
number++;
break;
} else {
/* non-numbered argument case */
if (number > 0) {
*rv = -1;
return NULL;
}
i = 1;
break;
}
}
c = *p++;
}
}
if (number == 0) {
return NULL;
}
if (number > NAS_DEFAULT_NUM) {
if (!nas) {
*rv = -1;
return NULL;
}
} else {
}
for (i = 0; i < number; i++) {
}
/*
** second pass:
** set nas[].type
*/
p = fmt;
while ((c = *p++) != 0) {
if (c != '%') {
continue;
}
c = *p++;
if (c == '%') {
continue;
}
cn = 0;
/* should imporve error check later */
while (c && c != '$') {
c = *p++;
}
*rv = -1;
break;
}
/* nas[cn] starts from 0, and make sure
nas[cn].type is not assigned */
cn--;
continue;
}
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 */
if (c == 'h') {
c = *p++;
} else if (c == 'L') {
/* XXX not quite sure here */
c = *p++;
} else if (c == 'l') {
c = *p++;
if (c == 'l') {
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':
break;
case 'p':
/* XXX should use cpp */
if (sizeof(void *) == sizeof(PRInt32)) {
} else if (sizeof(void *) == sizeof(PRInt64)) {
} else if (sizeof(void *) == sizeof(PRIntn)) {
} else {
}
break;
case 'C':
/* XXX not supported I suppose */
PR_ASSERT(0);
break;
case 'S':
break;
case 's':
break;
case 'n':
break;
default:
PR_ASSERT(0);
break;
}
/* get a legal para. */
*rv = -1;
break;
}
}
/*
** third pass
** fill the nas[cn].ap
*/
if (*rv < 0) {
}
return NULL;
}
cn = 0;
cn++;
continue;
}
case TYPE_INT16:
case TYPE_UINT16:
case TYPE_INTN:
default:
}
*rv = -1;
return NULL;
}
cn++;
}
return nas;
}
/*
** The workhorse sprintf code.
*/
{
PRUnichar c;
union {
int i;
long l;
double d;
const char *s;
const PRUnichar *S;
int *ip;
} u;
int rv, i;
/* in "%4$.2f" dolPt will point to . */
/*
** build an argument array, IF the fmt is numbered argument
** list style, to contain the Numbered Argument list pointers
*/
if (rv < 0) {
/* the fmt contains error Numbered Argument format, jliu@netscape.com */
PR_ASSERT(0);
return rv;
}
while ((c = *fmt++) != 0) {
if (c != '%') {
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 %% */
if (rv < 0) {
return rv;
}
continue;
}
/* the fmt contains the Numbered Arguments feature */
i = 0;
/* should imporve error check later */
while (c && c != '$') {
i = (i * 10) + (c - '0');
c = *fmt++;
}
}
return -1;
}
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')) {
c = *fmt++;
}
/* width */
if (c == '*') {
c = *fmt++;
} else {
width = 0;
while ((c >= '0') && (c <= '9')) {
c = *fmt++;
}
}
/* precision */
prec = -1;
if (c == '.') {
c = *fmt++;
if (c == '*') {
c = *fmt++;
} else {
prec = 0;
while ((c >= '0') && (c <= '9')) {
c = *fmt++;
}
}
}
/* size */
if (c == 'h') {
type = TYPE_INT16;
c = *fmt++;
} else if (c == 'L') {
/* XXX not quite sure here */
type = TYPE_INT64;
c = *fmt++;
} else if (c == 'l') {
type = TYPE_INT32;
c = *fmt++;
if (c == 'l') {
type = TYPE_INT64;
c = *fmt++;
}
}
/* format */
switch (c) {
case 'd':
radix = 10;
goto fetch_and_convert;
case 'o': /* octal */
radix = 8;
type |= 1;
goto fetch_and_convert;
case 'u': /* unsigned decimal */
radix = 10;
type |= 1;
goto fetch_and_convert;
case 'x': /* unsigned hex */
radix = 16;
type |= 1;
goto fetch_and_convert;
case 'X': /* unsigned HEX */
radix = 16;
type |= 1;
goto fetch_and_convert;
switch (type) {
case TYPE_INT16:
if (u.l < 0) {
u.l = -u.l;
}
goto do_long;
case TYPE_UINT16:
goto do_long;
case TYPE_INTN:
if (u.l < 0) {
u.l = -u.l;
}
goto do_long;
case TYPE_UINTN:
goto do_long;
case TYPE_INT32:
if (u.l < 0) {
u.l = -u.l;
}
goto do_long;
case TYPE_UINT32:
if (rv < 0) {
return rv;
}
break;
case TYPE_INT64:
if (!LL_GE_ZERO(u.ll)) {
}
goto do_longlong;
case TYPE_UINT64:
if (rv < 0) {
return rv;
}
break;
}
break;
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
if (rv < 0) {
return rv;
}
break;
case 'c':
while (width-- > 1) {
if (rv < 0) {
return rv;
}
}
}
if (rv < 0) {
return rv;
}
while (width-- > 1) {
if (rv < 0) {
return rv;
}
}
}
break;
case 'p':
if (sizeof(void *) == sizeof(PRInt32)) {
type = TYPE_UINT32;
} else if (sizeof(void *) == sizeof(PRInt64)) {
type = TYPE_UINT64;
} else if (sizeof(void *) == sizeof(int)) {
type = TYPE_UINTN;
} else {
PR_ASSERT(0);
break;
}
radix = 16;
goto fetch_and_convert;
#if 0
case 'C':
/* XXX not supported I suppose */
PR_ASSERT(0);
break;
#endif
case 'S':
if (rv < 0) {
return rv;
}
break;
case 's':
if (rv < 0) {
return rv;
}
break;
case 'n':
if (u.ip) {
}
break;
default:
/* Not a % token after all... skip it */
#if 0
PR_ASSERT(0);
#endif
if (rv < 0) {
return rv;
}
if (rv < 0) {
return rv;
}
}
}
/* Stuff trailing NUL */
}
return rv;
}
/************************************************************************/
static int
{
// we can assume contiguous storage
return 0;
}
/*
** Stuff routine that automatically grows the malloc'd output buffer
** before it overflows.
*/
{
/* Grow the buffer */
} else {
}
if (!newbase) {
/* Ran out of memory */
return -1;
}
}
/* Copy data */
while (len) {
--len;
}
return 0;
}
/*
** sprintf into a malloc'd buffer
*/
{
return rv;
}
{
return rv;
}
/*
** Free memory allocated, for the caller, by smprintf
*/
{
}
{
return n ? n - 1 : n;
}
{
int rv;
if (rv < 0) {
}
return 0;
}
}
/*
** Stuff routine that discards overflow data
*/
{
}
while (len) {
--len;
}
return 0;
}
/*
** sprintf into a fixed size buffer. Make sure there is a NUL at the end
** when finished.
*/
{
return 0;
}
return rv;
}
{
PRUint32 n;
return 0;
}
/* If we added chars, and we didn't append a null, do it now. */
return n ? n - 1 : n;
}
{
return rv;
}
{
int rv;
if (last) {
} else {
}
if (rv < 0) {
}
return 0;
}
}
#ifdef DEBUG
{
char utf8[] = "Hello";
int d=3;
int ret;
return passed;
}
#endif