Skip to content

Commit 172b5ad

Browse files
author
Fernando Alvarruiz
committed
Allow empty field names in struct and cell2struct functions (bug #67255).
* ov-struct.cc (Fstruct): If first argument is an empty char array (''), consider it a field name. Add test. * ov-struct.cc (get_cell2struct_fields): Allow char arrays with zero rows. * ov-struct.cc (Fcell2struct): Add test with empty field name.
1 parent d5855d7 commit 172b5ad

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

libinterp/octave-value/ov-struct.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,8 +1817,8 @@ orderfields, isstruct, structfun}
18171817

18181818
// struct ([]) returns an empty struct.
18191819

1820-
// struct (empty_matrix) returns an empty struct with the same
1821-
// dimensions as the empty matrix.
1820+
// struct (empty_matrix) or struct (empty_matrix, cell_array_of field_names)
1821+
// return an empty struct with the same dimensions as the empty matrix.
18221822

18231823
// Note that struct () creates a 1x1 struct with no fields for
18241824
// compatibility with Matlab.
@@ -1830,7 +1830,8 @@ orderfields, isstruct, structfun}
18301830
return ovl (args(0).map_value ());
18311831

18321832
if ((nargin == 1 || nargin == 2)
1833-
&& args(0).isempty () && args(0).is_real_matrix ())
1833+
&& args(0).isempty () && args(0).is_real_matrix ()
1834+
&& ! args(0).is_char_matrix ())
18341835
{
18351836
if (nargin == 2)
18361837
{
@@ -1933,6 +1934,10 @@ orderfields, isstruct, structfun}
19331934
%!error <arguments must occur as "field", VALUE pairs> struct (1,2,3,4)
19341935
%!fail ('struct ("1",2,"3")',
19351936
%! 'struct: additional arguments must occur as "field", VALUE pairs')
1937+
1938+
%!test <*67255> # Test empty string as field name
1939+
% s.('') = 3;
1940+
% assert (struct ('', 3), s)
19361941
*/
19371942

19381943
DEFUN (isstruct, args, ,
@@ -2069,7 +2074,7 @@ get_cell2struct_fields (const octave_value& arg)
20692074
{
20702075
if (arg.is_string ())
20712076
{
2072-
if (arg.rows () != 1)
2077+
if (arg.rows () > 1)
20732078
invalid_cell2struct_fields_error ();
20742079

20752080
return Array<std::string> (dim_vector (1, 1), arg.string_value ());
@@ -2085,7 +2090,7 @@ get_cell2struct_fields (const octave_value& arg)
20852090
{
20862091
const octave_value val = c(i);
20872092

2088-
if (! val.is_string () || val.rows () != 1)
2093+
if (! val.is_string () || val.rows () > 1)
20892094
invalid_cell2struct_fields_error ();
20902095

20912096
retval(i) = c(i).string_value ();
@@ -2202,6 +2207,9 @@ S(1)
22022207
%!assert (cell2struct ({1, 2, 3, 4}, {'a', 'b'; 'c', 'd'}, 2),
22032208
%! struct ('a', 1, 'c', 2, 'b', 3, 'd', 4));
22042209
%!error cell2struct ({1, 2, 3, 4}, {'a', 'b'; 'c', 'd'})
2210+
2211+
%!test <*67255> # Test empty string as field name
2212+
% assert (cell2struct ({3}, {''}), struct ('', {3}))
22052213
*/
22062214

22072215
DEFUN (rmfield, args, ,

0 commit comments

Comments
 (0)