Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
bb205d94
Commit
bb205d94
authored
Nov 29, 2019
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix fallback pointer formatting on big endian
parent
ef7369ce
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
14 deletions
+30
-14
include/fmt/format-inl.h
include/fmt/format-inl.h
+1
-1
include/fmt/format.h
include/fmt/format.h
+28
-12
test/format-impl-test.cc
test/format-impl-test.cc
+1
-1
No files found.
include/fmt/format-inl.h
View file @
bb205d94
...
@@ -239,7 +239,7 @@ FMT_FUNC void system_error::init(int err_code, string_view format_str,
...
@@ -239,7 +239,7 @@ FMT_FUNC void system_error::init(int err_code, string_view format_str,
namespace
internal
{
namespace
internal
{
template
<
>
FMT_FUNC
int
count_digits
<
4
>
(
internal
::
fallback_uintptr
n
)
{
template
<
>
FMT_FUNC
int
count_digits
<
4
>
(
internal
::
fallback_uintptr
n
)
{
//
Assume little endian; pointer formatting is implementation-defined anyway
.
//
fallback_uintptr is always stored in little endian
.
int
i
=
static_cast
<
int
>
(
sizeof
(
void
*
))
-
1
;
int
i
=
static_cast
<
int
>
(
sizeof
(
void
*
))
-
1
;
while
(
i
>
0
&&
n
.
value
[
i
]
==
0
)
--
i
;
while
(
i
>
0
&&
n
.
value
[
i
]
==
0
)
--
i
;
auto
char_digits
=
std
::
numeric_limits
<
unsigned
char
>::
digits
/
4
;
auto
char_digits
=
std
::
numeric_limits
<
unsigned
char
>::
digits
/
4
;
...
...
include/fmt/format.h
View file @
bb205d94
...
@@ -207,17 +207,7 @@ namespace internal {
...
@@ -207,17 +207,7 @@ namespace internal {
// warnings.
// warnings.
template
<
typename
T
>
inline
T
const_check
(
T
value
)
{
return
value
;
}
template
<
typename
T
>
inline
T
const_check
(
T
value
)
{
return
value
;
}
// A fallback implementation of uintptr_t for systems that lack it.
// An equivalent of `*reinterpret_cast<Dest*>(&source)` that doesn't have
struct
fallback_uintptr
{
unsigned
char
value
[
sizeof
(
void
*
)];
};
#ifdef UINTPTR_MAX
using
uintptr_t
=
::
uintptr_t
;
#else
using
uintptr_t
=
fallback_uintptr
;
#endif
// An equivalent of `*reinterpret_cast<Dest*>(&source)` that doesn't produce
// undefined behavior (e.g. due to type aliasing).
// undefined behavior (e.g. due to type aliasing).
// Example: uint64_t d = bit_cast<uint64_t>(2.718);
// Example: uint64_t d = bit_cast<uint64_t>(2.718);
template
<
typename
Dest
,
typename
Source
>
template
<
typename
Dest
,
typename
Source
>
...
@@ -228,6 +218,32 @@ inline Dest bit_cast(const Source& source) {
...
@@ -228,6 +218,32 @@ inline Dest bit_cast(const Source& source) {
return
dest
;
return
dest
;
}
}
inline
bool
is_big_endian
()
{
auto
u
=
1u
;
struct
bytes
{
char
data
[
sizeof
(
u
)];
};
return
bit_cast
<
bytes
>
(
u
).
data
[
0
]
==
0
;
}
// A fallback implementation of uintptr_t for systems that lack it.
struct
fallback_uintptr
{
unsigned
char
value
[
sizeof
(
void
*
)];
fallback_uintptr
()
=
default
;
explicit
fallback_uintptr
(
const
void
*
p
)
{
*
this
=
bit_cast
<
fallback_uintptr
>
(
p
);
if
(
is_big_endian
())
std
::
memmove
(
value
,
value
,
sizeof
(
void
*
));
}
};
#ifdef UINTPTR_MAX
using
uintptr_t
=
::
uintptr_t
;
inline
uintptr_t
to_uintptr
(
const
void
*
p
)
{
return
bit_cast
<
uintptr_t
>
(
p
);
}
#else
using
uintptr_t
=
fallback_uintptr
;
inline
fallback_uintptr
to_uintptr
(
const
void
*
p
)
{
return
fallback_uintptr
(
p
);
}
#endif
// Returns the largest possible value for type T. Same as
// Returns the largest possible value for type T. Same as
// std::numeric_limits<T>::max() but shorter and not affected by the max macro.
// std::numeric_limits<T>::max() but shorter and not affected by the max macro.
template
<
typename
T
>
constexpr
T
max_value
()
{
template
<
typename
T
>
constexpr
T
max_value
()
{
...
@@ -1829,7 +1845,7 @@ class arg_formatter_base {
...
@@ -1829,7 +1845,7 @@ class arg_formatter_base {
}
}
void
write_pointer
(
const
void
*
p
)
{
void
write_pointer
(
const
void
*
p
)
{
writer_
.
write_pointer
(
internal
::
bit_cast
<
internal
::
uintptr_t
>
(
p
),
specs_
);
writer_
.
write_pointer
(
internal
::
to_uintptr
(
p
),
specs_
);
}
}
protected:
protected:
...
...
test/format-impl-test.cc
View file @
bb205d94
...
@@ -447,7 +447,7 @@ TEST(UtilTest, CountDigits) {
...
@@ -447,7 +447,7 @@ TEST(UtilTest, CountDigits) {
TEST
(
UtilTest
,
WriteUIntPtr
)
{
TEST
(
UtilTest
,
WriteUIntPtr
)
{
fmt
::
memory_buffer
buf
;
fmt
::
memory_buffer
buf
;
fmt
::
internal
::
writer
writer
(
buf
);
fmt
::
internal
::
writer
writer
(
buf
);
writer
.
write_pointer
(
fmt
::
internal
::
bit_cast
<
fmt
::
internal
::
fallback_uintptr
>
(
writer
.
write_pointer
(
fmt
::
internal
::
fallback_uintptr
(
reinterpret_cast
<
void
*>
(
0xface
)),
reinterpret_cast
<
void
*>
(
0xface
)),
nullptr
);
nullptr
);
EXPECT_EQ
(
"0xface"
,
to_string
(
buf
));
EXPECT_EQ
(
"0xface"
,
to_string
(
buf
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment