# HG changeset patch # User John W. Eaton # Date 1444341357 14400 # Node ID 729a85dafba8455407734a2f5484557dbe27c5da # Parent 10ec79b47808cc8eaa5454f157f690440cc60a4c use new string_value method to handle value extraction errors * ov-class.cc, ov-classdef.cc, ov-fcn-handle.cc, ov-fcn-inline.cc, ov.cc, pt-idx.cc: Use new string_value method. diff -r 10ec79b47808 -r 729a85dafba8 libinterp/octave-value/ov-class.cc --- a/libinterp/octave-value/ov-class.cc Thu Oct 08 18:15:56 2015 -0400 +++ b/libinterp/octave-value/ov-class.cc Thu Oct 08 17:55:57 2015 -0400 @@ -1856,52 +1856,47 @@ // Called as class constructor octave_function *fcn = octave_call_stack::caller (); - if (args(1).is_string ()) + std::string id = args(1).string_value ("class: ID (class name) must be a string"); + + if (fcn) { - std::string id = args(1).string_value (); - - if (fcn) + if (fcn->is_class_constructor (id) || fcn->is_class_method (id)) { - if (fcn->is_class_constructor (id) || fcn->is_class_method (id)) + octave_map m = args(0).map_value (); + + if (! error_state) { - octave_map m = args(0).map_value (); - - if (! error_state) + if (nargin == 2) + retval + = octave_value (new octave_class + (m, id, std::list ())); + else { - if (nargin == 2) - retval - = octave_value (new octave_class - (m, id, std::list ())); - else - { - octave_value_list parents = args.slice (2, nargin-2); + octave_value_list parents = args.slice (2, nargin-2); - retval - = octave_value (new octave_class (m, id, parents)); - } + retval + = octave_value (new octave_class (m, id, parents)); + } - octave_class::exemplar_const_iterator it - = octave_class::exemplar_map.find (id); + octave_class::exemplar_const_iterator it + = octave_class::exemplar_map.find (id); - if (it == octave_class::exemplar_map.end ()) - octave_class::exemplar_map[id] - = octave_class::exemplar_info (retval); - else if (! it->second.compare (retval)) - error ("class: object of class '%s' does not match previously constructed objects", - id.c_str ()); - } - else - error ("class: expecting structure S as first argument"); + if (it == octave_class::exemplar_map.end ()) + octave_class::exemplar_map[id] + = octave_class::exemplar_info (retval); + else if (! it->second.compare (retval)) + error ("class: object of class '%s' does not match previously constructed objects", + id.c_str ()); } else - error ("class: '%s' is invalid as a class name in this context", - id.c_str ()); + error ("class: expecting structure S as first argument"); } else - error ("class: invalid call from outside class constructor or method"); + error ("class: '%s' is invalid as a class name in this context", + id.c_str ()); } else - error ("class: ID (class name) must be a string"); + error ("class: invalid call from outside class constructor or method"); } return retval; @@ -2187,12 +2182,7 @@ for (int i = 0; i < args.length (); i++) { - std::string inf_class = args(i).string_value (); - if (error_state) - { - error ("superiorto: expecting argument to be class name"); - break; - } + std::string inf_class = args(i).string_value ("superiorto: expecting argument to be class name"); // User defined classes always have higher precedence // than built-in classes @@ -2233,12 +2223,7 @@ for (int i = 0; i < args.length (); i++) { - std::string sup_class = args(i).string_value (); - if (error_state) - { - error ("inferiorto: expecting argument to be class name"); - break; - } + std::string sup_class = args(i).string_value ("inferiorto: expecting argument to be class name"); if (is_built_in_class (sup_class)) { diff -r 10ec79b47808 -r 729a85dafba8 libinterp/octave-value/ov-classdef.cc --- a/libinterp/octave-value/ov-classdef.cc Thu Oct 08 18:15:56 2015 -0400 +++ b/libinterp/octave-value/ov-classdef.cc Thu Oct 08 17:55:57 2015 -0400 @@ -528,12 +528,9 @@ if (args.length () == 1) { - std::string name = args(0).string_value (); - - if (! error_state) - retval(0) = to_ov (lookup_class (name)); - else - error ("fromName: invalid class name, expected a string value"); + std::string name = args(0).string_value ("fromName: invalid class name, expected a string value"); + + retval(0) = to_ov (lookup_class (name)); } else error ("fromName: invalid number of parameters"); @@ -552,26 +549,21 @@ if (! error_state) { - std::string meth_name = args(1).string_value (); - - if (! error_state) + std::string meth_name = args(1).string_value ("fevalStatic: invalid method name, expected a string value"); + + cdef_method meth = cls.find_method (meth_name); + + if (meth.ok ()) { - cdef_method meth = cls.find_method (meth_name); - - if (meth.ok ()) - { - if (meth.is_static ()) - retval = meth.execute (args.splice (0, 2), nargout, - true, "fevalStatic"); - else - error ("fevalStatic: method `%s' is not static", - meth_name.c_str ()); - } + if (meth.is_static ()) + retval = meth.execute (args.splice (0, 2), nargout, + true, "fevalStatic"); else - error ("fevalStatic: method not found: %s", meth_name.c_str ()); + error ("fevalStatic: method `%s' is not static", + meth_name.c_str ()); } else - error ("fevalStatic: invalid method name, expected a string value"); + error ("fevalStatic: method not found: %s", meth_name.c_str ()); } else error ("fevalStatic: invalid object, expected a meta.class object"); @@ -594,26 +586,21 @@ if (! error_state) { - std::string prop_name = args(1).string_value (); - - if (! error_state) + std::string prop_name = args(1).string_value ("getConstant: invalid property name, expected a string value"); + + cdef_property prop = cls.find_property (prop_name); + + if (prop.ok ()) { - cdef_property prop = cls.find_property (prop_name); - - if (prop.ok ()) - { - if (prop.is_constant ()) - retval(0) = prop.get_value (true, "getConstant"); - else - error ("getConstant: property `%s' is not constant", - prop_name.c_str ()); - } + if (prop.is_constant ()) + retval(0) = prop.get_value (true, "getConstant"); else - error ("getConstant: property not found: %s", + error ("getConstant: property `%s' is not constant", prop_name.c_str ()); } else - error ("getConstant: invalid property name, expected a string value"); + error ("getConstant: property not found: %s", + prop_name.c_str ()); } else error ("getConstant: invalid object, expected a meta.class object"); @@ -2413,49 +2400,44 @@ if (idx.front ().length () == 1) { - std::string nm = idx.front ()(0).string_value (); - - if (! error_state) + std::string nm = idx.front ()(0).string_value ("invalid meta.class indexing, expected a method or property name"); + + cdef_method meth = find_method (nm); + + if (meth.ok ()) { - cdef_method meth = find_method (nm); - - if (meth.ok ()) + if (meth.is_static ()) { - if (meth.is_static ()) + octave_value_list args; + + if (type.length () > 1 && idx.size () > 1 + && type[1] == '(') { - octave_value_list args; - - if (type.length () > 1 && idx.size () > 1 - && type[1] == '(') - { - args = *(++(idx.begin ())); - skip++; - } - - retval = meth.execute (args, (type.length () > skip - ? 1 : nargout), true, - "meta.class"); + args = *(++(idx.begin ())); + skip++; } - else - error ("method `%s' is not static", nm.c_str ()); + + retval = meth.execute (args, (type.length () > skip + ? 1 : nargout), true, + "meta.class"); } else - { - cdef_property prop = find_property (nm); - - if (prop.ok ()) - { - if (prop.is_constant ()) - retval(0) = prop.get_value (true, "meta.class"); - else - error ("property `%s' is not constant", nm.c_str ()); - } - else - error ("no such method or property `%s'", nm.c_str ()); - } + error ("method `%s' is not static", nm.c_str ()); } else - error ("invalid meta.class indexing, expected a method or property name"); + { + cdef_property prop = find_property (nm); + + if (prop.ok ()) + { + if (prop.is_constant ()) + retval(0) = prop.get_value (true, "meta.class"); + else + error ("property `%s' is not constant", nm.c_str ()); + } + else + error ("no such method or property `%s'", nm.c_str ()); + } } else error ("invalid meta.class indexing"); @@ -3305,12 +3287,9 @@ if (args.length () == 1) { - std::string name = args(0).string_value (); - - if (! error_state) - retval(0) = to_ov (lookup_package (name)); - else - error ("fromName: invalid package name, expected a string value"); + std::string name = args(0).string_value ("fromName: invalid package name, expected a string value"); + + retval(0) = to_ov (lookup_package (name)); } else error ("fromName: invalid number of parameters"); @@ -3465,54 +3444,49 @@ case '.': if (idx.front ().length () == 1) { - std::string nm = idx.front ()(0).string_value (); - - if (! error_state) - { + std::string nm = idx.front ()(0).string_value ("invalid meta.package indexing, expected a symbol name"); + #if DEBUG_TRACE - std::cerr << "meta.package query: " << nm << std::endl; + std::cerr << "meta.package query: " << nm << std::endl; #endif - octave_value o = find (nm); - - if (o.is_defined ()) + octave_value o = find (nm); + + if (o.is_defined ()) + { + if (o.is_function ()) { - if (o.is_function ()) + octave_function* fcn = o.function_value (); + + // NOTE: the case where the package query is the last + // part of this subsref index is handled in the parse + // tree, because there is some logic to handle magic + // "end" that makes it impossible to execute the + // function call at this stage. + + if (type.size () > 1 + && ! fcn->is_postfix_index_handled (type[1])) { - octave_function* fcn = o.function_value (); - - // NOTE: the case where the package query is the last - // part of this subsref index is handled in the parse - // tree, because there is some logic to handle magic - // "end" that makes it impossible to execute the - // function call at this stage. - - if (type.size () > 1 - && ! fcn->is_postfix_index_handled (type[1])) - { - octave_value_list tmp_args; - - retval = o.do_multi_index_op (nargout, - tmp_args); - } - else - retval(0) = o; - - if (type.size () > 1 && idx.size () > 1) - retval = retval(0).next_subsref (nargout, type, - idx, 1); + octave_value_list tmp_args; + + retval = o.do_multi_index_op (nargout, + tmp_args); } - else if (type.size () > 1 && idx.size () > 1) - retval = o.next_subsref (nargout, type, idx, 1); else retval(0) = o; + + if (type.size () > 1 && idx.size () > 1) + retval = retval(0).next_subsref (nargout, type, + idx, 1); } - else if (! error_state) - error ("member `%s' in package `%s' does not exist", - nm.c_str (), get_name ().c_str ()); + else if (type.size () > 1 && idx.size () > 1) + retval = o.next_subsref (nargout, type, idx, 1); + else + retval(0) = o; } - else - error ("invalid meta.package indexing, expected a symbol name"); + else if (! error_state) + error ("member `%s' in package `%s' does not exist", + nm.c_str (), get_name ().c_str ()); } else error ("invalid meta.package indexing"); @@ -3855,12 +3829,9 @@ if (args.length () == 1) { - std::string cname = args(0).string_value (); - - if (! error_state) - retval = to_ov (lookup_package (cname)); - else - error ("invalid package name, expected a string value"); + std::string cname = args(0).string_value ("invalid package name, expected a string value"); + + retval = to_ov (lookup_package (cname)); } else print_usage (); @@ -3893,12 +3864,9 @@ if (args.length () == 1) { - std::string cls = args(0).string_value (); - - if (! error_state) - retval = to_ov (lookup_class (cls)); - else - error ("invalid class name, expected a string value"); + std::string cls = args(0).string_value ("invalid class name, expected a string value"); + + retval = to_ov (lookup_class (cls)); } else print_usage (); diff -r 10ec79b47808 -r 729a85dafba8 libinterp/octave-value/ov-fcn-handle.cc --- a/libinterp/octave-value/ov-fcn-handle.cc Thu Oct 08 18:15:56 2015 -0400 +++ b/libinterp/octave-value/ov-fcn-handle.cc Thu Oct 08 17:55:57 2015 -0400 @@ -1859,23 +1859,19 @@ if (nargin == 1 || nargin == 2) { - if (args(0).is_string ()) + std::string nm = args(0).string_value ("str2func: FCN_NAME must be a string"); + + if (nm[0] == '@') { - std::string nm = args(0).string_value (); - if (nm[0] == '@') - { - int parse_status; - octave_value anon_fcn_handle = - eval_string (nm, true, parse_status); + int parse_status; + octave_value anon_fcn_handle = + eval_string (nm, true, parse_status); - if (parse_status == 0) - retval = anon_fcn_handle; - } - else - retval = make_fcn_handle (nm, nargin != 2); + if (parse_status == 0) + retval = anon_fcn_handle; } else - error ("str2func: FCN_NAME must be a string"); + retval = make_fcn_handle (nm, nargin != 2); } else print_usage (); diff -r 10ec79b47808 -r 729a85dafba8 libinterp/octave-value/ov-fcn-inline.cc --- a/libinterp/octave-value/ov-fcn-inline.cc Thu Oct 08 18:15:56 2015 -0400 +++ b/libinterp/octave-value/ov-fcn-inline.cc Thu Oct 08 17:55:57 2015 -0400 @@ -681,162 +681,150 @@ if (nargin > 0) { - if (args(0).is_string ()) + std::string fun = args(0).string_value ("inline: STR argument must be a string"); + + string_vector fargs; + + if (nargin == 1) { - std::string fun = args(0).string_value (); - string_vector fargs; + bool is_arg = false; + bool in_string = false; + std::string tmp_arg; + size_t i = 0; + size_t fun_length = fun.length (); - if (nargin == 1) + while (i < fun_length) { - bool is_arg = false; - bool in_string = false; - std::string tmp_arg; - size_t i = 0; - size_t fun_length = fun.length (); + bool terminate_arg = false; + char c = fun[i++]; - while (i < fun_length) + if (in_string) { - bool terminate_arg = false; - char c = fun[i++]; + if (c == '\'' || c == '\"') + in_string = false; + } + else if (c == '\'' || c == '\"') + { + in_string = true; + if (is_arg) + terminate_arg = true; + } + else if (! isalpha (c) && c != '_') + if (! is_arg) + continue; + else if (isdigit (c)) + tmp_arg.append (1, c); + else + { + // Before we do anything remove trailing whitespaces. + while (i < fun_length && isspace (c)) + c = fun[i++]; - if (in_string) - { - if (c == '\'' || c == '\"') - in_string = false; - } - else if (c == '\'' || c == '\"') - { - in_string = true; - if (is_arg) - terminate_arg = true; - } - else if (! isalpha (c) && c != '_') - if (! is_arg) - continue; - else if (isdigit (c)) - tmp_arg.append (1, c); + // Do we have a variable or a function? + if (c != '(') + terminate_arg = true; else { - // Before we do anything remove trailing whitespaces. - while (i < fun_length && isspace (c)) - c = fun[i++]; - - // Do we have a variable or a function? - if (c != '(') - terminate_arg = true; - else - { - tmp_arg = std::string (); - is_arg = false; - } + tmp_arg = std::string (); + is_arg = false; } - else if (! is_arg) + } + else if (! is_arg) + { + if (c == 'e' || c == 'E') { - if (c == 'e' || c == 'E') - { - // possible number in exponent form, not arg - if (isdigit (fun[i]) - || fun[i] == '-' || fun[i] == '+') - continue; - } - is_arg = true; - tmp_arg.append (1, c); + // possible number in exponent form, not arg + if (isdigit (fun[i]) + || fun[i] == '-' || fun[i] == '+') + continue; } - else - { - tmp_arg.append (1, c); - } - - if (terminate_arg || (i == fun_length && is_arg)) - { - bool have_arg = false; - - for (int j = 0; j < fargs.numel (); j++) - if (tmp_arg == fargs (j)) - { - have_arg = true; - break; - } - - if (! have_arg && tmp_arg != "i" && tmp_arg != "j" - && tmp_arg != "NaN" && tmp_arg != "nan" - && tmp_arg != "Inf" && tmp_arg != "inf" - && tmp_arg != "NA" && tmp_arg != "pi" - && tmp_arg != "e" && tmp_arg != "eps") - fargs.append (tmp_arg); - - tmp_arg = std::string (); - is_arg = false; - } + is_arg = true; + tmp_arg.append (1, c); + } + else + { + tmp_arg.append (1, c); } - // Sort the arguments into ascii order. - fargs.sort (); + if (terminate_arg || (i == fun_length && is_arg)) + { + bool have_arg = false; + + for (int j = 0; j < fargs.numel (); j++) + if (tmp_arg == fargs (j)) + { + have_arg = true; + break; + } - if (fargs.numel () == 0) - fargs.append (std::string ("x")); + if (! have_arg && tmp_arg != "i" && tmp_arg != "j" + && tmp_arg != "NaN" && tmp_arg != "nan" + && tmp_arg != "Inf" && tmp_arg != "inf" + && tmp_arg != "NA" && tmp_arg != "pi" + && tmp_arg != "e" && tmp_arg != "eps") + fargs.append (tmp_arg); + tmp_arg = std::string (); + is_arg = false; + } } - else if (nargin == 2 && args(1).is_numeric_type ()) - { - if (! args(1).is_scalar_type ()) - { - error ("inline: N must be an integer"); - return retval; - } - int n = args(1).int_value (); + // Sort the arguments into ascii order. + fargs.sort (); - if (! error_state) - { - if (n >= 0) - { - fargs.resize (n+1); + if (fargs.numel () == 0) + fargs.append (std::string ("x")); - fargs(0) = "x"; + } + else if (nargin == 2 && args(1).is_numeric_type ()) + { + if (! args(1).is_scalar_type ()) + { + error ("inline: N must be an integer"); + return retval; + } + + int n = args(1).int_value (); - for (int i = 1; i < n+1; i++) - { - std::ostringstream buf; - buf << "P" << i; - fargs(i) = buf.str (); - } - } - else + if (! error_state) + { + if (n >= 0) + { + fargs.resize (n+1); + + fargs(0) = "x"; + + for (int i = 1; i < n+1; i++) { - error ("inline: N must be a positive integer or zero"); - return retval; + std::ostringstream buf; + buf << "P" << i; + fargs(i) = buf.str (); } } else { - error ("inline: N must be an integer"); + error ("inline: N must be a positive integer or zero"); return retval; } } else { - fargs.resize (nargin - 1); - - for (int i = 1; i < nargin; i++) - { - if (args(i).is_string ()) - { - std::string s = args(i).string_value (); - fargs(i-1) = s; - } - else - { - error ("inline: expecting string arguments"); - return retval; - } - } + error ("inline: N must be an integer"); + return retval; } - - retval = octave_value (new octave_fcn_inline (fun, fargs)); } else - error ("inline: STR argument must be a string"); + { + fargs.resize (nargin - 1); + + for (int i = 1; i < nargin; i++) + { + std::string s = args(i).string_value ("inline: expecting string arguments"); + fargs(i-1) = s; + } + } + + retval = octave_value (new octave_fcn_inline (fun, fargs)); } else print_usage (); diff -r 10ec79b47808 -r 729a85dafba8 libinterp/octave-value/ov.cc --- a/libinterp/octave-value/ov.cc Thu Oct 08 18:15:56 2015 -0400 +++ b/libinterp/octave-value/ov.cc Thu Oct 08 17:55:57 2015 -0400 @@ -2834,24 +2834,17 @@ for (int k = 0; k < nel; k++) { - if (type(k).is_string ()) - { - std::string item = type(k).string_value (); - if (item == "{}") - type_string[k] = '{'; - else if (item == "()") - type_string[k] = '('; - else if (item == ".") - type_string[k] = '.'; - else - { - error ("%s: invalid indexing type '%s'", name, item.c_str ()); - return; - } - } + std::string item = type(k).string_value ("%s: type(%d) must be a string", name, k+1); + + if (item == "{}") + type_string[k] = '{'; + else if (item == "()") + type_string[k] = '('; + else if (item == ".") + type_string[k] = '.'; else { - error ("%s: type(%d) must be a string", name, k+1); + error ("%s: invalid indexing type '%s'", name, item.c_str ()); return; } diff -r 10ec79b47808 -r 729a85dafba8 libinterp/parse-tree/pt-idx.cc --- a/libinterp/parse-tree/pt-idx.cc Thu Oct 08 18:15:56 2015 -0400 +++ b/libinterp/parse-tree/pt-idx.cc Thu Oct 08 17:55:57 2015 -0400 @@ -209,10 +209,7 @@ { octave_value t = df->rvalue1 (); - if (t.is_string () && t.rows () == 1) - fn = t.string_value (); - else - error ("dynamic structure field names must be strings"); + fn = t.string_value ("dynamic structure field names must be strings"); } else panic_impossible ();