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
88e0db84
Commit
88e0db84
authored
Sep 05, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Test format_error_code.
parent
d4916d92
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
13 deletions
+36
-13
format.cc
format.cc
+9
-11
format.h
format.h
+1
-1
test/format-impl-test.cc
test/format-impl-test.cc
+26
-1
No files found.
format.cc
View file @
88e0db84
...
...
@@ -138,7 +138,6 @@ const char RESET_COLOR[] = "\x1b[0m";
typedef
void
(
*
FormatFunc
)(
fmt
::
Writer
&
,
int
,
fmt
::
StringRef
);
// TODO: test
void
format_error_code
(
fmt
::
Writer
&
out
,
int
error_code
,
fmt
::
StringRef
message
)
FMT_NOEXCEPT
(
true
)
{
// Report error code making sure that the output fits into
...
...
@@ -147,12 +146,11 @@ void format_error_code(fmt::Writer &out, int error_code,
out
.
clear
();
static
const
char
SEP
[]
=
": "
;
static
const
char
ERROR
[]
=
"error "
;
// SEP and ERROR add two terminating null characters, so subtract 1 as
// we need only one.
fmt
::
internal
::
IntTraits
<
int
>::
MainType
ec_value
=
error_code
;
// Subtract 2 to account for terminating null characters in SEP and ERROR.
std
::
size_t
error_code_size
=
sizeof
(
SEP
)
+
sizeof
(
ERROR
)
+
fmt
::
internal
::
count_digits
(
ec_value
)
-
1
;
if
(
message
.
size
()
<
fmt
::
internal
::
INLINE_BUFFER_SIZE
-
error_code_size
)
sizeof
(
SEP
)
+
sizeof
(
ERROR
)
+
fmt
::
internal
::
count_digits
(
ec_value
)
-
2
;
if
(
message
.
size
()
<
=
fmt
::
internal
::
INLINE_BUFFER_SIZE
-
error_code_size
)
out
<<
message
<<
SEP
;
out
<<
ERROR
<<
error_code
;
assert
(
out
.
size
()
<=
fmt
::
internal
::
INLINE_BUFFER_SIZE
);
...
...
@@ -160,12 +158,12 @@ void format_error_code(fmt::Writer &out, int error_code,
void
report_error
(
FormatFunc
func
,
int
error_code
,
fmt
::
StringRef
message
)
FMT_NOEXCEPT
(
true
)
{
try
{
fmt
::
Writer
full_message
;
func
(
full_message
,
error_code
,
message
);
std
::
fwrite
(
full_message
.
c_str
(),
full_message
.
size
(),
1
,
stderr
);
std
::
fputc
(
'\n'
,
stderr
);
}
catch
(...)
{}
fmt
::
Writer
full_message
;
func
(
full_message
,
error_code
,
message
)
;
// Use Writer::data instead of Writer::c_str to avoid potential memory
// allocation.
std
::
fwrite
(
full_message
.
data
(),
full_message
.
size
(),
1
,
stderr
);
std
::
fputc
(
'\n'
,
stderr
);
}
// IsZeroInt::visit(arg) returns true iff arg is a zero integer.
...
...
format.h
View file @
88e0db84
...
...
@@ -1395,7 +1395,7 @@ class BasicWriter {
Returns a pointer to the output buffer content. No terminating null
character is appended.
*/
const
Char
*
data
()
const
{
return
&
buffer_
[
0
];
}
const
Char
*
data
()
const
FMT_NOEXCEPT
(
true
)
{
return
&
buffer_
[
0
];
}
/**
Returns a pointer to the output buffer content with terminating null
...
...
test/format-impl-test.cc
View file @
88e0db84
...
...
@@ -38,10 +38,35 @@ TEST(FormatTest, ArgConverter) {
EXPECT_EQ
(
Arg
::
LONG_LONG
,
arg
.
type
);
}
TEST
(
Format
ter
Test
,
FormatNegativeNaN
)
{
TEST
(
FormatTest
,
FormatNegativeNaN
)
{
double
nan
=
std
::
numeric_limits
<
double
>::
quiet_NaN
();
if
(
getsign
(
-
nan
))
EXPECT_EQ
(
"-nan"
,
fmt
::
format
(
"{}"
,
-
nan
));
else
fmt
::
print
(
"Warning: compiler doesn't handle negative NaN correctly"
);
}
TEST
(
FormatTest
,
FormatErrorCode
)
{
std
::
string
msg
=
"error 42"
,
sep
=
": "
;
{
fmt
::
Writer
w
;
w
<<
"garbage"
;
format_error_code
(
w
,
42
,
"test"
);
EXPECT_EQ
(
"test: "
+
msg
,
w
.
str
());
}
{
fmt
::
Writer
w
;
std
::
string
prefix
(
fmt
::
internal
::
INLINE_BUFFER_SIZE
-
msg
.
size
()
-
sep
.
size
()
+
1
,
'x'
);
format_error_code
(
w
,
42
,
prefix
);
EXPECT_EQ
(
msg
,
w
.
str
());
}
{
fmt
::
Writer
w
;
std
::
string
prefix
(
fmt
::
internal
::
INLINE_BUFFER_SIZE
-
msg
.
size
()
-
sep
.
size
(),
'x'
);
format_error_code
(
w
,
42
,
prefix
);
EXPECT_EQ
(
prefix
+
sep
+
msg
,
w
.
str
());
EXPECT_EQ
(
fmt
::
internal
::
INLINE_BUFFER_SIZE
,
w
.
size
());
}
}
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