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
d4885cea
Commit
d4885cea
authored
Jul 14, 2016
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Document BasicStringWriter
parent
903357c8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
143 additions
and
0 deletions
+143
-0
doc/api.rst
doc/api.rst
+3
-0
fmt/string.h
fmt/string.h
+83
-0
test/string-test.cc
test/string-test.cc
+57
-0
No files found.
doc/api.rst
View file @
d4885cea
...
...
@@ -157,6 +157,9 @@ store output elsewhere by subclassing `~fmt::BasicWriter`.
.. doxygenclass:: fmt::BasicArrayWriter
:members:
.. doxygenclass:: fmt::BasicStringWriter
:members:
.. doxygenfunction:: bin(int)
.. doxygenfunction:: oct(int)
...
...
fmt/string.h
View file @
d4885cea
...
...
@@ -14,6 +14,89 @@
namespace
fmt
{
namespace
internal
{
// A buffer that stores data in ``std::string``.
template
<
typename
Char
>
class
StringBuffer
:
public
Buffer
<
Char
>
{
private:
std
::
string
data_
;
protected:
virtual
void
grow
(
std
::
size_t
size
)
{
data_
.
resize
(
size
);
this
->
ptr_
=
&
data_
[
0
];
this
->
capacity_
=
size
;
}
public:
// Moves the data to ``str`` clearing the buffer.
void
move_to
(
std
::
string
&
str
)
{
data_
.
resize
(
this
->
size_
);
str
.
swap
(
data_
);
this
->
capacity_
=
this
->
size_
=
0
;
this
->
ptr_
=
0
;
}
};
}
// namespace internal
/**
\rst
This class template provides operations for formatting and writing data
into a character stream. The output is stored in ``std::string`` that grows
dynamically.
You can use one of the following typedefs for common character types
and the standard allocator:
+---------------+----------------------------+
| Type | Definition |
+===============+============================+
| StringWriter | BasicStringWriter<char> |
+---------------+----------------------------+
| WStringWriter | BasicStringWriter<wchar_t> |
+---------------+----------------------------+
**Example**::
StringWriter out;
out << "The answer is " << 42 << "\n";
This will write the following output to the ``out`` object:
.. code-block:: none
The answer is 42
The output can be moved to an ``std::string`` with ``out.move_to()``.
\endrst
*/
template
<
typename
Char
>
class
BasicStringWriter
:
public
BasicWriter
<
Char
>
{
private:
internal
::
StringBuffer
<
Char
>
buffer_
;
public:
/**
\rst
Constructs a :class:`fmt::BasicStringWriter` object.
\endrst
*/
BasicStringWriter
()
:
Writer
(
buffer_
)
{}
/**
\rst
Moves the buffer content to *str* clearing the buffer.
\endrst
*/
void
move_to
(
std
::
string
&
str
)
{
buffer_
.
move_to
(
str
);
}
};
typedef
BasicStringWriter
<
char
>
StringWriter
;
typedef
BasicStringWriter
<
wchar_t
>
WStringWriter
;
/**
\rst
Converts *value* to ``std::string`` using the default format for type *T*.
...
...
test/string-test.cc
View file @
d4885cea
...
...
@@ -10,6 +10,63 @@
#include "fmt/string.h"
#include "gtest/gtest.h"
using
fmt
::
internal
::
StringBuffer
;
TEST
(
StringBufferTest
,
Empty
)
{
StringBuffer
<
char
>
buffer
;
EXPECT_EQ
(
0
,
buffer
.
size
());
EXPECT_EQ
(
0
,
buffer
.
capacity
());
std
::
string
data
;
// std::string may have initial capacity.
std
::
size_t
capacity
=
data
.
capacity
();
buffer
.
move_to
(
data
);
EXPECT_EQ
(
""
,
data
);
EXPECT_EQ
(
capacity
,
data
.
capacity
());
}
TEST
(
StringBufferTest
,
Reserve
)
{
StringBuffer
<
char
>
buffer
;
std
::
size_t
capacity
=
std
::
string
().
capacity
()
+
10
;
buffer
.
reserve
(
capacity
);
EXPECT_EQ
(
0
,
buffer
.
size
());
EXPECT_EQ
(
capacity
,
buffer
.
capacity
());
std
::
string
data
;
buffer
.
move_to
(
data
);
EXPECT_EQ
(
""
,
data
);
}
TEST
(
StringBufferTest
,
Resize
)
{
StringBuffer
<
char
>
buffer
;
std
::
size_t
size
=
std
::
string
().
capacity
()
+
10
;
buffer
.
resize
(
size
);
EXPECT_EQ
(
size
,
buffer
.
size
());
EXPECT_EQ
(
size
,
buffer
.
capacity
());
std
::
string
data
;
buffer
.
move_to
(
data
);
EXPECT_EQ
(
size
,
data
.
size
());
}
TEST
(
StringBufferTest
,
MoveTo
)
{
StringBuffer
<
char
>
buffer
;
std
::
size_t
size
=
std
::
string
().
capacity
()
+
10
;
buffer
.
resize
(
size
);
const
char
*
p
=
&
buffer
[
0
];
std
::
string
data
;
buffer
.
move_to
(
data
);
EXPECT_EQ
(
p
,
&
data
[
0
]);
EXPECT_EQ
(
0
,
buffer
.
size
());
EXPECT_EQ
(
0
,
buffer
.
capacity
());
}
TEST
(
StringWriterTest
,
MoveTo
)
{
fmt
::
StringWriter
out
;
out
<<
"The answer is "
<<
42
<<
"
\n
"
;
std
::
string
s
;
out
.
move_to
(
s
);
EXPECT_EQ
(
"The answer is 42
\n
"
,
s
);
EXPECT_EQ
(
0
,
out
.
size
());
}
TEST
(
StringTest
,
ToString
)
{
EXPECT_EQ
(
"42"
,
fmt
::
to_string
(
42
));
}
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