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
9d74f954
Commit
9d74f954
authored
Jul 16, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
IsNegative -> is_negative. Simplify WidthHandler and ArgFormatter.
parent
512e2ced
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
47 deletions
+48
-47
format.cc
format.cc
+16
-31
format.h
format.h
+32
-16
No files found.
format.cc
View file @
9d74f954
...
...
@@ -171,27 +171,29 @@ const Char *find_closing_brace(const Char *s, int num_open_braces = 1) {
// Checks if an argument is a valid printf width specifier and sets
// left alignment if it is negative.
struct
WidthHandler
:
public
fmt
::
internal
::
ArgVisitor
<
WidthHandler
,
ULongLong
>
{
struct
WidthHandler
:
public
fmt
::
internal
::
ArgVisitor
<
WidthHandler
,
unsigned
>
{
private:
fmt
::
FormatSpec
&
spec_
;
public:
explicit
WidthHandler
(
fmt
::
FormatSpec
&
spec
)
:
spec_
(
spec
)
{}
ULongLong
visit_unhandled_arg
()
{
unsigned
visit_unhandled_arg
()
{
throw
fmt
::
FormatError
(
"width is not integer"
);
}
ULongLong
visit_any_int
(
fmt
::
LongLong
value
)
{
ULongLong
width
=
value
;
if
(
value
<
0
)
{
template
<
typename
T
>
unsigned
visit_any_int
(
T
value
)
{
typedef
typename
fmt
::
internal
::
IntTraits
<
T
>::
MainType
UnsignedType
;
UnsignedType
width
=
value
;
if
(
fmt
::
internal
::
is_negative
(
value
))
{
spec_
.
align_
=
fmt
::
ALIGN_LEFT
;
width
=
0
-
width
;
}
return
width
;
if
(
width
>
INT_MAX
)
throw
fmt
::
FormatError
(
"number is too big in format"
);
return
static_cast
<
unsigned
>
(
width
);
}
ULongLong
visit_any_uint
(
ULongLong
value
)
{
return
value
;
}
};
// This function template is used to prevent compile errors when handling
...
...
@@ -421,24 +423,11 @@ class fmt::internal::ArgFormatter :
ArgFormatter
(
fmt
::
BasicWriter
<
Char
>
&
w
,
fmt
::
FormatSpec
&
s
,
const
Char
*
fmt
)
:
writer_
(
w
),
spec_
(
s
),
format_
(
fmt
)
{}
void
visit_int
(
int
value
)
{
writer_
.
FormatInt
(
value
,
spec_
);
}
void
visit_uint
(
unsigned
value
)
{
writer_
.
FormatInt
(
value
,
spec_
);
}
void
visit_long_long
(
LongLong
value
)
{
writer_
.
FormatInt
(
value
,
spec_
);
}
void
visit_ulong_long
(
ULongLong
value
)
{
writer_
.
FormatInt
(
value
,
spec_
);
}
void
visit_double
(
double
value
)
{
writer_
.
FormatDouble
(
value
,
spec_
);
}
void
visit_long_double
(
long
double
value
)
{
writer_
.
FormatDouble
(
value
,
spec_
);
}
template
<
typename
T
>
void
visit_any_int
(
T
value
)
{
writer_
.
FormatInt
(
value
,
spec_
);
}
template
<
typename
T
>
void
visit_any_double
(
T
value
)
{
writer_
.
FormatDouble
(
value
,
spec_
);
}
void
visit_char
(
int
value
)
{
if
(
spec_
.
type_
&&
spec_
.
type_
!=
'c'
)
...
...
@@ -773,11 +762,7 @@ unsigned fmt::internal::PrintfParser<Char>::ParseHeader(
spec
.
width_
=
ParseNonnegativeInt
(
s
,
error
);
}
else
if
(
*
s
==
'*'
)
{
++
s
;
ULongLong
width
=
WidthHandler
(
spec
).
visit
(
HandleArgIndex
(
UINT_MAX
,
error
));
if
(
width
<=
INT_MAX
)
spec
.
width_
=
static_cast
<
unsigned
>
(
width
);
else
if
(
!
error
)
error
=
"number is too big in format"
;
spec
.
width_
=
WidthHandler
(
spec
).
visit
(
HandleArgIndex
(
UINT_MAX
,
error
));
}
return
arg_index
;
}
...
...
format.h
View file @
9d74f954
...
...
@@ -393,20 +393,20 @@ struct TypeSelector<false> { typedef uint64_t Type; };
template
<
bool
IsSigned
>
struct
SignChecker
{
template
<
typename
T
>
static
bool
IsN
egative
(
T
)
{
return
false
;
}
static
bool
is_n
egative
(
T
)
{
return
false
;
}
};
template
<
>
struct
SignChecker
<
true
>
{
template
<
typename
T
>
static
bool
IsN
egative
(
T
value
)
{
return
value
<
0
;
}
static
bool
is_n
egative
(
T
value
)
{
return
value
<
0
;
}
};
// Returns true if value is negative, false otherwise.
// Same as (value < 0) but doesn't produce warnings if T is an unsigned type.
template
<
typename
T
>
inline
bool
IsN
egative
(
T
value
)
{
return
SignChecker
<
std
::
numeric_limits
<
T
>::
is_signed
>::
IsN
egative
(
value
);
inline
bool
is_n
egative
(
T
value
)
{
return
SignChecker
<
std
::
numeric_limits
<
T
>::
is_signed
>::
is_n
egative
(
value
);
}
int
SignBitNoInline
(
double
value
);
...
...
@@ -734,31 +734,47 @@ class ArgVisitor {
Result
visit_unhandled_arg
()
{
return
Result
();
}
Result
visit_int
(
int
value
)
{
return
FMT_DISPATCH
(
visit_any_
int
(
value
));
return
FMT_DISPATCH
(
visit_any_
signed
(
value
));
}
Result
visit_long_long
(
LongLong
value
)
{
return
FMT_DISPATCH
(
visit_any_
int
(
value
));
return
FMT_DISPATCH
(
visit_any_
signed
(
value
));
}
Result
visit_any_int
(
LongLong
)
{
return
FMT_DISPATCH
(
visit_unhandled_arg
());
// Visit any signed integer.
template
<
typename
T
>
Result
visit_any_signed
(
T
value
)
{
return
FMT_DISPATCH
(
visit_any_int
(
value
));
}
Result
visit_uint
(
unsigned
value
)
{
return
FMT_DISPATCH
(
visit_any_u
int
(
value
));
return
FMT_DISPATCH
(
visit_any_u
nsigned
(
value
));
}
Result
visit_ulong_long
(
ULongLong
value
)
{
return
FMT_DISPATCH
(
visit_any_u
int
(
value
));
return
FMT_DISPATCH
(
visit_any_u
nsigned
(
value
));
}
Result
visit_any_uint
(
ULongLong
)
{
return
FMT_DISPATCH
(
visit_unhandled_arg
());
// Visit any unsigned integer.
template
<
typename
T
>
Result
visit_any_unsigned
(
T
value
)
{
return
FMT_DISPATCH
(
visit_any_int
(
value
));
}
Result
visit_double
(
double
)
{
template
<
typename
T
>
Result
visit_any_int
(
T
)
{
return
FMT_DISPATCH
(
visit_unhandled_arg
());
}
Result
visit_long_double
(
long
double
)
{
Result
visit_double
(
double
value
)
{
return
FMT_DISPATCH
(
visit_any_double
(
value
));
}
Result
visit_long_double
(
long
double
value
)
{
return
FMT_DISPATCH
(
visit_any_double
(
value
));
}
template
<
typename
T
>
Result
visit_any_double
(
T
)
{
return
FMT_DISPATCH
(
visit_unhandled_arg
());
}
Result
visit_char
(
int
)
{
return
FMT_DISPATCH
(
visit_unhandled_arg
());
}
...
...
@@ -1603,7 +1619,7 @@ void BasicWriter<Char>::FormatInt(T value, const Spec &spec) {
typedef
typename
internal
::
IntTraits
<
T
>::
MainType
UnsignedType
;
UnsignedType
abs_value
=
value
;
char
prefix
[
4
]
=
""
;
if
(
internal
::
IsN
egative
(
value
))
{
if
(
internal
::
is_n
egative
(
value
))
{
prefix
[
0
]
=
'-'
;
++
prefix_size
;
abs_value
=
0
-
abs_value
;
...
...
@@ -1892,7 +1908,7 @@ class FormatInt {
template
<
typename
T
>
inline
void
FormatDec
(
char
*&
buffer
,
T
value
)
{
typename
internal
::
IntTraits
<
T
>::
MainType
abs_value
=
value
;
if
(
internal
::
IsN
egative
(
value
))
{
if
(
internal
::
is_n
egative
(
value
))
{
*
buffer
++
=
'-'
;
abs_value
=
0
-
abs_value
;
}
...
...
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