annotate liboctave/util/lo-array-gripes.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 4197fc428c7d
children e54ecb33727e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11135
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 /*
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19442
diff changeset
3 Copyright (C) 2003-2015 John W. Eaton
11135
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4 Copyright (C) 2009 VZLU Prague
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 This file is part of Octave.
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 option) any later version.
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 for more details.
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 along with Octave; see the file COPYING. If not, see
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20 <http://www.gnu.org/licenses/>.
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 */
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25 #include <config.h>
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 #endif
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27
20574
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
28 #include <string.h>
11135
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 #include "lo-array-gripes.h"
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30 #include "lo-error.h"
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 const char *error_id_nonconformant_args = "Octave:nonconformant-args";
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34 const char *error_id_index_out_of_bounds = "Octave:index-out-of-bounds";
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 const char *error_id_invalid_index = "Octave:invalid-index";
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37
19442
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
38 const char *warning_id_nearly_singular_matrix = "Octave:nearly-singular-matrix";
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
39
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
40 const char *warning_id_singular_matrix = "Octave:singular-matrix";
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
41
11135
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 void
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 gripe_nan_to_logical_conversion (void)
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44 {
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45 (*current_liboctave_error_handler)
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 ("invalid conversion from NaN to logical");
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 }
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 void
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50 gripe_nan_to_character_conversion (void)
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 {
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 (*current_liboctave_error_handler)
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53 ("invalid conversion from NaN to character");
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 }
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 void
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 gripe_nonconformant (const char *op, octave_idx_type op1_len,
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58 octave_idx_type op2_len)
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59 {
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60 const char *err_id = error_id_nonconformant_args;
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62 (*current_liboctave_error_with_id_handler)
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 (err_id, "%s: nonconformant arguments (op1 len: %d, op2 len: %d)",
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64 op, op1_len, op2_len);
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 }
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67 void
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68 gripe_nonconformant (const char *op,
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69 octave_idx_type op1_nr, octave_idx_type op1_nc,
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70 octave_idx_type op2_nr, octave_idx_type op2_nc)
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71 {
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72 const char *err_id = error_id_nonconformant_args;
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
73
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74 (*current_liboctave_error_with_id_handler)
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75 (err_id, "%s: nonconformant arguments (op1 is %dx%d, op2 is %dx%d)",
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76 op, op1_nr, op1_nc, op2_nr, op2_nc);
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77 }
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79 void
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80 gripe_nonconformant (const char *op, const dim_vector& op1_dims,
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81 const dim_vector& op2_dims)
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82 {
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83 const char *err_id = error_id_nonconformant_args;
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85 std::string op1_dims_str = op1_dims.str ();
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86 std::string op2_dims_str = op2_dims.str ();
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88 (*current_liboctave_error_with_id_handler)
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89 (err_id, "%s: nonconformant arguments (op1 is %s, op2 is %s)",
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90 op, op1_dims_str.c_str (), op2_dims_str.c_str ());
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
91 }
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93 void
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94 gripe_del_index_out_of_range (bool is1d, octave_idx_type idx,
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
95 octave_idx_type ext)
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96 {
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
97 const char *err_id = error_id_index_out_of_bounds;
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
99 (*current_liboctave_error_with_id_handler)
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
100 (err_id, "A(%s) = []: index out of bounds; value %d out of bound %d",
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 is1d ? "I" : "..,I,..", idx, ext);
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
102 }
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
103
20574
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
104
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
105
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
106 // Common procedures of base class index_exception, thrown whenever an
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
107 // object is indexed incorrectly, such as by an index that is out of
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
108 // range, negative, fractional, complex, or of a non-numeric type.
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
109
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
110 const char *
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
111 index_exception::err (void) throw ()
11135
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
112 {
20574
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
113 msg = access () + "; " + explain ();
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
114 return msg.c_str ();
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
115 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
116
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
117 // Show what was illegally accessed, e.g., "A(-1,_)", "A(0+1i)", "A(_,3)"
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
118 // Show how many indices come before/after the offending one,
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
119 // e.g., (<error>), (<error>,_), or (_,<error>,...[x5]...)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
120
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
121 std::string
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
122 index_exception:: access (void) const
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
123 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
124 // FIXME: don't use a fixed size buffer!
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
125
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
126 int buf_len = 300;
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
127
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
128 char output [buf_len];
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
129 char pre [buf_len];
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
130 char post [buf_len];
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
131
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
132 // dim == 0 if position not yet given, or
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
133 // <static_cast unsigned int>(-1) if explicitly shown to be unknown
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
134 // both are caught by this condition
11135
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
135
20574
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
136 if (static_cast <unsigned int> (dim-1) > 100000)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
137 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
138 // No parentheses are given if the dimension is not known.
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
139 pre[0] = post[0] = '\0';
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
140 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
141 else
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
142 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
143 if (dim < 5)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
144 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
145 pre[0] = '(';
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
146 octave_idx_type i;
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
147
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
148 for (i = 1; i < dim; i++)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
149 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
150 pre[2*i-1] = '_';
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
151 pre[2*i] = ',';
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
152 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
153
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
154 pre[2*i-1] = '\0'; // i == min (1, dim)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
155 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
156 else
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
157 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
158 sprintf (pre, "(...[x%d]...", dim-1);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
159 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
160
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
161 if (static_cast <unsigned int> (nd-dim) < 5)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
162 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
163 for (octave_idx_type i = 0; i < nd-dim; i++)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
164 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
165 post[2*i] = ',';
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
166 post[2*i+1] = '_';
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
167 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
168
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
169 if (nd >= dim)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
170 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
171 post[2*(nd-dim)] = ')';
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
172 post[2*(nd-dim)+1] = '\0';
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
173 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
174 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
175 else
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
176 sprintf (post, "...[x%d]...)", nd-dim);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
177 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
178
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
179 const char *v;
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
180
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
181 if (var[0] == '\0' || var == "<unknown>")
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
182 v = "index ";
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
183 else
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
184 v = var.c_str ();
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
185
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
186 snprintf (output, buf_len, "%s%s%s%s", v, pre, idx(), post);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
187
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
188 return output;
11135
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
189 }
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
190
20574
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
191 class invalid_index : public index_exception
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
192 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
193 public:
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
194
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
195 invalid_index (const char *value, octave_idx_type ndim,
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
196 octave_idx_type dimen)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
197 : index_exception (value, ndim, dimen)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
198 { }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
199
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
200 const char* explain (void) const
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
201 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
202 #ifdef USE_64_BIT_IDX_T
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
203 return "subscripts must be either integers 1 to (2^63)-1 or logicals";
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
204 #else
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
205 return "subscripts must be either integers 1 to (2^31)-1 or logicals";
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
206 #endif
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
207 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
208
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
209 // ID of error to throw
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
210 const char* id (void) const
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
211 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
212 return error_id_invalid_index;
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
213 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
214 };
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
215
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
216 // Complain of an index that is: negative, fractional, or too big.
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
217
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
218 void
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
219 gripe_invalid_index (const char *idx, octave_idx_type nd,
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
220 octave_idx_type dim, const char * /* var */)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
221 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
222 invalid_index e (idx, nd, dim);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
223
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
224 throw e;
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
225 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
226
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
227 void
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
228 gripe_invalid_index (octave_idx_type n, octave_idx_type nd,
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
229 octave_idx_type dim, const char *var)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
230 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
231 // FIXME: don't use a fixed size buffer!
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
232 char buf [100];
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
233
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
234 sprintf (buf, "%d", n+1);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
235
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
236 gripe_invalid_index (buf, nd, dim, var);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
237 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
238
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
239 void
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
240 gripe_invalid_index (double n, octave_idx_type nd, octave_idx_type dim,
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
241 const char *var)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
242 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
243 // FIXME: don't use a fixed size buffer!
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
244 char buf [100];
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
245
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
246 sprintf (buf, "%g", n+1);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
247
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
248 gripe_invalid_index (buf, nd, dim, var);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
249 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
250
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
251
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
252 // Gripe and exception for read access beyond the bounds of an array.
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
253
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
254 class out_of_range : public index_exception
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
255 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
256 public:
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
257
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
258 out_of_range (const char *value, octave_idx_type nd_in,octave_idx_type dim_in)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
259 : index_exception (value, nd_in, dim_in), extent(0)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
260 { }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
261
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
262 const char* explain (void) const
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
263 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
264 static std::string expl; // should probably be member variable, but
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
265 // then explain() can't be const.
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
266
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
267 if (nd >= size.length ()) // if not an index slice
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
268 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
269 if (var != "")
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
270 expl = "but " + var + " has size ";
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
271 else
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
272 expl = "but object has size ";
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
273
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
274 expl = expl + size.str ('x');
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
275 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
276 else
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
277 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
278 // FIXME: don't use a fixed size buffer!
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
279 char buf [100];
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
280 sprintf (buf, "%d", extent);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
281 expl = "out of bound " + std::string (buf);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
282 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
283
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
284 return expl.c_str ();
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
285 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
286
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
287 // ID of error to throw.
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
288 const char* id (void) const
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
289 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
290 return (error_id_index_out_of_bounds);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
291 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
292
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
293 void set_size (const dim_vector& size_in) { size = size_in; }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
294
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
295 void set_extent (octave_idx_type ext) { extent = ext; }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
296
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
297 private:
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
298
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
299 dim_vector size; // dimension of object being accessed
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
300
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
301 octave_idx_type extent; // length of dimension being accessed
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
302 };
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
303
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
304 // Complain of an index that is out of range, but we don't know matrix size
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
305 void
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
306 gripe_index_out_of_range (int nd, int dim, octave_idx_type idx,
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
307 octave_idx_type ext)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
308 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
309 char buf [100];
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
310 sprintf (buf, "%d", idx);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
311 out_of_range e (buf, nd, dim);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
312
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
313 e.set_extent (ext);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
314 dim_vector d (1,1,1,1,1,1,1); // make explain() give extent not size
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
315 e.set_size (d);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
316 throw e;
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
317 }
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
318
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
319 // Complain of an index that is out of range
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
320 void
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
321 gripe_index_out_of_range (int nd, int dim, octave_idx_type idx,
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
322 octave_idx_type ext, const dim_vector& d)
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
323 {
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
324 char buf [100];
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
325 sprintf (buf, "%d", idx);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
326 out_of_range e (buf, nd, dim);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
327
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
328 e.set_extent (ext);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
329 e.set_size (d);
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
330 throw e;
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
331 }
11135
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
332
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
333 void
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
334 gripe_invalid_resize (void)
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
335 {
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
336 (*current_liboctave_error_with_id_handler)
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
337 ("Octave:invalid-resize",
11590
4ced6b90fffb style fixes for warning and error messages in source files
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
338 "Invalid resizing operation or ambiguous assignment to an out-of-bounds array element");
11135
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
339 }
047b0e877a14 add files for previous change
John W. Eaton <jwe@octave.org>
parents:
diff changeset
340
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
341 void
19442
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
342 gripe_singular_matrix (double rcond)
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
343 {
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
344 if (rcond == 0.0)
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
345 {
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
346 (*current_liboctave_warning_with_id_handler)
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
347 (warning_id_singular_matrix,
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
348 "matrix singular to machine precision");
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
349 }
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
350 else
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
351 {
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
352 (*current_liboctave_warning_with_id_handler)
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
353 (warning_id_nearly_singular_matrix,
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
354 "matrix singular to machine precision, rcond = %g", rcond);
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
355 }
a0c7001cf1a8 consistent messages and IDs for singular matrix warnings
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
356 }
20574
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
357
dd6345fd8a97 use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 19731
diff changeset
358 /* Tests in test/index.tst */