octave-patch-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-patch-tracker] [patch #10299] extractBefore


From: Hans
Subject: [Octave-patch-tracker] [patch #10299] extractBefore
Date: Thu, 8 Dec 2022 08:47:10 -0500 (EST)

Follow-up Comment #1, patch #10299 (project octave):

= extractBefore.m =
== bug fix: numerical seperator fixed ==

function extractedText = extractBefore(text,seperator)
% extractBefore - cuts text from the beginning of a string up to a seperator
%    usage:
%       extractedText = extractBefore(text,seperator)
%    input:
%       text is a string, a character array, or
%          a cell array of strings/character arrays
%       seperator is a single character or a character array. But it can also
be
%          a number N. A cell array containing numbers or charaters is also
possible.
%    output:
%       extractedText is text(1:start_position_of_the_first_seperator-1) or
%          in case of numbers text is cut as text(1:N-1). If text and
seperator are
%          cell arrays they are processed pairwise.

## Copyright (C) 2022-2031 Andreas Stark <alzi123@hotmail.de>
##
## 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 <http://www.gnu.org/licenses/>.

   % test the number of input parameters. They have to be exactly 2.
   if (nargin~=2)
      error('extractBefore: function called with only one parameter. For
additional information see help extractBefore.');
   endif

   % free from cell if length is only 1
   if (iscell(text) && length(text)==1)
      text=text{1};
   endif
   if (iscell(seperator) && length(seperator)==1)
      seperator=seperator{1};
   endif

   % convert parameters
   if (isstring(text))
      text=char(text);  % for a octave string implementation
   endif

   % test first parameter
   if (iscell(text))
      valid=logical(sum(~cellfun('ischar',text))==0); % all cell entries have
to be char arrays
   else
      valid=ischar(text); % text has to be char
   endif
   if (~valid)
      error('extractBefore: first argument has to be char or a cell array of
chars. For additional information see help extractBefore.');
   endif

   % test second parameter
   % all cell entries have to be char arrays or numbers
   if (iscell(seperator))
      valid=(sum(~cellfun(@(x)
ischar(x)||isscalar(x),seperator,'UniformOutput',true))==0);
   else
      valid=ischar(seperator)||isscalar(seperator);
   endif
   if (~valid)
      error('extractBefore: second argument has to be cell, char or number.
For additional information see help extractBefore.');
   endif

   % mutual changes
   if (iscell(seperator) && ischar(text))
      text=repmat({text},size(seperator)); % multiply text if seperator is a
cell
   elseif (iscell(text) && (ischar(seperator)||isscalar(seperator)))
      seperator=repmat({seperator},size(text)); % multiply seperator if text
is a cell
   endif
   if (iscell(seperator) && iscell(text) && length(seperator)~=length(text))
      error('extractBefore: cell length of text and seperator has to match');
   endif
   % cellify to simplify programm flow
   %if ( ischar(seperator) || isscalar(seperator) ) && ischar(text),
text={text}; seperator={seperator}; end
   if (ischar(seperator)||isscalar(seperator))
      seperator={seperator};
   endif
   if (ischar(text))
      text={text};
   endif

   % do the cuts
   if (iscell(text) && iscell(seperator))
      n=length(text);
      for i=1:n
         if (ischar(seperator{i}))
            seperator(i)=min(strfind(text{i},seperator{i}));
         endif
         if (isempty(seperator(i)))
            seperator(i)=0;
         endif
         if (seperator{i}<=length(text{i}))
            text(i)=text{i}(1:seperator{i}-1);
         else
            error(sprintf('extractBefore: seperator exceeds the text length in
position %i.',i));
         endif
      endfor
   endif

   % free from cell if length is only 1
   if (iscell(text) && length(text)==1)
      text=text{1};
   endif

   % return values
   extractedText=text;

end



    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/patch/?10299>

_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]