Skip to content

Commit 12721b0

Browse files
author
MarcoGorelli
committed
replace macro with function
1 parent afc4d96 commit 12721b0

File tree

2 files changed

+71
-40
lines changed

2 files changed

+71
-40
lines changed

.pre-commit-config.yaml

+8-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ repos:
4848
# this particular codebase (e.g. src/headers, src/klib). However,
4949
# we can lint all header files since they aren't "generated" like C files are.
5050
exclude: ^pandas/_libs/src/(klib|headers)/
51-
args: [--quiet, '--extensions=c,h', '--headers=h', --recursive, '--filter=-readability/casting,-runtime/int,-build/include_subdir']
51+
args: [
52+
--quiet,
53+
'--extensions=c,h',
54+
'--headers=h',
55+
--recursive,
56+
'--filter=-readability/casting,-runtime/int,-build/include_subdir',
57+
'--linelength=88'
58+
]
5259
- repo: https://github.com/PyCQA/flake8
5360
rev: 5.0.4
5461
hooks:

pandas/_libs/tslibs/src/datetime/np_datetime_strings.c

+63-39
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,15 @@ This file implements string parsing and creation for NumPy datetime.
6767
* Returns 0 on success, -1 on failure.
6868
*/
6969

70-
#define FORMAT_STARTSWITH(ch) \
71-
/* Always error on character mismatch conditioned on non-exhausted format, \
72-
or when format is exhausted in the exact case. */ \
73-
if ((format_len && *format != ch) || (exact && !format_len)){ \
74-
goto parse_error; \
75-
} \
76-
/* Advance if format is not exhausted */ \
77-
if (format_len) { \
78-
++format; \
79-
--format_len; \
80-
} \
70+
int format_startswith(char ch, int format_len, char format, int exact) {
71+
if ((format_len && format != ch) || (exact && !format_len)) {
72+
return 0;
73+
}
74+
return 1;
75+
}
8176

