repr
0.1
Reconstructable string representations and more
Toggle main menu visibility
Main Page
Installation
Usage
Customization
Extending
FAQ
CI Results
Test
Benchmark
API Documentation
Namespace List
Namespace List
Namespace Members
All
_
a
c
d
e
f
g
i
l
m
n
o
p
r
s
t
u
v
w
Functions
_
a
c
d
e
f
g
i
l
m
n
o
r
s
t
u
v
w
Variables
Typedefs
Enumerations
Concept List
Class List
Class List
Class Index
Class Hierarchy
Class Members
All
_
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
~
Functions
_
a
c
e
f
g
i
l
m
n
o
p
r
s
t
v
w
~
Variables
a
b
c
d
e
f
g
i
k
l
m
n
o
p
q
r
s
t
u
v
Typedefs
a
c
d
e
f
g
h
l
m
p
r
s
t
u
v
w
Enumerations
Enumerator
Related Symbols
File List
File List
File Members
All
_
l
n
o
r
u
Variables
Macros
_
l
o
r
u
Examples
Source Code
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
Concepts
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
#pragma once
#include <
cstring
>
#include <
iosfwd
>
#include <
string
>
#include <
string_view
>
#include <
utility
>
#include <
librepr/macro/assert.h
>
#include <
librepr/macro/util.h
>
namespace
librepr::detail
{
struct
StringBuffer
{
StringBuffer
() =
default
;
explicit
StringBuffer
(
std::string_view
in) {
// We can hopefully avoid reallocations
// by doing this ahead of time.
buffer.reserve(in.size() * 2);
}
explicit
StringBuffer
(
std::string_view
in) {
…
}
void
write
(
const
char
*
beg
,
const
char
* end) {
LIBREPR_ASSERT
(end >=
beg
,
"Invalid write. (end < beg)"
);
auto
& target = (cursor == std::string::npos) ? buffer : inplace_buffer;
if
(
auto
len
= (end -
beg
);
len
> 0) {
auto
lpos
= target.size();
target.resize(
lpos
+
len
);
std::memcpy
(target.data() +
lpos
,
beg
,
len
);
}
}
void
write
(
const
char
*
beg
,
const
char
* end) {
…
}
LIBREPR_HINT_INLINE
void
write
(
std::string_view
sv
) {
auto
*
beg
=
sv
.data();
this->
write
(
beg
,
beg
+ sv.size());
}
LIBREPR_HINT_INLINE
void
write
(
std::string_view
sv
) {
…
}
LIBREPR_HINT_INLINE
void
write
(
char
c) {
auto
& target = (cursor == std::string::npos) ? buffer : inplace_buffer;
target.push_back(c);
}
LIBREPR_HINT_INLINE
void
write
(
char
c) {
…
}
LIBREPR_HINT_INLINE
std::string
&&
extract
() {
flush
();
// buf.shrink_to_fit();
return
std::move(buffer);
}
LIBREPR_HINT_INLINE
std::string
&&
extract
() {
…
}
explicit
operator
std::string
() {
flush
();
return
buffer;
}
LIBREPR_HINT_INLINE
const
std::string
*
operator->
()
const
{
return
&buffer;
}
LIBREPR_HINT_INLINE
const
std::string
*
operator->
()
const
{
…
}
friend
StringBuffer
&
operator<<
(
StringBuffer
& buf,
std::string_view
sv
){
buf.
write
(
sv
);
return
buf;
}
friend
StringBuffer
&
operator<<
(
StringBuffer
& buf,
std::string_view
sv
) {
…
};
friend
StringBuffer
&
operator<<
(
StringBuffer
& buf,
char
c){
buf.
write
(c);
return
buf;
}
friend
StringBuffer
&
operator<<
(
StringBuffer
& buf,
char
c) {
…
};
friend
std::ostream
&
operator<<
(
std::ostream
&
stream
,
StringBuffer
& buf){
buf.
flush
();
return
stream
<< buf.buffer;
}
friend
std::ostream
&
operator<<
(
std::ostream
&
stream
,
StringBuffer
& buf) {
…
};
std::size_t
size
() {
return
buffer.size() + inplace_buffer.size();
}
std::size_t
size
() {
…
}
void
set_cursor
(
std::size_t
index = std::string::npos) {
flush
();
cursor = index;
}
void
set_cursor
(
std::size_t
index = std::string::npos) {
…
}
void
flush
() {
if
(!inplace_buffer.empty()) {
// flush inplace buffer if it's not empty
buffer.insert(cursor, inplace_buffer);
inplace_buffer.clear();
}
}
void
flush
() {
…
}
private
:
std::string
buffer{};
std::string
inplace_buffer{};
std::size_t
cursor{std::string::npos};
};
struct
StringBuffer
{
…
};
}
// namespace librepr::detail
include
librepr
util
string
buffer.h
Generated by
1.9.8, using the excellent
Doxygen Awesome
Theme