Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unreachable code & constexpr constructor bigint() #4388

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 14 additions & 4 deletions include/fmt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,19 @@ constexpr FMT_ALWAYS_INLINE const char* narrow(const char* s) { return s; }
template <typename Char>
FMT_CONSTEXPR auto compare(const Char* s1, const Char* s2, std::size_t n)
-> int {
if (!is_constant_evaluated() && sizeof(Char) == 1) return memcmp(s1, s2, n);
if (!is_constant_evaluated() && sizeof(Char) == 1)
{
return memcmp(s1, s2, n);
}
for (; n != 0; ++s1, ++s2, --n) {
if (*s1 < *s2) return -1;
if (*s1 > *s2) return 1;
if (*s1 < *s2)
{
return -1;
}
if (*s1 > *s2)
{
return 1;
}
}
return 0;
}
Expand Down Expand Up @@ -542,10 +551,11 @@ template <typename Char> class basic_string_view {
size_ = __builtin_strlen(detail::narrow(s));
return;
}
#endif
#else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely not a good idea because you would completely skip the strlen if Char is not a char, say a wchar_t. The better answer would involve some kind of if constexpr setup so that you have a fallback for wide chars.

Copy link
Author

@jonasbhjulstad jonasbhjulstad Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I noticed issues with the fix.

size_t len = 0;
while (*s++) ++len;
size_ = len;
#endif
}

/// Constructs a string reference from a `std::basic_string` or a
Expand Down
4 changes: 2 additions & 2 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2516,7 +2516,7 @@ class bigint {
enum { bigit_bits = num_bits<bigit>() };
enum { bigits_capacity = 32 };
basic_memory_buffer<bigit, bigits_capacity> bigits_;
int exp_;
int exp_ = 0;

friend struct formatter<bigint>;

Expand Down Expand Up @@ -2594,7 +2594,7 @@ class bigint {
}

public:
FMT_CONSTEXPR bigint() : exp_(0) {}
FMT_CONSTEXPR bigint() = default;
explicit bigint(uint64_t n) { assign(n); }

bigint(const bigint&) = delete;
Expand Down
Loading