changeset 28732:f9201e7cbe00

Add warning when multi-row char matrix is converted to string (bug #49536) * ov-str-mat.cc (string_value): Check for rows > 1 and issue a warning if true. * ov-str-mat.cc (short_disp): Use row_as_string() rather than string_value() for conversion. * warning_ids.m: Document new warning ID "Octave:charmat-truncated".
author Rik <rik@octave.org>
date Sun, 13 Sep 2020 16:05:20 -0700
parents 4f1e8a79bd44
children 376ecc6794e4
files libinterp/octave-value/ov-str-mat.cc scripts/help/warning_ids.m
diffstat 2 files changed, 22 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-str-mat.cc	Sun Sep 13 11:28:42 2020 -0700
+++ b/libinterp/octave-value/ov-str-mat.cc	Sun Sep 13 16:05:20 2020 -0700
@@ -234,10 +234,23 @@
 
   charMatrix chm (matrix);
 
+  if (chm.rows () > 1)
+    warning_with_id ("Octave:charmat-truncated",
+                     "multi-row character matrix converted to a string, only the first row is used");
+
   // FIXME: Is this correct?
   return chm.row_as_string (0);
 }
 
+/*
+%!test <*49536>
+%! warning ("on", "Octave:charmat-truncated", "local");
+%! s = char ("this", "is", "a", "char", "matrix");
+%! fail ("sprintf (s)", ...
+%!       "warning",     ...
+%!       "multi-row character matrix converted to a string");
+*/
+
 Array<std::string>
 octave_char_matrix_str::cellstr_value (void) const
 {
@@ -268,7 +281,8 @@
 {
   if (matrix.ndims () == 2 && numel () > 0)
     {
-      std::string tmp = string_value ();
+      charMatrix chm (matrix);
+      std::string tmp = chm.row_as_string (0);
 
       // FIXME: should this be configurable?
       size_t max_len = 100;
--- a/scripts/help/warning_ids.m	Sun Sep 13 11:28:42 2020 -0700
+++ b/scripts/help/warning_ids.m	Sun Sep 13 16:05:20 2020 -0700
@@ -139,6 +139,13 @@
 ## By default, the @code{Octave:built-in-variable-assignment} warning is
 ## enabled.
 ##
+## @item Octave:charmat-truncated
+## If the @code{Octave:charmat-truncated} warning is enabled, a warning is
+## printed when a character matrix with multiple rows is converted to a string.
+## In this case, the Octave interpreter keeps only the first row and discards
+## the others.
+## By default, the @code{Octave:charmat-truncated} warning is enabled.
+##
 ## @item Octave:classdef-to-struct
 ## If the @code{Octave:classdef-to-struct} warning is enabled, a warning
 ## is issued when a classdef object is forcibly converted into a struct with