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
73f13eeb
Commit
73f13eeb
authored
Dec 25, 2012
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement '#' flag.
parent
3f73987a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
5 deletions
+51
-5
format.cc
format.cc
+18
-5
format_test.cc
format_test.cc
+33
-0
No files found.
format.cc
View file @
73f13eeb
...
...
@@ -55,7 +55,7 @@ using fmt::StringRef;
namespace
{
// Flags.
enum
{
SIGN_FLAG
=
1
,
PLUS_FLAG
=
2
,
H
EX_PREFIX
_FLAG
=
4
};
enum
{
SIGN_FLAG
=
1
,
PLUS_FLAG
=
2
,
H
ASH
_FLAG
=
4
};
void
ReportUnknownType
(
char
code
,
const
char
*
type
)
{
if
(
std
::
isprint
(
static_cast
<
unsigned
char
>
(
code
)))
{
...
...
@@ -236,7 +236,7 @@ void Formatter::FormatInt(T value, const FormatSpec &spec) {
}
case
'x'
:
case
'X'
:
{
UnsignedType
n
=
abs_value
;
bool
print_prefix
=
(
spec
.
flags
&
H
EX_PREFIX
_FLAG
)
!=
0
;
bool
print_prefix
=
(
spec
.
flags
&
H
ASH
_FLAG
)
!=
0
;
if
(
print_prefix
)
size
+=
2
;
do
{
++
size
;
...
...
@@ -250,12 +250,14 @@ void Formatter::FormatInt(T value, const FormatSpec &spec) {
}
while
((
n
>>=
4
)
!=
0
);
if
(
print_prefix
)
{
*
p
--
=
spec
.
type
;
*
p
--
=
'0'
;
*
p
=
'0'
;
}
break
;
}
case
'o'
:
{
UnsignedType
n
=
abs_value
;
bool
print_prefix
=
(
spec
.
flags
&
HASH_FLAG
)
!=
0
;
if
(
print_prefix
)
++
size
;
do
{
++
size
;
}
while
((
n
>>=
3
)
!=
0
);
...
...
@@ -264,6 +266,8 @@ void Formatter::FormatInt(T value, const FormatSpec &spec) {
do
{
*
p
--
=
'0'
+
(
n
&
7
);
}
while
((
n
>>=
3
)
!=
0
);
if
(
print_prefix
)
*
p
=
'0'
;
break
;
}
default:
...
...
@@ -310,11 +314,13 @@ void Formatter::FormatDouble(T value, const FormatSpec &spec, int precision) {
}
// Build format string.
enum
{
MAX_FORMAT_SIZE
=
10
};
// longest format: %
+0
*.*Lg
enum
{
MAX_FORMAT_SIZE
=
10
};
// longest format: %
#-
*.*Lg
char
format
[
MAX_FORMAT_SIZE
];
char
*
format_ptr
=
format
;
*
format_ptr
++
=
'%'
;
unsigned
width_for_sprintf
=
width
;
if
((
spec
.
flags
&
HASH_FLAG
)
!=
0
)
*
format_ptr
++
=
'#'
;
if
(
spec
.
align
==
ALIGN_CENTER
)
{
width_for_sprintf
=
0
;
}
else
{
...
...
@@ -503,6 +509,13 @@ void Formatter::DoFormat() {
break
;
}
if
(
*
s
==
'#'
)
{
if
(
arg
.
type
>
LAST_NUMERIC_TYPE
)
ReportError
(
s
,
"format specifier '#' requires numeric argument"
);
spec
.
flags
|=
HASH_FLAG
;
++
s
;
}
// Parse width and zero flag.
if
(
'0'
<=
*
s
&&
*
s
<=
'9'
)
{
if
(
*
s
==
'0'
)
{
...
...
@@ -634,7 +647,7 @@ void Formatter::DoFormat() {
case
POINTER
:
if
(
spec
.
type
&&
spec
.
type
!=
'p'
)
ReportUnknownType
(
spec
.
type
,
"pointer"
);
spec
.
flags
=
H
EX_PREFIX
_FLAG
;
spec
.
flags
=
H
ASH
_FLAG
;
spec
.
type
=
'x'
;
FormatInt
(
reinterpret_cast
<
uintptr_t
>
(
arg
.
pointer_value
),
spec
);
break
;
...
...
format_test.cc
View file @
73f13eeb
...
...
@@ -425,6 +425,39 @@ TEST(FormatterTest, SpaceSign) {
FormatError
,
"format specifier ' ' requires numeric argument"
);
}
TEST
(
FormatterTest
,
HashFlag
)
{
// TODO
EXPECT_EQ
(
"42"
,
str
(
Format
(
"{0:#}"
)
<<
42
));
EXPECT_EQ
(
"-42"
,
str
(
Format
(
"{0:#}"
)
<<
-
42
));
EXPECT_EQ
(
"0x42"
,
str
(
Format
(
"{0:#x}"
)
<<
0x42
));
EXPECT_EQ
(
"-0x42"
,
str
(
Format
(
"{0:#x}"
)
<<
-
0x42
));
EXPECT_EQ
(
"042"
,
str
(
Format
(
"{0:#o}"
)
<<
042
));
EXPECT_EQ
(
"-042"
,
str
(
Format
(
"{0:#o}"
)
<<
-
042
));
EXPECT_EQ
(
"42"
,
str
(
Format
(
"{0:#}"
)
<<
42u
));
EXPECT_EQ
(
"0x42"
,
str
(
Format
(
"{0:#x}"
)
<<
0x42u
));
EXPECT_EQ
(
"042"
,
str
(
Format
(
"{0:#o}"
)
<<
042u
));
EXPECT_EQ
(
"-42"
,
str
(
Format
(
"{0:#}"
)
<<
-
42l
));
EXPECT_EQ
(
"0x42"
,
str
(
Format
(
"{0:#x}"
)
<<
0x42l
));
EXPECT_EQ
(
"-0x42"
,
str
(
Format
(
"{0:#x}"
)
<<
-
0x42l
));
EXPECT_EQ
(
"042"
,
str
(
Format
(
"{0:#o}"
)
<<
042l
));
EXPECT_EQ
(
"-042"
,
str
(
Format
(
"{0:#o}"
)
<<
-
042l
));
EXPECT_EQ
(
"42"
,
str
(
Format
(
"{0:#}"
)
<<
42ul
));
EXPECT_EQ
(
"0x42"
,
str
(
Format
(
"{0:#x}"
)
<<
0x42ul
));
EXPECT_EQ
(
"042"
,
str
(
Format
(
"{0:#o}"
)
<<
042ul
));
EXPECT_EQ
(
"-42.0000"
,
str
(
Format
(
"{0:#}"
)
<<
-
42.0
));
EXPECT_EQ
(
"-42.0000"
,
str
(
Format
(
"{0:#}"
)
<<
-
42.0
l
));
EXPECT_THROW_MSG
(
Format
(
"{0:#"
)
<<
'c'
,
FormatError
,
"unmatched '{' in format"
);
EXPECT_THROW_MSG
(
Format
(
"{0:#}"
)
<<
'c'
,
FormatError
,
"format specifier '#' requires numeric argument"
);
EXPECT_THROW_MSG
(
Format
(
"{0:#}"
)
<<
"abc"
,
FormatError
,
"format specifier '#' requires numeric argument"
);
EXPECT_THROW_MSG
(
Format
(
"{0:#}"
)
<<
reinterpret_cast
<
void
*>
(
0x42
),
FormatError
,
"format specifier '#' requires numeric argument"
);
EXPECT_THROW_MSG
(
Format
(
"{0:#}"
)
<<
TestString
(),
FormatError
,
"format specifier '#' requires numeric argument"
);
}
TEST
(
FormatterTest
,
ZeroFlag
)
{
EXPECT_EQ
(
"42"
,
str
(
Format
(
"{0:0}"
)
<<
42
));
EXPECT_EQ
(
"-0042"
,
str
(
Format
(
"{0:05}"
)
<<
-
42
));
...
...
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