Skip to content

Commit

Permalink
std::span for BigInt constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
reneme committed Jan 4, 2024
1 parent d76e317 commit e707fdb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/lib/math/bigint/bigint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ BigInt::BigInt(std::string_view str) {
base = Hexadecimal;
}

*this = decode(cast_char_ptr_to_uint8(str.data()) + markers, str.length() - markers, base);
std::span<const uint8_t> strdata(cast_char_ptr_to_uint8(str.data()), str.length());
*this = decode(strdata.subspan(markers), base);

if(negative) {
set_sign(Negative);
Expand All @@ -88,15 +89,15 @@ BigInt::BigInt(std::string_view str) {
}
}

BigInt::BigInt(const uint8_t input[], size_t length) {
binary_decode(input, length);
BigInt::BigInt(std::span<const uint8_t> input) {
binary_decode(input);
}

/*
* Construct a BigInt from an encoded BigInt
*/
BigInt::BigInt(const uint8_t input[], size_t length, Base base) {
*this = decode(input, length, base);
BigInt::BigInt(std::span<const uint8_t> input, Base base) {
*this = decode(input.data(), input.size(), base);
}

//static
Expand Down
13 changes: 10 additions & 3 deletions src/lib/math/bigint/bigint.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,28 @@ class BOTAN_PUBLIC_API(2, 0) BigInt final {
* @param buf the byte array holding the value
* @param length size of buf
*/
BigInt(const uint8_t buf[], size_t length);
BigInt(const uint8_t buf[], size_t length) : BigInt(std::span{buf, length}) {}

/**
* Create a BigInt from an integer in a byte array
* @param vec the byte vector holding the value
*/
explicit BigInt(std::span<const uint8_t> vec) : BigInt(vec.data(), vec.size()) {}
explicit BigInt(std::span<const uint8_t> vec);

/**
* Create a BigInt from an integer in a byte array
* @param buf the byte array holding the value
* @param length size of buf
* @param base is the number base of the integer in buf
*/
BigInt(const uint8_t buf[], size_t length, Base base);
BigInt(const uint8_t buf[], size_t length, Base base) : BigInt(std::span{buf, length}, base) {}

/**
* Create a BigInt from an integer in a byte array
* @param buf the byte array holding the value
* @param base is the number base of the integer in buf
*/
BigInt(std::span<const uint8_t> buf, Base base);

/**
* Create a BigInt from an integer in a byte array
Expand Down

0 comments on commit e707fdb

Please sign in to comment.