mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 13:52:28 +00:00
Fix int parsing with base 2 and 16 (#107)
This commit is contained in:
parent
fb7b7c6e21
commit
813e11b90b
4 changed files with 19 additions and 6 deletions
|
@ -77,8 +77,8 @@ intmax_t strtoimax(const char *s, char **endptr, int base) {
|
||||||
}
|
}
|
||||||
} else if (*s == '0') {
|
} else if (*s == '0') {
|
||||||
++s;
|
++s;
|
||||||
if (base == 2 && *s == 'b' && *s == 'B') ++s;
|
if (base == 2 && (*s == 'b' || *s == 'B')) ++s;
|
||||||
if (base == 16 && *s == 'x' && *s == 'X') ++s;
|
if (base == 16 && (*s == 'x' || *s == 'X')) ++s;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|
|
@ -51,10 +51,10 @@ uintmax_t strtoumax(const char *s, char **endptr, int base) {
|
||||||
} else {
|
} else {
|
||||||
base = 10;
|
base = 10;
|
||||||
}
|
}
|
||||||
} else if (*s == '0') {
|
} else if (*p == '0') {
|
||||||
++s;
|
++p;
|
||||||
if (base == 2 && *s == 'b' && *s == 'B') ++s;
|
if (base == 2 && (*p == 'b' || *p == 'B')) ++p;
|
||||||
if (base == 16 && *s == 'x' && *s == 'X') ++s;
|
if (base == 16 && (*p == 'x' || *p == 'X')) ++p;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|
|
@ -30,10 +30,17 @@ TEST(strtoimax, testDecimal) {
|
||||||
|
|
||||||
TEST(strtoimax, testHex) {
|
TEST(strtoimax, testHex) {
|
||||||
EXPECT_EQ(-255, strtoimax("-0xff", NULL, 0));
|
EXPECT_EQ(-255, strtoimax("-0xff", NULL, 0));
|
||||||
|
EXPECT_EQ(255, strtoimax("0xff", NULL, 16));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(strtoimax, testOctal) {
|
TEST(strtoimax, testOctal) {
|
||||||
EXPECT_EQ(-123, strtoimax("-0173", NULL, 0));
|
EXPECT_EQ(-123, strtoimax("-0173", NULL, 0));
|
||||||
|
EXPECT_EQ(123, strtoimax("0173", NULL, 8));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(strtoimax, testBinary) {
|
||||||
|
EXPECT_EQ(-42, strtoimax("-0b101010", NULL, 0));
|
||||||
|
EXPECT_EQ(42, strtoimax("0b101010", NULL, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(strtoimax, testLimits) {
|
TEST(strtoimax, testLimits) {
|
||||||
|
|
|
@ -29,9 +29,15 @@ TEST(strtoumax, testDecimal) {
|
||||||
}
|
}
|
||||||
TEST(strtoumax, testHex) {
|
TEST(strtoumax, testHex) {
|
||||||
EXPECT_EQ(255, strtoumax("0xff", NULL, 0));
|
EXPECT_EQ(255, strtoumax("0xff", NULL, 0));
|
||||||
|
EXPECT_EQ(255, strtoumax("0xff", NULL, 16));
|
||||||
}
|
}
|
||||||
TEST(strtoumax, testOctal) {
|
TEST(strtoumax, testOctal) {
|
||||||
EXPECT_EQ(123, strtoumax("0173", NULL, 0));
|
EXPECT_EQ(123, strtoumax("0173", NULL, 0));
|
||||||
|
EXPECT_EQ(123, strtoumax("0173", NULL, 8));
|
||||||
|
}
|
||||||
|
TEST(strtoumax, testBinary) {
|
||||||
|
EXPECT_EQ(42, strtoumax("0b101010", NULL, 0));
|
||||||
|
EXPECT_EQ(42, strtoumax("0b101010", NULL, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(strtoumax, testMaximum) {
|
TEST(strtoumax, testMaximum) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue