Skip to content

Commit e0da899

Browse files
author
MarcoGorelli
committed
simplify
1 parent 466d515 commit e0da899

File tree

1 file changed

+90
-123
lines changed

1 file changed

+90
-123
lines changed

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

Lines changed: 90 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ This file implements string parsing and creation for NumPy datetime.
8080
// On failure will return -1 without incrementing
8181
static int compare_format(const char **format, int *characters_remaining,
8282
const char *compare_to, int n, const int exact) {
83+
if (exact == 2) {
84+
return 0;
85+
}
8386
if (*characters_remaining < n) {
8487
// TODO(pandas-dev): PyErr to differentiate what went wrong
8588
return -1;
@@ -137,13 +140,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
137140
while (sublen > 0 && isspace(*substr)) {
138141
++substr;
139142
--sublen;
140-
if (exact != 2) {
141-
if (exact == 0 && !format_len) {
142-
goto finish;
143-
}
144-
if (compare_format(&format, &format_len, " ", 1, exact)) {
145-
goto parse_error;
146-
}
143+
if (exact == 0 && !format_len) {
144+
goto finish;
145+
}
146+
if (compare_format(&format, &format_len, " ", 1, exact)) {
147+
goto parse_error;
147148
}
148149
}
149150

@@ -158,13 +159,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
158159
}
159160

160161
/* PARSE THE YEAR (4 digits) */
161-
if (exact != 2) {
162-
if (exact == 0 && !format_len) {
163-
goto finish;
164-
}
165-
if (compare_format(&format, &format_len, "%Y", 2, exact)) {
166-
goto parse_error;
167-
}
162+
if (exact == 0 && !format_len) {
163+
goto finish;
164+
}
165+
if (compare_format(&format, &format_len, "%Y", 2, exact)) {
166+
goto parse_error;
168167
}
169168

170169
out->year = 0;
@@ -210,13 +209,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
210209
++substr;
211210
--sublen;
212211

213-
if (exact != 2) {
214-
if (exact == 0 && !format_len) {
215-
goto finish;
216-
}
217-
if (compare_format(&format, &format_len, &ymd_sep, 1, exact)) {
218-
goto parse_error;
219-
}
212+
if (exact == 0 && !format_len) {
213+
goto finish;
214+
}
215+
if (compare_format(&format, &format_len, &ymd_sep, 1, exact)) {
216+
goto parse_error;
220217
}
221218
/* Cannot have trailing separator */
222219
if (sublen == 0 || !isdigit(*substr)) {
@@ -225,13 +222,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
225222
}
226223

227224
/* PARSE THE MONTH */
228-
if (exact != 2) {
229-
if (exact == 0 && !format_len) {
230-
goto finish;
231-
}
232-
if (compare_format(&format, &format_len, "%m", 2, exact)) {
233-
goto parse_error;
234-
}
225+
if (exact == 0 && !format_len) {
226+
goto finish;
227+
}
228+
if (compare_format(&format, &format_len, "%m", 2, exact)) {
229+
goto parse_error;
235230
}
236231
/* First digit required */
237232
out->month = (*substr - '0');
@@ -276,25 +271,21 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
276271
}
277272
++substr;
278273
--sublen;
279-
if (exact != 2) {
280-
if (exact == 0 && !format_len) {
281-
goto finish;
282-
}
283-
if (compare_format(&format, &format_len, &ymd_sep, 1, exact)) {
284-
goto parse_error;
285-
}
286-
}
287-
}
288-
289-
/* PARSE THE DAY */
290-
if (exact != 2) {
291274
if (exact == 0 && !format_len) {
292275
goto finish;
293276
}
294-
if (compare_format(&format, &format_len, "%d", 2, exact)) {
277+
if (compare_format(&format, &format_len, &ymd_sep, 1, exact)) {
295278
goto parse_error;
296279
}
297280
}
281+
282+
/* PARSE THE DAY */
283+
if (exact == 0 && !format_len) {
284+
goto finish;
285+
}
286+
if (compare_format(&format, &format_len, "%d", 2, exact)) {
287+
goto parse_error;
288+
}
298289
/* First digit required */
299290
if (!isdigit(*substr)) {
300291
goto parse_error;
@@ -334,25 +325,21 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
334325
if ((*substr != 'T' && *substr != ' ') || sublen == 1) {
335326
goto parse_error;
336327
}
337-
if (exact != 2) {
338-
if (exact == 0 && !format_len) {
339-
goto finish;
340-
}
341-
if (compare_format(&format, &format_len, substr, 1, exact)) {
342-
goto parse_error;
343-
}
344-
}
345-
++substr;
346-
--sublen;
347-
348-
/* PARSE THE HOURS */
349-
if (exact != 2) {
350328
if (exact == 0 && !format_len) {
351329
goto finish;
352330
}
353-
if (compare_format(&format, &format_len, "%H", 2, exact)) {
331+
if (compare_format(&format, &format_len, substr, 1, exact)) {
354332
goto parse_error;
355333
}
334+
++substr;
335+
--sublen;
336+
337+
/* PARSE THE HOURS */
338+
if (exact == 0 && !format_len) {
339+
goto finish;
340+
}
341+
if (compare_format(&format, &format_len, "%H", 2, exact)) {
342+
goto parse_error;
356343
}
357344
/* First digit required */
358345
if (!isdigit(*substr)) {
@@ -397,13 +384,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
397384
if (sublen == 0 || !isdigit(*substr)) {
398385
goto parse_error;
399386
}
400-
if (exact != 2) {
401-
if (exact == 0 && !format_len) {
402-
goto finish;
403-
}
404-
if (compare_format(&format, &format_len, ":", 1, exact)) {
405-
goto parse_error;
406-
}
387+
if (exact == 0 && !format_len) {
388+
goto finish;
389+
}
390+
if (compare_format(&format, &format_len, ":", 1, exact)) {
391+
goto parse_error;
407392
}
408393
} else if (!isdigit(*substr)) {
409394
if (!hour_was_2_digits) {
@@ -413,13 +398,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
413398
}
414399

415400
/* PARSE THE MINUTES */
416-
if (exact != 2) {
417-
if (exact == 0 && !format_len) {
418-
goto finish;
419-
}
420-
if (compare_format(&format, &format_len, "%M", 2, exact)) {
421-
goto parse_error;
422-
}
401+
if (exact == 0 && !format_len) {
402+
goto finish;
403+
}
404+
if (compare_format(&format, &format_len, "%M", 2, exact)) {
405+
goto parse_error;
423406
}
424407
/* First digit required */
425408
out->min = (*substr - '0');
@@ -453,13 +436,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
453436
/* If we make it through this condition block, then the next
454437
* character is a digit. */
455438
if (has_hms_sep && *substr == ':') {
456-
if (exact != 2) {
457-
if (exact == 0 && !format_len) {
458-
goto finish;
459-
}
460-
if (compare_format(&format, &format_len, ":", 1, exact)) {
461-
goto parse_error;
462-
}
439+
if (exact == 0 && !format_len) {
440+
goto finish;
441+
}
442+
if (compare_format(&format, &format_len, ":", 1, exact)) {
443+
goto parse_error;
463444
}
464445
++substr;
465446
--sublen;
@@ -473,13 +454,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
473454
}
474455

475456
/* PARSE THE SECONDS */
476-
if (exact != 2) {
477-
if (exact == 0 && !format_len) {
478-
goto finish;
479-
}
480-
if (compare_format(&format, &format_len, "%S", 2, exact)) {
481-
goto parse_error;
457+
if (exact == 0 && !format_len) {
458+
goto finish;
482459
}
460+
if (compare_format(&format, &format_len, "%S", 2, exact)) {
461+
goto parse_error;
483462
}
484463
/* First digit required */
485464
out->sec = (*substr - '0');
@@ -506,27 +485,23 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
506485
if (sublen > 0 && *substr == '.') {
507486
++substr;
508487
--sublen;
509-
if (exact != 2) {
510-
if (exact == 0 && !format_len) {
511-
goto finish;
512-
}
513-
if (compare_format(&format, &format_len, ".", 1, exact)) {
514-
goto parse_error;
515-
}
488+
if (exact == 0 && !format_len) {
489+
goto finish;
490+
}
491+
if (compare_format(&format, &format_len, ".", 1, exact)) {
492+
goto parse_error;
516493
}
517494
} else {
518495
bestunit = NPY_FR_s;
519496
goto parse_timezone;
520497
}
521498

522499
/* PARSE THE MICROSECONDS (0 to 6 digits) */
523-
if (exact != 2) {
524-
if (exact == 0 && !format_len) {
525-
goto finish;
526-
}
527-
if (compare_format(&format, &format_len, "%f", 2, exact)) {
528-
goto parse_error;
500+
if (exact == 0 && !format_len) {
501+
goto finish;
529502
}
503+
if (compare_format(&format, &format_len, "%f", 2, exact)) {
504+
goto parse_error;
530505
}
531506
numdigits = 0;
532507
for (i = 0; i < 6; ++i) {
@@ -592,13 +567,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
592567
while (sublen > 0 && isspace(*substr)) {
593568
++substr;
594569
--sublen;
595-
if (exact != 2) {
596-
if (exact == 0 && !format_len) {
597-
goto finish;
598-
}
599-
if (compare_format(&format, &format_len, " ", 1, exact)) {
600-
goto parse_error;
601-
}
570+
if (exact == 0 && !format_len) {
571+
goto finish;
572+
}
573+
if (compare_format(&format, &format_len, " ", 1, exact)) {
574+
goto parse_error;
602575
}
603576
}
604577

@@ -612,13 +585,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
612585

613586
/* UTC specifier */
614587
if (*substr == 'Z') {
615-
if (exact != 2) {
616-
if (exact == 0 && !format_len) {
617-
goto finish;
618-
}
619-
if (compare_format(&format, &format_len, "%z", 2, exact)) {
620-
goto parse_error;
621-
}
588+
if (exact == 0 && !format_len) {
589+
goto finish;
590+
}
591+
if (compare_format(&format, &format_len, "%z", 2, exact)) {
592+
goto parse_error;
622593
}
623594
/* "Z" should be equivalent to tz offset "+00:00" */
624595
if (out_local != NULL) {
@@ -639,13 +610,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
639610
--sublen;
640611
}
641612
} else if (*substr == '-' || *substr == '+') {
642-
if (exact != 2) {
643-
if (exact == 0 && !format_len) {
644-
goto finish;
645-
}
646-
if (compare_format(&format, &format_len, "%z", 2, exact)) {
647-
goto parse_error;
648-
}
613+
if (exact == 0 && !format_len) {
614+
goto finish;
615+
}
616+
if (compare_format(&format, &format_len, "%z", 2, exact)) {
617+
goto parse_error;
649618
}
650619
/* Time zone offset */
651620
int offset_neg = 0, offset_hour = 0, offset_minute = 0;
@@ -730,13 +699,11 @@ int parse_iso_8601_datetime(const char *str, int len, int want_exc,
730699
while (sublen > 0 && isspace(*substr)) {
731700
++substr;
732701
--sublen;
733-
if (exact != 2) {
734-
if (exact == 0 && !format_len) {
735-
goto finish;
736-
}
737-
if (compare_format(&format, &format_len, " ", 1, exact)) {
738-
goto parse_error;
739-
}
702+
if (exact == 0 && !format_len) {
703+
goto finish;
704+
}
705+
if (compare_format(&format, &format_len, " ", 1, exact)) {
706+
goto parse_error;
740707
}
741708
}
742709

0 commit comments

Comments
 (0)