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
87b691d8
Commit
87b691d8
authored
Feb 17, 2017
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Merge StringWriter into StringBuffer
parent
c2f02169
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
77 deletions
+40
-77
fmt/string.h
fmt/string.h
+31
-55
test/string-test.cc
test/string-test.cc
+9
-22
No files found.
fmt/string.h
View file @
87b691d8
...
@@ -14,53 +14,25 @@
...
@@ -14,53 +14,25 @@
namespace
fmt
{
namespace
fmt
{
namespace
internal
{
// A buffer that stores data in ``std::string``.
template
<
typename
Char
>
class
StringBuffer
:
public
basic_buffer
<
Char
>
{
private:
std
::
basic_string
<
Char
>
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
::
basic_string
<
Char
>
&
str
)
{
data_
.
resize
(
this
->
size_
);
str
.
swap
(
data_
);
this
->
capacity_
=
this
->
size_
=
0
;
this
->
ptr_
=
0
;
}
};
}
// namespace internal
/**
/**
\rst
\rst
This class template provides operations for formatting and writing data
This class template represents a character buffer backed by std::string.
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
You can use one of the following typedefs for common character types
and the standard allocator:
and the standard allocator:
+---------------
+
----------------------------+
+---------------
-+--
----------------------------+
| Type
| Definition
|
| Type
| Definition
|
+===============
+
============================+
+===============
=+==
============================+
|
StringWriter | BasicStringWrit
er<char> |
|
string_buffer | basic_string_buff
er<char> |
+---------------
+
----------------------------+
+---------------
-+--
----------------------------+
|
WStringWriter | BasicStringWrit
er<wchar_t> |
|
wstring_buffer | basic_string_buff
er<wchar_t> |
+---------------
+
----------------------------+
+---------------
-+--
----------------------------+
**Example**::
**Example**::
StringWrit
er out;
string_buff
er out;
out << "The answer is " << 42 << "\n"
;
format_to(out, "The answer is {}", 42)
;
This will write the following output to the ``out`` object:
This will write the following output to the ``out`` object:
...
@@ -70,32 +42,34 @@ class StringBuffer : public basic_buffer<Char> {
...
@@ -70,32 +42,34 @@ class StringBuffer : public basic_buffer<Char> {
The output can be moved to an ``std::string`` with ``out.move_to()``.
The output can be moved to an ``std::string`` with ``out.move_to()``.
\endrst
\endrst
*/
*/
template
<
typename
Char
>
template
<
typename
Char
>
class
basic_string_buffer
:
public
basic_buffer
<
Char
>
{
class
BasicStringWriter
:
public
basic_writer
<
Char
>
{
private:
private:
internal
::
StringBuffer
<
Char
>
buffer
_
;
std
::
basic_string
<
Char
>
data
_
;
public:
protected:
/**
virtual
void
grow
(
std
::
size_t
size
)
{
\rst
data_
.
resize
(
size
);
Constructs a :class:`fmt::BasicStringWriter` object.
this
->
ptr_
=
&
data_
[
0
];
\endrst
this
->
capacity_
=
size
;
*/
}
BasicStringWriter
()
:
basic_writer
<
Char
>
(
buffer_
)
{}
public:
/**
/**
\rst
\rst
Moves the buffer content to *str* clearing the buffer.
Moves the buffer content to *str* clearing the buffer.
\endrst
\endrst
*/
*/
void
move_to
(
std
::
basic_string
<
Char
>
&
str
)
{
void
move_to
(
std
::
basic_string
<
Char
>
&
str
)
{
buffer_
.
move_to
(
str
);
data_
.
resize
(
this
->
size_
);
str
.
swap
(
data_
);
this
->
capacity_
=
this
->
size_
=
0
;
this
->
ptr_
=
0
;
}
}
};
};
typedef
BasicStringWriter
<
char
>
StringWrit
er
;
typedef
basic_string_buffer
<
char
>
string_buff
er
;
typedef
BasicStringWriter
<
wchar_t
>
WStringWrit
er
;
typedef
basic_string_buffer
<
wchar_t
>
wstring_buff
er
;
/**
/**
\rst
\rst
...
@@ -110,9 +84,11 @@ typedef BasicStringWriter<wchar_t> WStringWriter;
...
@@ -110,9 +84,11 @@ typedef BasicStringWriter<wchar_t> WStringWriter;
*/
*/
template
<
typename
T
>
template
<
typename
T
>
std
::
string
to_string
(
const
T
&
value
)
{
std
::
string
to_string
(
const
T
&
value
)
{
fmt
::
MemoryWriter
w
;
string_buffer
buf
;
w
.
write
(
value
);
basic_writer
<
char
>
(
buf
).
write
(
value
);
return
w
.
str
();
std
::
string
str
;
buf
.
move_to
(
str
);
return
str
;
}
}
}
}
...
...
test/string-test.cc
View file @
87b691d8
...
@@ -10,10 +10,10 @@
...
@@ -10,10 +10,10 @@
#include "fmt/string.h"
#include "fmt/string.h"
#include "gtest/gtest.h"
#include "gtest/gtest.h"
using
fmt
::
internal
::
StringB
uffer
;
using
fmt
::
string_b
uffer
;
TEST
(
StringBufferTest
,
Empty
)
{
TEST
(
StringBufferTest
,
Empty
)
{
StringBuffer
<
char
>
buffer
;
string_buffer
buffer
;
EXPECT_EQ
(
0
,
buffer
.
size
());
EXPECT_EQ
(
0
,
buffer
.
size
());
EXPECT_EQ
(
0
,
buffer
.
capacity
());
EXPECT_EQ
(
0
,
buffer
.
capacity
());
std
::
string
data
;
std
::
string
data
;
...
@@ -25,7 +25,7 @@ TEST(StringBufferTest, Empty) {
...
@@ -25,7 +25,7 @@ TEST(StringBufferTest, Empty) {
}
}
TEST
(
StringBufferTest
,
Reserve
)
{
TEST
(
StringBufferTest
,
Reserve
)
{
StringBuffer
<
char
>
buffer
;
string_buffer
buffer
;
std
::
size_t
capacity
=
std
::
string
().
capacity
()
+
10
;
std
::
size_t
capacity
=
std
::
string
().
capacity
()
+
10
;
buffer
.
reserve
(
capacity
);
buffer
.
reserve
(
capacity
);
EXPECT_EQ
(
0
,
buffer
.
size
());
EXPECT_EQ
(
0
,
buffer
.
size
());
...
@@ -36,7 +36,7 @@ TEST(StringBufferTest, Reserve) {
...
@@ -36,7 +36,7 @@ TEST(StringBufferTest, Reserve) {
}
}
TEST
(
StringBufferTest
,
Resize
)
{
TEST
(
StringBufferTest
,
Resize
)
{
StringBuffer
<
char
>
buffer
;
string_buffer
buffer
;
std
::
size_t
size
=
std
::
string
().
capacity
()
+
10
;
std
::
size_t
size
=
std
::
string
().
capacity
()
+
10
;
buffer
.
resize
(
size
);
buffer
.
resize
(
size
);
EXPECT_EQ
(
size
,
buffer
.
size
());
EXPECT_EQ
(
size
,
buffer
.
size
());
...
@@ -47,7 +47,7 @@ TEST(StringBufferTest, Resize) {
...
@@ -47,7 +47,7 @@ TEST(StringBufferTest, Resize) {
}
}
TEST
(
StringBufferTest
,
MoveTo
)
{
TEST
(
StringBufferTest
,
MoveTo
)
{
StringBuffer
<
char
>
buffer
;
string_buffer
buffer
;
std
::
size_t
size
=
std
::
string
().
capacity
()
+
10
;
std
::
size_t
size
=
std
::
string
().
capacity
()
+
10
;
buffer
.
resize
(
size
);
buffer
.
resize
(
size
);
const
char
*
p
=
&
buffer
[
0
];
const
char
*
p
=
&
buffer
[
0
];
...
@@ -58,25 +58,12 @@ TEST(StringBufferTest, MoveTo) {
...
@@ -58,25 +58,12 @@ TEST(StringBufferTest, MoveTo) {
EXPECT_EQ
(
0
,
buffer
.
capacity
());
EXPECT_EQ
(
0
,
buffer
.
capacity
());
}
}
TEST
(
StringWriterTest
,
MoveTo
)
{
TEST
(
StringBufferTest
,
WString
)
{
fmt
::
StringWriter
out
;
fmt
::
wstring_buffer
out
;
out
.
write
(
"The answer is "
);
out
.
push_back
(
L'x'
);
out
.
write
(
42
);
out
.
write
(
"
\n
"
);
std
::
string
s
;
out
.
move_to
(
s
);
EXPECT_EQ
(
"The answer is 42
\n
"
,
s
);
EXPECT_EQ
(
0
,
out
.
size
());
}
TEST
(
StringWriterTest
,
WString
)
{
fmt
::
WStringWriter
out
;
out
.
write
(
"The answer is "
);
out
.
write
(
42
);
out
.
write
(
"
\n
"
);
std
::
wstring
s
;
std
::
wstring
s
;
out
.
move_to
(
s
);
out
.
move_to
(
s
);
EXPECT_EQ
(
L"
The answer is 42
\n
"
,
s
);
EXPECT_EQ
(
L"
x
"
,
s
);
}
}
TEST
(
StringTest
,
ToString
)
{
TEST
(
StringTest
,
ToString
)
{
...
...
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