Skip to content

Commit

Permalink
[feat] "ulbn.hpp": toString now accpet lvalue reference and custom …
Browse files Browse the repository at this point in the history
…allocator
  • Loading branch information
DreamPast committed Dec 8, 2024
1 parent 13d6681 commit 9d98781
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions ulbn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1006,17 +1006,34 @@ class BigInt {
}


std::string toString(int base = 10) const {
Wrapper<std::string> wrapper(std::string{});
template<class StringAllocator = std::allocator<char>>
auto toString(int base = 10) const {
typedef std::basic_string<char, std::char_traits<char>, StringAllocator> String;
Wrapper<String> wrapper(String{});
int err = ulbi_print_ex(
_ctx(),
[](void* opaque, const char* str, size_t len) -> int {
return reinterpret_cast<Wrapper<std::string>*>(opaque)->call([&](std::string& s) { s.append(str, len); });
return reinterpret_cast<Wrapper<String>*>(opaque)->call([&](String& s) { s.append(str, len); });
},
&wrapper, _value, base
);
return wrapper.check(err);
}
template<class StringAllocator>
auto& toString(std::basic_string<char, std::char_traits<char>, StringAllocator>& dst, int base = 10) const {
typedef std::basic_string<char, std::char_traits<char>, StringAllocator> String;
Wrapper<String&> wrapper(dst);
dst.clear();
int err = ulbi_print_ex(
_ctx(),
[](void* opaque, const char* str, size_t len) -> int {
return reinterpret_cast<Wrapper<String>*>(opaque)->call([&](String& s) { s.append(str, len); });
},
&wrapper, _value, base
);
return wrapper.check(err);
}

friend std::ostream& operator<<(std::ostream& ost, const BigInt& value) {
value.print(ost);
return ost;
Expand Down

0 comments on commit 9d98781

Please sign in to comment.