Mercurial > octave
annotate scripts/miscellaneous/swapbytes.m @ 26600:f6730533820e stable
doc: clean up doc example blocks for accuracy and consistent formatting
* bitset.m, circshift.m, idivide.m, int2str.m, integral3.m, num2str.m, rat.m,
repelem.m, shiftdim.m, structfun.m, trapz.m, condeig.m, cross.m, qzhess.m,
computer.m, fullfile.m, orderfields.m, substruct.m, swapbytes.m, contourc.m,
isprime.m, runlength.m, clock.m, date.m: Fix presentation and formatting of
example block results.
* graphics.cc (Fset): Likewise.
* sparse.cc (Fsparse): Likewise.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Tue, 22 Jan 2019 13:45:11 -0800 |
parents | 00f796120a6d |
children | b442ec6dda5c |
rev | line source |
---|---|
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
1 ## Copyright (C) 2007-2019 David Bateman |
6869 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
5 ## Octave is free software: you can redistribute it and/or modify it |
6869 | 6 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
7 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
8 ## (at your option) any later version. |
6869 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## GNU General Public License for more details. |
6869 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
17 ## <https://www.gnu.org/licenses/>. |
6869 | 18 |
19 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
20 ## @deftypefn {} {} swapbytes (@var{x}) |
20164
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
21 ## Swap the byte order on values, converting from little endian to big endian |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
22 ## and vice versa. |
19209
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
23 ## |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
24 ## For example: |
6869 | 25 ## |
26 ## @example | |
27 ## @group | |
28 ## swapbytes (uint16 (1:4)) | |
26600
f6730533820e
doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
29 ## @result{} 256 512 768 1024 |
6869 | 30 ## @end group |
31 ## @end example | |
32 ## | |
33 ## @seealso{typecast, cast} | |
34 ## @end deftypefn | |
35 | |
36 function y = swapbytes (x) | |
12847
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
37 |
6869 | 38 if (nargin != 1) |
39 print_usage (); | |
40 endif | |
41 | |
19209
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
42 cls = class (x); |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
43 if (strcmp (cls, "int8") || strcmp (cls, "uint8") || isempty (x)) |
6869 | 44 y = x; |
45 else | |
19209
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
46 if (strcmp (cls, "int16") || strcmp (cls, "uint16")) |
6869 | 47 nb = 2; |
19209
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
48 elseif (strcmp (cls, "single") |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
49 || strcmp (cls, "int32") || strcmp (cls, "uint32")) |
6869 | 50 nb = 4; |
19209
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
51 elseif (strcmp (cls, "double") |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
52 || strcmp (cls, "int64") || strcmp (cls, "uint64")) |
6869 | 53 nb = 8; |
54 else | |
19209
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
55 error ("swapbytes: invalid object of class '%s'", cls); |
6869 | 56 endif |
57 y = reshape (typecast (reshape (typecast (x(:), "uint8"), nb, numel (x)) | |
19209
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
58 ([nb : -1 : 1], :) (:), cls), size (x)); |
6869 | 59 endif |
12847
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
60 |
6869 | 61 endfunction |
12847
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
62 |
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
63 |
19209
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
64 %!assert (swapbytes (uint16 (1:4)), uint16 ([256 512 768 1024])) |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
65 %!test |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
66 %! assert (swapbytes (swapbytes (pi)), pi); |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
67 %! assert (swapbytes (swapbytes (single (pi))), single (pi)); |
12847
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
68 |
19209
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
69 ## Test input validation |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
70 %!error swapbytes () |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
71 %!error swapbytes (1, 2) |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
72 %!error <invalid object of class 'cell'> swapbytes ({1}) |