######################################################################## ## ## 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} =} convertCharsToStrings (@var{a}) ## @deftypefnx {} {[@var{r1}, @var{r2}, @dots{}, @var{rn}] =} convertCharsToStrings (@var{a1}, @var{a2}, @dots{}, @var{an}) ## Convert character arrays into string arrays. ## ## If @var{a} is a character row vector, @var{r} is a string scalar. If @var{a} ## is a cell array of character row vectors, @var{r} is a string array. ## If @var{a} is a character array, @var{r} is a string scalar where the ## characters have been concatenated column-wise. ## Other variables types are left unchanged. ## ## Programming Note: Octave does not yet implement string arrays so this ## function will return its input arguments unmodified. ## @seealso{convertStringsToChars, convertContainedStringsToChars} ## @end deftypefn function varargout = convertCharsToStrings (varargin) if (! (nargin < 2 && ! nargout) && (nargin != nargout)) error ("Mismatch between number of input and output arguments."); endif varargout = varargin; ## FIXME: when string arrays are implemented, this should be removed, ## and tests should be uncommented. return; for i = 1:numel(varargin) if (ischar (varargin{i})) varargout{i} = string (varargin{i}(:)'); elseif (iscellstr (varargin{i})) varargout{i} = string (varargin{i}); endif endfor endfunction %!test %! convertCharsToStrings; %! convertCharsToStrings ("Octave"); %!test %! r = convertCharsToStrings ('Octave'); #%! assert (isstring (r)); %! r = convertCharsToStrings ("Octave"); #%! assert (isstring (r)); %! [r1, r2] = convertCharsToStrings ("GNU", 'Octave'); #%! assert (isstring (r1)); #%! assert (isstring (r2)); %!test %! r = convertCharsToStrings (""); #%! assert (isstring (r)); %! assert (size (r), [0, 0]); %!test %! a = ["GNU", "Octave"]; %! r = convertCharsToStrings (a); #%! assert (isstring (r)); %!test %! r = convertCharsToStrings ({'A', 'B'}); #%! assert (isstring (r)); #%! assert (size (r), [1, 2]); %! r = convertCharsToStrings ({'A'; 'B'}); #%! assert (isstring (r)); #%! assert (size (r), [2, 1]); %! r = convertCharsToStrings ({"A", "B"}); %! assert (iscell (r)); %!test %! r = convertCharsToStrings (['a1';'b2']); #%! assert (isstring (r)); #%! assert (r, "ab12"); %!test %! [r1, r2, r3, r4] = convertCharsToStrings ({"GNU"}, "Octave", 'pi', pi); #%! assert ({r1, r2, r3, r4}, {{"GNU"}, "Octave", "pi", pi}); %!error r = convertCharsToStrings; %!error r = convertCharsToStrings ("GNU", "Octave"); %!error [r1, r2] = convertCharsToStrings ("Octave");