X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;ds=sidebyside;f=octave_packages%2Fm%2Fstrings%2Fstrtrunc.m;fp=octave_packages%2Fm%2Fstrings%2Fstrtrunc.m;h=4d3745de134a7515dca7775056e1dfb056d0f3a0;hb=1c0469ada9531828709108a4882a751d2816994a;hp=0000000000000000000000000000000000000000;hpb=63de9f36673d49121015e3695f2c336ea92bc278;p=CreaPhase.git diff --git a/octave_packages/m/strings/strtrunc.m b/octave_packages/m/strings/strtrunc.m new file mode 100644 index 0000000..4d3745d --- /dev/null +++ b/octave_packages/m/strings/strtrunc.m @@ -0,0 +1,78 @@ +## Copyright (C) 2006-2012 William Poetra Yoga Hadisoeseno +## +## 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 {Function File} {} strtrunc (@var{s}, @var{n}) +## Truncate the character string @var{s} to length @var{n}. If @var{s} +## is a character matrix, then the number of columns is adjusted. +## If @var{s} is a cell array of strings, then the operation is performed +## on each cell element and the new cell array is returned. +## @end deftypefn + +function s = strtrunc (s, n) + + if (nargin != 2) + print_usage (); + endif + + n = fix (n); + if (! isscalar (n) || n < 0) + error ("strtrunc: length N must be a positive integer (N >= 0)"); + endif + + if (ischar (s)) + if (n < columns (s)) + s = s(:, 1:n); + endif + elseif (iscellstr (s)) + ## Convoluted approach converts cellstr to char matrix, trims the character + ## matrix using indexing, and then converts back to cellstr with mat2cell. + ## This approach is 24X faster than using cellfun with call to strtrunc + idx = cellfun ("size", s, 2) > n; + rows = cellfun ("size", s(idx), 1); + if (! isempty (rows)) + s(idx) = mat2cell (char (s(idx))(:, 1:n), rows); + endif + else + error ("strtrunc: S must be a character string or a cell array of strings"); + endif + +endfunction + + +%!assert (strtrunc("abcdefg", 4), "abcd"); +%!assert (strtrunc("abcdefg", 10), "abcdefg"); +%!assert (strtrunc(char ("abcdef", "fedcba"), 3), ["abc"; "fed"]); +%!assert (strtrunc({"abcdef", "fedcba"}, 3), {"abc", "fed"}); +%!assert (strtrunc({"", "1", "21", "321"}, 1), {"", "1", "2", "3"}) +%!assert (strtrunc({"1", "", "2"}, 1), {"1", "", "2"}) +%!test +%! cstr = {"line1"; ["line2"; "line3"]; "line4"}; +%! y = strtrunc (cstr, 4); +%! assert (size (y), [3, 1]); +%! assert (size (y{2}), [2, 4]); +%! assert (y{2}, repmat ("line", 2, 1)); + +%% Test input validation +%!error strtrunc () +%!error strtrunc ("abcd") +%!error strtrunc ("abcd", 4, 5) +%!error strtrunc ("abcd", ones (2,2)) +%!error strtrunc ("abcd", -1) +%!error strtrunc (1, 1) +