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
43aebf51
Commit
43aebf51
authored
Jan 08, 2015
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow precision specifier for non-float arguments
parent
cd828a86
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
63 deletions
+49
-63
doc/syntax.rst
doc/syntax.rst
+2
-1
format.cc
format.cc
+10
-3
test/format-test.cc
test/format-test.cc
+37
-59
No files found.
doc/syntax.rst
View file @
43aebf51
...
...
@@ -157,7 +157,8 @@ displayed after the decimal point for a floating-point value formatted with
``'f'`` and ``'F'``, or before and after the decimal point for a floating-point
value formatted with ``'g'`` or ``'G'``. For non-number types the field
indicates the maximum field size - in other words, how many characters will be
used from the field content. The *precision* is not allowed for integer values.
used from the field content. The *precision* is not allowed for integer values
or pointers.
Finally, the *type* determines how the data should be presented.
...
...
format.cc
View file @
43aebf51
...
...
@@ -561,9 +561,13 @@ class fmt::internal::ArgFormatter :
if
(
spec_
.
align_
==
ALIGN_NUMERIC
||
spec_
.
flags_
!=
0
)
FMT_THROW
(
FormatError
(
"invalid format specifier for char"
));
typedef
typename
fmt
::
BasicWriter
<
Char
>::
CharPtr
CharPtr
;
Char
fill
=
static_cast
<
Char
>
(
spec_
.
fill
());
if
(
spec_
.
precision_
==
0
)
{
std
::
fill_n
(
writer_
.
grow_buffer
(
spec_
.
width_
),
spec_
.
width_
,
fill
);
return
;
}
CharPtr
out
=
CharPtr
();
if
(
spec_
.
width_
>
1
)
{
Char
fill
=
static_cast
<
Char
>
(
spec_
.
fill
());
out
=
writer_
.
grow_buffer
(
spec_
.
width_
);
if
(
spec_
.
align_
==
fmt
::
ALIGN_RIGHT
)
{
std
::
fill_n
(
out
,
spec_
.
width_
-
1
,
fill
);
...
...
@@ -615,6 +619,8 @@ void fmt::BasicWriter<Char>::write_str(
if
(
*
str_value
)
str_size
=
std
::
char_traits
<
StrChar
>::
length
(
str_value
);
}
if
(
spec
.
precision_
>=
0
&&
spec
.
precision_
<
str_size
)
str_size
=
spec
.
precision_
;
write_str
(
str_value
,
str_size
,
spec
);
}
...
...
@@ -1011,9 +1017,10 @@ const Char *fmt::BasicFormatter<Char>::format(
}
else
{
FMT_THROW
(
FormatError
(
"missing precision specifier"
));
}
if
(
arg
.
type
!=
Arg
::
DOUBLE
&&
arg
.
type
!=
Arg
::
LONG_DOUBLE
)
{
if
(
arg
.
type
<
Arg
::
LAST_INTEGER_TYPE
||
arg
.
type
==
Arg
::
POINTER
)
{
FMT_THROW
(
FormatError
(
"precision specifier requires floating-point argument"
));
fmt
::
format
(
"precision not allowed in {} format specifier"
,
arg
.
type
==
Arg
::
POINTER
?
"pointer"
:
"integer"
)));
}
}
...
...
test/format-test.cc
View file @
43aebf51
...
...
@@ -824,53 +824,42 @@ TEST(FormatterTest, Precision) {
FormatError
,
"missing precision specifier"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2"
,
0
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2}"
,
42
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2f}"
,
42
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2}"
,
42u
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2f}"
,
42u
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2}"
,
42l
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2f}"
,
42l
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2}"
,
42ul
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2f}"
,
42ul
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2}"
,
42ll
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2f}"
,
42ll
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2}"
,
42ull
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2f}"
,
42ull
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_EQ
(
"1.2"
,
format
(
"{0:.2}"
,
1.2345
));
EXPECT_EQ
(
"1.2"
,
format
(
"{0:.2}"
,
1.2345
l
));
EXPECT_THROW_MSG
(
format
(
"{0:.2}"
,
reinterpret_cast
<
void
*>
(
0xcafe
)),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in pointer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2f}"
,
reinterpret_cast
<
void
*>
(
0xcafe
)),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in pointer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2}"
,
'x'
),
FormatError
,
"precision specifier requires floating-point argument"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2f}"
,
'x'
),
FormatError
,
"precision specifier requires floating-point argument"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2}"
,
"str"
),
FormatError
,
"precision specifier requires floating-point argument"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2f}"
,
"str"
),
FormatError
,
"precision specifier requires floating-point argument"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2}"
,
TestString
()),
FormatError
,
"precision specifier requires floating-point argument"
);
EXPECT_THROW_MSG
(
format
(
"{0:.2f}"
,
TestString
()),
FormatError
,
"precision specifier requires floating-point argument"
);
EXPECT_EQ
(
" "
,
format
(
"{0:3.0}"
,
'x'
));
EXPECT_EQ
(
"st"
,
format
(
"{0:.2}"
,
"str"
));
EXPECT_EQ
(
"te"
,
format
(
"{0:.2}"
,
TestString
(
"test"
)));
}
TEST
(
FormatterTest
,
RuntimePrecision
)
{
...
...
@@ -893,7 +882,7 @@ TEST(FormatterTest, RuntimePrecision) {
EXPECT_THROW_MSG
(
format
(
"{0:.{x}}"
,
0
),
FormatError
,
"invalid format string"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}"
,
0
,
0
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}}"
,
0
),
FormatError
,
"argument index out of range"
);
...
...
@@ -920,51 +909,40 @@ TEST(FormatterTest, RuntimePrecision) {
FormatError
,
"precision is not integer"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}}"
,
42
,
2
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}f}"
,
42
,
2
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}}"
,
42u
,
2
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}f}"
,
42u
,
2
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}}"
,
42l
,
2
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}f}"
,
42l
,
2
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}}"
,
42ul
,
2
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}f}"
,
42ul
,
2
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}}"
,
42ll
,
2
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}f}"
,
42ll
,
2
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}}"
,
42ull
,
2
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}f}"
,
42ull
,
2
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in integer format specifier
"
);
EXPECT_EQ
(
"1.2"
,
format
(
"{0:.{1}}"
,
1.2345
,
2
));
EXPECT_EQ
(
"1.2"
,
format
(
"{1:.{0}}"
,
2
,
1.2345
l
));
EXPECT_THROW_MSG
(
format
(
"{0:.{1}}"
,
reinterpret_cast
<
void
*>
(
0xcafe
),
2
),
FormatError
,
"precision
specifier requires floating-point argument
"
);
FormatError
,
"precision
not allowed in pointer format specifier
"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}f}"
,
reinterpret_cast
<
void
*>
(
0xcafe
),
2
),
FormatError
,
"precision specifier requires floating-point argument"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}}"
,
'x'
,
2
),
FormatError
,
"precision specifier requires floating-point argument"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}f}"
,
'x'
,
2
),
FormatError
,
"precision specifier requires floating-point argument"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}}"
,
"str"
,
2
),
FormatError
,
"precision specifier requires floating-point argument"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}f}"
,
"str"
,
2
),
FormatError
,
"precision specifier requires floating-point argument"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}}"
,
TestString
(),
2
),
FormatError
,
"precision specifier requires floating-point argument"
);
EXPECT_THROW_MSG
(
format
(
"{0:.{1}f}"
,
TestString
(),
2
),
FormatError
,
"precision specifier requires floating-point argument"
);
FormatError
,
"precision not allowed in pointer format specifier"
);
EXPECT_EQ
(
" "
,
format
(
"{0:3.{1}}"
,
'x'
,
0
));
EXPECT_EQ
(
"st"
,
format
(
"{0:.{1}}"
,
"str"
,
2
));
EXPECT_EQ
(
"te"
,
format
(
"{0:.{1}}"
,
TestString
(
"test"
),
2
));
}
template
<
typename
T
>
...
...
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