E-MailRelay

glinestore.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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. An iterator class can iterate over the
38/// combined data. Used in the implementation of GNet::LineBuffer
39/// as a zero-copy optimisation.
40///
41 class GNet::LineStore
42{
43 public:
44 LineStore() ;
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 sub-string. Returns npos if not
83 ///< found.
84
85 std::size_t findSubStringAtEnd( const std::string & s , std::size_t startpos = 0U ) const ;
86 ///< Finds a non-empty leading substring 's' that
87 ///< appears at the end of the data. 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 void operator=( const LineStore & ) = delete ;
111 void 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:42
GNet::LineStore::discard
void discard(std::size_t n)
Discards the first 'n' bytes and consolidates the residue.
Definition: glinestore.cpp:235
GNet::LineStore::append
void append(const std::string &)
Appends to the store (by copying).
Definition: glinestore.cpp:203
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:374
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:417
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:222
GNet::LineStore::LineStore
LineStore()
Default constructor.
GNet::LineStore::str
std::string str() const
Returns the complete string.
Definition: glinestore.cpp:409
GNet::LineStore::find
std::size_t find(char c, std::size_t startpos=0U) const
Finds the given character.
Definition: glinestore.cpp:280
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:228
GNet::LineStore::extend
void extend(const char *, std::size_t)
Sets the extension.
Definition: glinestore.cpp:215
GNet::LineStore::findSubStringAtEnd
std::size_t findSubStringAtEnd(const std::string &s, std::size_t startpos=0U) const
Finds a non-empty leading substring 's' that appears at the end of the data.
Definition: glinestore.cpp:342
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:1115