comparison libinterp/corefcn/hex2num.cc @ 20939:b17fda023ca6

maint: Use new C++ archetype in more files. Place input validation first in files. Move declaration of retval down in function to be closer to point of usage. Eliminate else clause after if () error. Use "return ovl()" where it makes sense. * find.cc, gammainc.cc, gcd.cc, getgrent.cc, getpwent.cc, givens.cc, graphics.cc, help.cc, hess.cc, hex2num.cc, input.cc, kron.cc, load-path.cc, load-save.cc, lookup.cc, mappers.cc, matrix_type.cc, mgorth.cc, nproc.cc, ordschur.cc, pager.cc, pinv.cc, pr-output.cc, profiler.cc, psi.cc, quad.cc, rcond.cc, regexp.cc, schur.cc, sighandlers.cc, sparse.cc, str2double.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc, symtab.cc, syscalls.cc, sysdep.cc, time.cc, toplev.cc, tril.cc, tsearch.cc, typecast.cc, urlwrite.cc, utils.cc, variables.cc, __delaunayn__.cc, __eigs__.cc, __glpk__.cc, __magick_read__.cc, __osmesa_print__.cc, __voronoi__.cc, amd.cc, audiodevinfo.cc, audioread.cc, chol.cc, colamd.cc, dmperm.cc, fftw.cc, qr.cc, symbfact.cc, symrcm.cc, ov-bool-mat.cc, ov-cell.cc, ov-class.cc, ov-classdef.cc, ov-fcn-handle.cc, ov-fcn-inline.cc, ov-flt-re-mat.cc, ov-java.cc, ov-null-mat.cc, ov-oncleanup.cc, ov-re-mat.cc, ov-struct.cc, ov-typeinfo.cc, ov-usr-fcn.cc, ov.cc, octave.cc: Use new C++ archetype in more files.
author Rik <rik@octave.org>
date Fri, 18 Dec 2015 15:37:22 -0800
parents 1142cf6abc0d
children 48b2ad5ee801
comparison
equal deleted inserted replaced
20938:aac911d8847b 20939:b17fda023ca6
63 @end group\n\ 63 @end group\n\
64 @end example\n\ 64 @end example\n\
65 @seealso{num2hex, hex2dec, dec2hex}\n\ 65 @seealso{num2hex, hex2dec, dec2hex}\n\
66 @end deftypefn") 66 @end deftypefn")
67 { 67 {
68 octave_value retval;
69
70 int nargin = args.length (); 68 int nargin = args.length ();
71 69
72 if (nargin < 1 || nargin > 2) 70 if (nargin < 1 || nargin > 2)
73 print_usage (); 71 print_usage ();
74 72
75 if (nargin == 2 && ! args(1).is_string ()) 73 if (nargin == 2 && ! args(1).is_string ())
76 error ("hex2num: CLASS must be a string"); 74 error ("hex2num: CLASS must be a string");
75
76 const charMatrix cmat = args(0).char_matrix_value ();
77 std::string prec = (nargin == 2) ? args(1).string_value () : "double";
78 bool is_single = (prec == "single");
79 octave_idx_type nchars = (is_single) ? 8 : 16;
80
81 if (cmat.columns () > nchars)
82 error ("hex2num: S must be no more than %d characters", nchars);
83 else if (prec != "double" && prec != "single")
84 error ("hex2num: CLASS must be either \"double\" or \"single\"");
85
86 octave_value retval;
87 octave_idx_type nr = cmat.rows ();
88 octave_idx_type nc = cmat.columns ();
89
90 if (is_single)
91 {
92 FloatColumnVector m (nr);
93
94 for (octave_idx_type i = 0; i < nr; i++)
95 {
96 union
97 {
98 uint32_t ival;
99 float dval;
100 } num;
101
102 num.ival = 0;
103
104 for (octave_idx_type j = 0; j < nc; j++)
105 {
106 unsigned char ch = cmat.elem (i, j);
107
108 if (isxdigit (ch))
109 {
110 num.ival <<= 4;
111 if (ch >= 'a')
112 num.ival += static_cast<uint32_t> (ch - 'a' + 10);
113 else if (ch >= 'A')
114 num.ival += static_cast<uint32_t> (ch - 'A' + 10);
115 else
116 num.ival += static_cast<uint32_t> (ch - '0');
117 }
118 else
119 {
120 error ("hex2num: illegal character found in string S");
121 break;
122 }
123 }
124
125 if (nc < nchars)
126 num.ival <<= (nchars - nc) * 4;
127
128 m(i) = num.dval;
129 }
130
131 retval = m;
132 }
77 else 133 else
78 { 134 {
79 const charMatrix cmat = args(0).char_matrix_value (); 135 ColumnVector m (nr);
80 std::string prec = (nargin == 2) ? args(1).string_value () : "double"; 136
81 bool is_single = (prec == "single"); 137 for (octave_idx_type i = 0; i < nr; i++)
82 octave_idx_type nchars = (is_single) ? 8 : 16; 138 {
83 139 union
84 if (cmat.columns () > nchars) 140 {
85 error ("hex2num: S must be no more than %d characters", nchars); 141 uint64_t ival;
86 else if (prec != "double" && prec != "single") 142 double dval;
87 error ("hex2num: CLASS must be either \"double\" or \"single\""); 143 } num;
88 else 144
89 { 145 num.ival = 0;
90 octave_idx_type nr = cmat.rows (); 146
91 octave_idx_type nc = cmat.columns (); 147 for (octave_idx_type j = 0; j < nc; j++)
92 148 {
93 if (is_single) 149 unsigned char ch = cmat.elem (i, j);
94 { 150
95 FloatColumnVector m (nr); 151 if (isxdigit (ch))
96
97 for (octave_idx_type i = 0; i < nr; i++)
98 { 152 {
99 union 153 num.ival <<= 4;
100 { 154 if (ch >= 'a')
101 uint32_t ival; 155 num.ival += static_cast<uint64_t> (ch - 'a' + 10);
102 float dval; 156 else if (ch >= 'A')
103 } num; 157 num.ival += static_cast<uint64_t> (ch - 'A' + 10);
104 158 else
105 num.ival = 0; 159 num.ival += static_cast<uint64_t> (ch - '0');
106
107 for (octave_idx_type j = 0; j < nc; j++)
108 {
109 unsigned char ch = cmat.elem (i, j);
110
111 if (isxdigit (ch))
112 {
113 num.ival <<= 4;
114 if (ch >= 'a')
115 num.ival += static_cast<uint32_t> (ch - 'a' + 10);
116 else if (ch >= 'A')
117 num.ival += static_cast<uint32_t> (ch - 'A' + 10);
118 else
119 num.ival += static_cast<uint32_t> (ch - '0');
120 }
121 else
122 {
123 error ("hex2num: illegal character found in string S");
124 break;
125 }
126 }
127
128 if (nc < nchars)
129 num.ival <<= (nchars - nc) * 4;
130
131 m(i) = num.dval;
132 } 160 }
133 161 else
134 retval = m; 162 error ("hex2num: illegal character found in string S");
135 } 163 }
136 else 164
137 { 165 if (nc < nchars)
138 ColumnVector m (nr); 166 num.ival <<= (nchars - nc) * 4;
139 167
140 for (octave_idx_type i = 0; i < nr; i++) 168 m(i) = num.dval;
141 { 169 }
142 union 170
143 { 171 retval = m;
144 uint64_t ival;
145 double dval;
146 } num;
147
148 num.ival = 0;
149
150 for (octave_idx_type j = 0; j < nc; j++)
151 {
152 unsigned char ch = cmat.elem (i, j);
153
154 if (isxdigit (ch))
155 {
156 num.ival <<= 4;
157 if (ch >= 'a')
158 num.ival += static_cast<uint64_t> (ch - 'a' + 10);
159 else if (ch >= 'A')
160 num.ival += static_cast<uint64_t> (ch - 'A' + 10);
161 else
162 num.ival += static_cast<uint64_t> (ch - '0');
163 }
164 else
165 {
166 error ("hex2num: illegal character found in string S");
167 break;
168 }
169 }
170
171 if (nc < nchars)
172 num.ival <<= (nchars - nc) * 4;
173
174 m(i) = num.dval;
175 }
176
177 retval = m;
178 }
179 }
180 } 172 }
181 173
182 return retval; 174 return retval;
183 } 175 }
184 176
218 @end group\n\ 210 @end group\n\
219 @end example\n\ 211 @end example\n\
220 @seealso{hex2num, hex2dec, dec2hex}\n\ 212 @seealso{hex2num, hex2dec, dec2hex}\n\
221 @end deftypefn") 213 @end deftypefn")
222 { 214 {
223 octave_value retval;
224
225 if (args.length () != 1) 215 if (args.length () != 1)
226 print_usage (); 216 print_usage ();
217
218 octave_value retval;
227 219
228 if (args(0).is_single_type ()) 220 if (args(0).is_single_type ())
229 { 221 {
230 const FloatColumnVector v (args(0).float_vector_value ()); 222 const FloatColumnVector v (args(0).float_vector_value ());
231 223