8277
int parse_iso_8601_datetime(const char *str, int len, int want_exc,
78+
8379
npy_datetimestruct *out,
8480
NPY_DATETIMEUNIT *out_bestunit,
8581
int *out_local, int *out_tzoffset,
@@ -118,18 +114,22 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
118114
while (sublen > 0 && isspace(*substr)) {
119115
++substr;
120116
--sublen;
121-
FORMAT_STARTSWITH(' ');
117+
if (!format_startswith(' ', format_len, *format, exact)) goto parse_error;
118+
if (format_len) {++format; --format_len;}
122119
}
123120

124121
/* Leading '-' sign for negative year */
125122
if (*substr == '-') {
126123
++substr;
127124
--sublen;
128-
FORMAT_STARTSWITH('-');
125+
if (!format_startswith('-', format_len, *format, exact)) goto parse_error;
126+
if (format_len) {++format; --format_len;}
129127
}
130128

131-
FORMAT_STARTSWITH('%');
132-
FORMAT_STARTSWITH('Y');
129+
if (!format_startswith('%', format_len, *format, exact)) goto parse_error;
130+
if (format_len) {++format; --format_len;}
131+
if (!format_startswith('Y', format_len, *format, exact)) goto parse_error;
132+
if (format_len) {++format; --format_len;}
133133

134134
if (sublen == 0) {
135135
goto parse_error;
@@ -178,7 +178,8 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
178178
ymd_sep = valid_ymd_sep[i];
179179
++substr;
180180
--sublen;
181-
FORMAT_STARTSWITH(ymd_sep);
181+
if (!format_startswith(ymd_sep, format_len, *format, exact)) goto parse_error;
182+
if (format_len) {++format; --format_len;}
182183
/* Cannot have trailing separator */
183184
if (sublen == 0 || !isdigit(*substr)) {
184185
goto parse_error;
@@ -190,8 +191,10 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
190191
out->month = (*substr - '0');
191192
++substr;
192193
--sublen;
193-
FORMAT_STARTSWITH('%');
194-
FORMAT_STARTSWITH('m');
194+
if (!format_startswith('%', format_len, *format, exact)) goto parse_error;
195+
if (format_len) {++format; --format_len;}
196+
if (!format_startswith('m', format_len, *format, exact)) goto parse_error;
197+
if (format_len) {++format; --format_len;}
195198
/* Second digit optional if there was a separator */
196199
if (isdigit(*substr)) {
197200
out->month = 10 * out->month + (*substr - '0');
@@ -231,7 +234,8 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
231234
}
232235
++substr;
233236
--sublen;
234-
FORMAT_STARTSWITH(ymd_sep);
237+
if (!format_startswith(ymd_sep, format_len, *format, exact)) goto parse_error;
238+
if (format_len) {++format; --format_len;}
235239
}
236240

237241
/* PARSE THE DAY */
@@ -242,8 +246,10 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
242246
out->day = (*substr - '0');
243247
++substr;
244248
--sublen;
245-
FORMAT_STARTSWITH('%');
246-
FORMAT_STARTSWITH('d');
249+
if (!format_startswith('%', format_len, *format, exact)) goto parse_error;
250+
if (format_len) {++format; --format_len;}
251+
if (!format_startswith('d', format_len, *format, exact)) goto parse_error;
252+
if (format_len) {++format; --format_len;}
247253
/* Second digit optional if there was a separator */
248254
if (isdigit(*substr)) {
249255
out->day = 10 * out->day + (*substr - '0');
@@ -276,7 +282,8 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
276282
if ((*substr != 'T' && *substr != ' ') || sublen == 1) {
277283
goto parse_error;
278284
}
279-
FORMAT_STARTSWITH(*substr);
285+
if (!format_startswith(*substr, format_len, *format, exact)) goto parse_error;
286+
if (format_len) {++format; --format_len;}
280287
++substr;
281288
--sublen;
282289

@@ -285,8 +292,10 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
285292
if (!isdigit(*substr)) {
286293
goto parse_error;
287294
}
288-
FORMAT_STARTSWITH('%');
289-
FORMAT_STARTSWITH('H');
295+
if (!format_startswith('%', format_len, *format, exact)) goto parse_error;
296+
if (format_len) {++format; --format_len;}
297+
if (!format_startswith('H', format_len, *format, exact)) goto parse_error;
298+
if (format_len) {++format; --format_len;}
290299
out->hour = (*substr - '0');
291300
++substr;
292301
--sublen;
@@ -326,7 +335,8 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
326335
if (sublen == 0 || !isdigit(*substr)) {
327336
goto parse_error;
328337
}
329-
FORMAT_STARTSWITH(':');
338+
if (!format_startswith(':', format_len, *format, exact)) goto parse_error;
339+
if (format_len) {++format; --format_len;}
330340
} else if (!isdigit(*substr)) {
331341
if (!hour_was_2_digits) {
332342
goto parse_error;
@@ -339,8 +349,10 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
339349
out->min = (*substr - '0');
340350
++substr;
341351
--sublen;
342-
FORMAT_STARTSWITH('%');
343-
FORMAT_STARTSWITH('M');
352+
if (!format_startswith('%', format_len, *format, exact)) goto parse_error;
353+
if (format_len) {++format; --format_len;}
354+
if (!format_startswith('M', format_len, *format, exact)) goto parse_error;
355+
if (format_len) {++format; --format_len;}
344356
/* Second digit optional if there was a separator */
345357
if (isdigit(*substr)) {
346358
out->min = 10 * out->min + (*substr - '0');
@@ -369,7 +381,8 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
369381
/* If we make it through this condition block, then the next
370382
* character is a digit. */
371383
if (has_hms_sep && *substr == ':') {
372-
FORMAT_STARTSWITH(':');
384+
if (!format_startswith(':', format_len, *format, exact)) goto parse_error;
385+
if (format_len) {++format; --format_len;}
373386
++substr;
374387
--sublen;
375388
/* Cannot have a trailing ':' */
@@ -386,8 +399,10 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
386399
out->sec = (*substr - '0');
387400
++substr;
388401
--sublen;
389-
FORMAT_STARTSWITH('%');
390-
FORMAT_STARTSWITH('S');
402+
if (!format_startswith('%', format_len, *format, exact)) goto parse_error;
403+
if (format_len) {++format; --format_len;}
404+
if (!format_startswith('S', format_len, *format, exact)) goto parse_error;
405+
if (format_len) {++format; --format_len;}
391406
/* Second digit optional if there was a separator */
392407
if (isdigit(*substr)) {
393408
out->sec = 10 * out->sec + (*substr - '0');
@@ -409,15 +424,18 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
409424
if (sublen > 0 && *substr == '.') {
410425
++substr;
411426
--sublen;
412-
FORMAT_STARTSWITH('.');
427+
if (!format_startswith('.', format_len, *format, exact)) goto parse_error;
428+
if (format_len) {++format; --format_len;}
413429
} else {
414430
bestunit = NPY_FR_s;
415431
goto parse_timezone;
416432
}
417433

418434
/* PARSE THE MICROSECONDS (0 to 6 digits) */
419-
FORMAT_STARTSWITH('%');
420-
FORMAT_STARTSWITH('f');
435+
if (!format_startswith('%', format_len, *format, exact)) goto parse_error;
436+
if (format_len) {++format; --format_len;}
437+
if (!format_startswith('f', format_len, *format, exact)) goto parse_error;
438+
if (format_len) {++format; --format_len;}
421439
numdigits = 0;
422440
for (i = 0; i < 6; ++i) {
423441
out->us *= 10;
@@ -482,7 +500,8 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
482500
while (sublen > 0 && isspace(*substr)) {
483501
++substr;
484502
--sublen;
485-
FORMAT_STARTSWITH(' ');
503+
if (!format_startswith(' ', format_len, *format, exact)) goto parse_error;
504+
if (format_len) {++format; --format_len;}
486505
}
487506

488507
if (sublen == 0) {
@@ -495,8 +514,10 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
495514

496515
/* UTC specifier */
497516
if (*substr == 'Z') {
498-
FORMAT_STARTSWITH('%');
499-
FORMAT_STARTSWITH('Z');
517+
if (!format_startswith('%', format_len, *format, exact)) goto parse_error;
518+
if (format_len) {++format; --format_len;}
519+
if (!format_startswith('Z', format_len, *format, exact)) goto parse_error;
520+
if (format_len) {++format; --format_len;}
500521

501522
/* "Z" should be equivalent to tz offset "+00:00" */
502523
if (out_local != NULL) {
@@ -517,8 +538,10 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
517538
--sublen;
518539
}
519540
} else if (*substr == '-' || *substr == '+') {
520-
FORMAT_STARTSWITH('%');
521-
FORMAT_STARTSWITH('z');
541+
if (!format_startswith('%', format_len, *format, exact)) goto parse_error;
542+
if (format_len) {++format; --format_len;}
543+
if (!format_startswith('z', format_len, *format, exact)) goto parse_error;
544+
if (format_len) {++format; --format_len;}
522545
/* Time zone offset */
523546
int offset_neg = 0, offset_hour = 0, offset_minute = 0;
524547

@@ -602,7 +625,8 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
602625
while (sublen > 0 && isspace(*substr)) {
603626
++substr;
604627
--sublen;
605-
FORMAT_STARTSWITH(' ');
628+
if (!format_startswith(' ', format_len, *format, exact)) goto parse_error;
629+
if (format_len) {++format; --format_len;}
606630
}
607631

608632
if ((sublen != 0) || (format_len != 0)) {

0 commit comments

Comments
 (0)