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
03dccc3c
Commit
03dccc3c
authored
Feb 02, 2013
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename BasicFormatter to BasicWriter.
parent
687301c5
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
54 deletions
+55
-54
format.cc
format.cc
+0
-1
format.h
format.h
+23
-20
format_test.cc
format_test.cc
+32
-33
No files found.
format.cc
View file @
03dccc3c
...
...
@@ -48,7 +48,6 @@ inline int SignBit(double value) { return signbit(value); }
#include <algorithm>
using
std
::
size_t
;
using
fmt
::
BasicFormatter
;
using
fmt
::
IntFormatter
;
using
fmt
::
Formatter
;
using
fmt
::
AlignSpec
;
...
...
format.h
View file @
03dccc3c
...
...
@@ -310,7 +310,7 @@ IntFormatter<int, TypeSpec<'X'> > hexu(int value);
**Example**::
std::string s = str(Basic
Format
ter() << pad(hex(0xcafe), 8, '0'));
std::string s = str(Basic
Wri
ter() << pad(hex(0xcafe), 8, '0'));
// s == "0000cafe"
\endrst
...
...
@@ -352,7 +352,7 @@ DEFINE_INT_FORMATTERS(unsigned)
DEFINE_INT_FORMATTERS
(
unsigned
long
)
template
<
typename
Char
>
class
Basic
Format
ter
{
class
Basic
Wri
ter
{
private:
// Returns the number of decimal digits in n. Trailing zeros are not counted
// except for n == 0 in which case CountDigits returns 1.
...
...
@@ -438,26 +438,26 @@ class BasicFormatter {
return
std
::
basic_string
<
Char
>
(
&
buffer_
[
0
],
buffer_
.
size
());
}
Basic
Format
ter
&
operator
<<
(
int
value
)
{
Basic
Wri
ter
&
operator
<<
(
int
value
)
{
return
*
this
<<
IntFormatter
<
int
,
TypeSpec
<
0
>
>
(
value
,
TypeSpec
<
0
>
());
}
Basic
Format
ter
&
operator
<<
(
unsigned
value
)
{
Basic
Wri
ter
&
operator
<<
(
unsigned
value
)
{
return
*
this
<<
IntFormatter
<
unsigned
,
TypeSpec
<
0
>
>
(
value
,
TypeSpec
<
0
>
());
}
Basic
Format
ter
&
operator
<<
(
Char
value
)
{
Basic
Wri
ter
&
operator
<<
(
Char
value
)
{
*
GrowBuffer
(
1
)
=
value
;
return
*
this
;
}
Basic
Format
ter
&
operator
<<
(
const
Char
*
value
)
{
Basic
Wri
ter
&
operator
<<
(
const
Char
*
value
)
{
std
::
size_t
size
=
std
::
strlen
(
value
);
std
::
strncpy
(
GrowBuffer
(
size
),
value
,
size
);
return
*
this
;
}
template
<
typename
T
,
typename
Spec
>
Basic
Format
ter
&
operator
<<
(
const
IntFormatter
<
T
,
Spec
>
&
f
);
Basic
Wri
ter
&
operator
<<
(
const
IntFormatter
<
T
,
Spec
>
&
f
);
void
Write
(
const
std
::
basic_string
<
char
>
&
s
,
const
FormatSpec
&
spec
)
{
FormatString
(
s
.
data
(),
s
.
size
(),
spec
);
...
...
@@ -471,7 +471,7 @@ class BasicFormatter {
// Fills the padding around the content and returns the pointer to the
// content area.
template
<
typename
Char
>
Char
*
Basic
Format
ter
<
Char
>::
FillPadding
(
Char
*
buffer
,
Char
*
Basic
Wri
ter
<
Char
>::
FillPadding
(
Char
*
buffer
,
unsigned
total_size
,
std
::
size_t
content_size
,
char
fill
)
{
std
::
size_t
padding
=
total_size
-
content_size
;
std
::
size_t
left_padding
=
padding
/
2
;
...
...
@@ -483,7 +483,7 @@ Char *BasicFormatter<Char>::FillPadding(Char *buffer,
}
template
<
typename
Char
>
void
Basic
Format
ter
<
Char
>::
FormatDecimal
(
void
Basic
Wri
ter
<
Char
>::
FormatDecimal
(
Char
*
buffer
,
uint64_t
value
,
unsigned
num_digits
)
{
--
num_digits
;
while
(
value
>=
100
)
{
...
...
@@ -506,7 +506,7 @@ void BasicFormatter<Char>::FormatDecimal(
}
template
<
typename
Char
>
Char
*
Basic
Format
ter
<
Char
>::
PrepareFilledBuffer
(
Char
*
Basic
Wri
ter
<
Char
>::
PrepareFilledBuffer
(
unsigned
size
,
const
AlignSpec
&
spec
,
char
sign
)
{
unsigned
width
=
spec
.
width
();
if
(
width
<=
size
)
{
...
...
@@ -542,7 +542,7 @@ Char *BasicFormatter<Char>::PrepareFilledBuffer(
template
<
typename
Char
>
template
<
typename
T
>
void
Basic
Format
ter
<
Char
>::
FormatDouble
(
void
Basic
Wri
ter
<
Char
>::
FormatDouble
(
T
value
,
const
FormatSpec
&
spec
,
int
precision
)
{
// Check type.
char
type
=
spec
.
type
();
...
...
@@ -687,7 +687,7 @@ void BasicFormatter<Char>::FormatDouble(
}
template
<
typename
Char
>
char
*
Basic
Format
ter
<
Char
>::
FormatString
(
char
*
Basic
Wri
ter
<
Char
>::
FormatString
(
const
char
*
s
,
std
::
size_t
size
,
const
FormatSpec
&
spec
)
{
char
*
out
=
0
;
if
(
spec
.
width
()
>
size
)
{
...
...
@@ -709,7 +709,7 @@ char *BasicFormatter<Char>::FormatString(
template
<
typename
Char
>
template
<
typename
T
,
typename
Spec
>
Basic
Formatter
<
Char
>
&
BasicFormat
ter
<
Char
>::
operator
<<
(
Basic
Writer
<
Char
>
&
BasicWri
ter
<
Char
>::
operator
<<
(
const
IntFormatter
<
T
,
Spec
>
&
f
)
{
T
value
=
f
.
value
();
unsigned
size
=
0
;
...
...
@@ -726,9 +726,9 @@ BasicFormatter<Char> &BasicFormatter<Char>::operator<<(
}
switch
(
f
.
type
())
{
case
0
:
case
'd'
:
{
unsigned
num_digits
=
Basic
Format
ter
::
CountDigits
(
abs_value
);
unsigned
num_digits
=
Basic
Wri
ter
::
CountDigits
(
abs_value
);
Char
*
p
=
PrepareFilledBuffer
(
size
+
num_digits
,
f
,
sign
)
-
num_digits
+
1
;
Basic
Format
ter
::
FormatDecimal
(
p
,
abs_value
,
num_digits
);
Basic
Wri
ter
::
FormatDecimal
(
p
,
abs_value
,
num_digits
);
break
;
}
case
'x'
:
case
'X'
:
{
...
...
@@ -774,9 +774,12 @@ BasicFormatter<Char> &BasicFormatter<Char>::operator<<(
return
*
this
;
}
typedef
BasicWriter
<
char
>
Writer
;
typedef
BasicWriter
<
wchar_t
>
WWriter
;
// The default formatting function.
template
<
typename
Char
,
typename
T
>
void
Format
(
Basic
Format
ter
<
Char
>
&
f
,
const
FormatSpec
&
spec
,
const
T
&
value
)
{
void
Format
(
Basic
Wri
ter
<
Char
>
&
f
,
const
FormatSpec
&
spec
,
const
T
&
value
)
{
std
::
basic_ostringstream
<
Char
>
os
;
os
<<
value
;
f
.
Write
(
os
.
str
(),
spec
);
...
...
@@ -806,7 +809,7 @@ void Format(BasicFormatter<Char> &f, const FormatSpec &spec, const T &value) {
The buffer can be accessed using :meth:`data` or :meth:`c_str`.
\endrst
*/
class
Formatter
:
public
Basic
Format
ter
<
char
>
{
class
Formatter
:
public
Basic
Wri
ter
<
char
>
{
private:
enum
Type
{
// Numeric types should go first.
...
...
@@ -927,7 +930,7 @@ class Formatter : public BasicFormatter<char> {
// Formats an argument of a custom type, such as a user-defined class.
template
<
typename
T
>
void
FormatCustomArg
(
const
void
*
arg
,
const
FormatSpec
&
spec
)
{
Basic
Format
ter
&
f
=
*
this
;
Basic
Wri
ter
&
f
=
*
this
;
Format
(
f
,
spec
,
*
static_cast
<
const
T
*>
(
arg
));
}
...
...
@@ -960,12 +963,12 @@ class Formatter : public BasicFormatter<char> {
}
;
template
<
typename
Char
>
inline
std
::
basic_string
<
Char
>
str
(
const
Basic
Format
ter
<
Char
>
&
f
)
{
inline
std
::
basic_string
<
Char
>
str
(
const
Basic
Wri
ter
<
Char
>
&
f
)
{
return
f
.
str
();
}
template
<
typename
Char
>
inline
const
Char
*
c_str
(
const
Basic
Format
ter
<
Char
>
&
f
)
{
return
f
.
c_str
();
}
inline
const
Char
*
c_str
(
const
Basic
Wri
ter
<
Char
>
&
f
)
{
return
f
.
c_str
();
}
std
::
string
str
(
internal
::
FormatterProxy
p
);
const
char
*
c_str
(
internal
::
FormatterProxy
p
);
...
...
format_test.cc
View file @
03dccc3c
...
...
@@ -46,7 +46,7 @@ using std::size_t;
using
std
::
sprintf
;
using
fmt
::
internal
::
Array
;
using
fmt
::
Basic
Format
ter
;
using
fmt
::
Basic
Wri
ter
;
using
fmt
::
Formatter
;
using
fmt
::
Format
;
using
fmt
::
FormatError
;
...
...
@@ -863,8 +863,7 @@ class Date {
}
template
<
typename
Char
>
friend
BasicFormatter
<
Char
>
&
operator
<<
(
BasicFormatter
<
Char
>
&
f
,
const
Date
&
d
)
{
friend
BasicWriter
<
Char
>
&
operator
<<
(
BasicWriter
<
Char
>
&
f
,
const
Date
&
d
)
{
return
f
<<
d
.
year_
<<
'-'
<<
d
.
month_
<<
'-'
<<
d
.
day_
;
}
};
...
...
@@ -880,7 +879,7 @@ TEST(FormatterTest, FormatUsingIOStreams) {
class
Answer
{};
template
<
typename
Char
>
void
Format
(
fmt
::
BasicFormat
ter
<
Char
>
&
f
,
const
fmt
::
FormatSpec
&
spec
,
Answer
)
{
void
Format
(
BasicWri
ter
<
Char
>
&
f
,
const
fmt
::
FormatSpec
&
spec
,
Answer
)
{
f
.
Write
(
"42"
,
spec
);
}
...
...
@@ -921,7 +920,7 @@ TEST(FormatterTest, FormatterAppend) {
TEST
(
FormatterTest
,
FormatterExamples
)
{
using
fmt
::
hex
;
EXPECT_EQ
(
"0000cafe"
,
str
(
Basic
Format
ter
<
char
>
()
<<
pad
(
hex
(
0xcafe
),
8
,
'0'
)));
EXPECT_EQ
(
"0000cafe"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
pad
(
hex
(
0xcafe
),
8
,
'0'
)));
std
::
string
message
=
str
(
Format
(
"The answer is {}"
)
<<
42
);
EXPECT_EQ
(
"The answer is 42"
,
message
);
...
...
@@ -1071,11 +1070,11 @@ TEST(TempFormatterTest, Examples) {
TEST
(
StrTest
,
oct
)
{
using
fmt
::
oct
;
EXPECT_EQ
(
"12"
,
str
(
Basic
Format
ter
<
char
>
()
<<
oct
(
static_cast
<
short
>
(
012
))));
EXPECT_EQ
(
"12"
,
str
(
Basic
Format
ter
<
char
>
()
<<
oct
(
012
)));
EXPECT_EQ
(
"34"
,
str
(
Basic
Format
ter
<
char
>
()
<<
oct
(
034u
)));
EXPECT_EQ
(
"56"
,
str
(
Basic
Format
ter
<
char
>
()
<<
oct
(
056l
)));
EXPECT_EQ
(
"70"
,
str
(
Basic
Format
ter
<
char
>
()
<<
oct
(
070ul
)));
EXPECT_EQ
(
"12"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
oct
(
static_cast
<
short
>
(
012
))));
EXPECT_EQ
(
"12"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
oct
(
012
)));
EXPECT_EQ
(
"34"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
oct
(
034u
)));
EXPECT_EQ
(
"56"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
oct
(
056l
)));
EXPECT_EQ
(
"70"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
oct
(
070ul
)));
}
TEST
(
StrTest
,
hex
)
{
...
...
@@ -1085,17 +1084,17 @@ TEST(StrTest, hex) {
// This shouldn't compile:
//fmt::IntFormatter<short, fmt::TypeSpec<'x'> > (*phex2)(short value) = hex;
EXPECT_EQ
(
"cafe"
,
str
(
Basic
Format
ter
<
char
>
()
<<
hex
(
0xcafe
)));
EXPECT_EQ
(
"babe"
,
str
(
Basic
Format
ter
<
char
>
()
<<
hex
(
0xbabeu
)));
EXPECT_EQ
(
"dead"
,
str
(
Basic
Format
ter
<
char
>
()
<<
hex
(
0xdeadl
)));
EXPECT_EQ
(
"beef"
,
str
(
Basic
Format
ter
<
char
>
()
<<
hex
(
0xbeeful
)));
EXPECT_EQ
(
"cafe"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
hex
(
0xcafe
)));
EXPECT_EQ
(
"babe"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
hex
(
0xbabeu
)));
EXPECT_EQ
(
"dead"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
hex
(
0xdeadl
)));
EXPECT_EQ
(
"beef"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
hex
(
0xbeeful
)));
}
TEST
(
StrTest
,
hexu
)
{
EXPECT_EQ
(
"CAFE"
,
str
(
Basic
Format
ter
<
char
>
()
<<
hexu
(
0xcafe
)));
EXPECT_EQ
(
"BABE"
,
str
(
Basic
Format
ter
<
char
>
()
<<
hexu
(
0xbabeu
)));
EXPECT_EQ
(
"DEAD"
,
str
(
Basic
Format
ter
<
char
>
()
<<
hexu
(
0xdeadl
)));
EXPECT_EQ
(
"BEEF"
,
str
(
Basic
Format
ter
<
char
>
()
<<
hexu
(
0xbeeful
)));
EXPECT_EQ
(
"CAFE"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
hexu
(
0xcafe
)));
EXPECT_EQ
(
"BABE"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
hexu
(
0xbabeu
)));
EXPECT_EQ
(
"DEAD"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
hexu
(
0xdeadl
)));
EXPECT_EQ
(
"BEEF"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
hexu
(
0xbeeful
)));
}
class
ISO8601DateFormatter
{
...
...
@@ -1105,8 +1104,8 @@ public:
ISO8601DateFormatter
(
const
Date
&
d
)
:
date_
(
&
d
)
{}
template
<
typename
Char
>
friend
Basic
Format
ter
<
Char
>
&
operator
<<
(
Basic
Format
ter
<
Char
>
&
f
,
const
ISO8601DateFormatter
&
d
)
{
friend
Basic
Wri
ter
<
Char
>
&
operator
<<
(
Basic
Wri
ter
<
Char
>
&
f
,
const
ISO8601DateFormatter
&
d
)
{
return
f
<<
pad
(
d
.
date_
->
year
(),
4
,
'0'
)
<<
'-'
<<
pad
(
d
.
date_
->
month
(),
2
,
'0'
)
<<
'-'
<<
pad
(
d
.
date_
->
day
(),
2
,
'0'
);
}
...
...
@@ -1116,17 +1115,17 @@ ISO8601DateFormatter iso8601(const Date &d) { return ISO8601DateFormatter(d); }
TEST
(
StrTest
,
pad
)
{
using
fmt
::
hex
;
EXPECT_EQ
(
" cafe"
,
str
(
Basic
Format
ter
<
char
>
()
<<
pad
(
hex
(
0xcafe
),
8
)));
EXPECT_EQ
(
" babe"
,
str
(
Basic
Format
ter
<
char
>
()
<<
pad
(
hex
(
0xbabeu
),
8
)));
EXPECT_EQ
(
" dead"
,
str
(
Basic
Format
ter
<
char
>
()
<<
pad
(
hex
(
0xdeadl
),
8
)));
EXPECT_EQ
(
" beef"
,
str
(
Basic
Format
ter
<
char
>
()
<<
pad
(
hex
(
0xbeeful
),
8
)));
EXPECT_EQ
(
" cafe"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
pad
(
hex
(
0xcafe
),
8
)));
EXPECT_EQ
(
" babe"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
pad
(
hex
(
0xbabeu
),
8
)));
EXPECT_EQ
(
" dead"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
pad
(
hex
(
0xdeadl
),
8
)));
EXPECT_EQ
(
" beef"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
pad
(
hex
(
0xbeeful
),
8
)));
EXPECT_EQ
(
" 11"
,
str
(
Basic
Format
ter
<
char
>
()
<<
pad
(
11
,
7
)));
EXPECT_EQ
(
" 22"
,
str
(
Basic
Format
ter
<
char
>
()
<<
pad
(
22u
,
7
)));
EXPECT_EQ
(
" 33"
,
str
(
Basic
Format
ter
<
char
>
()
<<
pad
(
33l
,
7
)));
EXPECT_EQ
(
" 44"
,
str
(
Basic
Format
ter
<
char
>
()
<<
pad
(
44lu
,
7
)));
EXPECT_EQ
(
" 11"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
pad
(
11
,
7
)));
EXPECT_EQ
(
" 22"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
pad
(
22u
,
7
)));
EXPECT_EQ
(
" 33"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
pad
(
33l
,
7
)));
EXPECT_EQ
(
" 44"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
pad
(
44lu
,
7
)));
Basic
Format
ter
<
char
>
f
;
Basic
Wri
ter
<
char
>
f
;
f
.
Clear
();
f
<<
pad
(
42
,
5
,
'0'
);
EXPECT_EQ
(
"00042"
,
f
.
str
());
...
...
@@ -1141,12 +1140,12 @@ TEST(StrTest, pad) {
TEST
(
StrTest
,
NoConflictWithIOManip
)
{
using
namespace
std
;
using
namespace
fmt
;
EXPECT_EQ
(
"cafe"
,
str
(
Basic
Format
ter
<
char
>
()
<<
hex
(
0xcafe
)));
EXPECT_EQ
(
"12"
,
str
(
Basic
Format
ter
<
char
>
()
<<
oct
(
012
)));
EXPECT_EQ
(
"cafe"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
hex
(
0xcafe
)));
EXPECT_EQ
(
"12"
,
str
(
Basic
Wri
ter
<
char
>
()
<<
oct
(
012
)));
}
TEST
(
StrTest
,
Basic
Format
terWChar
)
{
EXPECT_EQ
(
L"cafe"
,
str
(
Basic
Format
ter
<
wchar_t
>
()
<<
fmt
::
hex
(
0xcafe
)));
TEST
(
StrTest
,
Basic
Wri
terWChar
)
{
EXPECT_EQ
(
L"cafe"
,
str
(
Basic
Wri
ter
<
wchar_t
>
()
<<
fmt
::
hex
(
0xcafe
)));
}
template
<
typename
T
>
...
...
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