bugGNU Octave - Bugs: bug #49659, Non-empty ouput for non-matching...

 
 

bug #49659: Non-empty ouput for non-matching regexp with 'names' option

Submitter:  Guillaume <gyom>
Submitted:  Mon 21 Nov 2016 12:38:16 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Guillaume Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Mon 09 Jan 2017 05:06:17 PM UTC, comment #3: 

I checked in a fix here (http://hg.savannah.gnu.org/hgweb/octave/rev/cb09392b5b1c).  I also added an announcement to the NEWS file since some coders may be relying on the existing format.

Rik <rik5>
Group administrator
Mon 21 Nov 2016 05:50:34 PM UTC, comment #2: 

Interesting.  This feature was probably implemented without a lot of checking for Matlab compatibility.  It should be relatively easy to return a struct array, rather than a scalar structure.

The file to modify is regexp.cc in libinterp/corefcn.

There are two functions, octregexp and octcellregexp, which probably need modification.  The relevant code for octregexp is shown below.


  // Converted the linked list in the correct form for the return values

  octave_idx_type i = 0;
  octave_scalar_map nmap;

  retval.resize (7);

  if (sz == 1)
    {
      string_vector named_tokens = rx_lst.begin ()->named_tokens ();

      for (int j = 0; j < named_pats.numel (); j++)
        nmap.assign (named_pats(j), named_tokens(j));

      retval(5) = nmap;
    }
  else
    {
      for (int j = 0; j < named_pats.numel (); j++)
        {
          Cell tmp (dim_vector (1, sz));

          i = 0;
          for (octave::regexp::match_data::const_iterator p = rx_lst.begin ();
               p != rx_lst.end (); p++)
            {
              string_vector named_tokens = p->named_tokens ();

              tmp(i++) = named_tokens(j);
            }

          nmap.assign (named_pats(j), octave_value (tmp));
        }

      retval(5) = nmap;
    }


As you can see, in the case of multiple return arguments, it creates a temporary cell and stuffs the results into that cell array, and then assigns to the structure, under the ptn name, the temporary cell array.


Rik <rik5>
Group administrator
Mon 21 Nov 2016 12:53:19 PM UTC, comment #1: 

The output also differs  if there is more than one match as the output should be a structure array but Octave creates a structure with cell arrays in fields:


>> str='aaa 10  bbb 20';
>> regexp(str,'(?<file>\S+) (?<id>[0-9]+)','names')
ans =

  scalar structure containing the fields:

    file =
    {
      [1,1] = aaa
      [1,2] = bbb
    }
    id =
    {
      [1,1] = 10
      [1,2] = 20
    }


while Matlab does:


>> str='aaa 10  bbb 20';
>> regexp(str,'(?<file>\S+) (?<id>[0-9]+)','names')
ans =
  1×2 struct array with fields:
    file
    id


Guillaume <gyom>
Mon 21 Nov 2016 12:38:16 PM UTC, original submission:  

Octave's output differs from Matlab's when using the 'names' flag and nothing matches.

Octave:


>> r = regexp('nothing','(?<file>\S+) (?<id>[0-9]+)','names')
r =

  scalar structure containing the fields:

    file = {}(1x0)
    id = {}(1x0)

>> size(r)
ans =

   1   1


and Matlab:


>> r = regexp('nothing','(?<file>\S+) (?<id>[0-9]+)','names')
r =
  0×0 empty struct array with fields:
    file
    id
>> size(r)
ans =
     0     0


Guillaume <gyom>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by gyom (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2017-01-09 rik5 StatusNone Fixed
        Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.14-f13d.
    Corresponding source code