summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Habicher <mhabicher@mozilla.com>2015-02-09 07:32:16 -0500
committerMike Habicher <mhabicher@mozilla.com>2015-02-09 07:32:16 -0500
commit086430a29b6dadab2f198106b772629a43477542 (patch)
treea8b2624ef57c93b602500f6e85c4823e7a3940d8
parentec6b694e9bdc3f5443a16dedb2e49e2f6c334937 (diff)
downloadnspr-hg-NSPR_4_10_9_BETA1.tar.gz
bug 1088790 - Add support for (s)size_t types (e.g. "%zu") to dosprintf(). r=tedNSPR_4_10_9_BETA1
-rw-r--r--pr/src/io/prprf.c26
-rw-r--r--pr/tests/Makefile.in1
-rw-r--r--pr/tests/README.TXT3
-rw-r--r--pr/tests/prfz.c76
-rwxr-xr-xpr/tests/runtests.pl1
-rwxr-xr-xpr/tests/runtests.sh1
6 files changed, 105 insertions, 3 deletions
diff --git a/pr/src/io/prprf.c b/pr/src/io/prprf.c
index ea6e4086..1a891415 100644
--- a/pr/src/io/prprf.c
+++ b/pr/src/io/prprf.c
@@ -66,7 +66,11 @@ struct NumArg {
#define NAS_DEFAULT_NUM 20 /* default number of NumberedArgument array */
-
+/*
+** For numeric types, the signed versions must have even values,
+** and their corresponding unsigned versions must have the subsequent
+** odd value.
+*/
#define TYPE_INT16 0
#define TYPE_UINT16 1
#define TYPE_INTN 2
@@ -376,8 +380,8 @@ static int cvt_s(SprintfState *ss, const char *str, int width, int prec,
/*
** BuildArgArray stands for Numbered Argument list Sprintf
-** for example,
-** fmp = "%4$i, %2$d, %3s, %1d";
+** for example,
+** fmt = "%4$i, %2$d, %3s, %1d";
** the number must start from 1, and no gap among them
*/
@@ -515,6 +519,15 @@ static struct NumArg* BuildArgArray( const char *fmt, va_list ap, int* rv, struc
nas[cn].type = TYPE_INT64;
c = *p++;
}
+ } else if (c == 'z') {
+ if (sizeof(size_t) == sizeof(PRInt32)) {
+ nas[ cn ].type = TYPE_INT32;
+ } else if (sizeof(size_t) == sizeof(PRInt64)) {
+ nas[ cn ].type = TYPE_INT64;
+ } else {
+ nas[ cn ].type = TYPE_UNKNOWN;
+ }
+ c = *p++;
}
/* format */
@@ -809,6 +822,13 @@ static int dosprintf(SprintfState *ss, const char *fmt, va_list ap)
type = TYPE_INT64;
c = *fmt++;
}
+ } else if (c == 'z') {
+ if (sizeof(size_t) == sizeof(PRInt32)) {
+ type = TYPE_INT32;
+ } else if (sizeof(size_t) == sizeof(PRInt64)) {
+ type = TYPE_INT64;
+ }
+ c = *fmt++;
}
/* format */
diff --git a/pr/tests/Makefile.in b/pr/tests/Makefile.in
index 9ebd9233..df1f1f2d 100644
--- a/pr/tests/Makefile.in
+++ b/pr/tests/Makefile.in
@@ -111,6 +111,7 @@ CSRCS = \
prftest.c \
prftest1.c \
prftest2.c \
+ prfz.c \
primblok.c \
priotest.c \
provider.c \
diff --git a/pr/tests/README.TXT b/pr/tests/README.TXT
index eaab44f2..94349fa2 100644
--- a/pr/tests/README.TXT
+++ b/pr/tests/README.TXT
@@ -227,6 +227,9 @@ prftest1.c
prftest2.c
Obsolete. Subsumed in prftest.c
+prfz.c
+ Tests printf handling of (s)size_t formats
+
priotest.c
Limited use. Tests NSPR thread dispatching priority.
diff --git a/pr/tests/prfz.c b/pr/tests/prfz.c
new file mode 100644
index 00000000..30a36222
--- /dev/null
+++ b/pr/tests/prfz.c
@@ -0,0 +1,76 @@
+/* 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/. */
+
+/*
+ * This is a simple test of the PR_fprintf() function for size_t formats.
+ */
+
+#include "prprf.h"
+#include <sys/types.h>
+#include <limits.h>
+#include <string.h>
+
+int
+main(int argc, char **argv)
+{
+ char buffer[128];
+
+ size_t unsigned_small = 266;
+ ssize_t signed_small_p = 943;
+ ssize_t signed_small_n = -1;
+ size_t unsigned_max = SIZE_MAX;
+ size_t unsigned_min = 0;
+ ssize_t signed_max = SSIZE_MAX;
+
+ printf("Test: unsigned small '%%zu' : ");
+ PR_snprintf(buffer, sizeof(buffer), "%zu", unsigned_small);
+ if (strncmp(buffer, "266", sizeof(buffer)) != 0) {
+ printf("Failed, got '%s'\n", buffer);
+ return -1;
+ }
+ printf("OK\n");
+
+ printf("Test: signed small positive '%%zd' : ");
+ PR_snprintf(buffer, sizeof(buffer), "%zd", signed_small_p);
+ if (strncmp(buffer, "943", sizeof(buffer)) != 0) {
+ printf("Failed, got '%s'\n", buffer);
+ return -1;
+ }
+ printf("OK\n");
+
+ printf("Test: signed small negative '%%zd' : ");
+ PR_snprintf(buffer, sizeof(buffer), "%zd", signed_small_n);
+ if (strncmp(buffer, "-1", sizeof(buffer)) != 0) {
+ printf("Failed, got '%s'\n", buffer);
+ return -1;
+ }
+ printf("OK\n");
+
+ printf("Test: 0 '%%zu' : ");
+ PR_snprintf(buffer, sizeof(buffer), "%zu", unsigned_min);
+ if (strncmp(buffer, "0", sizeof(buffer)) != 0) {
+ printf("Failed, got '%s'\n", buffer);
+ return -1;
+ }
+ printf("OK\n");
+
+ printf("Test: SIZE_MAX '%%zx' : ");
+ PR_snprintf(buffer, sizeof(buffer), "%zx", unsigned_max);
+ if (strspn(buffer, "f") != sizeof(size_t) * 2) {
+ printf("Failed, got '%s'\n", buffer);
+ return -1;
+ }
+ printf("OK\n");
+
+ printf("Test: SSIZE_MAX '%%zx' : ");
+ PR_snprintf(buffer, sizeof(buffer), "%zx", signed_max);
+ if (*buffer != '7' ||
+ strspn(buffer + 1, "f") != sizeof(ssize_t) * 2 - 1) {
+ printf("Failed, got '%s'\n", buffer);
+ return -1;
+ }
+ printf("OK\n");
+
+ return 0;
+}
diff --git a/pr/tests/runtests.pl b/pr/tests/runtests.pl
index 3a164b17..5dbc649b 100755
--- a/pr/tests/runtests.pl
+++ b/pr/tests/runtests.pl
@@ -310,6 +310,7 @@ $prog = shift; # Program to test
"poll_to",
"pollable",
"prftest",
+"prfz",
"primblok",
"provider",
"prpollml",
diff --git a/pr/tests/runtests.sh b/pr/tests/runtests.sh
index 535b93af..760f0329 100755
--- a/pr/tests/runtests.sh
+++ b/pr/tests/runtests.sh
@@ -140,6 +140,7 @@ poll_nm
poll_to
pollable
prftest
+prfz
primblok
provider
prpollml