comparison src/strfns.cc @ 4358:83d4452bc522

[project @ 2003-02-23 02:16:53 by jwe]
author jwe
date Sun, 23 Feb 2003 02:16:54 +0000
parents ccfdb55c8156
children d7d9ca19960a
comparison
equal deleted inserted replaced
4357:d700cfed902a 4358:83d4452bc522
33 #include "gripes.h" 33 #include "gripes.h"
34 #include "ov.h" 34 #include "ov.h"
35 #include "oct-obj.h" 35 #include "oct-obj.h"
36 #include "utils.h" 36 #include "utils.h"
37 37
38 DEFUN (char, args, ,
39 "-*- texinfo -*-\n\
40 @deftypefn {Built-in Function} {} char (@var{x})\n\
41 @deftypefnx {Built-in Function} {} char (@var{cell_array})\n\
42 @deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\
43 Create a string array from a numeric matrix, cell array, or list of\n\
44 \n\
45 If the argument is a numeric matrix, each element of the matrix is\n\
46 converted to the corresponding ASCII character. For example,\n\
47 \n\
48 @example\n\
49 @group\n\
50 setstr ([97, 98, 99])\n\
51 @result{} \"abc\"\n\
52 @end group\n\
53 @end example\n\
54 \n\
55 If the argument is a cell array of strings, the result is a string array\n\
56 with each element corresponding to one element of the cell array.\n\
57 \n\
58 For multiple string arguments, the result is a string array with each\n\
59 element corresponding to the arguments.\n\
60 \n\
61 The returned values are padded with blanks as needed to make each row\n\
62 of the string array have the same length.\n\
63 @end deftypefn")
64 {
65 octave_value retval;
66
67 int nargin = args.length ();
68
69 if (nargin == 1)
70 retval = args(0).convert_to_str (true);
71 else if (nargin > 1)
72 {
73 int n_elts = 0;
74
75 int max_len = 0;
76
77 for (int i = 0; i < nargin; i++)
78 {
79 string_vector s = args(i).all_strings ();
80
81 if (error_state)
82 {
83 error ("char: expecting arguments to be strings");
84 return retval;
85 }
86
87 n_elts += s.length ();
88
89 int s_max_len = s.max_length ();
90
91 if (s_max_len > max_len)
92 max_len = s_max_len;
93 }
94
95 string_vector result (n_elts);
96
97 int k = 0;
98
99 for (int i = 0; i < nargin; i++)
100 {
101 string_vector s = args(i).all_strings ();
102
103 int n = s.length ();
104
105 for (int j = 0; j < n; j++)
106 {
107 std::string t = s[j];
108 int t_len = t.length ();
109
110 if (max_len > t_len)
111 t += std::string (max_len - t_len, ' ');
112
113 result[k++] = t;
114 }
115 }
116
117 retval = result;
118 }
119 else
120 print_usage ("char");
121
122 return retval;
123 }
124
38 DEFUN (isstr, args, , 125 DEFUN (isstr, args, ,
39 "-*- texinfo -*-\n\ 126 "-*- texinfo -*-\n\
40 @deftypefn {Built-in Function} {} isstr (@var{a})\n\ 127 @deftypefn {Built-in Function} {} isstr (@var{a})\n\
41 Return 1 if @var{a} is a string. Otherwise, return 0.\n\ 128 Return 1 if @var{a} is a string. Otherwise, return 0.\n\
42 @end deftypefn") 129 @end deftypefn")
51 print_usage ("isstr"); 138 print_usage ("isstr");
52 139
53 return retval; 140 return retval;
54 } 141 }
55 142
56 DEFUN (setstr, args, ,
57 "-*- texinfo -*-\n\
58 @deftypefn {Built-in Function} {} setstr (@var{x})\n\
59 Convert a matrix to a string. Each element of the matrix is converted\n\
60 to the corresponding ASCII \n\
61 character. For example,\n\
62 \n\
63 @example\n\
64 @group\n\
65 setstr ([97, 98, 99])\n\
66 @result{} \"abc\"\n\
67 @end group\n\
68 @end example\n\
69 @end deftypefn")
70 {
71 octave_value retval;
72
73 int nargin = args.length ();
74
75 if (nargin == 1 && args(0).is_defined ())
76 retval = args(0).convert_to_str ();
77 else
78 print_usage ("setstr");
79
80 return retval;
81 }
82
83 /* 143 /*
84 ;;; Local Variables: *** 144 ;;; Local Variables: ***
85 ;;; mode: C++ *** 145 ;;; mode: C++ ***
86 ;;; End: *** 146 ;;; End: ***
87 */ 147 */