annotate scripts/strings/strjoin.m @ 31239:dd6b37f67db2

Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089) dec2bin.m: Accept negative inputs lower than -flintmax down to -2^63 Return 64-bit string in certain cases for Matlab compatibility Activate BISTs commented out earlier dec2hex.m: Remove repeated code and call dec2bin instead Accept negative inputs lower than -flintmax down to -2^63 Update and activate BISTs commented out earlier
author Arun Giridhar <arungiridhar@gmail.com>
date Sun, 25 Sep 2022 06:22:25 -0400
parents 796f54d4ddbf
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
3 ## Copyright (C) 2007-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27069
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
7 ##
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
8 ## This file is part of Octave.
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23084
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
11 ## 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: 23084
diff changeset
12 ## 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
13 ## (at your option) any later version.
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
14 ##
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
19 ##
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
21 ## 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: 23084
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
25
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20164
diff changeset
27 ## @deftypefn {} {@var{str} =} strjoin (@var{cstr})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20164
diff changeset
28 ## @deftypefnx {} {@var{str} =} strjoin (@var{cstr}, @var{delimiter})
18186
1874a5a142ee doc: Fix documentation for strsplit (bug #41062).
Rik <rik@octave.org>
parents: 17338
diff changeset
29 ## Join the elements of the cell string array, @var{cstr}, into a single
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
30 ## string.
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
31 ##
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
32 ## If no @var{delimiter} is specified, the elements of @var{cstr} are
16826
a4969508008e doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents: 16817
diff changeset
33 ## separated by a space.
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
34 ##
18186
1874a5a142ee doc: Fix documentation for strsplit (bug #41062).
Rik <rik@octave.org>
parents: 17338
diff changeset
35 ## If @var{delimiter} is specified as a string, the cell string array is
16816
12005245b645 doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 16815
diff changeset
36 ## joined using the string. Escape sequences are supported.
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
37 ##
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
38 ## If @var{delimiter} is a cell string array whose length is one less than
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
39 ## @var{cstr}, then the elements of @var{cstr} are joined by interleaving the
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
40 ## cell string elements of @var{delimiter}. Escape sequences are not
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
41 ## supported.
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
42 ##
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
43 ## @example
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
44 ## @group
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
45 ## strjoin (@{'Octave','Scilab','Lush','Yorick'@}, '*')
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
46 ## @result{} 'Octave*Scilab*Lush*Yorick'
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
47 ## @end group
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
48 ## @end example
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
49 ## @seealso{strsplit}
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
50 ## @end deftypefn
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
51
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27985
diff changeset
52 function rval = strjoin (cstr, delimiter = " ")
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
53
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27985
diff changeset
54 if (nargin < 1)
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
55 print_usage ();
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
56 elseif (! (iscellstr (cstr) && (ischar (delimiter) || iscellstr (delimiter))))
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
57 print_usage ();
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
58 endif
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
59
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
60 if (numel (cstr) == 1)
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
61 rval = cstr{1};
17312
088d014a7fe2 Use semicolon after "return" statement in core m-files.
Rik <rik@octave.org>
parents: 17306
diff changeset
62 return;
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
63 endif
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
64
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
65 if (ischar (delimiter))
16763
70ea511edbc4 strjoin.m: delimiter can include escape sequences (matlab compatibility)
Stefan Mahr <dac922@gmx.de>
parents: 16761
diff changeset
66 delimiter = do_string_escapes (delimiter);
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
67 delimiter = {delimiter};
17306
09543e9c8f40 Use explicit form of end (endif, endfor, etc.) in core m-files.
Rik <rik@octave.org>
parents: 16826
diff changeset
68 endif
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
69
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18186
diff changeset
70 num = numel (cstr);
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
71 if (numel (delimiter) == 1 && num > 1)
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
72 delimiter = repmat (delimiter, 1, num);
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
73 delimiter(end) = {""};
16815
7a97ff5ef42e strjoin() should return an empty [0x0] string for an empty input.
Ben Abbott <bpabbott@mac.com>
parents: 16768
diff changeset
74 elseif (num > 0 && numel (delimiter) != num - 1)
27069
0a62d9a6aa2d Place Octave's warning and error IDs in to the "Octave" namespace (bug #56213).
Rik <rik@octave.org>
parents: 26376
diff changeset
75 error ("Octave:strjoin:cellstring_delimiter_mismatch",
21385
89fa0694aa2e Fix check-missing-semicolon QA target (bug #47277).
Rik <rik@octave.org>
parents: 20852
diff changeset
76 "strjoin: the number of delimiters does not match the number of strings");
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
77 else
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
78 delimiter(end+1) = {""};
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
79 endif
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
80
16815
7a97ff5ef42e strjoin() should return an empty [0x0] string for an empty input.
Ben Abbott <bpabbott@mac.com>
parents: 16768
diff changeset
81 if (num == 0)
16817
893e9cec62f1 Add missing semicolon to strjoin (missed in 7a97ff5ef42e).
Ben Abbott <bpabbott@mac.com>
parents: 16816
diff changeset
82 rval = "";
16815
7a97ff5ef42e strjoin() should return an empty [0x0] string for an empty input.
Ben Abbott <bpabbott@mac.com>
parents: 16768
diff changeset
83 else
7a97ff5ef42e strjoin() should return an empty [0x0] string for an empty input.
Ben Abbott <bpabbott@mac.com>
parents: 16768
diff changeset
84 rval = [[cstr(:).'; delimiter(:).']{:}];
7a97ff5ef42e strjoin() should return an empty [0x0] string for an empty input.
Ben Abbott <bpabbott@mac.com>
parents: 16768
diff changeset
85 endif
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
86
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
87 endfunction
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
88
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17312
diff changeset
89
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
90 %!assert (strjoin ({"hello"}, "-"), "hello")
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
91 %!assert (strjoin ({"hello", "world"}), "hello world")
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
92 %!assert (strjoin ({"Octave", "Scilab", "Lush", "Yorick"}, "*"),
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
93 %! "Octave*Scilab*Lush*Yorick")
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
94 %!assert (strjoin ({"space", "comma", "dash", "semicolon", "done"},
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
95 %! {" ", ",", "-", ";"}), "space comma,dash-semicolon;done")
16763
70ea511edbc4 strjoin.m: delimiter can include escape sequences (matlab compatibility)
Stefan Mahr <dac922@gmx.de>
parents: 16761
diff changeset
96 %!assert (strjoin ({'Octave','Scilab'},'\n'), "Octave\nScilab")
70ea511edbc4 strjoin.m: delimiter can include escape sequences (matlab compatibility)
Stefan Mahr <dac922@gmx.de>
parents: 16761
diff changeset
97 %!assert (strjoin ({'Octave','Scilab'},{'\n'}), "Octave\\nScilab")
16815
7a97ff5ef42e strjoin() should return an empty [0x0] string for an empty input.
Ben Abbott <bpabbott@mac.com>
parents: 16768
diff changeset
98 %!assert (strjoin ({},'foo'), "")