Skip to content

Commit a2fa23f

Browse files
committed
Enhancing of error message
1 parent f72e46b commit a2fa23f

File tree

6 files changed

+67
-19
lines changed

6 files changed

+67
-19
lines changed

po/data.table.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ msgid "Unknown 'measure.vars' type %s at index %d of list"
10241024
msgstr ""
10251025

10261026
#: fmelt.c:187
1027-
msgid "One or more values in 'measure.vars' is invalid."
1027+
msgid "One or more values in 'measure.vars' is invalid; please fix by removing: %s"
10281028
msgstr ""
10291029

10301030
#: fmelt.c:189

po/es.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,8 +1281,8 @@ msgid "Unknown 'measure.vars' type %s at index %d of list"
12811281
msgstr "Tipo 'measure.vars' desconocido %s en el índice %d de la lista"
12821282

12831283
#: fmelt.c:187
1284-
msgid "One or more values in 'measure.vars' is invalid."
1285-
msgstr "Uno o más valores en 'measure.vars' no son válidos."
1284+
msgid "One or more values in 'measure.vars' is invalid; please fix by removing: %s"
1285+
msgstr "Uno o más valores en 'measure.vars' no son válidos; por favor corrige eliminando: %s"
12861286

12871287
#: fmelt.c:189
12881288
msgid "One or more values in 'id.vars' is invalid."

po/fr.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,8 +1301,8 @@ msgid "Unknown 'measure.vars' type %s at index %d of list"
13011301
msgstr "Type inconnu de 'measure.vars' %s à l'indice %d de la liste"
13021302

13031303
#: fmelt.c:187
1304-
msgid "One or more values in 'measure.vars' is invalid."
1305-
msgstr "Une ou plusieurs valeurs de 'measure.vars' ne sont pas valides."
1304+
msgid "One or more values in 'measure.vars' is invalid; please fix by removing: %s"
1305+
msgstr "Une ou plusieurs valeurs de 'measure.vars' ne sont pas valides; veuillez corriger en supprimant : %s"
13061306

13071307
#: fmelt.c:189
13081308
msgid "One or more values in 'id.vars' is invalid."

po/pt_BR.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,8 +1281,8 @@ msgid "Unknown 'measure.vars' type %s at index %d of list"
12811281
msgstr "'measure.vars'com tipo desconhecido %s no índice %d da lista"
12821282

12831283
#: fmelt.c:187
1284-
msgid "One or more values in 'measure.vars' is invalid."
1285-
msgstr "Um ou mais valores em 'measure.vars' são inválidos."
1284+
msgid "One or more values in 'measure.vars' is invalid; please fix by removing: %s"
1285+
msgstr "Um ou mais valores em 'measure.vars' são inválidos; por favor, corrija removendo: %s"
12861286

12871287
#: fmelt.c:189
12881288
msgid "One or more values in 'id.vars' is invalid."

po/zh_CN.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,8 @@ msgid "Unknown 'measure.vars' type %s at index %d of list"
11541154
msgstr "未知'measure.vars'类型 %s,位于列表中 %d"
11551155

11561156
#: fmelt.c:187
1157-
msgid "One or more values in 'measure.vars' is invalid."
1158-
msgstr "'measure.vars'里,一或多个数值无效"
1157+
msgid "One or more values in 'measure.vars' is invalid; please fix by removing: %s"
1158+
msgstr "'measure.vars'里,一或多个数值无效;请通过删除以下数值来修复:%s"
11591159

11601160
#: fmelt.c:189
11611161
msgid "One or more values in 'id.vars' is invalid."

src/fmelt.c

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "data.table.h"
22
#include <Rdefines.h>
3+
4+
35
// #include <signal.h> // the debugging machinery + breakpoint aidee
46
// raise(SIGINT);
57

