Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 39 additions & 52 deletions src/ccc/int128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,33 +132,48 @@ std::string u128::to_string()
return result;
}

std::optional<u128> u128::from_string(const std::string&& hex)
std::optional<u128> u128::from_string(const std::string& hex)
{
size_t end_of_number = hex.size();
for (size_t i = 0; i < hex.size(); i++) {
if ((hex[i] < '0' || hex[i] > '9') && (hex[i] < 'A' || hex[i] > 'F') && (hex[i] < 'a' || hex[i] > 'f')) {
end_of_number = i;
break;
}
}

if (end_of_number == 0) {
return std::nullopt;
}

u128 result;
for (u32 i = 0; i < 16; i++) {
char c = hex[i];

for (size_t i = 0; i < std::min(end_of_number, static_cast<size_t>(16)); i++) {
char c = hex[end_of_number - i - 1];
if (c >= '0' && c <= '9') {
result.high |= static_cast<u64>(c - '0') << ((15 - i) * 4);
result.low |= static_cast<u64>(c - '0') << (i * 4);
} else if (c >= 'A' && c <= 'F') {
result.high |= static_cast<u64>(10 + c - 'A') << ((15 - i) * 4);
result.low |= static_cast<u64>(10 + c - 'A') << (i * 4);
} else if (c >= 'a' && c <= 'f') {
result.high |= static_cast<u64>(10 + c - 'a') << ((15 - i) * 4);
} else {
return std::nullopt;
}
result.low |= static_cast<u64>(10 + c - 'a') << (i * 4);
}
}
for (u32 i = 0; i < 16; i++) {
char c = hex[16 + i];

if (end_of_number <= 16) {
return result;
}

for (size_t i = 0; i < std::min(end_of_number - 16, static_cast<size_t>(16)); i++) {
char c = hex[end_of_number - i - 17];
if (c >= '0' && c <= '9') {
result.low |= static_cast<u64>(c - '0') << ((15 - i) * 4);
result.high |= static_cast<u64>(c - '0') << (i * 4);
} else if (c >= 'A' && c <= 'F') {
result.low |= static_cast<u64>(10 + c - 'A') << ((15 - i) * 4);
result.high |= static_cast<u64>(10 + c - 'A') << (i * 4);
} else if (c >= 'a' && c <= 'f') {
result.low |= static_cast<u64>(10 + c - 'a') << ((15 - i) * 4);
} else {
return std::nullopt;
}
result.high |= static_cast<u64>(10 + c - 'a') << (i * 4);
}
}

return result;
}

Expand Down Expand Up @@ -283,44 +298,16 @@ s128 s128::operator>>(u64 bits)

std::string s128::to_string()
{
std::string result(32, '\0');
for (u32 i = 0; i < 16; i++) {
result[i] = HEX_DIGITS[(high >> ((15 - i) * 4)) & 0xf];
}
for (u32 i = 0; i < 16; i++) {
result[16 + i] = HEX_DIGITS[(low >> ((15 - i) * 4)) & 0xf];
}
return result;
return u128(*this).to_string();
}

std::optional<s128> s128::from_string(const std::string&& hex)
std::optional<s128> s128::from_string(const std::string& hex)
{
s128 result;
for (u32 i = 0; i < 16; i++) {
char c = hex[i];
if (c >= '0' && c <= '9') {
result.high |= static_cast<u64>(c - '0') << ((15 - i) * 4);
} else if (c >= 'A' && c <= 'F') {
result.high |= static_cast<u64>(10 + c - 'A') << ((15 - i) * 4);
} else if (c >= 'a' && c <= 'f') {
result.high |= static_cast<u64>(10 + c - 'a') << ((15 - i) * 4);
} else {
return std::nullopt;
}
}
for (u32 i = 0; i < 16; i++) {
char c = hex[16 + i];
if (c >= '0' && c <= '9') {
result.low |= static_cast<u64>(c - '0') << ((15 - i) * 4);
} else if (c >= 'A' && c <= 'F') {
result.low |= static_cast<u64>(10 + c - 'A') << ((15 - i) * 4);
} else if (c >= 'a' && c <= 'f') {
result.low |= static_cast<u64>(10 + c - 'a') << ((15 - i) * 4);
} else {
return std::nullopt;
}
}
return result;
std::optional<u128> value = u128::from_string(hex);
if (!value.has_value())
return std::nullopt;

return s128(*value);
}

}
4 changes: 2 additions & 2 deletions src/ccc/int128.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct u128 {
friend bool operator!=(const u128& lhs, const u128& rhs) = default;

std::string to_string();
static std::optional<u128> from_string(const std::string&& hex);
static std::optional<u128> from_string(const std::string& hex);
};

