[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 07:59:36 -0500 (EST) |
URL:
<https://savannah.gnu.org/patch/?10299>
Summary: extractBefore
Project: GNU Octave
Submitter: alzi123
Submitted: Thu 08 Dec 2022 12:59:34 PM UTC
Category: Core : new function
Priority: 5 - Normal
Status: None
Privacy: Public
Assigned to: None
Originator Email:
Open/Closed: Open
Discussion Lock: Any
_______________________________________________________
Follow-up Comments:
-------------------------------------------------------
Date: Thu 08 Dec 2022 12:59:34 PM UTC By: Hans <alzi123>
= extractBefore.m =
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 is 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('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('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))
seperator=repmat({seperator},size(text)); % multiply seperator if text
is a cell
endif
if (iscell(seperator) && iscell(text) && length(seperator)~=length(text))
error('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))
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
text(i)=text{i}(1:seperator{i}-1);
endfor
endif
% free from cell is length is only 1
if (iscell(text) && length(text)==1)
text=text{1};
endif
% return values
extractedText=text;
end
_______________________________________________________
File Attachments:
-------------------------------------------------------
Date: Thu 08 Dec 2022 12:59:34 PM UTC Name: extractBefore.m Size: 4KiB By:
alzi123
<http://savannah.gnu.org/patch/download.php?file_id=54071>
_______________________________________________________
Reply to this item at:
<https://savannah.gnu.org/patch/?10299>
_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/
- [Octave-patch-tracker] [patch #10299] extractBefore,
Hans <=