From afbac2e8eb0f85fa7cb23dda2cc1a2b7cb79bb68 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemian=28Charybdis=29?=
 <stephane.adjemian@univ-lemans.fr>
Date: Mon, 12 Mar 2018 21:24:38 +0100
Subject: [PATCH] Added strsplit routine in missing.

---
 matlab/dynare_config.m                      |  7 +-
 matlab/missing/strsplit/private/ischarint.m | 27 +++++++
 matlab/missing/strsplit/private/ischarnum.m | 35 ++++++++
 matlab/missing/strsplit/strsplit.m          | 90 +++++++++++++++++++++
 4 files changed, 158 insertions(+), 1 deletion(-)
 create mode 100644 matlab/missing/strsplit/private/ischarint.m
 create mode 100644 matlab/missing/strsplit/private/ischarnum.m
 create mode 100644 matlab/missing/strsplit/strsplit.m

diff --git a/matlab/dynare_config.m b/matlab/dynare_config.m
index bcf97de2e..ae534c290 100644
--- a/matlab/dynare_config.m
+++ b/matlab/dynare_config.m
@@ -125,6 +125,11 @@ if isoctave || matlab_ver_less_than('9.3')
     p{end+1} = '/missing/isfile';
 end
 
+% strsplit is missing in Matlab<R2013a
+if ~isoctave && matlab_ver_less_than('8.1')
+    p{end+1} = '/missing/strsplit';
+end
+
 P = cellfun(@(c)[dynareroot(1:end-1) c], p, 'uni',false);
 
 % Get mex files folder(s)
@@ -242,4 +247,4 @@ end
 % Initialization of the dates and dseries classes (recursive).
 initialize_dseries_toolbox;
 
-cd(origin);
\ No newline at end of file
+cd(origin);
diff --git a/matlab/missing/strsplit/private/ischarint.m b/matlab/missing/strsplit/private/ischarint.m
new file mode 100644
index 000000000..d325015d4
--- /dev/null
+++ b/matlab/missing/strsplit/private/ischarint.m
@@ -0,0 +1,27 @@
+function l = ischarint(x)
+
+% Returns true if and only if char x represents an integer.
+
+% Copyright © 2018 DynareTeam
+%
+% This file is part of Dynare.
+%
+% Dynare 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.
+%
+% Dynare 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 Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+s = warning;
+warning off;
+
+l = isint(str2double(x));
+
+warning(s);
\ No newline at end of file
diff --git a/matlab/missing/strsplit/private/ischarnum.m b/matlab/missing/strsplit/private/ischarnum.m
new file mode 100644
index 000000000..57b7c7e8c
--- /dev/null
+++ b/matlab/missing/strsplit/private/ischarnum.m
@@ -0,0 +1,35 @@
+function l = ischarnum(x)
+
+% Returns true if and only if char x represents a real number.
+
+% Copyright © 2018 DynareTeam
+%
+% This file is part of Dynare.
+%
+% Dynare 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.
+%
+% Dynare 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 Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+l = false;
+
+s = warning;
+warning off;
+
+number = str2double(x);
+
+warning(s);
+
+if ~isempty(number)
+    if isreal(number)
+        l = true;
+    end
+end
\ No newline at end of file
diff --git a/matlab/missing/strsplit/strsplit.m b/matlab/missing/strsplit/strsplit.m
new file mode 100644
index 000000000..926bb50f3
--- /dev/null
+++ b/matlab/missing/strsplit/strsplit.m
@@ -0,0 +1,90 @@
+function tok = strsplit(str, delimiters)
+
+% Splits a string into multiple terms.
+%
+% INPUTS
+% - str        [char]                String to be splitted.
+% - delimiters [char, cell(char)]    Delimiters.
+%
+% OUTPUTS
+% - tok        [cell(char)]          Terms.
+
+% Copyright © 2018 DynareTeam
+%
+% This file is part of Dynare.
+%
+% Dynare 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.
+%
+% Dynare 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 Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+remove_empty = true;
+remove_numbers = false;
+
+% Check first input arguments
+assert(ischar(str) && ndims(str)==2 && size(str,1)<=1, 'The first arugment has to be a row char array!');
+
+% Set default value for second input arguments
+if nargin<2
+    delimiters = {' '};
+end
+
+% If second input argument is a char transform it into a sigleton cell of char
+if nargin>1
+    if ischar(delimiters)
+        assert(ndims(delimiters)==2 && size(delimiters,1)==1, 'The second input argument has to be be a char string!');
+        delimiters = {delimiters};
+    end
+end
+
+% Check that `delimiters` is a one dimensional cell
+assert(ndim(delimiters)<=1, 'The second input argument has to be a one dimensional cell array!')
+
+% Check that `delimiters` is a cell of row char arrays
+assert(all(cellfun(@ischar, delimiters)) && all(cellfun(@rows, delimiters)==1), 'The second input argument has to be a cell of row char arrays!')
+
+% If space is one of the delimiters obtain the index in `delimiters`
+idspace = strmatch(' ', delimiters);
+
+% Get the number of delimiters
+n = length(delimiters);
+
+% Remove unnecessary spaces
+delimiters(setdiff(1:n, idspace)) = strtrim(delimiters(setdiff(1:n, idspace)));
+
+% Join all the delimiters (strjoin is not available with matlab version less than R2013a)
+if n>1
+    delimiter = '';
+    for i=1:n
+        if isspace(delimiters{i})
+            delimiter = horzcat(delimiter, '\s');
+        else
+            delimiter = horzcat(delimiter, delimiters{i});
+        end
+        delimiter = horzcat(delimiter,'|');
+    end
+    delimiter = horzcat(delimiter, '\W');
+else
+    delimiter = delimiters{1};
+end
+
+% Get tokens
+tok = regexp(str, delimiter, 'split');
+
+if remove_empty
+    % Remove empty tokens
+    tok = tok(find(~cellfun(@isempty, tok)));
+end
+
+if remove_numbers
+    % Remove numbers
+    tok = tok(find(~cellfun(@ischarnum, tok)));
+end
\ No newline at end of file
-- 
GitLab