* The following two string conversion functions are largely equivalent * in Pandas. They are in the header file here, to ensure they can be easily * inline in the other function. * Unlike pandas, pass in end-pointer (do not rely on \0) and return 0 or -1. * * The actual functions are defined using macro templating below. */
| 17 | * The actual functions are defined using macro templating below. |
| 18 | */ |
| 19 | NPY_FINLINE int |
| 20 | str_to_int64( |
| 21 | const Py_UCS4 *p_item, const Py_UCS4 *p_end, |
| 22 | int64_t int_min, int64_t int_max, int64_t *result) |
| 23 | { |
| 24 | const Py_UCS4 *p = (const Py_UCS4 *)p_item; |
| 25 | bool isneg = 0; |
| 26 | int64_t number = 0; |
| 27 | |
| 28 | // Skip leading spaces. |
| 29 | while (Py_UNICODE_ISSPACE(*p)) { |
| 30 | ++p; |
| 31 | } |
| 32 | |
| 33 | // Handle sign. |
| 34 | if (*p == '-') { |
| 35 | isneg = true; |
| 36 | ++p; |
| 37 | } |
| 38 | else if (*p == '+') { |
| 39 | p++; |
| 40 | } |
| 41 | |
| 42 | // Check that there is a first digit. |
| 43 | if (!isdigit(*p)) { |
| 44 | return -1; |
| 45 | } |
| 46 | |
| 47 | if (isneg) { |
| 48 | // If number is greater than pre_min, at least one more digit |
| 49 | // can be processed without overflowing. |
| 50 | int dig_pre_min = -(int_min % 10); |
| 51 | int64_t pre_min = int_min / 10; |
| 52 | |
| 53 | // Process the digits. |
| 54 | int d = *p; |
| 55 | while (isdigit(d)) { |
| 56 | if ((number > pre_min) || ((number == pre_min) && (d - '0' <= dig_pre_min))) { |
| 57 | number = number * 10 - (d - '0'); |
| 58 | d = *++p; |
| 59 | } |
| 60 | else { |
| 61 | return -1; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | else { |
| 66 | // If number is less than pre_max, at least one more digit |
| 67 | // can be processed without overflowing. |
| 68 | int64_t pre_max = int_max / 10; |
| 69 | int dig_pre_max = int_max % 10; |
| 70 | |
| 71 | // Process the digits. |
| 72 | int d = *p; |
| 73 | while (isdigit(d)) { |
| 74 | if ((number < pre_max) || ((number == pre_max) && (d - '0' <= dig_pre_max))) { |
| 75 | number = number * 10 + (d - '0'); |
| 76 | d = *++p; |
no test coverage detected