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
3f73987a
Commit
3f73987a
authored
Dec 25, 2012
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move single argument formatting to BasicFormatter.
parent
17ca8091
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
32 deletions
+36
-32
format.cc
format.cc
+18
-17
format.h
format.h
+18
-15
No files found.
format.cc
View file @
3f73987a
...
...
@@ -42,6 +42,7 @@
#include <algorithm>
using
std
::
size_t
;
using
fmt
::
BasicFormatter
;
using
fmt
::
Formatter
;
using
fmt
::
FormatSpec
;
using
fmt
::
StringRef
;
...
...
@@ -135,6 +136,22 @@ void FormatDecimal(char *buffer, uint64_t value, unsigned num_digits) {
}
}
void
BasicFormatter
::
operator
<<
(
int
value
)
{
unsigned
abs_value
=
value
;
unsigned
num_digits
=
0
;
char
*
out
=
0
;
if
(
value
>=
0
)
{
num_digits
=
CountDigits
(
abs_value
);
out
=
GrowBuffer
(
num_digits
);
}
else
{
abs_value
=
0
-
abs_value
;
num_digits
=
CountDigits
(
abs_value
);
out
=
GrowBuffer
(
num_digits
+
1
);
*
out
++
=
'-'
;
}
FormatDecimal
(
out
,
abs_value
,
num_digits
);
}
// Throws Exception(message) if format contains '}', otherwise throws
// FormatError reporting unmatched '{'. The idea is that unmatched '{'
// should override other errors.
...
...
@@ -153,7 +170,7 @@ void Formatter::ReportError(const char *s, StringRef message) const {
// Fills the padding around the content and returns the pointer to the
// content area.
char
*
FillPadding
(
char
*
buffer
,
unsigned
total_size
,
unsigned
content_size
,
char
fill
)
{
unsigned
total_size
,
std
::
size_t
content_size
,
char
fill
)
{
unsigned
padding
=
total_size
-
content_size
;
unsigned
left_padding
=
padding
/
2
;
std
::
fill_n
(
buffer
,
left_padding
,
fill
);
...
...
@@ -634,19 +651,3 @@ void Formatter::DoFormat() {
buffer_
.
append
(
start
,
s
+
1
);
buffer_
.
resize
(
buffer_
.
size
()
-
1
);
// Don't count the terminating zero.
}
void
Formatter
::
operator
<<
(
int
value
)
{
unsigned
abs_value
=
value
;
unsigned
num_digits
=
0
;
char
*
out
=
0
;
if
(
value
>=
0
)
{
num_digits
=
CountDigits
(
abs_value
);
out
=
GrowBuffer
(
num_digits
);
}
else
{
abs_value
=
0
-
abs_value
;
num_digits
=
CountDigits
(
abs_value
);
out
=
GrowBuffer
(
num_digits
+
1
);
*
out
++
=
'-'
;
}
FormatDecimal
(
out
,
abs_value
,
num_digits
);
}
format.h
View file @
3f73987a
...
...
@@ -164,6 +164,23 @@ struct FormatSpec {
FormatSpec
()
:
align
(
ALIGN_DEFAULT
),
flags
(
0
),
width
(
0
),
type
(
0
),
fill
(
' '
)
{}
};
class
BasicFormatter
{
protected:
enum
{
INLINE_BUFFER_SIZE
=
500
};
internal
::
Array
<
char
,
INLINE_BUFFER_SIZE
>
buffer_
;
// Output buffer.
// Grows the buffer by n characters and returns a pointer to the newly
// allocated area.
char
*
GrowBuffer
(
std
::
size_t
n
)
{
std
::
size_t
size
=
buffer_
.
size
();
buffer_
.
resize
(
size
+
n
);
return
&
buffer_
[
size
];
}
public:
void
operator
<<
(
int
value
);
};
// Formatter provides string formatting functionality similar to Python's
// str.format. The output is stored in a memory buffer that grows dynamically.
// Usage:
...
...
@@ -178,11 +195,7 @@ struct FormatSpec {
// (-3.140000, +3.140000)
//
// The buffer can be accessed using Formatter::data() or Formatter::c_str().
class
Formatter
{
private:
enum
{
INLINE_BUFFER_SIZE
=
500
};
internal
::
Array
<
char
,
INLINE_BUFFER_SIZE
>
buffer_
;
// Output buffer.
class
Formatter
:
public
BasicFormatter
{
enum
Type
{
// Numeric types should go first.
INT
,
UINT
,
LONG
,
ULONG
,
DOUBLE
,
LONG_DOUBLE
,
...
...
@@ -328,14 +341,6 @@ class Formatter {
DoFormat
();
}
// Grows the buffer by n characters and returns a pointer to the newly
// allocated area.
char
*
GrowBuffer
(
std
::
size_t
n
)
{
std
::
size_t
size
=
buffer_
.
size
();
buffer_
.
resize
(
size
+
n
);
return
&
buffer_
[
size
];
}
public
:
Formatter
()
:
format_
(
0
)
{
buffer_
[
0
]
=
0
;
}
...
...
@@ -344,8 +349,6 @@ class Formatter {
// using inserter operator<<.
internal
::
ArgInserter
operator
()(
const
char
*
format
);
void
operator
<<
(
int
value
);
std
::
size_t
size
()
const
{
return
buffer_
.
size
();
}
const
char
*
data
()
const
{
return
&
buffer_
[
0
];
}
...
...
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