changeset 29198:ec33b1617cd5

dec2base.m: Overhaul input validation. * dec2base.m: Document that any whitespace is not allowed as a symbol. Force a row vector for string input of base. Use numel() in preference to length(). Add many more validation tests for BASE inputs. Add input validation of LEN input. Rewrite BIST tests to include expected error message. Add BIST tests for new input validation code.
author Rik <rik@octave.org>
date Fri, 18 Dec 2020 16:16:53 -0800
parents 1a3cb2e1644a
children 0b18887bc997
files scripts/strings/dec2base.m
diffstat 1 files changed, 27 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/strings/dec2base.m	Fri Dec 18 15:23:00 2020 -0500
+++ b/scripts/strings/dec2base.m	Fri Dec 18 16:16:53 2020 -0800
@@ -41,8 +41,8 @@
 ## largest value.
 ##
 ## If @var{base} is a string then the characters of @var{base} are used as
-## the symbols for the digits of @var{d}.  Space (' ') may not be used as a
-## symbol.
+## the symbols for the digits of @var{d}.  Whitespace (spaces, tabs, newlines,
+##, etc.@:) may not be used as a symbol.
 ##
 ## @example
 ## @group
@@ -78,18 +78,16 @@
 
   symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   if (ischar (base))
-    symbols = base;
-    base = length (symbols);
-    if (length (unique (symbols)) != base)
+    symbols = base(:).';  # force a row vector
+    base = numel (symbols);
+    if (numel (unique (symbols)) != base)
       error ("dec2base: symbols representing digits must be unique");
-    endif
-    if (any (isspace (symbols)))
+    elseif (any (isspace (symbols)))
       error ("dec2base: whitespace characters are not valid symbols");
     endif
-  elseif (! isscalar (base))
-    error ("dec2base: cannot convert from several bases at once");
-  elseif (base < 2 || base > length (symbols))
-    error ("dec2base: BASE must be between 2 and 36, or a string of symbols");
+  elseif (! isscalar (base) || ! isreal (base) || fix (base) != base
+          || base < 2 || base > 36)
+    error ("dec2base: BASE must be an integer between 2 and 36, or a string of symbols");
   endif
 
   ## determine number of digits required to handle all numbers, can overflow
@@ -97,6 +95,9 @@
   max_len = round (log (max (max (d), 1)) / log (base)) + 1;
 
   if (nargin == 3)
+    if (! (isscalar (len) && isreal (len) && len >= 0 && len == fix (len)))
+      error ("dec2base: LEN must be a non-negative integer");
+    endif
     max_len = max (max_len, len);
   endif
 
@@ -165,12 +166,18 @@
 ## Test input validation
 %!error <Invalid call> dec2base ()
 %!error <Invalid call> dec2base (1)
-%!error dec2base ("A")
-%!error dec2base (2i)
-%!error dec2base (-1)
-%!error dec2base (1.1)
-%!error dec2base (1, "ABA")
-%!error dec2base (1, "A B")
-%!error dec2base (1, ones (2))
-%!error dec2base (1, 1)
-%!error dec2base (1, 37)
+%!error <input must be real non-negative integers> dec2base ("A", 10)
+%!error <input must be real non-negative integers> dec2base (2i, 10)
+%!error <input must be real non-negative integers> dec2base (-1, 10)
+%!error <input must be real non-negative integers> dec2base (1.1, 10)
+%!error <symbols representing digits must be unique> dec2base (1, "ABA")
+%!error <whitespace characters are not valid symbols> dec2base (1, "A B")
+%!error <BASE must be an integer> dec2base (1, ones (2))
+%!error <BASE must be an integer> dec2base (1, 2i)
+%!error <BASE must be an integer> dec2base (1, 2.5)
+%!error <BASE must be .* between 2 and 36> dec2base (1, 1)
+%!error <BASE must be .* between 2 and 36> dec2base (1, 37)
+%!error <LEN must be a non-negative integer> dec2base (1, 2, ones(2))
+%!error <LEN must be a non-negative integer> dec2base (1, 2, 2i)
+%!error <LEN must be a non-negative integer> dec2base (1, 2, -1)
+%!error <LEN must be a non-negative integer> dec2base (1, 2, 2.5)