Skip to content

Commit

Permalink
Add initial support for assigning from a string
Browse files Browse the repository at this point in the history
  • Loading branch information
OTheDev committed Feb 23, 2024
1 parent 1f2341f commit e75fdd3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/bi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class BI_API bi_t {
bi_t& operator=(bi_t&& other) noexcept;
template <std::integral T>
bi_t& operator=(T);
bi_t& operator=(const std::string&);
bi_t& operator=(const char*);

// Unary operators
bi_t operator+() const;
Expand Down
13 changes: 13 additions & 0 deletions src/bi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,19 @@ bi_t& bi_t::operator=(T value) {
return *this;
}

bi_t& bi_t::operator=(const std::string& s) {
h_::init_string(*this, s);
return *this;
}

bi_t& bi_t::operator=(const char* s) {
if (s == nullptr) {
throw std::invalid_argument("Null string pointer provided.");
}
h_::init_string(*this, std::string(s));
return *this;
}

///@}

/**
Expand Down
1 change: 1 addition & 0 deletions src/h_.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,7 @@ void h_::init_string(bi_t& x, const std::string& s) {
const size_t n_digits = uints::div_ceil(n_base10, max_batch_size);

x.reserve_(n_digits);
x.resize_unsafe_(0);

for (auto dec_it = start_digit; dec_it < it;) {
/* We could replace all the code in the body of this loop with just
Expand Down
46 changes: 45 additions & 1 deletion test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ TEST_F(BITest, ConstructFromString) {
EXPECT_EQ(zero, 0);

bi_t zeron{"-0"s};
EXPECT_EQ(zero, 0);
EXPECT_EQ(zeron, 0);

bi_t zerop{"+0"s};
EXPECT_EQ(zerop, 0);
Expand Down Expand Up @@ -1730,6 +1730,50 @@ TEST_F(BITest, WithinIntegral) {
EXPECT_FALSE(fits_in_int16);
}

TEST_F(BITest, AssignString) {
bi_t x;

x = "0"s;
EXPECT_EQ(x, 0);

x = "3239"s;
EXPECT_EQ(x, 3239);

x = "-3239"s;
EXPECT_EQ(x, -3239);

EXPECT_THROW(x = "", std::invalid_argument);
EXPECT_THROW(x = nullptr, std::invalid_argument);
EXPECT_THROW(x = " -", std::invalid_argument);

auto str = "999909090093232329302932309230930923230992094029424204"s;
x = str;
EXPECT_EQ(x.to_string(), str);

auto strn = "-9999090900932323293029323092309309232309920940294242"s;
x = strn;
EXPECT_EQ(x.to_string(), strn);

std::random_device rdev;
std::mt19937 rng(rdev());

EXPECT_EQ(x = bltin_int_to_string(sddigit_min), sddigit_min);
EXPECT_EQ(x = bltin_int_to_string(ddigit_max), ddigit_max);

std::uniform_int_distribution<sddigit> udist(sddigit_min, sddigit_max);
std::uniform_int_distribution<int16_t> udist_16(
std::numeric_limits<int16_t>::min(), std::numeric_limits<int16_t>::max());
for (int16_t i = 0; i < INT16_MAX; ++i) {
sddigit rv = udist(rng);
std::string s = bltin_int_to_string(rv);
ASSERT_EQ(x = s, rv);

int16_t rv_16 = udist_16(rng);
s = bltin_int_to_string(rv_16);
ASSERT_EQ(x = s, rv_16);
}
}

// NOLINTEND(cppcoreguidelines-avoid-magic-numbers)

} // namespace

0 comments on commit e75fdd3

Please sign in to comment.