# HG changeset patch
# User Nicholas R. Jankowski <jankowskin@asme.org>
# Date 1605552997 18000
#      Mon Nov 16 13:56:37 2020 -0500
# Node ID f48b5c3ef57c6064e32bd03002b2cc8582485b30
# Parent  a4060a00d0bb1e385322eb7c55229c1fafa06b22
Updated input handling for di2be (bug #59466)
* /inst/de2bi.m: updated input handling to avoid error when calling MSB-flag or passing a [], added p input check, added BISTS
* /NEWS: added note about function change to top of file

diff -r a4060a00d0bb -r f48b5c3ef57c NEWS
--- a/NEWS	Sun Jan 05 12:28:00 2020 +0100
+++ b/NEWS	Mon Nov 16 13:56:37 2020 -0500
@@ -1,3 +1,7 @@
+ ** Fixed a bug in 'de2bi' that gave an error when only specifying 
+    input decimal and MSB direction, and also improved Matlab 
+	compatibility of input option handling.
+
 Summary of important user-visible changes for communications 1.2.2:
 ------------------------------------------------------------------
 
diff -r a4060a00d0bb -r f48b5c3ef57c inst/de2bi.m
--- a/inst/de2bi.m	Sun Jan 05 12:28:00 2020 +0100
+++ b/inst/de2bi.m	Mon Nov 16 13:56:37 2020 -0500
@@ -17,7 +17,7 @@
 ## @deftypefn  {Function File} {@var{b} =} de2bi (@var{d})
 ## @deftypefnx {Function File} {@var{b} =} de2bi (@var{d}, @var{n})
 ## @deftypefnx {Function File} {@var{b} =} de2bi (@var{d}, @var{n}, @var{p})
-## @deftypefnx {Function File} {@var{b} =} de2bi (@var{d}, @var{n}, @var{p}, @var{f})
+## @deftypefnx {Function File} {@var{b} =} de2bi (@var{d}, @dots{}, @var{f})
 ##
 ## Convert a non-negative integer to bit vector.
 ##
@@ -31,7 +31,7 @@
 ##
 ## If @var{p} is defined then it is used as the base for the decomposition
 ## of the returned values. That is the elements of the returned value are
-## between '0' and 'p-1'.
+## between '0' and 'p-1'. (@var{p} must have a value of 2 or higher.)
 ##
 ## The variable @var{f} defines whether the first or last element of @var{b}
 ## is considered to be the most-significant. Valid values of @var{f} are
@@ -40,30 +40,65 @@
 ## @seealso{bi2de}
 ## @end deftypefn
 
-function b = de2bi (d, n, p, f)
+function b = de2bi (d, varargin)
 
   if (nargin == 1)
     p = 2;
     n = floor ( log (max (max (d), 1)) ./ log (p) ) + 1;
     f = "right-msb";
-  elseif (nargin == 2)
-    p = 2;
-    f = "right-msb";
-  elseif (nargin == 3)
-    if (ischar (p))
-      f = p;
-      p = 2;
+  else
+    n = [];
+    p = []; 
+    f = [];
+
+    % first pull out non-numeric inputs
+    msb_flag_chk = cellfun (@ischar, varargin);
+    if any(msb_flag_chk)
+      if sum (msb_flag_chk) > 1
+        %should never be more than one string input
+        print_usage ();
+      else
+        f = varargin{msb_flag_chk};
+        varargin = varargin(~msb_flag_chk);
+      endif
     else
       f = "right-msb";
     endif
-  elseif (nargin == 4)
-    if (ischar (p))
-      tmp = f;
-      f = p;
-      p = tmp;
+
+    %varargin should now be length 0, 1, or 2, all non-char
+    numer_inputs = numel (varargin);
+
+    if numer_inputs == 2
+      n = varargin{1};
+      p = varargin{2};
+    elseif numer_inputs == 1
+      n = varargin{1};
+      p = 2;
+    elseif numer_inputs == 0
+      p = 2;
+      n = floor ( log (max (max (d), 1)) ./ log (p) ) + 1;
+    else
+      print_usage ();
     endif
-  else
-    print_usage ();
+
+    %if user passed any [] as inputs, set to defaults
+    if isempty (p)
+      p = 2;
+    endif
+    if isempty (n)
+      n = floor ( log (max (max (d), 1)) ./ log (p) ) + 1;
+    endif
+    if isempty (f)
+      f = "right-msb";
+    endif
+  endif
+
+  ##TODO: previous versions permitted negative base, but did not actually output
+  ## values according to negative-base system rules. If this is desired the p<0
+  ## check can be removed and proper negative-base arithmetic can be added.
+  ## Also removed p=0 and 1 which caused errors in n-calculation.  
+  if (p < 2)
+    error ("de2bi: conversion base must be 2 or greater");
   endif
 
   d = d(:);
@@ -97,6 +132,10 @@
 %!assert (size (de2bi (x, 16)), [100 16])
 %!assert (de2bi (x, 16, "right-msb"), de2bi (x, 16))
 %!assert (de2bi (x, 16, "left-msb"), fliplr (de2bi (x, 16)))
+%!assert (de2bi (13, "right-msb"), [1 0 1 1])
+%!assert (de2bi (13, "left-msb"), [1 1 0 1])
+%!assert (de2bi (13, [], "right-msb"), [1 0 1 1])
+%!assert (de2bi (13, [], [], "right-msb"), [1 0 1 1])
 
 %% Test input validation
 %!error de2bi ()
@@ -105,3 +144,6 @@
 %!error de2bi (1, 2, 3, "invalid")
 %!error de2bi (0.1)
 %!error de2bi (-1)
+%!error de2bi (5,[],1)
+%!error de2bi (5,[],0)
+%!error de2bi (5,[],-2)
