changed in Generator<>:

StreamType
 we use method write where the content should not be escaped (html escaping)
 we use operator<< where the content can be escaped (such as error messages)

PrintSpecialText() and PrintNormalText() are a little faster now
we use 'write' for a whole text instead of printing each character



git-svn-id: svn://ttmath.org/publicrep/ezc/trunk@328 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2010-11-25 01:55:32 +00:00
parent 33c86d494b
commit 5108495540
1 changed files with 73 additions and 119 deletions

View File

@ -46,8 +46,11 @@
namespace Ezc
{
/*
StreamType
we use method write where the content should not be escaped (html escaping)
we use operator<< where the content can be escaped (such as error messages)
*/
template<class StreamType>
class Generator
{
@ -68,6 +71,8 @@ public:
// \t will be a tabulator (9)
// \s will be a space
// \\ will be one '\'
// if the second character is different from those shown above the first slash will be printed
// so "\x" gives "\x"
// default: false
void RecognizeSpecialChars(bool spec);
@ -81,13 +86,13 @@ public:
// default: false
void SkipNewLine(bool skip);
// the main method for generating
void Generate();
private:
StreamType * output_stream;
Pattern * pattern;
Pattern * pattern;
Functions<StreamType> * functions;
// temporary error messages
@ -106,66 +111,6 @@ private:
const std::wstring empty;
void PutChar(wchar_t z, StreamType & out);
void PutStringGeneric(const wchar_t * start, size_t len, StreamType & out);
// !! tu bedzie chyba problem z kompilacja
// kiedy typ strumienia to bedzie std::ostream
// chwilowo zostawiam jak jest
/*
void PutString(const wchar_t * start, const wchar_t * end, std::ostream & out)
{
PutStringGeneric(start, end-start, out);
}
void PutString(const wchar_t * start, const wchar_t * end, std::iostream & out)
{
PutStringGeneric(start, end-start, out);
}
void PutString(const wchar_t * start, const wchar_t * end, std::ofstream & out)
{
PutStringGeneric(start, end-start, out);
}
void PutString(const wchar_t * start, const wchar_t * end, std::fstream & out)
{
PutStringGeneric(start, end-start, out);
}
void PutString(const wchar_t * start, const wchar_t * end, std::ostringstream & out)
{
PutStringGeneric(start, end-start, out);
}
void PutString(const wchar_t * start, const wchar_t * end, std::stringstream & out)
{
PutStringGeneric(start, end-start, out);
}
*/
void PutString(const std::wstring & str, StreamType & out)
{
PutString(str.c_str(), str.c_str() + str.size(), out);
}
void PutString(const wchar_t * start, const wchar_t * end, StreamType & out)
{
out.write(start, end-start);
}
bool Find(const std::wstring & key, typename Functions<StreamType>::Function ** function);
void Call(typename Functions<StreamType>::Function * function, FunInfo<StreamType> & info);
@ -176,6 +121,7 @@ void PutString(const wchar_t * start, const wchar_t * end, std::stringstream & o
void CallVariable(typename Functions<StreamType>::Function * function, FunInfo<StreamType> & info);
wchar_t CreateSpecialChar(wchar_t c);
const wchar_t * PrintSpecialChar(const wchar_t * start, const wchar_t * end);
void PrintSpecialText(const wchar_t * start, const wchar_t * end);
void PrintNormalText(const wchar_t * start, const wchar_t * end);
void TrimWhite(const wchar_t *& start, const wchar_t *& end);
@ -209,39 +155,6 @@ void PutString(const wchar_t * start, const wchar_t * end, std::stringstream & o
template<class StreamType>
void Generator<StreamType>::PutChar(wchar_t z, StreamType & out)
{
typedef typename StreamType::char_type CharType;
out << static_cast<CharType>(z);
}
/*
template<class StreamType>
void Generator<StreamType>::PutString(const wchar_t * start, const wchar_t * end, StreamType & out)
{
out.write(start, end-start);
}
template<class StreamType>
void Generator<StreamType>::PutString(const std::wstring & str, StreamType & out)
{
PutString(str.c_str(), str.c_str() + str.size(), out);
}
*/
template<class StreamType>
void Generator<StreamType>::PutStringGeneric(const wchar_t * start, size_t len, StreamType & out)
{
typedef typename StreamType::char_type CharType;
for(size_t i=0 ; i<len ; ++i)
out << static_cast<CharType>(start[i]);
}
@ -473,30 +386,58 @@ return res;
}
// a special character is precedeed by a slash '\'
// if the special character is unknown the first slash is printing
// so "\t" gives one character of code 9
// and "\x" gives "\x"
template<class StreamType>
const wchar_t * Generator<StreamType>::PrintSpecialChar(const wchar_t * start, const wchar_t * end)
{
wchar_t special = 0;
if( *start == '\\' && (start+1) != end )
special = CreateSpecialChar(*(start+1));
if( special )
{
output_stream->write(&special, 1);
start += 2;
}
else
{
if( !skip_new_line || *start != 10 )
output_stream->write(start, 1);
start += 1;
}
return start;
}
template<class StreamType>
void Generator<StreamType>::PrintSpecialText(const wchar_t * start, const wchar_t * end)
{
wchar_t special;
while( start != end )
{
special = 0;
const wchar_t * end2 = start;
if( *start == '\\' && (start+1) != end )
special = CreateSpecialChar(*(start+1));
// looking for a first new line character or a special char
// (for new line only if skip_new_line is true)
while( end2 != end && *end2 != '\\' && (!skip_new_line || *end2 != 10) )
end2 += 1;
if( special )
{
*output_stream << special;
start += 2;
}
else
{
if( !(skip_new_line && *start == 10) )
*output_stream << *start;
// printing the first part of the text
if( start != end2 )
output_stream->write(start, end2 - start);
start += 1;
}
start = end2;
// skipping one or more new line characters or special chars
// (new lines only if skip_new_line is true)
while( start != end && (*start == '\\' || (skip_new_line && *start == 10)) )
start = PrintSpecialChar(start, end);
}
}
@ -506,16 +447,29 @@ void Generator<StreamType>::PrintNormalText(const wchar_t * start, const wchar_t
{
if( skip_new_line )
{
for( ; start != end ; ++start)
while( start != end )
{
if( *start != 10 )
PutChar(*start, *output_stream);
const wchar_t * end2 = start;
// looking for a first new line character
while( end2 != end && *end2 != 10 )
end2 += 1;
// printing the first part of the text (until the new line)
if( start != end2 )
output_stream->write(start, end2 - start);
start = end2;
// skipping one or more new line characters
while( start != end && *start == 10 )
start += 1;
}
}
else
{
if( start != end )
PutString(start, end, *output_stream);
output_stream->write(start, end-start);
}
}
@ -569,7 +523,7 @@ template<class StreamType>
void Generator<StreamType>::CreateMsg(StreamType & o, const wchar_t * type, const wchar_t * arg)
{
pattern->CreateMsg(temp_msg, type, arg);
PutString(temp_msg, o);
o << temp_msg;
temp_msg.clear();
}
@ -630,7 +584,7 @@ void Generator<StreamType>::MakeTextNormal(Item & item)
bool called = Call(item.functions[0], 0, &pfun);
if( called && pfun->type == Functions<StreamType>::variable )
PutString(pfun->variable, *output_stream);
(*output_stream) << pfun->variable;
}