Mercurial > octave-nkf
comparison scripts/strings/base2dec.m @ 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 | 731e9e1539a8 |
children | 5bd9e47e9277 |
comparison
equal
deleted
inserted
replaced
14464:21ac4b576003 | 14478:e995b1c97e13 |
---|---|
79 error ("base2dec: BASE must be between 2 and 36, or a string of symbols"); | 79 error ("base2dec: BASE must be between 2 and 36, or a string of symbols"); |
80 else | 80 else |
81 s = toupper (s); | 81 s = toupper (s); |
82 endif | 82 endif |
83 | 83 |
84 ## Right justify the values before anything else. | 84 ## Right justify the values and squeeze out any spaces. |
85 s = strjust (s, "right"); | 85 ## This looks complicated, but indexing solution is very fast |
86 ## compared to alternatives which use cellstr or cellfun or looping. | |
87 [nr, nc] = size (s); | |
88 if (nc > 1) # Bug #35621 | |
89 s = s.'; | |
90 nonbl = s != " "; | |
91 num_nonbl = sum (nonbl); | |
92 nc = max (num_nonbl); | |
93 num_blank = nc - num_nonbl; | |
94 R = repmat ([1 2; 0 0], 1, nr); | |
95 R(2, 1:2:2*nr) = num_blank; | |
96 R(2, 2:2:2*nr) = num_nonbl; | |
97 idx = repelems ([false, true], R); | |
98 idx = reshape (idx, nc, nr); | |
99 | |
100 ## Create a blank matrix and position the nonblank characters. | |
101 s2 = repmat (" ", nc, nr); | |
102 s2(idx) = s(nonbl); | |
103 s = s2.'; | |
104 endif | |
86 | 105 |
87 ## Lookup value of symbols in symbol table, with invalid symbols | 106 ## Lookup value of symbols in symbol table, with invalid symbols |
88 ## evaluating to NaN and space evaluating to 0. | 107 ## evaluating to NaN and space evaluating to 0. |
89 table = NaN (1, 256); | 108 table = NaN (1, 256); |
90 table(toascii (symbols(1:base))) = 0 : base-1; | 109 table(toascii (symbols(1:base))) = 0 : base-1; |