changeset 24338:1212ffc13d24 stable

Fix concatenation of empty char matrices with other strings (bug #52542). * pt-mat.cc (tm_const::init): When executing special case of appending a 2-D char matrix, verify that the existing element is not empty before appending. If it is empty, just replace the existing element's dimension vector with the to-be-appended element's dimension vector. * parser.tst: Add concatenation tests for bug #52542 to other parser tests.
author Rik <rik@octave.org>
date Thu, 30 Nov 2017 09:13:07 -0800
parents 0771ce2d30ef
children 648616b26cc6 49fc0178028c
files libinterp/parse-tree/pt-mat.cc test/parser.tst
diffstat 2 files changed, 21 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/pt-mat.cc	Wed Nov 29 13:43:58 2017 -0800
+++ b/libinterp/parse-tree/pt-mat.cc	Thu Nov 30 09:13:07 2017 -0800
@@ -638,11 +638,20 @@
       else if (all_str && dv.ndims () == 2
                && this_elt_dv.ndims () == 2)
         {
-          // FIXME: this is Octave's specialty.
-          // Character matrices allow rows of unequal length.
-          if (this_elt_nc > cols ())
-            dv(1) = this_elt_nc;
-          dv(0) += this_elt_nr;
+          // This is Octave's specialty.
+          // Character matrices support rows of unequal length.
+          if (dv.any_zero ())
+            {
+              // Empty existing element (bug #52542).
+              // Replace empty element with non-empty one.
+              dv = this_elt_dv;
+            }
+          else
+            {
+              if (this_elt_nc > cols ())
+                dv(1) = this_elt_nc;
+              dv(0) += this_elt_nr;
+            }
         }
       else if ((! any_class) && (! dv.hvcat (this_elt_dv, 0)))
         eval_error ("vertical dimensions mismatch", dv, this_elt_dv);
--- a/test/parser.tst	Wed Nov 29 13:43:58 2017 -0800
+++ b/test/parser.tst	Thu Nov 30 09:13:07 2017 -0800
@@ -326,3 +326,10 @@
 ## Maybe unnecessary, but check that further changes to parser don't
 ## invalidate error handling (bug #46534).
 #!error <vertical dimensions mismatch \(1x2 vs 1x1\)> z = [1, 2; 3]
+
+## FIXME: We need a sequence of concatenation tests since this seems
+##        to be a frequently reported source of incompatibilities w/Matlab
+## Check concatenation of empty char matrices (bug #52542)
+%!assert (double ([char(ones(0,3)); 'A']), 65)
+%!assert (double ([char(ones(0,3)); char(ones(2,0)); 'A']), 65)
+