patchGNU Octave - Patches: patch #10299, new function: extractBefore

 
 

patch #10299: new function: extractBefore

Submitter:  Steve <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 Open/Closed:  Open
* Mandatory Fields

Add a New Comment Rich Markup
   

Wed 14 Dec 2022 10:39:47 PM UTC, comment #3: 

See Matlab2OctaveConverterScriptV0k3.m and extractBefore_matlab.m as an example to automatically apply style guidelines to matlab files.

(file #54096, file #54097)

Steve <alzi123>
Thu 08 Dec 2022 03:49:46 PM UTC, comment #2: 

thank you for this contribution. I haven't had time to look through  the code yet, but some thoughts if you're interested in helping get the code adopted into core octave:

1 - general practice here is to include m-files as attachments that can be downloaded, rather than needing to copy/paste code. 

2 - Also, please review the Octave contributor guides (https://wiki.octave.org/Contribute), in particular the m-file style guide: https://wiki.octave.org/Octave_style_guide, and help text style guide: https://wiki.octave.org/Help_text_style_guide

3 - feel free to look at any other octave functions as examples of how to structure the function definitions, help text, etc. ('edit integral' provides a decent example.) 

some immediate items of notice:
- Copyright block go first at the top , followed by texinfo formatted help, etc. 
- As was noted in the contributor agreement on the patch tracker submission page, octave core generally doesn't keep author in the copyright lines, but author attribution can be noted after the help text (see 'edit repelem' for an example).
- generally use # not % for comments, and ! not ~ for NOT.
- the nargin check can just use print_usage() instead of a custom error.
- whitespace - separate operators and functions by a single space except in the few cases that causes parsing problems (like within a [] array definition)
- no need to do a return value handoff at the end. can just return 'text' instead of creating an extractedText placeholder.

Nicholas Jankowski <nrjank>
Group Member
Thu 08 Dec 2022 01:47:10 PM UTC, comment #1: 

= 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

Steve <alzi123>
Thu 08 Dec 2022 12:59:34 PM UTC, original submission:  

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

Steve <alzi123>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #54096:  Matlab2OctaveConverterScriptV0k3.m added by alzi123 (13KiB - text/plain - might be wrong place, but very useful for others who want to contribute :))
file #54097:  extractBefore_matlab.m added by alzi123 (3KiB - text/plain - might be wrong place, but very useful for others who want to contribute :))
file #54095:  extractBefore.m added by alzi123 (5KiB - text/plain - file is now modified to style guidelines)
file #54071:  extractBefore.m added by alzi123 (4KiB - text/plain)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by nrjank (Posted a comment)
  • -email is unavailable- added by alzi123 (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2022-12-14 alzi123 Attached File- Added Matlab2OctaveConverterScriptV0k3.m, #54096
        Attached File- Added extractBefore_matlab.m, #54097
    2022-12-14 alzi123 Attached File- Added extractBefore.m, #54095
    2022-12-08 nrjank SummaryextractBefore new function: extractBefore
    2022-12-08 alzi123 Attached File- Added extractBefore.m, #54071

    Back to the top

    Powered by Savane 3.13-f810.
    Corresponding source code