supersede class SingleCharacterInputStreamDoubleBuffered

This commit is contained in:
Garux
2024-12-18 18:42:13 +05:00
parent b3ff10b287
commit 64708825a2
3 changed files with 10 additions and 42 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ public:
virtual void ungetToken() = 0;
virtual std::size_t getLine() const = 0;
virtual std::size_t getColumn() const = 0;
virtual bool bufferContains( const char* str ) const = 0;
virtual bool bufferContains( const char* str ) = 0;
};
class TextInputStream;
+2 -2
View File
@@ -40,7 +40,7 @@ class ScriptTokeniser final : public Tokeniser
Tokenise m_stack[3];
Tokenise* m_state;
SingleCharacterInputStreamDoubleBuffered<TextInputStream> m_istream;
SingleCharacterInputStream<TextInputStream> m_istream;
std::size_t m_scriptline;
std::size_t m_scriptcolumn;
@@ -340,7 +340,7 @@ public:
std::size_t getColumn() const {
return m_scriptcolumn;
}
bool bufferContains( const char* str ) const {
bool bufferContains( const char* str ){
return m_istream.bufferContains( str );
}
};
+7 -39
View File
@@ -355,49 +355,17 @@ public:
c = *m_cur++;
return true;
}
};
/// doublefuffered to be able to read forward for map format (valve220) detection
template<typename TextInputStreamType, int SIZE = 1024>
class SingleCharacterInputStreamDoubleBuffered
{
TextInputStreamType& m_inputStream;
char m_bu[SIZE];
char* m_buffer;
char* m_cur;
char* m_end;
char m_bu2[SIZE];
char* m_buffer2;
char* m_end2;
bool fillBuffer(){
std::swap( m_buffer, m_buffer2 );
// looks forward for map format (valve220) detection
bool bufferContains( const char* str ){
const size_t shift = m_cur - m_buffer;
std::memmove( m_buffer, m_cur, m_end - m_cur ); //shift not yet read data to the beginning
m_cur = m_buffer;
m_end = m_end2;
m_end2 = m_buffer2 + m_inputStream.read( m_buffer2, SIZE );
return m_cur != m_end;
}
public:
SingleCharacterInputStreamDoubleBuffered( TextInputStreamType& inputStream ) : m_inputStream( inputStream ), m_buffer( m_bu ), m_cur( m_buffer ), m_end( m_buffer ), m_buffer2( m_bu2 ){
m_end2 = m_buffer2 + m_inputStream.read( m_buffer2, SIZE );
}
bool readChar( char& c ){
if ( m_cur == m_end && !fillBuffer() ) {
return false;
}
c = *m_cur++;
return true;
}
bool bufferContains( const char* str ) const {
return ( std::search( m_cur, m_end, str, str + strlen( str ) ) != m_end ) ||
( std::search( m_buffer2, m_end2, str, str + strlen( str ) ) != m_end2 );
m_end -= shift;
m_end += m_inputStream.read( m_end, shift ); //fill freed space in the end
return std::search( m_cur, m_end, str, str + strlen( str ) ) != m_end;
}
};
/// \brief A wrapper for a TextOutputStream, optimised for writing a single character at a time.
class SingleCharacterOutputStream : public TextOutputStream
{