octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #51187] ISMEMBER fails if the string ends in a


From: Rik
Subject: [Octave-bug-tracker] [bug #51187] ISMEMBER fails if the string ends in a space:
Date: Fri, 9 Jun 2017 12:33:47 -0400 (EDT)
User-agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0

Follow-up Comment #10, bug #51187 (project octave):

It seems like the behavior is very complicated.  In the interest of minimizing
the coding burden I would not support the 'legacy' flag and only support the
most recent behavior, whatever it is.

As for cellstr and the trimming of spaces at the end, that is Matlab
compatible behavior which needs to be kept.  But notice that the C++ code for
cellstr uses an argument to the Cell constructor when it creates the Cell.


      string_vector s = args(0).xstring_vector_value ("cellstr: argument
STRING must be a 2-D character array");

      return ovl (s.is_empty () ? Cell (octave_value (""))
                                : Cell (s, true));


The constructor for Cell has several variations.  The one in question is 


  Cell (const string_vector& sv, bool trim = false);


One potential coding path would be to have a function, maybe one for internal
use with '__' prefix and suffix, which calls Cell (sv, false) so that spaces
are not trimmed.

Another option would be examine the code for lookup which is what ismember
uses internally.  The relevant code is


  else if (str_case)
    {
      Array<std::string> str_table = table.cellstr_value ();
      Array<std::string> str_y (dim_vector (1, 1));

      if (y.is_cellstr ())
        str_y = y.cellstr_value ();
      else
        str_y(0) = y.string_value ();

      Array<octave_idx_type> idx = str_table.lookup (str_y);


Maybe cell_value rather than cellstr_value would be more appropriate for the
initialization of str_table?

Or maybe the problem is with y.  In the m-file for either ismember or lookup
single row char arrays could be converted to cells.  This seems to work with
the examples given.


## I added { } around query to test single row char matrices being converted
ismember ({'b '}, {'a ', 'b '})
ans = 1
## multi-row char matrices are not converted, and give the same result as
Matlab
abc = ['a '; 'b '; 'c ']
abc =

a 
b 
c 

ismember (abc, {abc})
ans =

  0
  0
  0




A quick diff for that change is


diff -r 71bfd507663c scripts/set/ismember.m
--- a/scripts/set/ismember.m    Thu Jun 08 18:24:28 2017 -0700
+++ b/scripts/set/ismember.m    Fri Jun 09 09:31:32 2017 -0700
@@ -88,6 +88,10 @@ function [tf, s_idx] = ismember (a, s, v
     s = uint8 (s);
   endif
 
+  if (ischar (a) && rows (a) == 1 && iscell (s))
+    a = {a};
+  endif
+
   [a, s] = validsetargs ("ismember", a, s, varargin{:});
 
   by_rows = nargin == 3;


'test ismember' still passes which is a good thing, and the failing case also
passes.


    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?51187>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]