Mercurial > hg > octave-nkf
changeset 4257:e556870ce8f0
[project @ 2003-01-02 03:14:41 by jwe]
author | jwe |
---|---|
date | Thu, 02 Jan 2003 03:14:42 +0000 |
parents | 2b9c6dc25449 |
children | b8a4496d7b24 |
files | scripts/ChangeLog scripts/strings/strcmp.m src/ChangeLog src/file-io.cc src/oct-stream.cc src/ov-base.cc src/ov.cc src/ov.h src/pt-mat.cc |
diffstat | 9 files changed, 227 insertions(+), 88 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,7 @@ +2003-01-01 John W. Eaton <jwe@bevo.che.wisc.edu> + + * strings/strcmp.m: Handle cell arrays of strings. + 2002-12-18 John W. Eaton <jwe@bevo.che.wisc.edu> * general/int2str.m: Handle matrices.
--- a/scripts/strings/strcmp.m +++ b/scripts/strings/strcmp.m @@ -28,21 +28,116 @@ ## Author: jwe -function status = strcmp (s1, s2) +function retval = strcmp (s1, s2) if (nargin != 2) usage ("strcmp (s, t)"); endif - status = 0; - if (isstr (s1) && isstr(s2)) + retval = 0; + + if (isstr (s1)) + [r1, c1] = size (s1); + if (isstr (s2)) + [r2, c2] = size (s2); + if (r1 == r2 && c1 == c2) + if (c1 == 0) + retval = 1; + else + retval = all (all (s1 == s2)); + endif + endif + elseif (iscell (s2)) + [r2, c2] = size (s2); + if (r1 == 1) + t2 = s2(:); + n = length (t2); + retval = zeros (n, 1); + for i = 1:n + retval(i) = strcmp (s1, t2{i}); + endfor + retval = reshape (retval, r2, c2); + elseif (r1 > 1) + if (r2 == 1 && c2 == 1) + t2 = s2{1}; + retval = zeros (r1, 1); + for i = 1:r1 + retval(i) = strcmp (deblank (s1(i,:)), t2); + endfor + else + t2 = s2(:); + n = length (t2); + if (n == r1) + retval = zeros (n, 1); + for i = 1:n + retval(i) = strcmp (deblank (s1(i,:)), t2{i}); + endfor + retval = reshape (retval, r2, c2); + endif + endif + endif + endif + elseif (iscell (s1)) [r1, c1] = size (s1); - [r2, c2] = size (s2); - if (r1 == r2 && c1 == c2) - if (c1 == 0) - status = 1; + if (isstr (s2)) + [r2, c2] = size (s2); + if (r2 == 1) + t1 = s1(:); + n = length (t1); + retval = zeros (n, 1); + for i = 1:n + retval(i) = strcmp (t1{i}, s2); + endfor + retval = reshape (retval, r1, c1); + elseif (r2 > 1) + if (r1 == 1 && c1 == 1) + t1 = s1{1}; + retval = zeros (r2, 1); + for i = 1:r2 + retval(i) = strcmp (t1, deblank (s2(i,:))); + endfor + else + t1 = s1(:); + n = length (t1); + if (n == r2) + retval = zeros (n, 1); + for i = 1:n + retval(i) = strcmp (t2{i}, deblank (s2(i,:))); + endfor + retval = reshape (retval, r1, c1); + endif + endif + endif + elseif (iscell (s2)) + [r2, c2] = size (s2); + if (r1 == 1 && c1 == 1) + t1 = s1{:}; + t2 = s2(:); + n = length (t2); + retval = zeros (n, 1); + for i = 1:n + retval(i) = strcmp (t1, t2{i}); + endfor + retval = reshape (retval, r2, c2); + elseif (r2 == 1 && c2 == 1) + t1 = s1(:); + t2 = s2{:}; + n = length (t1); + retval = zeros (n, 1); + for i = 1:n + retval(i) = strcmp (t1{i}, t2); + endfor + retval = reshape (retval, r1, c1); + elseif (r1 == r2 && c1 == c2) + t1 = s1(:); + t2 = s2(:); + n = length (t1); + for i = 1:n + retval(i) = strcmp (t1{i}, t2{i}); + endfor + retval = reshape (retval, r1, c1); else - status = all (all (s1 == s2)); + error ("strcmp: nonconformant cell arrays"); endif endif endif
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,21 @@ +2003-01-01 John W. Eaton <jwe@bevo.che.wisc.edu> + + * ov-base.cc (octave_base_value::char_matrix_value, + octave_base_value::all_strings, octave_base_value::string_value): + Attempt conversions here if Vimplicit_num_to_num_ok is true. + + * ov.cc (Vimplicit_num_to_str_ok): Move here from pt-mat.cc and + make extern. + * ov.h: Provide decl. + + * oct-stream.cc (printf_value_cache::looking_at_string): Delete. + (printf_value_cache::string_value): Just attempt conversion. + (octave_base_stream::do_printf): When doing '%s' format, + just attempt converstion. + + * file-io.cc (Ffread): Allow size to be omitted even when + additional arguments are given. + 2002-12-31 John W. Eaton <jwe@bevo.che.wisc.edu> * pt-arg-list.cc (F__end__): Fail if rows or columns is negative.
--- a/src/file-io.cc +++ b/src/file-io.cc @@ -1184,17 +1184,24 @@ if (! error_state) { - octave_value size = (nargin > 1) - ? args(1) : octave_value (lo_ieee_inf_value ()); + octave_value size = lo_ieee_inf_value (); + octave_value prec = "uchar"; + octave_value skip = 0; + octave_value arch = "unknown"; - octave_value prec = (nargin > 2) - ? args(2) : octave_value ("uchar"); + int idx = 1; - octave_value skip = (nargin > 3) - ? args(3) : octave_value (0.0); + if (nargin > 1 && ! args(idx).is_string ()) + size = args(idx++); + + if (nargin > idx) + prec = args(idx++); - octave_value arch = (nargin > 4) - ? args(4) : octave_value ("unknown"); + if (nargin > idx) + skip = args(idx++); + + if (nargin > idx) + arch = args(idx++); int count = -1;
--- a/src/oct-stream.cc +++ b/src/oct-stream.cc @@ -2041,8 +2041,6 @@ bool exhausted (void) { return (val_idx >= n_vals); } - bool looking_at_string (void); - private: const octave_value_list values; @@ -2065,19 +2063,6 @@ printf_value_cache& operator = (const printf_value_cache&); }; -bool -printf_value_cache::looking_at_string (void) -{ - bool retval = false; - - int idx = (elt_idx == 0) ? val_idx : -1; - - if (idx >= 0 && idx < n_vals) - retval = values(idx).is_string (); - - return retval; -} - double printf_value_cache::double_value (void) { @@ -2158,31 +2143,29 @@ { std::string retval; - if (looking_at_string ()) + octave_value tval = values (val_idx++); + + if (tval.rows () == 1) + retval = tval.string_value (); + else { - octave_value tval = values (val_idx++); - - if (tval.rows () == 1) - retval = tval.string_value (); - else - { - // In the name of Matlab compatibility. - - charMatrix chm = tval.char_matrix_value (); - - int nr = chm.rows (); - int nc = chm.columns (); - - int k = 0; - - retval.resize (nr * nc, '\0'); - - for (int j = 0; j < nc; j++) - for (int i = 0; i < nr; i++) - retval[k++] = chm(i,j); - } + // In the name of Matlab compatibility. + + charMatrix chm = tval.char_matrix_value (); + + int nr = chm.rows (); + int nc = chm.columns (); + + int k = 0; + + retval.resize (nr * nc, '\0'); + + for (int j = 0; j < nc; j++) + for (int i = 0; i < nr; i++) + retval[k++] = chm(i,j); } - else + + if (error_state) curr_state = conversion_error; return retval; @@ -2298,7 +2281,7 @@ os << elt->text; retval += strlen (elt->text); } - else if (elt->type == 's' && val_cache.looking_at_string ()) + else if (elt->type == 's') { std::string val = val_cache.string_value ();
--- a/src/ov-base.cc +++ b/src/ov-base.cc @@ -301,8 +301,17 @@ octave_base_value::char_matrix_value (bool) const { charMatrix retval; - gripe_wrong_type_arg ("octave_base_value::char_matrix_value()", - type_name ()); + + if (Vimplicit_num_to_str_ok) + { + octave_value tmp = convert_to_str (); + + if (! error_state) + retval = tmp.char_matrix_value (); + } + else + gripe_wrong_type_arg ("octave_base_value::char_matrix_value()", + type_name ()); return retval; } @@ -310,7 +319,17 @@ octave_base_value::all_strings (void) const { string_vector retval; - gripe_wrong_type_arg ("octave_base_value::all_strings()", type_name ()); + + if (Vimplicit_num_to_str_ok) + { + octave_value tmp = convert_to_str (); + + if (! error_state) + retval = tmp.all_strings (); + } + else + gripe_wrong_type_arg ("octave_base_value::all_strings()", type_name ()); + return retval; } @@ -318,7 +337,17 @@ octave_base_value::string_value (void) const { std::string retval; - gripe_wrong_type_arg ("octave_base_value::string_value()", type_name ()); + + if (Vimplicit_num_to_str_ok) + { + octave_value tmp = convert_to_str (); + + if (! error_state) + retval = tmp.string_value (); + } + else + gripe_wrong_type_arg ("octave_base_value::string_value()", type_name ()); + return retval; }
--- a/src/ov.cc +++ b/src/ov.cc @@ -80,6 +80,9 @@ // for A already defined and a matrix type. bool Vdo_fortran_indexing; +// Should `[97, 98, 99, "123"]' be a string? +bool Vimplicit_num_to_str_ok; + // Should we allow things like: // // octave> 'abc' + 0 @@ -1723,6 +1726,14 @@ } static int +implicit_num_to_str_ok (void) +{ + Vimplicit_num_to_str_ok = check_preference ("implicit_num_to_str_ok"); + + return 0; +} + +static int implicit_str_to_num_ok (void) { Vimplicit_str_to_num_ok = check_preference ("implicit_str_to_num_ok"); @@ -1821,6 +1832,24 @@ the matrix. The default value is 0. \n\ @end defvr"); + DEFVAR (implicit_num_to_str_ok, false, implicit_num_to_str_ok, + "-*- texinfo -*-\n\ +@defvr {Built-in Variable} implicit_num_to_str_ok\n\ +If the value of @code{implicit_num_to_str_ok} is nonzero, implicit\n\ +conversions of numbers to their ASCII character equivalents are\n\ +allowed when strings are constructed using a mixture of strings and\n\ +numbers in matrix notation. Otherwise, an error message is printed and\n\ +control is returned to the top level. The default value is 0. For\n\ +example,\n\ +\n\ +@example\n\ +@group\n\ +[ \"f\", 111, 111 ]\n\ + @result{} \"foo\"\n\ +@end group\n\ +@end example\n\ +@end defvr"); + DEFVAR (implicit_str_to_num_ok, false, implicit_str_to_num_ok, "-*- texinfo -*-\n\ @defvr {Built-in Variable} implicit_str_to_num_ok\n\
--- a/src/ov.h +++ b/src/ov.h @@ -704,6 +704,9 @@ // for A already defined and a matrix type. extern bool Vdo_fortran_indexing; +// Should `[97, 98, 99, "123"]' be a string? +extern bool Vimplicit_num_to_str_ok; + // Should we allow things like: // // octave> 'abc' + 0
--- a/src/pt-mat.cc +++ b/src/pt-mat.cc @@ -48,9 +48,6 @@ // Zero means it should be considered an error. static int Vempty_list_elements_ok; -// Should `[97, 98, 99, "123"]' be a string? -static bool Vimplicit_num_to_str_ok; - // The character to fill with when creating string arrays. char Vstring_fill_char = ' '; @@ -585,14 +582,6 @@ } static int -implicit_num_to_str_ok (void) -{ - Vimplicit_num_to_str_ok = check_preference ("implicit_num_to_str_ok"); - - return 0; -} - -static int string_fill_char (void) { int status = 0; @@ -640,24 +629,6 @@ The default value is 1.\n\ @end defvr"); - DEFVAR (implicit_num_to_str_ok, false, implicit_num_to_str_ok, - "-*- texinfo -*-\n\ -@defvr {Built-in Variable} implicit_num_to_str_ok\n\ -If the value of @code{implicit_num_to_str_ok} is nonzero, implicit\n\ -conversions of numbers to their ASCII character equivalents are\n\ -allowed when strings are constructed using a mixture of strings and\n\ -numbers in matrix notation. Otherwise, an error message is printed and\n\ -control is returned to the top level. The default value is 0. For\n\ -example,\n\ -\n\ -@example\n\ -@group\n\ -[ \"f\", 111, 111 ]\n\ - @result{} \"foo\"\n\ -@end group\n\ -@end example\n\ -@end defvr"); - DEFVAR (string_fill_char, " ", string_fill_char, "-*- texinfo -*-\n\ @defvr {Built-in Variable} string_fill_char\n\