changeset 20195:ea5fdb2ae637

num2str.m: Correctly parse newlines in single-quoted format strings (bug #44684). * num2str.m: Use do_string_escapes on format specification before any further processing.
author Rik <rik@octave.org>
date Thu, 07 May 2015 22:04:10 +0200
parents 88efded31037
children 10414d7a25f8
files scripts/general/num2str.m
diffstat 1 files changed, 5 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/num2str.m	Thu May 14 14:52:09 2015 -0400
+++ b/scripts/general/num2str.m	Thu May 07 22:04:10 2015 +0200
@@ -115,7 +115,8 @@
         fmt = "%3d";
       endif
     endif
-    fmt = [deblank(repmat(fmt, 1, columns(x))), "\n"];
+    fmt = do_string_escapes (fmt);  # required now that '\n' is interpreted.
+    fmt = [deblank(repmat (fmt, 1, columns (x))), "\n"];
     nd = ndims (x);
     tmp = sprintf (fmt, permute (x, [2, 1, 3:nd]));
     retval = strtrim (char (ostrsplit (tmp(1:end-1), "\n")));
@@ -204,6 +205,9 @@
 %!xtest
 %! assert (num2str (1e23), "100000000000000000000000");
 
+## Test for bug #44864, extra rows generated from newlines in format
+%!assert (rows (num2str (magic (3), '%3d %3d %3d\n')), 3)
+
 %!error num2str ()
 %!error num2str (1, 2, 3)
 %!error <X must be a numeric> num2str ({1})