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
f4156b57
Commit
f4156b57
authored
Jul 30, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement 'hh' length specifier in printf.
parent
39b0930a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
18 deletions
+31
-18
format.cc
format.cc
+22
-11
format.h
format.h
+3
-3
test/printf-test.cc
test/printf-test.cc
+6
-4
No files found.
format.cc
View file @
f4156b57
...
...
@@ -207,23 +207,34 @@ class PrecisionHandler :
}
};
// MakeUnsigned<T>::Type gives an unsigned type corresponding to integer type T.
template
<
typename
T
>
struct
MakeUnsigned
{
typedef
T
Type
;
};
template
<
>
struct
MakeUnsigned
<
signed
char
>
{
typedef
unsigned
char
Type
;
};
template
<
>
struct
MakeUnsigned
<
short
>
{
typedef
unsigned
short
Type
;
};
// Converts an integer argument to type T.
template
<
typename
T
>
class
ArgConverter
:
public
fmt
::
internal
::
ArgVisitor
<
ArgConverter
<
T
>
,
void
>
{
private:
fmt
::
internal
::
Arg
&
arg_
;
char
type_
;
public:
explicit
ArgConverter
(
fmt
::
internal
::
Arg
&
arg
)
:
arg_
(
arg
)
{}
ArgConverter
(
fmt
::
internal
::
Arg
&
arg
,
char
type
)
:
arg_
(
arg
),
type_
(
type
)
{}
template
<
typename
U
>
void
visit_any_int
(
U
value
)
{
if
(
std
::
numeric_limits
<
T
>::
is_signed
)
{
if
(
type_
==
'd'
||
type_
==
'i'
)
{
arg_
.
type
=
fmt
::
internal
::
Arg
::
INT
;
arg_
.
int_value
=
static_cast
<
T
>
(
value
);
}
else
{
arg_
.
type
=
fmt
::
internal
::
Arg
::
UINT
;
arg_
.
uint_value
=
static_cast
<
T
>
(
value
);
arg_
.
uint_value
=
static_cast
<
typename
MakeUnsigned
<
T
>::
Type
>
(
value
);
}
}
};
...
...
@@ -890,16 +901,14 @@ void fmt::internal::PrintfFormatter<Char>::format(
spec
.
fill_
=
' '
;
// Ignore '0' flag for non-numeric types.
}
// Parse length.
// Parse length
and convert the argument to the required type
.
switch
(
*
s
)
{
case
'h'
:
{
++
s
;
// TODO: handle 'hh'
char
type
=
*
s
;
if
(
type
==
'd'
||
type
==
'i'
)
ArgConverter
<
short
>
(
arg
).
visit
(
arg
);
if
(
*
s
==
'h'
)
ArgConverter
<
signed
char
>
(
arg
,
*++
s
).
visit
(
arg
);
else
ArgConverter
<
unsigned
short
>
(
arg
).
visit
(
arg
);
ArgConverter
<
short
>
(
arg
,
*
s
).
visit
(
arg
);
break
;
}
case
'l'
:
...
...
@@ -918,8 +927,10 @@ void fmt::internal::PrintfFormatter<Char>::format(
if
(
error_
)
throw
FormatError
(
error_
);
spec
.
type_
=
static_cast
<
char
>
(
*
s
++
);
if
(
arg
.
type
<=
Arg
::
LAST_INTEGER_TYPE
&&
spec
.
type_
==
'u'
)
spec
.
type_
=
'd'
;
if
(
arg
.
type
<=
Arg
::
LAST_INTEGER_TYPE
&&
(
spec
.
type_
==
'i'
||
spec
.
type_
==
'u'
))
{
spec
.
type_
=
'd'
;
}
start
=
s
;
...
...
format.h
View file @
f4156b57
...
...
@@ -740,6 +740,9 @@ class ArgVisitor {
Result
visit_ulong_long
(
ULongLong
value
)
{
return
FMT_DISPATCH
(
visit_any_int
(
value
));
}
Result
visit_char
(
int
value
)
{
return
FMT_DISPATCH
(
visit_any_int
(
value
));
}
template
<
typename
T
>
Result
visit_any_int
(
T
)
{
return
FMT_DISPATCH
(
visit_unhandled_arg
());
...
...
@@ -756,9 +759,6 @@ class ArgVisitor {
return
FMT_DISPATCH
(
visit_unhandled_arg
());
}
Result
visit_char
(
int
)
{
return
FMT_DISPATCH
(
visit_unhandled_arg
());
}
Result
visit_string
(
Arg
::
StringValue
<
char
>
)
{
return
FMT_DISPATCH
(
visit_unhandled_arg
());
}
...
...
test/printf-test.cc
View file @
f4156b57
...
...
@@ -274,17 +274,17 @@ TEST(PrintfTest, DynamicPrecision) {
#define EXPECT_STD_PRINTF(format, arg) { \
char buffer[BUFFER_SIZE]; \
safe_sprintf(buffer,
"%hd"
, arg); \
EXPECT_PRINTF(buffer,
"%hd"
, arg); \
safe_sprintf(buffer,
fmt::StringRef(format).c_str()
, arg); \
EXPECT_PRINTF(buffer,
format
, arg); \
}
template
<
typename
T
>
void
TestLength
(
const
char
*
length_spec
)
{
EXPECT_STD_PRINTF
(
format
,
42
);
T
min
=
std
::
numeric_limits
<
T
>::
min
(),
max
=
std
::
numeric_limits
<
T
>::
max
();
const
char
types
[]
=
{
'd'
,
'i'
,
'u'
,
'o'
,
'x'
,
'X'
};
for
(
int
i
=
0
;
i
<
sizeof
(
types
);
++
i
)
{
std
::
string
format
=
fmt
::
format
(
"%{}{}"
,
length_spec
,
types
[
i
]);
//EXPECT_STD_PRINTF(format, 42);
EXPECT_STD_PRINTF
(
format
,
min
);
EXPECT_STD_PRINTF
(
format
,
max
);
EXPECT_STD_PRINTF
(
format
,
fmt
::
LongLong
(
min
)
-
1
);
...
...
@@ -300,9 +300,11 @@ void TestLength(const char *length_spec) {
}
}
TEST
(
PrintfTest
,
Short
Length
)
{
TEST
(
PrintfTest
,
Length
)
{
TestLength
<
short
>
(
"h"
);
TestLength
<
unsigned
short
>
(
"h"
);
TestLength
<
signed
char
>
(
"hh"
);
TestLength
<
unsigned
char
>
(
"hh"
);
// TODO: more tests
}
...
...
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