Mercurial > hg > octave-nkf
changeset 14478:e995b1c97e13 stable
Fix regression in bin2dec which did not allow space-separated input.
* base2dec.m: Squeeze spaces from input before applying algorithm.
* bin2dec.m: Add tests for using spaces in binary number.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Fri, 16 Mar 2012 16:51:06 -0700 |
parents | 21ac4b576003 |
children | 3d628878e109 |
files | scripts/strings/base2dec.m scripts/strings/bin2dec.m |
diffstat | 2 files changed, 33 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/strings/base2dec.m +++ b/scripts/strings/base2dec.m @@ -81,8 +81,27 @@ s = toupper (s); endif - ## Right justify the values before anything else. - s = strjust (s, "right"); + ## Right justify the values and squeeze out any spaces. + ## This looks complicated, but indexing solution is very fast + ## compared to alternatives which use cellstr or cellfun or looping. + [nr, nc] = size (s); + if (nc > 1) # Bug #35621 + s = s.'; + nonbl = s != " "; + num_nonbl = sum (nonbl); + nc = max (num_nonbl); + num_blank = nc - num_nonbl; + R = repmat ([1 2; 0 0], 1, nr); + R(2, 1:2:2*nr) = num_blank; + R(2, 2:2:2*nr) = num_nonbl; + idx = repelems ([false, true], R); + idx = reshape (idx, nc, nr); + + ## Create a blank matrix and position the nonblank characters. + s2 = repmat (" ", nc, nr); + s2(idx) = s(nonbl); + s = s2.'; + endif ## Lookup value of symbols in symbol table, with invalid symbols ## evaluating to NaN and space evaluating to 0.
--- a/scripts/strings/bin2dec.m +++ b/scripts/strings/bin2dec.m @@ -28,6 +28,16 @@ ## @end group ## @end example ## +## Spaces are ignored during conversion and may be used to make the binary +## number more readable. +## +## @example +## @group +## bin2dec ("1000 0001") +## @result{} 129 +## @end group +## @end example +## ## If @var{s} is a string matrix, return a column vector with one converted ## number per row of @var{s}; Invalid rows evaluate to NaN@. ## @@ -54,6 +64,8 @@ %!assert(bin2dec ("1110"), 14); %!assert(bin2dec ("11111111111111111111111111111111111111111111111111111"), 2^53-1); %!assert(bin2dec ({"1110", "1111"}), [14; 15]); +%!assert (bin2dec ("1 0 1"), 5) +%!assert (bin2dec (char ("1 0 1", " 1111")), [5; 15]); %%Test input validation %!error bin2dec ();