Skip to content

Commit bc8e0a0

Browse files
author
MarcoGorelli
committed
use enum in C too
1 parent e0da899 commit bc8e0a0

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

pandas/_libs/tslibs/np_datetime.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ cdef extern from "src/datetime/np_datetime_strings.h":
5353
npy_datetimestruct *out,
5454
NPY_DATETIMEUNIT *out_bestunit,
5555
int *out_local, int *out_tzoffset,
56-
const char *format, int format_len, int exact)
56+
const char *format, int format_len, Exact exact)
5757

5858

5959
# ----------------------------------------------------------------------

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ This file implements string parsing and creation for NumPy datetime.
7979
// and decrement characters_remaining by n on success
8080
// On failure will return -1 without incrementing
8181
static int compare_format(const char **format, int *characters_remaining,
82-
const char *compare_to, int n, const int exact) {
83-
if (exact == 2) {
82+
const char *compare_to, int n, const enum Exact exact) {
83+
if (exact == NO_MATCH) {
8484
return 0;
8585
}
8686
if (*characters_remaining < n) {
@@ -103,7 +103,8 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
103103
npy_datetimestruct *out,
104104
NPY_DATETIMEUNIT *out_bestunit,
105105
int *out_local, int *out_tzoffset,
106-
const char* format, int format_len, int exact) {
106+
const char* format, int format_len,
107+
enum Exact exact) {
107108
if (len < 0 || format_len < 0)
108109
goto parse_error;
109110
int year_leap = 0;
@@ -140,7 +141,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
140141
while (sublen > 0 && isspace(*substr)) {
141142
++substr;
142143
--sublen;
143-
if (exact == 0 && !format_len) {
144+
if (exact == INEXACT_MATCH && !format_len) {
144145
goto finish;
145146
}
146147
if (compare_format(&format, &format_len, " ", 1, exact)) {
@@ -159,7 +160,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
159160
}
160161

161162
/* PARSE THE YEAR (4 digits) */
162-
if (exact == 0 && !format_len) {
163+
if (exact == INEXACT_MATCH && !format_len) {
163164
goto finish;
164165
}
165166
if (compare_format(&format, &format_len, "%Y", 2, exact)) {
@@ -209,7 +210,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
209210
++substr;
210211
--sublen;
211212

212-
if (exact == 0 && !format_len) {
213+
if (exact == INEXACT_MATCH && !format_len) {
213214
goto finish;
214215
}
215216
if (compare_format(&format, &format_len, &ymd_sep, 1, exact)) {
@@ -222,7 +223,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
222223
}
223224

224225
/* PARSE THE MONTH */
225-
if (exact == 0 && !format_len) {
226+
if (exact == INEXACT_MATCH && !format_len) {
226227
goto finish;
227228
}
228229
if (compare_format(&format, &format_len, "%m", 2, exact)) {
@@ -271,7 +272,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
271272
}
272273
++substr;
273274
--sublen;
274-
if (exact == 0 && !format_len) {
275+
if (exact == INEXACT_MATCH && !format_len) {
275276
goto finish;
276277
}
277278
if (compare_format(&format, &format_len, &ymd_sep, 1, exact)) {
@@ -280,7 +281,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
280281
}
281282

282283
/* PARSE THE DAY */
283-
if (exact == 0 && !format_len) {
284+
if (exact == INEXACT_MATCH && !format_len) {
284285
goto finish;
285286
}
286287
if (compare_format(&format, &format_len, "%d", 2, exact)) {
@@ -325,7 +326,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
325326
if ((*substr != 'T' && *substr != ' ') || sublen == 1) {
326327
goto parse_error;
327328
}
328-
if (exact == 0 && !format_len) {
329+
if (exact == INEXACT_MATCH && !format_len) {
329330
goto finish;
330331
}
331332
if (compare_format(&format, &format_len, substr, 1, exact)) {
@@ -335,7 +336,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
335336
--sublen;
336337

337338
/* PARSE THE HOURS */
338-
if (exact == 0 && !format_len) {
339+
if (exact == INEXACT_MATCH && !format_len) {
339340
goto finish;
340341
}
341342
if (compare_format(&format, &format_len, "%H", 2, exact)) {
@@ -384,7 +385,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
384385
if (sublen == 0 || !isdigit(*substr)) {
385386
goto parse_error;
386387
}
387-
if (exact == 0 && !format_len) {
388+
if (exact == INEXACT_MATCH && !format_len) {
388389
goto finish;
389390
}
390391
if (compare_format(&format, &format_len, ":", 1, exact)) {
@@ -398,7 +399,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
398399
}
399400

400401
/* PARSE THE MINUTES */
401-
if (exact == 0 && !format_len) {
402+
if (exact == INEXACT_MATCH && !format_len) {
402403
goto finish;
403404
}
404405
if (compare_format(&format, &format_len, "%M", 2, exact)) {
@@ -436,7 +437,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
436437
/* If we make it through this condition block, then the next
437438
* character is a digit. */
438439
if (has_hms_sep && *substr == ':') {
439-
if (exact == 0 && !format_len) {
440+
if (exact == INEXACT_MATCH && !format_len) {
440441
goto finish;
441442
}
442443
if (compare_format(&format, &format_len, ":", 1, exact)) {
@@ -454,7 +455,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
454455
}
455456

456457
/* PARSE THE SECONDS */
457-
if (exact == 0 && !format_len) {
458+
if (exact == INEXACT_MATCH && !format_len) {
458459
goto finish;
459460
}
460461
if (compare_format(&format, &format_len, "%S", 2, exact)) {
@@ -485,7 +486,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
485486
if (sublen > 0 && *substr == '.') {
486487
++substr;
487488
--sublen;
488-
if (exact == 0 && !format_len) {
489+
if (exact == INEXACT_MATCH && !format_len) {
489490
goto finish;
490491
}
491492
if (compare_format(&format, &format_len, ".", 1, exact)) {
@@ -497,7 +498,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
497498
}
498499

499500
/* PARSE THE MICROSECONDS (0 to 6 digits) */
500-
if (exact == 0 && !format_len) {
501+
if (exact == INEXACT_MATCH && !format_len) {
501502
goto finish;
502503
}
503504
if (compare_format(&format, &format_len, "%f", 2, exact)) {
@@ -567,7 +568,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
567568
while (sublen > 0 && isspace(*substr)) {
568569
++substr;
569570
--sublen;
570-
if (exact == 0 && !format_len) {
571+
if (exact == INEXACT_MATCH && !format_len) {
571572
goto finish;
572573
}
573574
if (compare_format(&format, &format_len, " ", 1, exact)) {
@@ -585,7 +586,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
585586

586587
/* UTC specifier */
587588
if (*substr == 'Z') {
588-
if (exact == 0 && !format_len) {
589+
if (exact == INEXACT_MATCH && !format_len) {
589590
goto finish;
590591
}
591592
if (compare_format(&format, &format_len, "%z", 2, exact)) {
@@ -610,7 +611,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
610611
--sublen;
611612
}
612613
} else if (*substr == '-' || *substr == '+') {
613-
if (exact == 0 && !format_len) {
614+
if (exact == INEXACT_MATCH && !format_len) {
614615
goto finish;
615616
}
616617
if (compare_format(&format, &format_len, "%z", 2, exact)) {
@@ -699,7 +700,7 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
699700
while (sublen > 0 && isspace(*substr)) {
700701
++substr;
701702
--sublen;
702-
if (exact == 0 && !format_len) {
703+
if (exact == INEXACT_MATCH && !format_len) {
703704
goto finish;
704705
}
705706
if (compare_format(&format, &format_len, " ", 1, exact)) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ This file implements string parsing and creation for NumPy datetime.
2626
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
2727
#endif // NPY_NO_DEPRECATED_API
2828

29+
enum Exact {
30+
INEXACT_MATCH,
31+
EXACT_MATCH,
32+
NO_MATCH
33+
};
34+
2935
/*
3036
* Parses (almost) standard ISO 8601 date strings. The differences are:
3137
*
@@ -61,7 +67,7 @@ parse_iso_8601_datetime(const char *str, int len, int want_exc,
6167
int *out_tzoffset,
6268
const char* format,
6369
int format_len,
64-
int exact);
70+
enum Exact exact);
6571

6672
/*
6773
* Provides a string length to use for converting datetime

0 commit comments

Comments
 (0)