Mercurial > hg > octave-lyh
changeset 4358:83d4452bc522
[project @ 2003-02-23 02:16:53 by jwe]
author | jwe |
---|---|
date | Sun, 23 Feb 2003 02:16:54 +0000 |
parents | d700cfed902a |
children | 77605935a783 |
files | doc/interpreter/container.txi doc/interpreter/strings.txi doc/interpreter/struct.txi scripts/ChangeLog scripts/deprecated/setstr.m scripts/deprecated/struct_contains.m scripts/deprecated/struct_elements.m src/ChangeLog src/data.cc src/ov-base.cc src/ov-base.h src/ov-bool-mat.h src/ov-bool.cc src/ov-bool.h src/ov-cell.cc src/ov-cell.h src/ov-ch-mat.h src/ov-range.cc src/ov-range.h src/ov-re-mat.cc src/ov-re-mat.h src/ov-scalar.cc src/ov-scalar.h src/ov-str-mat.cc src/ov-str-mat.h src/ov-struct.cc src/ov.h src/strfns.cc |
diffstat | 28 files changed, 383 insertions(+), 141 deletions(-) [+] |
line wrap: on
line diff
--- a/doc/interpreter/container.txi +++ b/doc/interpreter/container.txi @@ -33,4 +33,6 @@ @DOCSTRING(cell) +@DOCSTRING(cellstr) + @DOCSTRING(iscell)
--- a/doc/interpreter/strings.txi +++ b/doc/interpreter/strings.txi @@ -127,12 +127,12 @@ @DOCSTRING(blanks) +@DOCSTRING(char) + @DOCSTRING(int2str) @DOCSTRING(com2str) -@DOCSTRING(setstr) - @DOCSTRING(strcat) @DOCSTRING(string_fill_char)
--- a/doc/interpreter/struct.txi +++ b/doc/interpreter/struct.txi @@ -183,6 +183,6 @@ @DOCSTRING(isstruct) -@DOCSTRING(struct_contains) +@DOCSTRING(fieldnames) -@DOCSTRING(struct_elements) +@DOCSTRING(isfield)
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2003-02-22 John W. Eaton <jwe@bevo.che.wisc.edu> + + * deprecated/struct_contains.m, deprecated/struct_elements.m: + New files. + 2003-02-20 John W. Eaton <jwe@bevo.che.wisc.edu> * statistics/tests/kolmogorov_smirnov_test.m: Use str2func to make
new file mode 100644 --- /dev/null +++ b/scripts/deprecated/setstr.m @@ -0,0 +1,31 @@ +## Copyright (C) 2003 John W. Eaton +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2, or (at your option) +## any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, write to the Free +## Software Foundation, 59 Temple Place - Suite 330, Boston, MA +## 02111-1307, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} setstr (@var{s}) +## This function has been deprecated. Use char instead. +## @end deftypefn + +## Author: jwe + +function retval = setstr (varargin) + + retval = char (varargin{:}); + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/deprecated/struct_contains.m @@ -0,0 +1,31 @@ +## Copyright (C) 2003 John W. Eaton +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2, or (at your option) +## any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, write to the Free +## Software Foundation, 59 Temple Place - Suite 330, Boston, MA +## 02111-1307, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} struct_contains (@var{expr}, @var{name}) +## This function has been deprecated. Use isfield instead. +## @end deftypefn + +## Author: jwe + +function retval = struct_contains (varargin) + + retval = isfield (varargin{:}); + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/deprecated/struct_elements.m @@ -0,0 +1,31 @@ +## Copyright (C) 2003 John W. Eaton +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2, or (at your option) +## any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, write to the Free +## Software Foundation, 59 Temple Place - Suite 330, Boston, MA +## 02111-1307, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} struct_elements (@var{struct}) +## This function has been deprecated. Use fieldnames instead. +## @end deftypefn + +## Author: jwe + +function retval = struct_elements (varargin) + + retval = char (fieldnames (varargin{:})); + +endfunction
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,18 @@ +2003-02-22 John W. Eaton <jwe@bevo.che.wisc.edu> + + * strfns.cc (Fchar): New function. + + * ov-cell.cc (octave_cell::all_strings): Allow elements to be + string arrays. + * ov.cc (octave_value::all_strings, octave_value::convert_to_str): + New optional arg, pad. Change all derived classes. + + * ov-struct.cc (Ffieldnames, Fisfield): Move here from data.cc. + + * data.cc (Ffieldnames): Rename from Fstruct_elements. Return + cell array instead of a string array. + (Fisfield): Rename from struct_contains. + 2003-02-22 Paul Kienzle <pkienzle@users.sf.net> * toplev.cc: Put config info in array of strings, then convert to map.
--- a/src/data.cc +++ b/src/data.cc @@ -36,7 +36,6 @@ #include "defun.h" #include "error.h" #include "gripes.h" -#include "oct-map.h" #include "ov.h" #include "ov-re-nd-array.h" #include "variables.h" @@ -930,86 +929,6 @@ return retval; } -DEFUN (isstruct, args, , - "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {} isstruct (@var{expr})\n\ -Return 1 if the value of the expression @var{expr} is a structure.\n\ -@end deftypefn") -{ - octave_value retval; - - if (args.length () == 1) - retval = args(0).is_map (); - else - print_usage ("isstruct"); - - return retval; -} - -DEFUN (struct_elements, args, , - "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {} struct_elements (@var{struct})\n\ -Return a list of strings naming the elements of the structure\n\ -@var{struct}. It is an error to call @code{struct_elements} with an\n\ -argument that is not a structure.\n\ -@end deftypefn") -{ - octave_value retval; - - int nargin = args.length (); - - if (nargin == 1) - { - if (args (0).is_map ()) - { - Octave_map m = args(0).map_value (); - retval = m.keys (); - } - else - gripe_wrong_type_arg ("struct_elements", args (0)); - } - else - print_usage ("struct_elements"); - - return retval; -} - -DEFUN (struct_contains, args, , - "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {} struct_contains (@var{expr}, @var{name})\n\ -Return 1 if the expression @var{expr} is a structure and it includes an\n\ -element named @var{name}. The first argument must be a structure and\n\ -the second must be a string.\n\ -@end deftypefn") -{ - octave_value retval; - - int nargin = args.length (); - - if (nargin == 2) - { - retval = false; - - // XXX FIXME XXX -- should this work for all types that can do - // structure reference operations? - - if (args(0).is_map () && args(1).is_string ()) - { - std::string key = args(1).string_value (); - - Octave_map m = args(0).map_value (); - - retval = m.contains (key); - } - else - print_usage ("struct_contains"); - } - else - print_usage ("struct_contains"); - - return retval; -} - static octave_value fill_matrix (const octave_value_list& args, double val, const char *fcn) {
--- a/src/ov-base.cc +++ b/src/ov-base.cc @@ -155,7 +155,7 @@ } octave_value -octave_base_value::convert_to_str (void) const +octave_base_value::convert_to_str (bool) const { gripe_wrong_type_arg ("octave_base_value::convert_to_str ()", type_name ()); @@ -317,13 +317,13 @@ } string_vector -octave_base_value::all_strings (void) const +octave_base_value::all_strings (bool pad) const { string_vector retval; if (Vimplicit_num_to_str_ok) { - octave_value tmp = convert_to_str (); + octave_value tmp = convert_to_str (pad); if (! error_state) retval = tmp.all_strings ();
--- a/src/ov-base.h +++ b/src/ov-base.h @@ -195,7 +195,7 @@ charMatrix char_matrix_value (bool = false) const; - string_vector all_strings (void) const; + string_vector all_strings (bool pad = false) const; std::string string_value (void) const; @@ -219,7 +219,7 @@ boolMatrix bool_matrix_value (void) const; - octave_value convert_to_str (void) const; + octave_value convert_to_str (bool pad = false) const; void convert_to_row_or_column_vector (void);
--- a/src/ov-bool-mat.h +++ b/src/ov-bool-mat.h @@ -98,7 +98,7 @@ boolMatrix bool_matrix_value (void) const { return matrix; } - octave_value convert_to_str (void) const + octave_value convert_to_str (bool = false) const { return octave_value (matrix); } protected:
--- a/src/ov-bool.cc +++ b/src/ov-bool.cc @@ -103,10 +103,10 @@ } octave_value -octave_bool::convert_to_str (void) const +octave_bool::convert_to_str (bool) const { char s[2]; - s[0] = (char) scalar; + s[0] = static_cast<char> (scalar); s[1] = '\0'; return octave_value (s);
--- a/src/ov-bool.h +++ b/src/ov-bool.h @@ -102,7 +102,7 @@ boolMatrix bool_matrix_value (void) const { return boolMatrix (1, 1, scalar); } - octave_value convert_to_str (void) const; + octave_value convert_to_str (bool pad = false) const; private:
--- a/src/ov-cell.cc +++ b/src/ov-cell.cc @@ -249,12 +249,39 @@ } string_vector -octave_cell::all_strings (void) const +octave_cell::all_strings (bool pad) const { + string_vector retval; + int nr = rows (); int nc = columns (); - string_vector retval (nr * nc); + int n_elts = 0; + + int max_len = 0; + + if (pad) + { + for (int j = 0; j < nc; j++) + { + for (int i = 0; i < nr; i++) + { + string_vector s = matrix(i,j).all_strings (); + + if (error_state) + return retval; + + n_elts += s.length (); + + int s_max_len = s.max_length (); + + if (s_max_len > max_len) + max_len = s_max_len; + } + } + } + + retval.resize (n_elts); int k = 0; @@ -262,13 +289,25 @@ { for (int i = 0; i < nr; i++) { - retval[k++] = matrix(i,j).string_value (); + string_vector s = matrix(i,j).all_strings (); + + int n = s.length (); - if (error_state) - return string_vector (); + for (int ii = 0; ii < n; ii++) + { + std::string t = s[ii]; + int t_len = t.length (); + + if (pad && max_len > t_len) + t += std::string (max_len - t_len, ' '); + + retval[k++] = t; + } } } + + return retval; } @@ -406,6 +445,30 @@ return retval; } +DEFUN (cellstr, args, , + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {} cellstr (@var{string})\n\ +Create a new cell array object from the elements of the string\n\ +array @var{string}.\n\ +@end deftypefn") +{ + octave_value retval; + + if (args.length () == 1) + { + string_vector s = args(0).all_strings (); + + if (! error_state) + retval = Cell (s); + else + error ("cellstr: expecting argument to be a string"); + } + else + print_usage ("cellstr"); + + return retval; +} + /* ;;; Local Variables: *** ;;; mode: C++ ***
--- a/src/ov-cell.h +++ b/src/ov-cell.h @@ -96,7 +96,10 @@ octave_value_list list_value (void) const; - string_vector all_strings (void) const; + octave_value convert_to_str (bool pad = false) const + { return all_strings (pad); } + + string_vector all_strings (bool pad = false) const; void print (std::ostream& os, bool pr_as_read_syntax = false) const;
--- a/src/ov-ch-mat.h +++ b/src/ov-ch-mat.h @@ -101,7 +101,7 @@ charMatrix char_matrix_value (bool = false) const { return matrix; } - octave_value convert_to_str (void) const + octave_value convert_to_str (bool = false) const { return octave_value (matrix, true); } protected:
--- a/src/ov-range.cc +++ b/src/ov-range.cc @@ -205,10 +205,10 @@ } octave_value -octave_range::convert_to_str (void) const +octave_range::convert_to_str (bool pad) const { octave_value tmp (range.matrix_value ()); - return tmp.convert_to_str (); + return tmp.convert_to_str (pad); } void
--- a/src/ov-range.h +++ b/src/ov-range.h @@ -154,7 +154,7 @@ Range range_value (void) const { return range; } - octave_value convert_to_str (void) const; + octave_value convert_to_str (bool pad = false) const; void print (std::ostream& os, bool pr_as_read_syntax = false) const;
--- a/src/ov-re-mat.cc +++ b/src/ov-re-mat.cc @@ -112,7 +112,7 @@ } octave_value -octave_matrix::convert_to_str (void) const +octave_matrix::convert_to_str (bool) const { octave_value retval;
--- a/src/ov-re-mat.h +++ b/src/ov-re-mat.h @@ -102,7 +102,7 @@ void decrement (void) { matrix -= 1.0; } - octave_value convert_to_str (void) const; + octave_value convert_to_str (bool pad = false) const; private:
--- a/src/ov-scalar.cc +++ b/src/ov-scalar.cc @@ -77,7 +77,7 @@ } octave_value -octave_scalar::convert_to_str (void) const +octave_scalar::convert_to_str (bool) const { octave_value retval;
--- a/src/ov-scalar.h +++ b/src/ov-scalar.h @@ -94,7 +94,7 @@ ComplexMatrix complex_matrix_value (bool = false) const { return ComplexMatrix (1, 1, Complex (scalar)); } - octave_value convert_to_str (void) const; + octave_value convert_to_str (bool pad = false) const; void increment (void) { ++scalar; }
--- a/src/ov-str-mat.cc +++ b/src/ov-str-mat.cc @@ -169,7 +169,7 @@ } string_vector -octave_char_matrix_str::all_strings (void) const +octave_char_matrix_str::all_strings (bool) const { int n = matrix.rows ();
--- a/src/ov-str-mat.h +++ b/src/ov-str-mat.h @@ -100,7 +100,7 @@ Matrix matrix_value (bool = false) const; - string_vector all_strings (void) const; + string_vector all_strings (bool pad = false) const; std::string string_value (void) const;
--- a/src/ov-struct.cc +++ b/src/ov-struct.cc @@ -31,7 +31,9 @@ #include <iostream> #include "Cell.h" +#include "defun.h" #include "error.h" +#include "gripes.h" #include "oct-lvalue.h" #include "ov-list.h" #include "ov-struct.h" @@ -467,6 +469,86 @@ return retval; } +DEFUN (isstruct, args, , + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {} isstruct (@var{expr})\n\ +Return 1 if the value of the expression @var{expr} is a structure.\n\ +@end deftypefn") +{ + octave_value retval; + + if (args.length () == 1) + retval = args(0).is_map (); + else + print_usage ("isstruct"); + + return retval; +} + +DEFUN (fieldnames, args, , + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {} fieldnames (@var{struct})\n\ +Return a cell array of strings naming the elements of the structure\n\ +@var{struct}. It is an error to call @code{fieldnames} with an\n\ +argument that is not a structure.\n\ +@end deftypefn") +{ + octave_value retval; + + int nargin = args.length (); + + if (nargin == 1) + { + if (args(0).is_map ()) + { + Octave_map m = args(0).map_value (); + retval = Cell (m.keys ()); + } + else + gripe_wrong_type_arg ("fieldnames", args(0)); + } + else + print_usage ("fieldnames"); + + return retval; +} + +DEFUN (isfield, args, , + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {} isfield (@var{expr}, @var{name})\n\ +Return true if the expression @var{expr} is a structure and it includes an\n\ +element named @var{name}. The first argument must be a structure and\n\ +the second must be a string.\n\ +@end deftypefn") +{ + octave_value retval; + + int nargin = args.length (); + + if (nargin == 2) + { + retval = false; + + // XXX FIXME XXX -- should this work for all types that can do + // structure reference operations? + + if (args(0).is_map () && args(1).is_string ()) + { + std::string key = args(1).string_value (); + + Octave_map m = args(0).map_value (); + + retval = m.contains (key) != 0; + } + else + print_usage ("isfield"); + } + else + print_usage ("isfield"); + + return retval; +} + /* ;;; Local Variables: *** ;;; mode: C++ ***
--- a/src/ov.h +++ b/src/ov.h @@ -478,8 +478,8 @@ virtual charMatrix char_matrix_value (bool frc_str_conv = false) const { return rep->char_matrix_value (frc_str_conv); } - virtual string_vector all_strings (void) const - { return rep->all_strings (); } + virtual string_vector all_strings (bool pad = false) const + { return rep->all_strings (pad); } virtual std::string string_value (void) const { return rep->string_value (); } @@ -536,8 +536,8 @@ // class wants a certain kind of constant, he should simply ask for // it, and we should convert it if possible. - virtual octave_value convert_to_str (void) const - { return rep->convert_to_str (); } + virtual octave_value convert_to_str (bool pad = false) const + { return rep->convert_to_str (pad); } virtual void convert_to_row_or_column_vector (void) { rep->convert_to_row_or_column_vector (); }
--- a/src/strfns.cc +++ b/src/strfns.cc @@ -35,6 +35,93 @@ #include "oct-obj.h" #include "utils.h" +DEFUN (char, args, , + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {} char (@var{x})\n\ +@deftypefnx {Built-in Function} {} char (@var{cell_array})\n\ +@deftypefnx {Built-in Function} {} char (@var{s1}, @var{s2}, @dots{})\n\ +Create a string array from a numeric matrix, cell array, or list of\n\ +\n\ +If the argument is a numeric matrix, each element of the matrix is\n\ +converted to the corresponding ASCII character. For example,\n\ +\n\ +@example\n\ +@group\n\ +setstr ([97, 98, 99])\n\ + @result{} \"abc\"\n\ +@end group\n\ +@end example\n\ +\n\ +If the argument is a cell array of strings, the result is a string array\n\ +with each element corresponding to one element of the cell array.\n\ +\n\ +For multiple string arguments, the result is a string array with each\n\ +element corresponding to the arguments.\n\ +\n\ +The returned values are padded with blanks as needed to make each row\n\ +of the string array have the same length.\n\ +@end deftypefn") +{ + octave_value retval; + + int nargin = args.length (); + + if (nargin == 1) + retval = args(0).convert_to_str (true); + else if (nargin > 1) + { + int n_elts = 0; + + int max_len = 0; + + for (int i = 0; i < nargin; i++) + { + string_vector s = args(i).all_strings (); + + if (error_state) + { + error ("char: expecting arguments to be strings"); + return retval; + } + + n_elts += s.length (); + + int s_max_len = s.max_length (); + + if (s_max_len > max_len) + max_len = s_max_len; + } + + string_vector result (n_elts); + + int k = 0; + + for (int i = 0; i < nargin; i++) + { + string_vector s = args(i).all_strings (); + + int n = s.length (); + + for (int j = 0; j < n; j++) + { + std::string t = s[j]; + int t_len = t.length (); + + if (max_len > t_len) + t += std::string (max_len - t_len, ' '); + + result[k++] = t; + } + } + + retval = result; + } + else + print_usage ("char"); + + return retval; +} + DEFUN (isstr, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {} isstr (@var{a})\n\ @@ -53,33 +140,6 @@ return retval; } -DEFUN (setstr, args, , - "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {} setstr (@var{x})\n\ -Convert a matrix to a string. Each element of the matrix is converted\n\ -to the corresponding ASCII \n\ -character. For example,\n\ -\n\ -@example\n\ -@group\n\ -setstr ([97, 98, 99])\n\ - @result{} \"abc\"\n\ -@end group\n\ -@end example\n\ -@end deftypefn") -{ - octave_value retval; - - int nargin = args.length (); - - if (nargin == 1 && args(0).is_defined ()) - retval = args(0).convert_to_str (); - else - print_usage ("setstr"); - - return retval; -} - /* ;;; Local Variables: *** ;;; mode: C++ ***