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
d473a795
Commit
d473a795
authored
Feb 13, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add FormatInt::size, and support for long and long long.
parent
2a3e0103
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
6 deletions
+15
-6
format-test.cc
format-test.cc
+6
-0
format.h
format.h
+9
-6
No files found.
format-test.cc
View file @
d473a795
...
@@ -1410,7 +1410,13 @@ TEST(FormatterTest, Examples) {
...
@@ -1410,7 +1410,13 @@ TEST(FormatterTest, Examples) {
TEST
(
FormatIntTest
,
FormatInt
)
{
TEST
(
FormatIntTest
,
FormatInt
)
{
EXPECT_EQ
(
"42"
,
fmt
::
FormatInt
(
42
).
str
());
EXPECT_EQ
(
"42"
,
fmt
::
FormatInt
(
42
).
str
());
EXPECT_EQ
(
2
,
fmt
::
FormatInt
(
42
).
size
());
EXPECT_EQ
(
"-42"
,
fmt
::
FormatInt
(
-
42
).
str
());
EXPECT_EQ
(
"-42"
,
fmt
::
FormatInt
(
-
42
).
str
());
EXPECT_EQ
(
3
,
fmt
::
FormatInt
(
-
42
).
size
());
EXPECT_EQ
(
"42"
,
fmt
::
FormatInt
(
42ul
).
str
());
EXPECT_EQ
(
"-42"
,
fmt
::
FormatInt
(
-
42l
).
str
());
EXPECT_EQ
(
"42"
,
fmt
::
FormatInt
(
42ull
).
str
());
EXPECT_EQ
(
"-42"
,
fmt
::
FormatInt
(
-
42ll
).
str
());
std
::
ostringstream
os
;
std
::
ostringstream
os
;
os
<<
std
::
numeric_limits
<
int64_t
>::
max
();
os
<<
std
::
numeric_limits
<
int64_t
>::
max
();
EXPECT_EQ
(
os
.
str
(),
fmt
::
FormatInt
(
std
::
numeric_limits
<
int64_t
>::
max
()).
str
());
EXPECT_EQ
(
os
.
str
(),
fmt
::
FormatInt
(
std
::
numeric_limits
<
int64_t
>::
max
()).
str
());
...
...
format.h
View file @
d473a795
...
@@ -1266,12 +1266,12 @@ class FormatInt {
...
@@ -1266,12 +1266,12 @@ class FormatInt {
private:
private:
// Buffer should be large enough to hold all digits (digits10 + 1),
// Buffer should be large enough to hold all digits (digits10 + 1),
// a sign and a null character.
// a sign and a null character.
enum
{
BUFFER_SIZE
=
std
::
numeric_limits
<
u
int64_t
>::
digits10
+
3
};
enum
{
BUFFER_SIZE
=
std
::
numeric_limits
<
u
nsigned
long
long
>::
digits10
+
3
};
char
buffer_
[
BUFFER_SIZE
];
char
buffer_
[
BUFFER_SIZE
];
char
*
str_
;
char
*
str_
;
// Formats value in reverse and returns the number of digits.
// Formats value in reverse and returns the number of digits.
char
*
FormatDecimal
(
u
int64_t
value
)
{
char
*
FormatDecimal
(
u
nsigned
long
long
value
)
{
char
*
buffer_end
=
buffer_
+
BUFFER_SIZE
;
char
*
buffer_end
=
buffer_
+
BUFFER_SIZE
;
*--
buffer_end
=
'\0'
;
*--
buffer_end
=
'\0'
;
while
(
value
>=
100
)
{
while
(
value
>=
100
)
{
...
@@ -1293,8 +1293,8 @@ class FormatInt {
...
@@ -1293,8 +1293,8 @@ class FormatInt {
return
buffer_end
;
return
buffer_end
;
}
}
void
FormatSigned
(
int64_t
value
)
{
void
FormatSigned
(
long
long
value
)
{
u
int64_t
abs_value
=
value
;
u
nsigned
long
long
abs_value
=
value
;
bool
negative
=
value
<
0
;
bool
negative
=
value
<
0
;
if
(
negative
)
if
(
negative
)
abs_value
=
0
-
value
;
abs_value
=
0
-
value
;
...
@@ -1305,12 +1305,15 @@ class FormatInt {
...
@@ -1305,12 +1305,15 @@ class FormatInt {
public:
public:
explicit
FormatInt
(
int
value
)
{
FormatSigned
(
value
);
}
explicit
FormatInt
(
int
value
)
{
FormatSigned
(
value
);
}
explicit
FormatInt
(
int64_t
value
)
{
FormatSigned
(
value
);
}
explicit
FormatInt
(
long
value
)
{
FormatSigned
(
value
);
}
explicit
FormatInt
(
long
long
value
)
{
FormatSigned
(
value
);
}
explicit
FormatInt
(
unsigned
value
)
:
str_
(
FormatDecimal
(
value
))
{}
explicit
FormatInt
(
unsigned
value
)
:
str_
(
FormatDecimal
(
value
))
{}
explicit
FormatInt
(
uint64_t
value
)
:
str_
(
FormatDecimal
(
value
))
{}
explicit
FormatInt
(
unsigned
long
value
)
:
str_
(
FormatDecimal
(
value
))
{}
explicit
FormatInt
(
unsigned
long
long
value
)
:
str_
(
FormatDecimal
(
value
))
{}
const
char
*
c_str
()
const
{
return
str_
;
}
const
char
*
c_str
()
const
{
return
str_
;
}
std
::
string
str
()
const
{
return
str_
;
}
std
::
string
str
()
const
{
return
str_
;
}
std
::
size_t
size
()
const
{
return
buffer_
-
str_
+
BUFFER_SIZE
-
1
;
}
};
};
/**
/**
...
...
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