Skip to content

Commit a5d861a

Browse files
committed
Add support for 0X prefix to from_string()
1 parent cad7d2b commit a5d861a

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

include/intx/intx.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ constexpr Int from_string(const char* str)
813813
auto x = Int{};
814814
int num_digits = 0;
815815

816-
if (s[0] == '0' && s[1] == 'x')
816+
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
817817
{
818818
s += 2;
819819
while (const auto c = *s++)

test/unittests/test_int128.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,26 @@ TEST(int128, literals)
392392

393393
TEST(int128, from_string)
394394
{
395-
constexpr auto ka = from_string<uint128>("18446744073709551617");
396-
static_assert(ka == uint128{1, 1});
397-
const auto* const sa = "18446744073709551617";
398-
const auto a = from_string<uint128>(sa);
399-
EXPECT_EQ(a, uint128(1, 1));
400-
401-
constexpr auto kb = from_string<uint128>("0x300aabbccddeeff99");
402-
static_assert(kb == uint128{0xaabbccddeeff99, 3});
403-
const auto* const sb = "0x300aabbccddeeff99";
404-
EXPECT_EQ(from_string<uint128>(sb), uint128(0xaabbccddeeff99, 3));
395+
{
396+
constexpr auto k = from_string<uint128>("18446744073709551617");
397+
static_assert(k == uint128{1, 1});
398+
const auto* const s = "18446744073709551617";
399+
EXPECT_EQ(from_string<uint128>(s), uint128(1, 1));
400+
}
401+
402+
{
403+
constexpr auto k = from_string<uint128>("0x300aabbccddeeff99");
404+
static_assert(k == uint128{0xaabbccddeeff99, 3});
405+
const auto* const s = "0x300aabbccddeeff99";
406+
EXPECT_EQ(from_string<uint128>(s), uint128(0xaabbccddeeff99, 3));
407+
}
408+
409+
{
410+
constexpr auto k = from_string<uint128>("0X300AABBCCDDEEFF99");
411+
static_assert(k == uint128{0xaabbccddeeff99, 3});
412+
const auto* const s = "0X300AABBCCDDEEFF99";
413+
EXPECT_EQ(from_string<uint128>(s), uint128(0xaabbccddeeff99, 3));
414+
}
405415
}
406416

407417
TEST(int128, from_string_exceptions)

0 commit comments

Comments
 (0)