Trim trailing whitespace in string-to-int/long conversions

This commit is contained in:
K. Lange 2022-06-29 14:55:46 +09:00
parent a354c4a2c1
commit 2000161d34
2 changed files with 10 additions and 2 deletions

View File

@ -1043,6 +1043,11 @@ static int convert_digit(char c) {
return 0; return 0;
} }
static int is_whitespace(char c) {
/* Same set we use in str.strip() by default */
return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
}
/** /**
* @brief Parse a number into a long. * @brief Parse a number into a long.
* *
@ -1057,7 +1062,10 @@ static int krk_long_parse_string(const char * str, KrkLong * num, unsigned int b
int sign = 1; int sign = 1;
/* Skip any leading whitespace */ /* Skip any leading whitespace */
while (c < end && *c && (*c == ' ' || *c == '\t')) c++; while (c < end && is_whitespace(*c)) c++;
/* Trim any trailing whitespace */
while (end > c && is_whitespace(end[-1])) end--;
/* If there's nothing here, that's invalid. */ /* If there's nothing here, that's invalid. */
if (c >= end) { if (c >= end) {

View File

@ -1,6 +1,6 @@
from fileio import open from fileio import open
let f = open('test/day1.in','r') let f = open('test/day1.in','r')
let lines = [int(x) for x in f.read().split("\n")][:-1] # because that split included a blank let lines = [int(x) for x in f.readlines()]
f.close() f.close()
# Make a bad set # Make a bad set