You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Refactor colClasses handling for readability (#6106)
Towards #6105. This PR is a simple refactor to reduce code nesting / improve readability. It makes the actual implementation of #6105, #6107, much simpler.
// UNPROTECTed directly just after this for loop. No nprotect++ here is correct.
386
386
for (intj=0; j<LENGTH(items); j++) {
387
-
intk=INTEGER(itemsInt)[j];
388
-
if (k==NA_INTEGER) {
387
+
intcolIdx=INTEGER(itemsInt)[j];// NB: 1-based
388
+
if (colIdx==NA_INTEGER) {
389
389
if (isString(items))
390
390
DTWARN(_("Column name '%s' (colClasses[[%d]][%d]) not found"), CHAR(STRING_ELT(items, j)), i+1, j+1);
391
391
else
392
392
DTWARN(_("colClasses[[%d]][%d] is NA"), i+1, j+1);
393
+
continue;
394
+
}
395
+
if (colIdx<1||colIdx>ncol) {
396
+
DTWARN(_("Column number %d (colClasses[[%d]][%d]) is out of range [1,ncol=%d]"), colIdx, i+1, j+1, ncol);
397
+
continue;
398
+
}
399
+
if (type[colIdx-1]<0) {
400
+
DTWARN(_("Column %d ('%s') appears more than once in colClasses. The second time is colClasses[[%d]][%d]."), colIdx, CHAR(STRING_ELT(colNamesSxp,colIdx-1)), i+1, j+1);
401
+
continue;
402
+
}
403
+
if (type[colIdx-1] ==CT_DROP) {
404
+
continue;
405
+
}
406
+
if (selectRankD) selectRankD[colIdx-1] =rank++;
407
+
// NB: mark as negative to indicate 'seen'
408
+
if (colClassType==CT_ISO8601_TIME&&type[colIdx-1]!=CT_ISO8601_TIME) {
409
+
type[colIdx-1] =-CT_STRING; // don't use in-built UTC parser, defer to character and as.POSIXct afterwards which reads in local time
DTWARN(_("Column %d ('%s') appears more than once in colClasses. The second time is colClasses[[%d]][%d]."), k, CHAR(STRING_ELT(colNamesSxp,k-1)), i+1, j+1);
397
-
elseif (type[k-1]!=CT_DROP) {
398
-
if (thisType==CT_ISO8601_TIME&&type[k-1]!=CT_ISO8601_TIME) {
399
-
type[k-1] =-CT_STRING; // don't use in-built UTC parser, defer to character and as.POSIXct afterwards which reads in local time
type[k-1] =-thisType; // freadMain checks bump up only not down. Deliberately don't catch here to test freadMain; e.g. test 959
403
-
if (w==NUT) SET_STRING_ELT(colClassesAs, k-1, STRING_ELT(listNames,i));
404
-
}
405
-
if (selectRankD) selectRankD[k-1] =rank++;
406
-
}
407
-
} else {
408
-
DTWARN(_("Column number %d (colClasses[[%d]][%d]) is out of range [1,ncol=%d]"), k, i+1, j+1, ncol);
409
-
}
412
+
type[colIdx-1] =-colClassType; // freadMain checks bump up only not down. Deliberately don't catch here to test freadMain; e.g. test 959
413
+
if (colClassTypeIdx==NUT) SET_STRING_ELT(colClassesAs, colIdx-1, STRING_ELT(listNames, i)); // unknown type --> defer to R
410
414
}
411
415
}
412
416
UNPROTECT(1); // UNPROTECTing itemsInt inside loop to save protection stack
413
417
}
414
418
for (inti=0; i<ncol; i++) {
415
419
if (type[i]<0) type[i] *= -1; // undo sign; was used to detect duplicates
416
-
elseif (selectColClasses) type[i] =CT_DROP; // reading will proceed in order of columns in file; reorder happens afterwards at R level
420
+
elseif (selectColClasses) type[i] =CT_DROP; // not seen --> drop; reading will proceed in order of columns in file; reorder happens afterwards at R level
0 commit comments