-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
CLN: use integer parsing functions from stdlib #62658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
c3cc4a1
2459313
d8a454e
87789e6
2287944
2bea3c2
fb38679
f9ede5c
798c263
2f06f19
280b55e
d85aaf0
9046ecc
a4e2fb8
ef82cf4
5afeb11
0ef47a7
c840ef0
132342b
e6977cc
8120eea
1f5d506
479a2ab
c37c355
7d55283
ffe50ce
af5ad71
9211704
d026b01
d76ff5f
b523a19
6265172
abed6c1
8616f9f
c4e0e25
0b19208
29d74f7
b5b8f3b
803a8bf
31f26cf
171b553
30f6bdc
554675b
7ea4454
82dc037
627df4c
1b3eba0
e1667fa
bee776a
d693345
593c614
ff4d48b
e3a88d3
b135738
ba8c9b3
818921f
3e067f7
c0ed83c
cb60adb
5117e89
e1e327a
ffcb7c2
47b87f9
cd536fb
1ef9259
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,8 +21,11 @@ GitHub. See Python Software Foundation License and BSD licenses for these. | |||||
|
|
||||||
| #include <ctype.h> | ||||||
| #include <float.h> | ||||||
| #include <limits.h> | ||||||
| #include <math.h> | ||||||
| #include <stdbool.h> | ||||||
| #include <stddef.h> | ||||||
| #include <stdlib.h> | ||||||
|
|
||||||
| #include "pandas/portable.h" | ||||||
| #include "pandas/vendored/klib/khash.h" // for kh_int64_t, kh_destroy_int64 | ||||||
|
|
@@ -1834,201 +1837,203 @@ int uint64_conflict(uint_state *self) { | |||||
| return self->seen_uint && (self->seen_sint || self->seen_null); | ||||||
| } | ||||||
|
|
||||||
| int64_t str_to_int64(const char *p_item, int64_t int_min, int64_t int_max, | ||||||
| int *error, char tsep) { | ||||||
| const char *p = p_item; | ||||||
| // Skip leading spaces. | ||||||
| while (isspace_ascii(*p)) { | ||||||
| ++p; | ||||||
| /** | ||||||
| * @brief Check if the character in the pointer indicates a number. | ||||||
| * It expects that you consumed all leading whitespace. | ||||||
| * | ||||||
| * @param p_item Pointer to verify | ||||||
| * @return Non-zero integer indicating that has a digit 0 otherwise. | ||||||
| */ | ||||||
| static inline bool has_digit_int(const char *str) { | ||||||
| if (!str || *str == '\0') { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| switch (*str) { | ||||||
| case '0': | ||||||
| case '1': | ||||||
| case '2': | ||||||
| case '3': | ||||||
| case '4': | ||||||
| case '5': | ||||||
| case '6': | ||||||
| case '7': | ||||||
| case '8': | ||||||
| case '9': | ||||||
| return true; | ||||||
| case '+': | ||||||
| case '-': | ||||||
| return isdigit_ascii(str[1]); | ||||||
|
||||||
| default: | ||||||
| return false; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Handle sign. | ||||||
| const bool isneg = *p == '-' ? true : false; | ||||||
| // Handle sign. | ||||||
| if (isneg || (*p == '+')) { | ||||||
| p++; | ||||||
| static inline bool has_only_spaces(const char *str) { | ||||||
| while (*str != '\0' && isspace_ascii(*str)) { | ||||||
| str++; | ||||||
| } | ||||||
| return *str == '\0'; | ||||||
| } | ||||||
|
|
||||||
| // Check that there is a first digit. | ||||||
| if (!isdigit_ascii(*p)) { | ||||||
| // Error... | ||||||
| *error = ERROR_NO_DIGITS; | ||||||
| static int power_int(int base, int exponent) { | ||||||
|
||||||
| // https://en.wikipedia.org/wiki/Exponentiation_by_squaring | ||||||
| if (exponent == 0) { | ||||||
| return 1; | ||||||
| } else if (exponent < 0) { | ||||||
| return 0; | ||||||
| } | ||||||
|
|
||||||
| int64_t number = 0; | ||||||
| if (isneg) { | ||||||
| // If number is greater than pre_min, at least one more digit | ||||||
| // can be processed without overflowing. | ||||||
| int dig_pre_min = -(int_min % 10); | ||||||
| int64_t pre_min = int_min / 10; | ||||||
|
|
||||||
| // Process the digits. | ||||||
| char d = *p; | ||||||
| if (tsep != '\0') { | ||||||
| while (1) { | ||||||
| if (d == tsep) { | ||||||
| d = *++p; | ||||||
| continue; | ||||||
| } else if (!isdigit_ascii(d)) { | ||||||
| break; | ||||||
| } | ||||||
| if ((number > pre_min) || | ||||||
| ((number == pre_min) && (d - '0' <= dig_pre_min))) { | ||||||
| number = number * 10 - (d - '0'); | ||||||
| d = *++p; | ||||||
| } else { | ||||||
| *error = ERROR_OVERFLOW; | ||||||
| return 0; | ||||||
| } | ||||||
| } | ||||||
| } else { | ||||||
| while (isdigit_ascii(d)) { | ||||||
| if ((number > pre_min) || | ||||||
| ((number == pre_min) && (d - '0' <= dig_pre_min))) { | ||||||
| number = number * 10 - (d - '0'); | ||||||
| d = *++p; | ||||||
| } else { | ||||||
| *error = ERROR_OVERFLOW; | ||||||
| return 0; | ||||||
| } | ||||||
| } | ||||||
| int result = 1; | ||||||
|
|
||||||
| while (exponent > 1) { | ||||||
| if (exponent % 2 == 1) { | ||||||
| result *= base; | ||||||
| exponent--; | ||||||
| } | ||||||
| } else { | ||||||
| // If number is less than pre_max, at least one more digit | ||||||
| // can be processed without overflowing. | ||||||
| int64_t pre_max = int_max / 10; | ||||||
| int dig_pre_max = int_max % 10; | ||||||
|
|
||||||
| // Process the digits. | ||||||
| char d = *p; | ||||||
| if (tsep != '\0') { | ||||||
| while (1) { | ||||||
| if (d == tsep) { | ||||||
| d = *++p; | ||||||
| continue; | ||||||
| } else if (!isdigit_ascii(d)) { | ||||||
| break; | ||||||
| } | ||||||
| if ((number < pre_max) || | ||||||
| ((number == pre_max) && (d - '0' <= dig_pre_max))) { | ||||||
| number = number * 10 + (d - '0'); | ||||||
| d = *++p; | ||||||
| result *= result; | ||||||
| exponent /= 2; | ||||||
| } | ||||||
|
|
||||||
| } else { | ||||||
| *error = ERROR_OVERFLOW; | ||||||
| return 0; | ||||||
| } | ||||||
| } | ||||||
| } else { | ||||||
| while (isdigit_ascii(d)) { | ||||||
| if ((number < pre_max) || | ||||||
| ((number == pre_max) && (d - '0' <= dig_pre_max))) { | ||||||
| number = number * 10 + (d - '0'); | ||||||
| d = *++p; | ||||||
| return result * base; | ||||||
|
||||||
| } | ||||||
|
|
||||||
| } else { | ||||||
| *error = ERROR_OVERFLOW; | ||||||
| return 0; | ||||||
| } | ||||||
| } | ||||||
| static inline int64_t add_int_check_overflow(int64_t lhs, int64_t rhs, | ||||||
|
||||||
| int64_t mul_lhs) { | ||||||
| // rhs will always be positive, because this function | ||||||
| // only executes after the first parse, hence the sign will always go to lhs. | ||||||
| // if lhs > 0: | ||||||
| // Will overflow if (mul_lhs * lhs) + rhs > INT_MAX | ||||||
| // iff lhs > (INT_MAX - rhs) / mul_lhs | ||||||
| // if lhs < 0: | ||||||
| // Will underflow if (mul_lhs * lhs) - rhs < INT_MIN | ||||||
| // iff lhs < (INT_MIN + rhs) / mul_lhs | ||||||
| if (lhs >= 0) { | ||||||
| if (lhs > (INT_MAX - rhs) / mul_lhs) { | ||||||
| errno = ERANGE; | ||||||
|
||||||
| } | ||||||
| } else { | ||||||
| if (lhs < (INT_MIN + rhs) / mul_lhs) { | ||||||
| errno = ERANGE; | ||||||
| } | ||||||
| rhs = -rhs; | ||||||
| } | ||||||
| return lhs * mul_lhs + rhs; | ||||||
| } | ||||||
|
|
||||||
| // Skip trailing spaces. | ||||||
| while (isspace_ascii(*p)) { | ||||||
| ++p; | ||||||
| static inline uint64_t add_uint_check_overflow(uint64_t lhs, uint64_t rhs, | ||||||
| uint64_t mul_lhs) { | ||||||
| if (lhs > (UINT_MAX - rhs) / mul_lhs) { | ||||||
| errno = ERANGE; | ||||||
| } | ||||||
| return lhs * mul_lhs + rhs; | ||||||
| } | ||||||
|
|
||||||
| // Did we use up all the characters? | ||||||
| if (*p) { | ||||||
| *error = ERROR_INVALID_CHARS; | ||||||
| int64_t str_to_int64(const char *p_item, int64_t int_min, int64_t int_max, | ||||||
| int *error, char tsep) { | ||||||
| if (!p_item || *p_item == '\0') { | ||||||
|
||||||
| *error = ERROR_NO_DIGITS; | ||||||
| return 0; | ||||||
| } | ||||||
|
|
||||||
| *error = 0; | ||||||
| return number; | ||||||
| while (isspace_ascii(*p_item)) { | ||||||
| ++p_item; | ||||||
| } | ||||||
|
|
||||||
| if (!has_digit_int(p_item)) { | ||||||
| *error = ERROR_NO_DIGITS; | ||||||
| return 0; | ||||||
| } | ||||||
|
|
||||||
| errno = 0; | ||||||
|
||||||
| char *endptr = NULL; | ||||||
WillAyd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| int64_t result = strtoll(p_item, &endptr, 10); | ||||||
|
|
||||||
| while (errno == 0 && tsep != '\0' && *endptr == tsep) { | ||||||
| // Skip multiple consecutive tsep | ||||||
| while (*endptr == tsep) { | ||||||
|
||||||
| endptr++; | ||||||
| } | ||||||
|
|
||||||
| char *new_end = NULL; | ||||||
|
||||||
| int64_t next_part = strtoll(endptr, &new_end, 10); | ||||||
| ptrdiff_t digits = new_end - endptr; | ||||||
| int64_t mul_result = power_int(10, (int)digits); | ||||||
|
||||||
| result = add_int_check_overflow(result, next_part, mul_result); | ||||||
| endptr = new_end; | ||||||
| } | ||||||
|
|
||||||
| if (!has_only_spaces(endptr)) { | ||||||
| // Check first for invalid characters because we may | ||||||
| // want to skip integer parsing if we find one. | ||||||
| *error = ERROR_INVALID_CHARS; | ||||||
| result = 0; | ||||||
| } else if (errno == ERANGE || result > int_max || result < int_min) { | ||||||
|
||||||
| data[i] = str_to_int64(word, INT64_MIN, INT64_MAX, | |
| &error, parser.thousands) |
Should I just check errno or I should also change the function signature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the function signature is fine (let's do as a separate PR)
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you removed the const char *p = p_item assignment as a cleanup and not for any type of functionality, but if that's true its inflating the diff. Should move cleanup items like that to a separate PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made several changes that complicate a lot the diff. I'll put back the const char *p assignment, and also remove the need for some auxiliary functions created.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great - thanks!
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't assign NULL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Going back to my comment made (in another PR?) - if we replace the
char **words member with a custom struct, we could add to that struct a member that flags this as the word is built, rather than coming back after the fact.Not something to tackle in this PR - just wanted to give you an idea