aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormax99x <max99x@gmail.com>2011-07-23 06:58:49 +0300
committermax99x <max99x@gmail.com>2011-07-24 05:25:45 +0300
commit5aea0ba8784bc385e52e769f8e90df5fcd833a7a (patch)
tree9f3c70018d11fbe67d5d800e89e35e4f8207f784
parenta48bfddf2b9f9d6f6b6ee5dec41004afed3a71ba (diff)
Added _formatString() special-case for null values (similar to glibc).
-rw-r--r--src/library.js20
-rw-r--r--tests/printf/output.txt2
-rw-r--r--tests/printf/test.c2
-rw-r--r--tests/runner.py2
4 files changed, 20 insertions, 6 deletions
diff --git a/src/library.js b/src/library.js
index 51fb6ce5..3e346280 100644
--- a/src/library.js
+++ b/src/library.js
@@ -2156,8 +2156,12 @@ LibraryManager.library = {
argText = argText.toUpperCase();
}
} else if (next == 'p'.charCodeAt(0)) {
- prefix = '0x';
- argText = currAbsArg.toString(16);
+ if (currAbsArg === 0) {
+ argText = '(nil)';
+ } else {
+ prefix = '0x';
+ argText = currAbsArg.toString(16);
+ }
}
if (precisionSet) {
while (argText.length < precision) {
@@ -2277,9 +2281,15 @@ LibraryManager.library = {
});
} else if (next == 's'.charCodeAt(0)) {
// String.
- var copiedString = String_copy(getNextArg());
- if (precisionSet && copiedString.length > precision) {
- copiedString = copiedString.slice(0, precision);
+ var arg = getNextArg();
+ var copiedString;
+ if (arg) {
+ copiedString = String_copy(arg);
+ if (precisionSet && copiedString.length > precision) {
+ copiedString = copiedString.slice(0, precision);
+ }
+ } else {
+ copiedString = intArrayFromString('(null)', true);
}
if (!flagLeftAlign) {
while (copiedString.length < width--) {
diff --git a/tests/printf/output.txt b/tests/printf/output.txt
index 412c6b0f..01822327 100644
--- a/tests/printf/output.txt
+++ b/tests/printf/output.txt
@@ -9,6 +9,8 @@ Some different radixes: 100 64 144 0x64 0144
floats: 3.14 +3e+00 3.141600E+00
Width trick: 10
A string %
+Null string: (null)
+Null pointer: (nil)
inf
INF
-inf
diff --git a/tests/printf/test.c b/tests/printf/test.c
index d12e0d2e..efd9d087 100644
--- a/tests/printf/test.c
+++ b/tests/printf/test.c
@@ -14,6 +14,8 @@ int main() {
printf("floats: %4.2f %+.0e %E\n", 3.1416, 3.1416, 3.1416);
printf("Width trick: %*d\n", 5, 10);
printf("%s %%\n", "A string");
+ printf("Null string: %7s\n", NULL);
+ printf("Null pointer: %p\n", NULL);
printf("%lf\n", INFINITY);
printf("%lF\n", INFINITY);
printf("%lf\n", -INFINITY);
diff --git a/tests/runner.py b/tests/runner.py
index d75d7e35..6edf9c20 100644
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -716,7 +716,7 @@ if 'benchmark' not in sys.argv:
return 0;
}
'''
- self.do_test(src, '*0x0*')
+ self.do_test(src, '*(nil)*')
def test_funcs(self):
src = '''