comparison libinterp/octave-value/ov-str-mat.cc @ 20574:dd6345fd8a97

use exceptions for better invalid index error reporting (bug #45957) * lo-array-gripes.h, lo-array-gripes.cc (index_exception): New base class for indexing errors. (invalid_index, out_of_range): New classes. (gripe_index_out_of_range): New overloaded function. (gripe_invalid_index): New overloaded functions. Delete version with no arguments. (gripe_invalid_assignment_size, gripe_assignment_dimension_mismatch): Delete. Change uses of gripe functions as needed. * Cell.cc (Cell::index, Cell::assign, Cell::delete_elements): Use exceptions to collect error info about and handle indexing errors. * data.cc (Fnth_element, do_accumarray_sum, F__accumarray_sum__, do_accumarray_minmax, do_accumarray_minmax_fun, F__accumdim_sum__): Likewise. * oct-map.cc (octave_map::index, octave_map::assign, octave_map::delete_elements): Likewise. * sparse.cc (Fsparse): Likewise. * sub2ind.cc (Fsub2ind, Find2sub): Likewise. New tests. * utils.cc (dims_to_numel): Likewise. * ov-base-diag.cc (octave_base_diag<DMT, MT>::do_index_op, octave_base_diag<DMT, MT>::subsasgn): Likewise. * ov-base-mat.cc (octave_base_matrix<MT>::subsref, octave_base_matrix<MT>::assign): Likewise. * ov-base-sparse.cc (octave_base_sparse<T>::do_index_op, octave_base_sparse<T>::assign, octave_base_sparse<MT>::delete_elements): Likewise. * ov-classdef.cc (cdef_object_array::subsref, cdef_object_array::subsasgn): Likewise. * ov-java.cc (make_java_index): Likewise. * ov-perm.cc (octave_perm_matrix::do_index_op): Likewise. * ov-range.cc (octave_range::do_index_op): Likewise. * ov-re-diag.cc (octave_diag_matrix::do_index_op): Likewise. * ov-str-mat.cc (octave_char_matrix_str::do_index_op_internal): Likewise. * pt-assign.cc (tree_simple_assignment::rvalue1): Likewise. * pt-idx.cc (tree_index_expression::rvalue, tree_index_expression::lvalue): Likewise. * Array-util.cc (sub2ind): Likewise. * toplev.cc (main_loop): Also catch unhandled index_exception exceptions. * ov-base.cc (octave_base_value::index_vector): Improve error message. * ov-re-sparse.cc (octave_sparse_matrix::index_vector): Likewise. * ov-complex.cc (complex_index): New class. (gripe_complex_index): New function. (octave_complex::index_vector): Use it. * pt-id.h, pt-id.cc (tree_identifier::is_variable, tree_black_hole::is_variable): Now const. * pt-idx.cc (final_index_error): New static function. (tree_index_expression::rvalue, tree_index_expression::lvalue): Use it. * index.tst: New tests.
author Lachlan Andrew <lachlanbis@gmail.com>
date Fri, 02 Oct 2015 15:07:37 -0400
parents df4165dfc676
children f90c8372b7ba
comparison
equal deleted inserted replaced
20573:e3c0fee87493 20574:dd6345fd8a97
92 { 92 {
93 octave_value retval; 93 octave_value retval;
94 94
95 octave_idx_type len = idx.length (); 95 octave_idx_type len = idx.length ();
96 96
97 switch (len) 97 // If we catch an indexing error in index_vector, we flag an error in
98 { 98 // index k. Ensure it is the right value befor each idx_vector call.
99 case 0: 99 // Same variable as used in the for loop in the default case.
100 retval = octave_value (matrix, type); 100
101 break; 101 octave_idx_type k = 0;
102 102
103 case 1: 103 try
104 { 104 {
105 idx_vector i = idx (0).index_vector (); 105 switch (len)
106 106 {
107 if (! error_state) 107 case 0:
108 retval = octave_value (charNDArray (matrix.index (i, resize_ok)), 108 retval = octave_value (matrix, type);
109 type); 109 break;
110 } 110
111 break; 111 case 1:
112 112 {
113 case 2: 113 idx_vector i = idx (0).index_vector ();
114 { 114
115 idx_vector i = idx (0).index_vector (); 115 if (! error_state)
116 idx_vector j = idx (1).index_vector (); 116 retval = octave_value (charNDArray (matrix.index (i, resize_ok)),
117 117 type);
118 if (! error_state) 118 }
119 retval = octave_value (charNDArray (matrix.index (i, j, resize_ok)), 119 break;
120 type); 120
121 } 121 case 2:
122 break; 122 {
123 123 idx_vector i = idx (0).index_vector ();
124 default: 124 k = 1;
125 { 125 idx_vector j = idx (1).index_vector ();
126 Array<idx_vector> idx_vec (dim_vector (len, 1)); 126
127 127 if (! error_state)
128 for (octave_idx_type i = 0; i < len; i++) 128 retval = octave_value (charNDArray (matrix.index (i, j, resize_ok)),
129 idx_vec(i) = idx(i).index_vector (); 129 type);
130 130 }
131 if (! error_state) 131 break;
132 retval = 132
133 octave_value (charNDArray (matrix.index (idx_vec, resize_ok)), 133 default:
134 type); 134 {
135 } 135 Array<idx_vector> idx_vec (dim_vector (len, 1));
136 break; 136
137 for (k = 0; k < len; k++)
138 idx_vec(k) = idx(k).index_vector ();
139
140 if (! error_state)
141 retval =
142 octave_value (charNDArray (matrix.index (idx_vec, resize_ok)),
143 type);
144 }
145 break;
146 }
147 }
148 catch (index_exception& e)
149 {
150 // Rethrow to allow more info to be reported later.
151 e.set_pos_if_unset (len, k+1);
152 throw;
137 } 153 }
138 154
139 return retval; 155 return retval;
140 } 156 }
141 157