changeset 22812:86b6f79d4de1

better compatibility for printf with empty args (bug #39375) * oct-stream.cc (printf_value_cache::get_next_value): If argument has no elements, return empty matrix. (octave_base_stream::do_printf): Handle empty matrix in numeric conversion case. * io.tst: New tests.
author John W. Eaton <jwe@octave.org>
date Wed, 23 Nov 2016 10:17:06 -0500
parents f55c208a31c1
children c49196c539fb
files libinterp/corefcn/oct-stream.cc test/io.tst
diffstat 2 files changed, 19 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/oct-stream.cc	Wed Nov 23 10:07:29 2016 -0500
+++ b/libinterp/corefcn/oct-stream.cc	Wed Nov 23 10:17:06 2016 -0500
@@ -5429,9 +5429,13 @@
 
           if (n_elts == 0)
             {
-              if (elt_idx == 0 && (type == 's' || type == 'c'))
+              if (elt_idx == 0)
                 {
-                  retval = "";
+                  if (type == 's' || type == 'c')
+                    retval = "";
+                  else
+                    retval = Matrix ();
+
                   break;
                 }
 
@@ -5800,8 +5804,11 @@
               octave_value val = val_cache.get_next_value ();
 
               if (val_cache)
-                retval += do_numeric_printf_conv (os, elt, nsa, sa_1,
-                                                  sa_2, val, who);
+                {
+                  if (! val.is_empty ())
+                    retval += do_numeric_printf_conv (os, elt, nsa, sa_1,
+                                                      sa_2, val, who);
+                }
               else
                 break;
             }
--- a/test/io.tst	Wed Nov 23 10:07:29 2016 -0500
+++ b/test/io.tst	Wed Nov 23 10:17:06 2016 -0500
@@ -727,3 +727,11 @@
 %!assert (sprintf ("%s", repmat ("blah", 2, 1)), "bbllaahh")
 %!assert (sprintf ("%c", repmat ("blah", 2, 1)), "bbllaahh")
 %!assert (sprintf ("%c %c %s", repmat ("blah", 2, 1)), "b b llaahh")
+
+## bug #39735
+%!assert (sprintf ("a %d b", []), "a  b")
+%!assert (sprintf ("a %d b", ''), "a  b")
+%!assert (sprintf ("a %d b", ' '), "a 32 b")
+%!assert (sprintf ("a %s b", []), "a  b")
+%!assert (sprintf ("a %s b", ''), "a  b")
+%!assert (sprintf ("a %s b", ' '), "a   b")