@@ -176,33 +178,79 @@ bool is_default_measure(SEXP vec) {
176178

177179
// maybe unlist, then unique, then set_diff.
178180
SEXP uniq_diff(SEXP int_or_list, int ncol, bool is_measure) {
181+
// Protect input list/vector, unlisting if necessary
179182
SEXP int_vec = PROTECT(isNewList(int_or_list) ? unlist_(int_or_list) : int_or_list);
183+
184+
// Check for duplicated elements in the input vector
180185
SEXP is_duplicated = PROTECT(duplicated(int_vec, FALSE));
186+
181187
int n_unique_cols = 0;
182-
for (int i=0; i<length(int_vec); ++i) {
188+
189+
// Allocate a vector to store invalid column indices (initially max size is length of int_vec)
190+
SEXP invalid_columns = PROTECT(allocVector(INTSXP, length(int_vec)));
191+
int* invalid_col_ptr = INTEGER(invalid_columns);
192+
int invalid_count = 0;
193+
194+
// Iterate through the column numbers to identify invalid and unique columns
195+
for (int i = 0; i < length(int_vec); ++i) {
183196
int col_number = INTEGER(int_vec)[i];
197+
198+
// Check if the column number is within valid range
184199
bool good_number = 0 < col_number && col_number <= ncol;
185-
if (is_measure) good_number |= (col_number==NA_INTEGER);
186-
if (!good_number) {
187-
if (is_measure) {
188-
error(_("One or more values in 'measure.vars' is invalid."));
189-
} else {
190-
error(_("One or more values in 'id.vars' is invalid."));
200+
201+
// Special check for 'measure' case (NA_INTEGER handling)
202+
if (is_measure) good_number |= (col_number == NA_INTEGER);
203+
204+
// Collect invalid columns if not valid or out of range
205+
if (!good_number || col_number == 0) {
206+
invalid_col_ptr[invalid_count++] = col_number;
207+
} else if (!LOGICAL(is_duplicated)[i]) {
208+
n_unique_cols++;
209+
}
210+
}
211+
212+
// If invalid columns are found, construct the error message
213+
if (invalid_count > 0) {
214+
// Buffer for concatenated invalid column messages
215+
char buffer[4096] = ""; // Large enough to store the concatenated string
216+
for (int i = 0; i < invalid_count; ++i) {
217+
char temp[32];
218+
snprintf(temp, 32, "[%d]", invalid_col_ptr[i]); // Format the column number
219+
220+
if (i > 0) {
221+
strncat(buffer, ", ", sizeof(buffer) - strlen(buffer) - 1); // Add separator
191222
}
192-
} else if (!LOGICAL(is_duplicated)[i]) n_unique_cols++;
223+
strncat(buffer, temp, sizeof(buffer) - strlen(buffer) - 1); // Append to the buffer
224+
}
225+
226+
// Throw the error with the concatenated message
227+
error(_("One or more values in '%s' are invalid; please fix by removing: %s"),
228+
is_measure ? "measure.vars" : "id.vars", buffer);
193229
}
230+
231+
// Proceed with collecting unique columns
194232
SEXP unique_col_numbers = PROTECT(allocVector(INTSXP, n_unique_cols));
195233
int unique_i = 0;
196-
for (int i=0; i<length(is_duplicated); ++i) {
234+
235+
// Populate the unique column numbers into the new vector
236+
for (int i = 0; i < length(is_duplicated); ++i) {
197237
if (!LOGICAL(is_duplicated)[i]) {
198238
INTEGER(unique_col_numbers)[unique_i++] = INTEGER(int_vec)[i];
199239
}
200240
}
241+
242+
// Apply set difference to get final unique column indices
201243
SEXP out = set_diff(unique_col_numbers, ncol);
202-
UNPROTECT(3);
244+
245+
// Unprotect all allocated objects
246+
UNPROTECT(4); // Unprotect input, duplication check, invalid columns, and unique columns
247+
203248
return out;
204249
}
205250

251+
252+
253+
206254
SEXP cols_to_int_or_list(SEXP cols, SEXP dtnames, bool is_measure) {
207255
switch(TYPEOF(cols)) {
208256
case STRSXP : return chmatch(cols, dtnames, 0);

0 commit comments

Comments
 (0)