E-MailRelay
glinestore.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file glinestore.h
19///
20
21#ifndef G_NET_LINE_STORE_H
22#define G_NET_LINE_STORE_H
23
24#include "gdef.h"
25#include "gassert.h"
26#include <string>
27#include <algorithm> // std::swap()
28#include <utility> // std::swap()
29
30namespace GNet
31{
32 class LineStore ;
33}
34
35//| \class GNet::LineStore
36/// A pair of character buffers, one kept by value and the other being
37/// an ephemeral extension. An iterator class can iterate over the
38/// combined data. Used in the implementation of GNet::LineBuffer
39/// as a zero-copy optimisation.
40///
42{
43public:
45 ///< Default constructor.
46
47 void append( const std::string & ) ;
48 ///< Appends to the store (by copying). Any existing
49 ///< extension is first consolidate()d.
50
51 void append( const char * , std::size_t ) ;
52 ///< Appends to the store (by copying). Any existing
53 ///< extension is first consolidate()d.
54
55 void extend( const char * , std::size_t ) ;
56 ///< Sets the extension. Any existing extension is
57 ///< consolidated(). Use consolidate(), discard() or
58 ///< clear() before the extension pointer becomes
59 ///< invalid.
60
61 void discard( std::size_t n ) ;
62 ///< Discards the first 'n' bytes and consolidates
63 ///< the residue.
64
65 void consolidate() ;
66 ///< Consolidates the extension into the store.
67
68 void clear() ;
69 ///< Clears all data.
70
71 std::size_t size() const ;
72 ///< Returns the overall size.
73
74 bool empty() const ;
75 ///< Returns true if size() is zero.
76
77 std::size_t find( char c , std::size_t startpos = 0U ) const ;
78 ///< Finds the given character. Returns npos if
79 ///< not found.
80
81 std::size_t find( const std::string & s , std::size_t startpos = 0U ) const ;
82 ///< Finds the given string. Returns npos if not
83 ///< found.
84
85 std::size_t findSubStringAtEnd( const std::string & s , std::size_t startpos = 0U ) const ;
86 ///< Tries to find some leading sub-string of 's' that
87 ///< appears right at the end of the data, starting
88 ///< with the longest sub-string. Returns npos
89 ///< if not found.
90
91 const char * data( std::size_t pos , std::size_t size ) const ;
92 ///< Returns a pointer for the data at the given
93 ///< position that is contiguous for the given size.
94 ///< Data is shuffled around as required, which
95 ///< means that previous pointers are invalidated.
96
97 char at( std::size_t n ) const ;
98 ///< Returns the n'th character.
99
100 std::string str() const ;
101 ///< Returns the complete string.
102
103 std::string head( std::size_t n ) const ;
104 ///< Returns the leading sub-string of str() of up
105 ///< to 'n' characters.
106
107public:
108 ~LineStore() = default ;
109 LineStore( const LineStore & ) = delete ;
110 LineStore( LineStore && ) = delete ;
111 LineStore & operator=( const LineStore & ) = delete ;
112 LineStore & operator=( LineStore && ) = delete ;
113
114private:
115 const char * dataimp( std::size_t pos , std::size_t size ) ;
116 std::size_t search( std::string::const_iterator , std::string::const_iterator , std::size_t ) const ;
117
118private:
119 std::string m_store ;
120 const char * m_extra_data{nullptr} ;
121 std::size_t m_extra_size{0U} ;
122} ;
123
124inline
125char GNet::LineStore::at( std::size_t n ) const
126{
127 G_ASSERT( n < size() ) ;
128 return n < m_store.size() ? m_store[n] : m_extra_data[n-m_store.size()] ;
129}
130
131inline
132std::size_t GNet::LineStore::size() const
133{
134 return m_store.size() + m_extra_size ;
135}
136
137inline
139{
140 return m_store.empty() && m_extra_size == 0U ;
141}
142
143#endif
A pair of character buffers, one kept by value and the other being an ephemeral extension.
Definition: glinestore.h:42
void discard(std::size_t n)
Discards the first 'n' bytes and consolidates the residue.
Definition: glinestore.cpp:231
void append(const std::string &)
Appends to the store (by copying).
Definition: glinestore.cpp:199
const char * data(std::size_t pos, std::size_t size) const
Returns a pointer for the data at the given position that is contiguous for the given size.
Definition: glinestore.cpp:370
std::string head(std::size_t n) const
Returns the leading sub-string of str() of up to 'n' characters.
Definition: glinestore.cpp:415
bool empty() const
Returns true if size() is zero.
Definition: glinestore.h:138
void clear()
Clears all data.
Definition: glinestore.cpp:218
LineStore()
Default constructor.
std::string str() const
Returns the complete string.
Definition: glinestore.cpp:406
std::size_t find(char c, std::size_t startpos=0U) const
Finds the given character.
Definition: glinestore.cpp:276
std::size_t size() const
Returns the overall size.
Definition: glinestore.h:132
void consolidate()
Consolidates the extension into the store.
Definition: glinestore.cpp:224
void extend(const char *, std::size_t)
Sets the extension.
Definition: glinestore.cpp:211
std::size_t findSubStringAtEnd(const std::string &s, std::size_t startpos=0U) const
Tries to find some leading sub-string of 's' that appears right at the end of the data,...
Definition: glinestore.cpp:338
char at(std::size_t n) const
Returns the n'th character.
Definition: glinestore.h:125
Network classes.
Definition: gdef.h:1144