struct s128 {
Expand Down Expand Up @@ -69,7 +69,7 @@ struct s128 {
friend bool operator!=(const s128& lhs, const s128& rhs) = default;

std::string to_string();
static std::optional<s128> from_string(const std::string&& hex);
static std::optional<s128> from_string(const std::string& hex);
};

}
32 changes: 24 additions & 8 deletions test/ccc/int128_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,18 @@ TEST(CCCInt128, UnsignedToString)

TEST(CCCInt128, UnsignedFromString)
{
std::optional<u128> a(u128(0x123456789abcdef0, 0xffeeddccbbaa9988));
std::optional<u128> b(std::nullopt);
EXPECT_EQ(a, u128::from_string("123456789abcdef0ffEEddCCbbAA9988"));
EXPECT_EQ(b, u128::from_string("hello"));
std::optional<u128> a(u128(0x0, 0x123));
std::optional<u128> b(u128(0x1, 0x123));
std::optional<u128> c(u128(0x123456789abcdef0, 0xffeeddccbbaa9988));
std::optional<u128> d(std::nullopt);
EXPECT_EQ(a, u128::from_string("123"));
EXPECT_EQ(a, u128::from_string("123hello"));
EXPECT_EQ(a, u128::from_string("00000000000000000000000000000123"));
EXPECT_EQ(b, u128::from_string("10000000000000123"));
EXPECT_EQ(b, u128::from_string("00000000000000010000000000000123"));
EXPECT_EQ(c, u128::from_string("123456789abcdef0ffEEddCCbbAA9988"));
EXPECT_EQ(c, u128::from_string("ffffffff123456789abcdef0ffEEddCCbbAA9988"));
EXPECT_EQ(d, u128::from_string("hello"));
}

TEST(CCCInt128, SignedAdd)
Expand Down Expand Up @@ -208,8 +216,16 @@ TEST(CCCInt128, SignedToString)

TEST(CCCInt128, SignedFromString)
{
std::optional<s128> a(s128(0x123456789abcdef0, 0xffeeddccbbaa9988));
std::optional<s128> b(std::nullopt);
EXPECT_EQ(a, s128::from_string("123456789abcdef0ffEEddCCbbAA9988"));
EXPECT_EQ(b, s128::from_string("hello"));
std::optional<u128> a(u128(0x0, 0x123));
std::optional<u128> b(u128(0x1, 0x123));
std::optional<u128> c(u128(0x123456789abcdef0, 0xffeeddccbbaa9988));
std::optional<u128> d(std::nullopt);
EXPECT_EQ(a, u128::from_string("123"));
EXPECT_EQ(a, u128::from_string("123hello"));
EXPECT_EQ(a, u128::from_string("00000000000000000000000000000123"));
EXPECT_EQ(b, u128::from_string("10000000000000123"));
EXPECT_EQ(b, u128::from_string("00000000000000010000000000000123"));
EXPECT_EQ(c, u128::from_string("123456789abcdef0ffEEddCCbbAA9988"));
EXPECT_EQ(c, u128::from_string("ffffffff123456789abcdef0ffEEddCCbbAA9988"));
EXPECT_EQ(d, u128::from_string("hello"));;
}
Loading