1########################################################################
2##
3## Copyright (C) 1996-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn {} {} strtrim (@var{s})
28## Remove leading and trailing whitespace from @var{s}.
29##
30## If @var{s} is a matrix, @var{strtrim} trims each row to the length of
31## longest string.  If @var{s} is a cell array of strings, operate recursively
32## on each string element.
33##
34## For example:
35##
36## @example
37## @group
38## strtrim ("    abc  ")
39##      @result{}  "abc"
40##
41## strtrim ([" abc   "; "   def   "])
42##      @result{}  ["abc  "  ; "  def"]
43## @end group
44## @end example
45## @seealso{deblank}
46## @end deftypefn
47
48## This function was derived from deblank.
49
50function s = strtrim (s)
51
52  if (nargin != 1)
53    print_usage ();
54  endif
55
56  if (ischar (s))
57
58    k = find (! isspace (s));
59    if (isempty (s) || isempty (k))
60      s = "";
61    else
62      s = s(:, ceil (min (k) / rows (s)):ceil (max (k) / rows (s)));
63    endif
64
65  elseif (iscell (s))
66
67    char_idx = cellfun ("isclass", s, "char");
68    cell_idx = cellfun ("isclass", s, "cell");
69    if (! all (char_idx | cell_idx))
70      error ("strtrim: S argument must be a string or cellstring");
71    endif
72
73    ## Divide work load.  Recursive cellfun strtrim call is slow
74    ## and avoided where possible.
75    s(char_idx) = regexprep (s(char_idx), "^[\\s\v]+|[\\s\v]+$", '');
76    s(cell_idx) = cellfun ("strtrim", s(cell_idx), "UniformOutput", false);
77
78  else
79    error ("strtrim: S argument must be a string or cellstring");
80  endif
81
82endfunction
83
84
85%!assert (strtrim ("    abc  "), "abc")
86%!assert (strtrim ("  "), "")
87%!assert (strtrim ("abc"), "abc")
88%!assert (strtrim (char (" abc   ", "   def   ")), ["abc  "; "  def"])
89%!assert (strtrim ({" abc   "; "   def   "}), {"abc"; "def"})
90%!assert (strtrim ({" abc   ", {"   def   "}}), {"abc", {"def"}})
91
92%!error <Invalid call to strtrim> strtrim ()
93%!error <Invalid call to strtrim> strtrim ("abc", "def")
94%!error <argument must be a string> strtrim (1)
95%!error <argument must be a string> strtrim ({[]})
96