Bug 384979 --> move nsTextFormatter to the glue code so it can be used with frozen linkage. Patch by prasad <prasad@medhas.org> sr=bsmedberg a=bsmedberg

This commit is contained in:
scott@scott-macgregor.org 2007-09-20 16:14:25 -07:00
Родитель d1f70a3e69
Коммит fc5215cfe6
8 изменённых файлов: 202 добавлений и 195 удалений

Просмотреть файл

@ -74,7 +74,6 @@ CPPSRCS = \
nsSupportsPrimitives.cpp \
nsUnicharBuffer.cpp \
nsVariant.cpp \
nsTextFormatter.cpp \
nsTimelineService.cpp \
nsValueArray.cpp \
nsArray.cpp \
@ -104,7 +103,6 @@ EXPORTS = \
nsTime.h \
nsUnitConversion.h \
nsVariant.h \
nsTextFormatter.h \
nsValueArray.h \
nsStringEnumerator.h \
nsHashPropertyBag.h \

Просмотреть файл

@ -1,115 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** 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
* http://www.mozilla.org/MPL/
*
* 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 ***** */
#ifndef nsTextFormatter_h___
#define nsTextFormatter_h___
/*
** API for PR printf like routines. Supports the following formats
** %d - decimal
** %u - unsigned decimal
** %x - unsigned hex
** %X - unsigned uppercase hex
** %o - unsigned octal
** %hd, %hu, %hx, %hX, %ho - 16-bit versions of above
** %ld, %lu, %lx, %lX, %lo - 32-bit versions of above
** %lld, %llu, %llx, %llX, %llo - 64 bit versions of above
** %s - utf8 string
** %S - PRUnichar string
** %c - character
** %p - pointer (deals with machine dependent pointer size)
** %f - float
** %g - float
*/
#include "prtypes.h"
#include "prio.h"
#include <stdio.h>
#include <stdarg.h>
#include "nscore.h"
#include "nsAString.h"
class NS_COM nsTextFormatter {
public:
/*
** sprintf into a fixed size buffer. Guarantees that a NUL is at the end
** of the buffer. Returns the length of the written output, NOT including
** the NUL, or (PRUint32)-1 if an error occurs.
*/
static PRUint32 snprintf(PRUnichar *out, PRUint32 outlen, const PRUnichar *fmt, ...);
/*
** sprintf into a PR_MALLOC'd buffer. Return a pointer to the malloc'd
** buffer on success, NULL on failure. Call "smprintf_free" to release
** the memory returned.
*/
static PRUnichar* smprintf(const PRUnichar *fmt, ...);
static PRUint32 ssprintf(nsAString& out, const PRUnichar* fmt, ...);
/*
** Free the memory allocated, for the caller, by smprintf
*/
static void smprintf_free(PRUnichar *mem);
/*
** "append" sprintf into a PR_MALLOC'd buffer. "last" is the last value of
** the PR_MALLOC'd buffer. sprintf will append data to the end of last,
** growing it as necessary using realloc. If last is NULL, PR_sprintf_append
** will allocate the initial string. The return value is the new value of
** last for subsequent calls, or NULL if there is a malloc failure.
*/
static PRUnichar* sprintf_append(PRUnichar *last, const PRUnichar *fmt, ...);
/*
** va_list forms of the above.
*/
static PRUint32 vsnprintf(PRUnichar *out, PRUint32 outlen, const PRUnichar *fmt, va_list ap);
static PRUnichar* vsmprintf(const PRUnichar *fmt, va_list ap);
static PRUint32 vssprintf(nsAString& out, const PRUnichar *fmt, va_list ap);
static PRUnichar* vsprintf_append(PRUnichar *last, const PRUnichar *fmt, va_list ap);
#ifdef DEBUG
static PRBool SelfTest();
#endif
};
#endif /* nsTextFormatter_h___ */

Просмотреть файл

@ -88,6 +88,7 @@ SDK_HEADERS = \
nsStringGlue.h \
nsRefPtrHashtable.h \
nsStringAPI.h \
nsTextFormatter.h \
nsTHashtable.h \
nsVoidArray.h \
nsTArray.h \

Просмотреть файл

