They are so insanely stupid.
if (result > INT_MAX / 10)
That first conditional breaks the AI's brain, I guess. Result represents the previous single digit added to the rolling result. MAX_INT is 2,147,483,647. That line stops it with a check of that rolling result against 214,748,364. If it's (result) greater than 214,748,364 it returns. AI doesn't understand that.
int TryInt(char *get, size_t size, int *outInt)
{ // printf("Bytes = %d\n", size);
if (!get || !outInt)
return 1;
int i = 0;
int result = 0;
int sign = 1;
if (get[0] == '-') {
sign = -1;
i = 1;
}
while (get[i] != '\0' && get[i] != '\n' && i < size) {
if (get[i] < '0' || get[i] > '9') /* Confirm (by negation) that we have a number in this [i] position. */
return -1;
/* INT_MAX = 2,147,483,647
* INT_MIN = -2,147,483,648 */
if (result > INT_MAX / 10) /* Confirm our current result does not exceed 214,748,364 (INT_MAX / 10), which would automatically be wrong. */
return -2;
int curDigit = get[i] - 48; /* This is the integer offset for char -> # | i.e. 0 = 48 .. 9 = 57 */
if (result == INT_MAX / 10 && ((sign == 1 && curDigit > 7) || (sign == -1 && curDigit > 8)))
return -2;
result = (result * 10) + ((int) get[i] - 48);
i++;
}
if (i > 0)
*outInt = result * sign;
return !(i > 0);
}
They are so insanely stupid.
if (result > INT_MAX / 10)
That first conditional breaks the AI's brain, I guess. Result represents the previous single digit added to the rolling result. MAX_INT is 2,147,483,647. That line stops it with a check of that rolling result against 214,748,364. If it's (result) greater than 214,748,364 it returns. AI doesn't understand that.
`int TryInt(char *get, size_t size, int *outInt)`
`{ // printf("Bytes = %d\n", size);`
` if (!get || !outInt)`
` return 1;`
` int i = 0;`
` int result = 0;`
` int sign = 1;`
` if (get[0] == '-') {`
` sign = -1;`
` i = 1;`
` }`
` while (get[i] != '\0' && get[i] != '\n' && i < size) {`
` if (get[i] < '0' || get[i] > '9') /* Confirm (by negation) that we have a number in this [i] position. */`
` return -1;`
` /* INT_MAX = 2,147,483,647`
` * INT_MIN = -2,147,483,648 */`
` if (result > INT_MAX / 10) /* Confirm our current result does not exceed 214,748,364 (INT_MAX / 10), which would automatically be wrong. */`
` return -2;`
` int curDigit = get[i] - 48; /* This is the integer offset for char -> # | i.e. 0 = 48 .. 9 = 57 */`
` if (result == INT_MAX / 10 && ((sign == 1 && curDigit > 7) || (sign == -1 && curDigit > 8)))`
` return -2;`
` result = (result * 10) + ((int) get[i] - 48);`
` i++;`
` }`
` if (i > 0)`
` *outInt = result * sign;`
` return !(i > 0);`
`}`