E-MailRelay

glinestore.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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
30 namespace 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. Used in the implementation of GNet::LineBuffer
38/// as a zero-copy optimisation.
39///
40 class GNet::LineStore
41{
42 public:
43 LineStore() ;
44 ///< Default constructor.
45
46 void append( const std::string & ) ;
47 ///< Appends to the store (by copying). Any existing
48 ///< extension is first consolidate()d.
49
50 void append( const char * , std::size_t ) ;
51 ///< Appends to the store (by copying). Any existing
52 ///< extension is first consolidate()d.
53
54 void extend( const char * , std::size_t ) ;
55 ///< Sets the extension. Any existing extension is
56 ///< consolidated(). Use consolidate(), discard() or
57 ///< clear() before the extension pointer becomes
58 ///< invalid.
59
60 void discard( std::size_t n ) ;
61 ///< Discards the first 'n' bytes and consolidates
62 ///< the residue.
63
64 void consolidate() ;
65 ///< Consolidates the extension into the store.
66
67 void clear() ;
68 ///< Clears all data.
69
70 std::size_t size() const ;
71 ///< Returns the overall size.
72
73 bool empty() const ;
74 ///< Returns true if size() is zero.
75
76 std::size_t find( char c , std::size_t startpos = 0U ) const ;
77 ///< Finds the given character. Returns npos if
78 ///< not found.
79
80 std::size_t find( const std::string & s , std::size_t startpos = 0U ) const ;
81 ///< Finds the given string. Returns npos if not
82 ///< found.
83
84 std::size_t findSubStringAtEnd( const std::string & s , std::size_t startpos = 0U ) const ;
85 ///< Tries to find some leading sub-string of 's' that
86 ///< appears right at the end of the data, starting
87 ///< with the longest sub-string. Returns npos
88 ///< if not found.
89
90 const char * data( std::size_t pos , std::size_t size ) const ;
91 ///< Returns a pointer for the data at the given
92 ///< position that is contiguous for the given size.
93 ///< Data is shuffled around as required, which
94 ///< means that previous pointers are invalidated.
95
96 char at( std::size_t n ) const ;
97 ///< Returns the n'th character.
98
99 std::string str() const ;
100 ///< Returns the complete string.
101
102 std::string head( std::size_t n ) const ;
103 ///< Returns the leading sub-string of str() of up
104 ///< to 'n' characters.
105
106 public:
107 ~LineStore() = default ;
108 LineStore( const LineStore & ) = delete ;
109 LineStore( LineStore && ) = delete ;
110 LineStore & operator=( const LineStore & ) = delete ;
111 LineStore & operator=( LineStore && ) = delete ;
112
113 private:
114 const char * dataimp( std::size_t pos , std::size_t size ) ;
115 std::size_t search( std::string::const_iterator , std::string::const_iterator , std::size_t ) const ;
116
117 private:
118 std::string m_store ;
119 const char * m_extra_data {nullptr} ;
120 std::size_t m_extra_size {0U} ;
121} ;
122
123 inline
124 char GNet::LineStore::at( std::size_t n ) const
125{
126 G_ASSERT( n < size() ) ;
127 return n < m_store.size() ? m_store[n] : m_extra_data[n-m_store.size()] ;
128}
129
130 inline
131std::size_t GNet::LineStore::size() const
132{
133 return m_store.size() + m_extra_size ;
134}
135
136 inline
137 bool GNet::LineStore::empty() const
138{
139 return m_store.empty() && m_extra_size == 0U ;
140}
141
142#endif
GNet::LineStore
A pair of character buffers, one kept by value and the other being an ephemeral extension.
Definition: glinestore.h:41
GNet::LineStore::discard
void discard(std::size_t n)
Discards the first 'n' bytes and consolidates the residue.
Definition: glinestore.cpp:212
GNet::LineStore::append
void append(const std::string &)
Appends to the store (by copying).
Definition: glinestore.cpp:180
GNet::LineStore::data
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:351
GNet::LineStore::head
std::string head(std::size_t n) const
Returns the leading sub-string of str() of up to 'n' characters.
Definition: glinestore.cpp:396
GNet::LineStore::empty
bool empty() const
Returns true if size() is zero.
Definition: glinestore.h:137
GNet::LineStore::clear
void clear()
Clears all data.
Definition: glinestore.cpp:199
GNet::LineStore::LineStore
LineStore()
Default constructor.
GNet::LineStore::str
std::string str() const
Returns the complete string.
Definition: glinestore.cpp:387
GNet::LineStore::find
std::size_t find(char c, std::size_t startpos=0U) const
Finds the given character.
Definition: glinestore.cpp:257
GNet::LineStore::size
std::size_t size() const
Returns the overall size.
Definition: glinestore.h:131
GNet::LineStore::consolidate
void consolidate()
Consolidates the extension into the store.
Definition: glinestore.cpp:205
GNet::LineStore::extend
void extend(const char *, std::size_t)
Sets the extension.
Definition: glinestore.cpp:192
GNet::LineStore::findSubStringAtEnd
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:319
GNet::LineStore::at
char at(std::size_t n) const
Returns the n'th character.
Definition: glinestore.h:124
gassert.h
GNet
Network classes.
Definition: gdef.h:1243