Mercurial > octave-nkf
comparison src/strfns.cc @ 8461:9d456730b7a8
strfns.cc: improve documentation strings, add examples and tests
author | Thorsten Meyer <thorsten.meyier@gmx.de> |
---|---|
date | Mon, 12 Jan 2009 12:13:05 -0500 |
parents | 8dff9cba15fe |
children | ebdf1e058d85 |
comparison
equal
deleted
inserted
replaced
8460:e4c9ecb64411 | 8461:9d456730b7a8 |
---|---|
43 DEFUN (char, args, , | 43 DEFUN (char, args, , |
44 "-*- texinfo -*-\n\ | 44 "-*- texinfo -*-\n\ |
45 @deftypefn {Built-in Function} {} char (@var{x})\n\ | 45 @deftypefn {Built-in Function} {} char (@var{x})\n\ |
46 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\ | 46 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\ |
47 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\ | 47 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\ |
48 Create a string array from a numeric matrix, cell array, or list of\n\ | 48 Create a string array from one or more numeric matrices, character\n\ |
49 \n\ | 49 matrices or cell arrays. For numerical input, each element is converted\n\ |
50 If the argument is a numeric matrix, each element of the matrix is\n\ | 50 to the corresponding ASCII character. The arguments (and elements of\n\ |
51 converted to the corresponding ASCII character. For example,\n\ | 51 cell array(s)) are concatenated vertically.\n\ |
52 The returned values are padded with blanks as needed to make each row\n\ | |
53 of the string array have the same length. Empty strings are not removed.\n\ | |
54 For example,\n\ | |
52 \n\ | 55 \n\ |
53 @example\n\ | 56 @example\n\ |
54 @group\n\ | 57 @group\n\ |
55 char ([97, 98, 99])\n\ | 58 char ([97, 98, 99], \"\", @{\"98\", \"99\", 100@}, [\"num\", \"bers\"])\n\ |
56 @result{} \"abc\"\n\ | 59 @result{} [\"abc \"\n\ |
60 \" \"\n\ | |
61 \"98 \"\n\ | |
62 \"99 \"\n\ | |
63 \"d \"\n\ | |
64 \"numbers\"]\n\ | |
57 @end group\n\ | 65 @end group\n\ |
58 @end example\n\ | 66 @end example\n\ |
59 \n\ | 67 \n\ |
60 If the argument is a cell array of strings, the result is a string array\n\ | |
61 with each element corresponding to one element of the cell array.\n\ | |
62 \n\ | |
63 For multiple string arguments, the result is a string array with each\n\ | |
64 element corresponding to the arguments.\n\ | |
65 \n\ | |
66 The returned values are padded with blanks as needed to make each row\n\ | |
67 of the string array have the same length.\n\ | |
68 @end deftypefn") | 68 @end deftypefn") |
69 { | 69 { |
70 octave_value retval; | 70 octave_value retval; |
71 | 71 |
72 int nargin = args.length (); | 72 int nargin = args.length (); |
154 | 154 |
155 | 155 |
156 DEFUN (ischar, args, , | 156 DEFUN (ischar, args, , |
157 "-*- texinfo -*-\n\ | 157 "-*- texinfo -*-\n\ |
158 @deftypefn {Built-in Function} {} ischar (@var{a})\n\ | 158 @deftypefn {Built-in Function} {} ischar (@var{a})\n\ |
159 Return 1 if @var{a} is a string. Otherwise, return 0.\n\ | 159 Return 1 if @var{a} is a character array. Otherwise, return 0.\n\ |
160 @end deftypefn") | 160 @end deftypefn") |
161 { | 161 { |
162 octave_value retval; | 162 octave_value retval; |
163 | 163 |
164 int nargin = args.length (); | 164 int nargin = args.length (); |
168 else | 168 else |
169 print_usage (); | 169 print_usage (); |
170 | 170 |
171 return retval; | 171 return retval; |
172 } | 172 } |
173 | |
174 /* | |
175 | |
176 %!assert (ischar ("a"), logical (1)); | |
177 %!assert (ischar (["ab";"cd"]), logical (1)); | |
178 %!assert (ischar ({"ab"}), logical (0)); | |
179 %!assert (ischar (1), logical (0)); | |
180 %!error <Invalid call to ischar.*> ischar (); | |
181 | |
182 */ | |
173 | 183 |
174 DEFUN (strcmp, args, , | 184 DEFUN (strcmp, args, , |
175 "-*- texinfo -*-\n\ | 185 "-*- texinfo -*-\n\ |
176 @deftypefn {Built-in Function} {} strcmp (@var{s1}, @var{s2})\n\ | 186 @deftypefn {Built-in Function} {} strcmp (@var{s1}, @var{s2})\n\ |
177 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\ | 187 Return 1 if the character strings @var{s1} and @var{s2} are the same,\n\ |
746 print_usage (); | 756 print_usage (); |
747 | 757 |
748 return retval; | 758 return retval; |
749 } | 759 } |
750 | 760 |
761 /* | |
762 %!error <Invalid call to strncmp.*> strncmp (); | |
763 %!error <Invalid call to strncmp.*> strncmp ("abc", "def"); | |
764 %!assert (strncmp ("abce", "abc", 3) == 1) | |
765 %!assert (strncmp (100, 100, 1) == 0) | |
766 %!assert (all (strncmp ("abce", {"abcd", "bca", "abc"}, 3) == [1, 0, 1])) | |
767 %!assert (all (strncmp ("abc", {"abcd", "bca", "abc"}, 4) == [0, 0, 0])) | |
768 %!assert (all (strncmp ({"abcd", "bca", "abc"},"abce", 3) == [1, 0, 1])) | |
769 %!assert (all (strncmp ({"abcd", "bca", "abc"},{"abcd", "bca", "abe"}, 3) == [1, 1, 0])) | |
770 */ | |
771 | |
772 | |
751 DEFUN (list_in_columns, args, , | 773 DEFUN (list_in_columns, args, , |
752 "-*- texinfo -*-\n\ | 774 "-*- texinfo -*-\n\ |
753 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\ | 775 @deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\ |
754 Return a string containing the elements of @var{arg} listed in\n\ | 776 Return a string containing the elements of @var{arg} listed in\n\ |
755 columns with an overall maximum width of @var{width}. The argument\n\ | 777 columns with an overall maximum width of @var{width}. The argument\n\ |
756 @var{arg} must be a cell array of character strings or a character array.\n\ | 778 @var{arg} must be a cell array of character strings or a character array.\n\ |
757 If @var{width} is not specified, the width of the terminal screen is used.\n\ | 779 If @var{width} is not specified, the width of the terminal screen is used.\n\ |
780 Newline characters are used to break the lines in the output string.\n\ | |
781 For example:\n\ | |
782 \n\ | |
783 @example\n\ | |
784 @group\n\ | |
785 list_in_columns (@{\"abc\", \"def\", \"ghijkl\", \"mnop\", \"qrs\", \"tuv\"@}, 20)\n\ | |
786 @result{} ans = abc mnop\n\ | |
787 def qrs\n\ | |
788 ghijkl tuv\n\ | |
789 \n\ | |
790 whos ans\n\ | |
791 @result{}\n\ | |
792 Variables in the current scope:\n\ | |
793 \n\ | |
794 Attr Name Size Bytes Class\n\ | |
795 ==== ==== ==== ===== =====\n\ | |
796 ans 1x37 37 char\n\ | |
797 \n\ | |
798 Total is 37 elements using 37 bytes\n\ | |
799 @end group\n\ | |
800 @end example\n\ | |
801 \n\ | |
758 @seealso{terminal_size}\n\ | 802 @seealso{terminal_size}\n\ |
759 @end deftypefn") | 803 @end deftypefn") |
760 { | 804 { |
761 octave_value retval; | 805 octave_value retval; |
762 | 806 |
793 | 837 |
794 return retval; | 838 return retval; |
795 } | 839 } |
796 | 840 |
797 /* | 841 /* |
842 %!error <Invalid call to list_in_columns.*> list_in_columns (); | |
843 %!error <Invalid call to list_in_columns.*> list_in_columns (["abc", "def"], 20, 2); | |
844 %!error <invalid conversion from string to real scalar.*> list_in_columns (["abc", "def"], "a"); | |
845 %!test | |
846 %! input = {"abc", "def", "ghijkl", "mnop", "qrs", "tuv"}; | |
847 %! result = "abc mnop\ndef qrs\nghijkl tuv\n"; | |
848 %! assert (list_in_columns (input, 20) == result); | |
849 %!test | |
850 %! input = ["abc"; "def"; "ghijkl"; "mnop"; "qrs"; "tuv"]; | |
851 %! result = "abc mnop \ndef qrs \nghijkl tuv \n"; | |
852 %! assert (list_in_columns (input, 20) == result); | |
853 */ | |
854 | |
855 /* | |
798 ;;; Local Variables: *** | 856 ;;; Local Variables: *** |
799 ;;; mode: C++ *** | 857 ;;; mode: C++ *** |
800 ;;; End: *** | 858 ;;; End: *** |
801 */ | 859 */ |