@ -20,6 +20,7 @@
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Prasad <prasad@medhas.org>
*
* 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"),
@ -46,6 +47,12 @@
* Daniele Nicolodi <daniele@grinta.net>
*/
/*
* Copied from xpcom/ds/nsTextFormatter.cpp r1.22
* Changed to use nsMemory and Frozen linkage
* -- Prasad <prasad@medhas.org>
*/
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
@ -53,12 +60,11 @@
#include "prdtoa.h"
#include "prlong.h"
#include "prlog.h"
#include "prmem.h"
#include "prprf.h"
#include "nsCRT.h"
#include "prmem.h"
#include "nsCRTGlue.h"
#include "nsTextFormatter.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsMemory.h"
/*
** Note: on some platforms va_list is defined as an array,
@ -517,7 +523,7 @@ static int cvt_f(SprintfState *ss, double d, int width, int prec,
while ((*rbufp++ = *bufp++)) { }
*rbufp = '\0';
return fill2(ss, rbuf, nsCRT::strlen(rbuf), width, flags);
return fill2(ss, rbuf, NS_strlen(rbuf), width, flags);
}
/*
@ -535,7 +541,7 @@ static int cvt_S(SprintfState *ss, const PRUnichar *s, int width,
}
/* Limit string length by precision value */
slen = s ? nsCRT::strlen(s) : 6;
slen = s ? NS_strlen(s) : 6;
if (prec > 0) {
if (prec < slen) {
slen = prec;
@ -622,7 +628,7 @@ static struct NumArgState* BuildArgArray(const PRUnichar *fmt,
}
if (number > NAS_DEFAULT_NUM) {
nas = (struct NumArgState*)PR_MALLOC(number * sizeof(struct NumArgState));
nas = (struct NumArgState*)nsMemory::Alloc(number * sizeof(struct NumArgState));
if (!nas) {
*rv = -1;
return NULL;
@ -1187,10 +1193,7 @@ StringStuff(SprintfState* ss, const PRUnichar* sp, PRUint32 len)
nsAString* str = static_cast<nsAString*>(ss->stuffclosure);
str->Append(sp, len);
// we can assume contiguous storage
nsAString::iterator begin;
str->BeginWriting(begin);
ss->base = begin.get();
ss->base = str->BeginWriting();
ss->cur = ss->base + off;
return 0;
@ -1211,9 +1214,9 @@ static int GrowStuff(SprintfState *ss, const PRUnichar *sp, PRUint32 len)
/* Grow the buffer */
newlen = ss->maxlen + ((len > 32) ? len : 32);
if (ss->base) {
newbase = (PRUnichar*) PR_REALLOC(ss->base, newlen*sizeof(PRUnichar));
newbase = (PRUnichar*) nsMemory::Realloc(ss->base, newlen*sizeof(PRUnichar));
} else {
newbase = (PRUnichar*) PR_MALLOC(newlen*sizeof(PRUnichar));
newbase = (PRUnichar*) nsMemory::Alloc(newlen*sizeof(PRUnichar));
}
if (!newbase) {
/* Ran out of memory */
@ -1258,14 +1261,6 @@ PRUint32 nsTextFormatter::ssprintf(nsAString& out, const PRUnichar* fmt, ...)
return rv;
}
/*
** Free memory allocated, for the caller, by smprintf
*/
void nsTextFormatter::smprintf_free(PRUnichar *mem)
{
PR_DELETE(mem);
}
PRUint32 nsTextFormatter::vssprintf(nsAString& out, const PRUnichar* fmt, va_list ap)
{
SprintfState ss;
@ -1361,63 +1356,11 @@ PRUint32 nsTextFormatter::vsnprintf(PRUnichar *out, PRUint32 outlen,const PRUnic
return n ? n - 1 : n;
}
PRUnichar * nsTextFormatter::sprintf_append(PRUnichar *last, const PRUnichar *fmt, ...)
/*
* Free memory allocated, for the caller, by smprintf
*/
void nsTextFormatter::smprintf_free(PRUnichar *mem)
{
va_list ap;
PRUnichar *rv;
va_start(ap, fmt);
rv = nsTextFormatter::vsprintf_append(last, fmt, ap);
va_end(ap);
return rv;
nsMemory::Free(mem);
}
PRUnichar * nsTextFormatter::vsprintf_append(PRUnichar *last, const PRUnichar *fmt, va_list ap)
{
SprintfState ss;
int rv;
ss.stuff = GrowStuff;
if (last) {
int lastlen = nsCRT::strlen(last);
ss.base = last;
ss.cur = last + lastlen;
ss.maxlen = lastlen;
} else {
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;
}
#ifdef DEBUG
PRBool nsTextFormatter::SelfTest()
{
PRBool passed = PR_TRUE ;
nsAutoString fmt(NS_LITERAL_STRING("%3$s %4$S %1$d %2$d"));
char utf8[] = "Hello";
PRUnichar ucs2[]={'W', 'o', 'r', 'l', 'd', 0x4e00, 0xAc00, 0xFF45, 0x0103};
int d=3;
PRUnichar buf[256];
int ret;
ret = nsTextFormatter::snprintf(buf, 256, fmt.get(), d, 333, utf8, ucs2);
printf("ret = %d\n", ret);
nsAutoString out(buf);
printf("%s \n", NS_LossyConvertUTF16toASCII(out).get());
const PRUnichar *uout = out.get();
for(PRUint32 i=0;i<out.Length();i++)
printf("%2X ", uout[i]);
return passed;
}
#endif

Просмотреть файл

@ -0,0 +1,108 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** 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
* http://www.mozilla.org/MPL/
*
* 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):
* Prasad <prasad@medhas.org>
*
* 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 ***** */
/*
* This code was copied from xpcom/ds/nsTextFormatter r1.3
* Memory model and Frozen linkage changes only.
* -- Prasad <prasad@medhas.org>
*/
#ifndef nsTextFormatter_h___
#define nsTextFormatter_h___
/*
** API for PR printf like routines. Supports the following formats
** %d - decimal
** %u - unsigned decimal
** %x - unsigned hex
** %X - unsigned uppercase hex
** %o - unsigned octal
** %hd, %hu, %hx, %hX, %ho - 16-bit versions of above
** %ld, %lu, %lx, %lX, %lo - 32-bit versions of above
** %lld, %llu, %llx, %llX, %llo - 64 bit versions of above
** %s - utf8 string
** %S - PRUnichar string
** %c - character
** %p - pointer (deals with machine dependent pointer size)
** %f - float
** %g - float
*/
#include "prtypes.h"
#include "prio.h"
#include <stdio.h>
#include <stdarg.h>
#include "nscore.h"
#include "nsStringGlue.h"
class NS_COM_GLUE nsTextFormatter {
public:
/*
* sprintf into a fixed size buffer. Guarantees that a NULL is at the end
* of the buffer. Returns the length of the written output, NOT including
* the NUL, or (PRUint32)-1 if an error occurs.
*/
static PRUint32 snprintf(PRUnichar *out, PRUint32 outlen, const PRUnichar *fmt, ...);
/*
* sprintf into a nsMemory::Alloc'd buffer. Return a pointer to
* buffer on success, NULL on failure.
*/
static PRUnichar* smprintf(const PRUnichar *fmt, ...);
static PRUint32 ssprintf(nsAString& out, const PRUnichar* fmt, ...);
/*
* va_list forms of the above.
*/
static PRUint32 vsnprintf(PRUnichar *out, PRUint32 outlen, const PRUnichar *fmt, va_list ap);
static PRUnichar* vsmprintf(const PRUnichar *fmt, va_list ap);
static PRUint32 vssprintf(nsAString& out, const PRUnichar *fmt, va_list ap);
/*
* Free the memory allocated, for the caller, by smprintf.
* -- Deprecated --
* Callers can substitute calling smprintf_free with nsMemory::Free
*/
static void smprintf_free(PRUnichar *mem);
};
#endif /* nsTextFormatter_h___ */

Просмотреть файл

@ -65,6 +65,7 @@ XPCOM_GLUE_SRC_LCPPSRCS = \
nsTObserverArray.cpp \
nsCycleCollectionParticipant.cpp \
nsDeque.cpp \
nsTextFormatter.cpp \
$(NULL)
XPCOM_GLUE_SRC_CPPSRCS = $(addprefix $(topsrcdir)/xpcom/glue/, $(XPCOM_GLUE_SRC_LCPPSRCS))

Просмотреть файл

@ -87,6 +87,7 @@ CPPSRCS += \
TestXPIDLString.cpp \
TestDeque.cpp \
TestStrings.cpp \
TestTextFormatter.cpp \
TestStorageStream.cpp \
TestExpirationTracker.cpp \
$(NULL)
@ -138,3 +139,5 @@ install::
check::
@echo "Running TestVersionComparator tests"
@$(PERL) -w $(srcdir)/TestVersionComparatorRunner.pl "$(RUN_TEST_PROGRAM) $(FINAL_TARGET)/TestVersionComparator$(BIN_SUFFIX)"
@echo "Running nsTextFormater tests"
@$(RUN_TEST_PROGRAM) $(DIST)/bin/run-mozilla.sh $(FINAL_TARGET)/TestTextFormatter$(BIN_SUFFIX)

Просмотреть файл

@ -0,0 +1,68 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** 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
* http://www.mozilla.org/MPL/
*
* 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):
* Prasad <prasad@medhas.org>
*
* 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 ***** */
#include <nsTextFormatter.h>
#include <nsStringGlue.h>
#include <stdio.h>
int main()
{
int test_ok = true;
nsAutoString fmt(NS_LITERAL_STRING("%3$s %4$S %1$d %2$d"));
char utf8[] = "Hello";
PRUnichar ucs2[]={'W', 'o', 'r', 'l', 'd', 0x4e00, 0xAc00, 0xFF45, 0x0103, 0x00};
int d=3;
PRUnichar buf[256];
nsTextFormatter::snprintf(buf, 256, fmt.get(), d, 333, utf8, ucs2);
nsAutoString out(buf);
if(strcmp("Hello World", NS_LossyConvertUTF16toASCII(out).get()))
test_ok = false;
const PRUnichar *uout = out.get();
const PRUnichar expected[] = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20,
0x57, 0x6F, 0x72, 0x6C, 0x64, 0x4E00,
0xAC00, 0xFF45, 0x0103, 0x20, 0x33,
0x20, 0x33, 0x33, 0x33};
for(PRUint32 i=0;i<out.Length();i++)
if(uout[i] != expected[i])
test_ok = false;
printf(test_ok? "nsTextFormatter: OK\n": "nsTextFormatter: FAIL\n");
}