########################################################################
##
## Copyright (C) 2021 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or .
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING. If not, see
## .
##
########################################################################
## -*- texinfo -*-
## @deftypefn {} {@var{r} =} convertStringsToChars (@var{a})
## @deftypefnx {} {[@var{r1}, @var{r2}, @dots{}, @var{rn}] =} convertStringsToChars (@var{a1}, @var{a2}, @dots{}, @var{an})
## Convert arrays of string objects into characters arrays.
##
## A string scalar @var{a} is converted into a character row vector @var{r}
## while a string array @var{a} is converted into a cell array of character row
## vectors. Other variables types are left unchanged.
##
## This function can be used for compatibility with functions that make use of
## characters arrays or cell arrays of strings as input arguments but are not
## compatible with string objects. For example:
##
## @example
## @group
## function example_function (a, b)
## [a, b] = convertStringsToChars (a, b);
## ...
## endfunction
## @end group
## @end example
## or
## @example
## @group
## function varargout = example_function (varargin)
## [vararging{1:nargin}] = convertStringsToChars (varargin{:});
## ...
## endfunction
## @end group
## @end example
##
## @seealso{convertContainedStringsToChars, convertCharsToStrings}
## @end deftypefn
function varargout = convertStringsToChars (varargin)
if (! (nargin < 2 && ! nargout) && (nargin != nargout))
error ("Mismatch between number of input and output arguments.");
endif
varargout = varargin;
for i = 1:numel(varargin)
if (isstring (varargin{i}))
if (isscalar (varargin{i}))
varargout{i} = char (varargin{i});
else
varargout{i} = cellstr (varargin{i});
endif
endif
endfor
endfunction
%!test
%! convertStringsToChars;
%! convertStringsToChars ("Octave");
%!test
%! r = convertStringsToChars ("Octave");
%! assert (ischar (r));
%! [r1, r2] = convertStringsToChars ("GNU", "Octave");
%! assert (ischar (r1));
%! assert (ischar (r2));
%!test
%! r = convertStringsToChars ("");
%! assert (ischar (r))
%! assert (size (r), [0, 0])
%!test
%! a = ["GNU", "Octave"];
%! r = convertStringsToChars (a);
## FIXME: when string arrays are implemented, this should be uncommented.
#%! assert (iscellstr (r))
%!test
%! r = convertStringsToChars ("Octave");
%! assert (ischar (r))
%! assert (r, 'Octave')
%!test
%! [r1, r2] = convertStringsToChars ("Octave", pi);
%! assert (ischar (r1))
%! assert (r1, 'Octave')
%! assert (r2, pi)
%!test
%! [r1, r2, r3, r4] = convertStringsToChars ({"GNU"}, "Octave", 'pi', pi);
%! assert ({r1, r2, r3, r4}, {{"GNU"}, 'Octave', 'pi', pi})
%!error r = convertStringsToChars;
%!error r = convertStringsToChars ("GNU", "Octave");
%!error [r1, r2] = convertStringsToChars ("Octave");