Skip to content

Commit 77fb8ad

Browse files
committed
size_t usage and casting cleanup
1 parent 1593221 commit 77fb8ad

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/fread.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,14 @@ static inline int64_t clamp_szt(int64_t x, int64_t lower, int64_t upper) {
197197
* is constructed manually (using say snprintf) that warning(), stop()
198198
* and Rprintf() are all called as warning(_("%s"), msg) and not warning(msg).
199199
*/
200-
static const char* strlim(const char *ch, size_t limit) {
200+
static const char* strlim(const char *ch, int64_t limit) {
201201
static char buf[1002];
202202
static int flip = 0;
203203
char *ptr = buf + 501 * flip;
204204
flip = 1 - flip;
205205
char *ch2 = ptr;
206-
if (limit>500) limit=500;
207-
size_t width = 0;
206+
limit = imin(limit, 500);
207+
int64_t width = 0;
208208
while ((*ch>'\r' || (*ch!='\0' && *ch!='\r' && *ch!='\n')) && width++<limit) {
209209
*ch2++ = *ch++;
210210
}
@@ -915,7 +915,7 @@ static void parse_double_hexadecimal(FieldParseContext *ctx)
915915
acc = (acc << 4) + digit;
916916
ch++;
917917
}
918-
size_t ndigits = (uint_fast8_t)(ch - ch0);
918+
ptrdiff_t ndigits = ch - ch0;
919919
if (ndigits > 13 || !(*ch=='p' || *ch=='P')) return;
920920
acc <<= (13 - ndigits) * 4;
921921
ch += 1 + (Eneg = ch[1]=='-') + (ch[1]=='+');
@@ -1868,7 +1868,7 @@ int freadMain(freadMainArgs _args) {
18681868
int64_t estnrow=1;
18691869
int64_t allocnrow=0; // Number of rows in the allocated DataTable
18701870
double meanLineLen=0.0; // Average length (in bytes) of a single line in the input file
1871-
size_t bytesRead=0; // Bytes in the data section (i.e. excluding column names, header and footer, if any)
1871+
ptrdiff_t bytesRead=0; // Bytes in the data section (i.e. excluding column names, header and footer, if any)
18721872
{
18731873
if (verbose) DTPRINT(_("[07] Detect column types, dec, good nrow estimate and whether first row is column names\n"));
18741874
if (verbose && args.header!=NA_BOOL8) DTPRINT(_(" 'header' changed by user from 'auto' to %s\n"), args.header?"true":"false");
@@ -1932,8 +1932,8 @@ int freadMain(freadMainArgs _args) {
19321932
}
19331933
firstRowStart = ch;
19341934
} else {
1935-
ch = (jump == nJumps-1) ? eof - (size_t)(0.5*jump0size) : // to almost-surely sample the last line
1936-
pos + (size_t)jump*((size_t)(eof-pos)/(size_t)(nJumps-1));
1935+
ch = (jump == nJumps-1) ? eof - (0.5*jump0size) : // to almost-surely sample the last line
1936+
pos + jump*((eof-pos)/(nJumps-1));
19371937
ch = nextGoodLine(ch, ncol);
19381938
}
19391939
if (ch<lastRowEnd) ch=lastRowEnd; // Overlap when apx 1,200 lines (just over 11*100) with short lines at the beginning and longer lines near the end, #2157
@@ -1970,7 +1970,7 @@ int freadMain(freadMainArgs _args) {
19701970
if (jump==0 && bumped) {
19711971
// apply bumps after each line in the first jump from the start in case invalid line stopped early on is in the first 100 lines.
19721972
// otherwise later jumps must complete fully before their bumps are applied. Invalid lines in those are more likely to be due to bad jump start.
1973-
memcpy(type, tmpType, (size_t)ncol);
1973+
memcpy(type, tmpType, ncol);
19741974
bumped = false; // detect_types() only updates &bumped when it's true. So reset to false here.
19751975
}
19761976
}
@@ -1984,7 +1984,7 @@ int freadMain(freadMainArgs _args) {
19841984
if (bumped) {
19851985
// when jump>0, apply the bumps (if any) at the end of the successfully completed jump sample
19861986
ASSERT(jump>0, "jump(%d)>0", jump);
1987-
memcpy(type, tmpType, (size_t)ncol);
1987+
memcpy(type, tmpType, ncol);
19881988
}
19891989
if (verbose && (bumped || jump==0 || jump==nJumps-1)) {
19901990
DTPRINT(_(" Type codes (jump %03d) : %s Quote rule %d\n"), jump, typesAsString(ncol), quoteRule);
@@ -2094,11 +2094,11 @@ int freadMain(freadMainArgs _args) {
20942094
if (verbose) DTPRINT(_(" All rows were sampled since file is small so we know nrow=%"PRIu64" exactly\n"), (uint64_t)sampleLines);
20952095
estnrow = allocnrow = sampleLines;
20962096
} else {
2097-
bytesRead = (size_t)(eof - firstRowStart);
2097+
bytesRead = eof - firstRowStart;
20982098
meanLineLen = (double)sumLen/sampleLines;
20992099
estnrow = CEIL(bytesRead/meanLineLen); // only used for progress meter and verbose line below
21002100
double sd = sqrt( (sumLenSq - (sumLen*sumLen)/sampleLines)/(sampleLines-1) );
2101-
allocnrow = clamp_szt((size_t)(bytesRead / fmax(meanLineLen - 2*sd, minLen)),
2101+
allocnrow = clamp_szt(bytesRead / fmax(meanLineLen - 2*sd, minLen),
21022102
(size_t)(1.1*estnrow), 2*estnrow);
21032103
// sd can be very close to 0.0 sometimes, so apply a +10% minimum
21042104
// blank lines have length 1 so for fill=true apply a +100% maximum. It'll be grown if needed.
@@ -2179,7 +2179,7 @@ int freadMain(freadMainArgs _args) {
21792179
{
21802180
if (verbose) DTPRINT(_("[09] Apply user overrides on column types\n"));
21812181
ch = pos;
2182-
memcpy(tmpType, type, (size_t)ncol) ;
2182+
memcpy(tmpType, type, ncol) ;
21832183
if (!userOverride(type, colNames, colNamesAnchor, ncol)) { // colNames must not be changed but type[] can be
21842184
if (verbose) DTPRINT(_(" Cancelled by user: userOverride() returned false.")); // # nocov
21852185
freadCleanup(); // # nocov
@@ -2252,7 +2252,7 @@ int freadMain(freadMainArgs _args) {
22522252
// For the 44GB file with 12875 columns, the max line len is 108,497. We may want each chunk to write to its
22532253
// own page (4k) of the final column, hence 1000 rows of the smallest type (4 byte int) is just
22542254
// under 4096 to leave space for R's header + malloc's header.
2255-
size_t chunkBytes = umax((size_t)(1000*meanLineLen), 1ULL/*MB*/ *1024*1024);
2255+
int64_t chunkBytes = umax((uint64_t)(1000*meanLineLen), 1ULL/*MB*/ *1024*1024);
22562256
// Index of the first jump to read. May be modified if we ever need to restart
22572257
// reading from the middle of the file.
22582258
int jump0 = 0;
@@ -2266,7 +2266,7 @@ int freadMain(freadMainArgs _args) {
22662266
nJumps = (int)(bytesRead/chunkBytes);
22672267
if (nJumps==0) nJumps=1;
22682268
else if (nJumps>nth) nJumps = nth*(1+(nJumps-1)/nth);
2269-
chunkBytes = bytesRead / (size_t)nJumps;
2269+
chunkBytes = bytesRead / nJumps;
22702270
} else {
22712271
ASSERT(nJumps==1 /*when nrowLimit supplied*/ || nJumps==2 /*small files*/, "nJumps (%d) != 1|2", nJumps);
22722272
nJumps=1;
@@ -2364,10 +2364,10 @@ int freadMain(freadMainArgs _args) {
23642364
}
23652365
}
23662366

2367-
const char *tch = jump==jump0 ? headPos : nextGoodLine(pos+(size_t)jump*chunkBytes, ncol);
2367+
const char *tch = jump==jump0 ? headPos : nextGoodLine(pos+jump*chunkBytes, ncol);
23682368
const char *thisJumpStart = tch; // "this" for prev/this/next adjective used later, rather than a (mere) t prefix for thread-local.
23692369
const char *tLineStart = tch;
2370-
const char *nextJumpStart = jump<nJumps-1 ? nextGoodLine(pos+(size_t)(jump+1)*chunkBytes, ncol) : eof;
2370+
const char *nextJumpStart = jump<nJumps-1 ? nextGoodLine(pos+(jump+1)*chunkBytes, ncol) : eof;
23712371

23722372
void *targets[9] = {NULL, ctx.buff1, NULL, NULL, ctx.buff4, NULL, NULL, NULL, ctx.buff8};
23732373
FieldParseContext fctx = {

0 commit comments

Comments
 (0)