ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
expressionformatter.cpp
1/// Utility types of camp \alib_expressions.
3
5 Compiler* pCompiler,
6 SPFormatter formatter,
7 character separatorChar )
8: compiler ( pCompiler )
9, stdFormatter ( formatter )
10, originalFormatString( pFormatString ) {
11 // use ALib standard formatter, if no dedicated instance was given.
12 if(!formatter.Get())
14
15 String256 escapedFS; escapedFS.DbgDisableBufferReplacementWarning();
16 StringEscaperStandard().Escape(pFormatString, escapedFS, EMPTY_STRING);
17
18 // parse format string
19 integer nonExprPortionStart= 0;
20 integer parsePos = 0;
21 while(parsePos < escapedFS.Length() ) {
22 // has a next parse position?
23 // Note: if bracket is found at the end of string, we just ignore this here. An according
24 // exception is thrown in formatter later.
25 if( ( parsePos= escapedFS.IndexOf( A_CHAR('{'), parsePos ) ) < 0
26 || parsePos == escapedFS.Length() - 1 )
27 {
28 formatStringStripped << escapedFS.Substring( nonExprPortionStart );
29 break;
30 }
31
32 // double Escape character? -> ignore
33 ++parsePos;
34 if( escapedFS[parsePos] == A_CHAR('{') ) {
35 ++parsePos;
36 continue;
37 }
38
39 // add the current portion to format string
40 formatStringStripped << escapedFS.Substring( nonExprPortionStart, parsePos - nonExprPortionStart );
41 formatSubstrings.push_back(formatStringStripped.Length() -1);
42
43 // Either find separator character or closing bracket of placeholder
44 integer endPos= parsePos;
45 while( endPos < escapedFS.Length() -1
46 && escapedFS[endPos] != separatorChar
47 && escapedFS[endPos] != A_CHAR('}') )
48 ++endPos;
49
50 // extract expression string and set start of non-expression portion
51 String expressionString= escapedFS.Substring( parsePos, endPos - parsePos );
52 nonExprPortionStart= endPos;
53 if( escapedFS[endPos] == separatorChar )
54 ++nonExprPortionStart;
55
56 // add expression
57 try { expressions.emplace_back( compiler->Compile( expressionString ) ); }
58 catch( Exception& e) {
60 expressions.size() + 1, escapedFS );
61 throw;
62 } }
63 formatSubstrings.push_back(formatStringStripped.Length());
64}
65
66
68 // evaluate expressions and collect boxes
69 try {
70 // the first string is the start-substring.
71 // Attn: must be done with formatter to unescape the string.
72 stdFormatter->Format( target, formatStringStripped.Substring(0, formatSubstrings[0]));
73 size_t idx= 1;
74
75 for( size_t expressionNo= 0; expressionNo < expressions.size() ; ++expressionNo ) {
76 Box exprResult= expressions[expressionNo]->Evaluate( scope );
77
78 try {
79 String formatString= formatStringStripped.Substring( formatSubstrings[idx-1],
80 formatSubstrings[idx] - formatSubstrings[idx-1] );
81 stdFormatter->Format( target, formatString, exprResult );
82 ++idx;
83 }
84 catch(Exception& e) {
87 throw;
88 } } }
89 catch( Exception& e)
90 {
93 throw;
94 }
95
96}
97
98} // namespace [alib::expressions::util]
#define ALIB_CALLER_NULLED
#define A_CHAR(STR)
Exception & Add(const lang::CallerInfo &ci, TEnum type, TArgs &&... args)
Compiler * compiler
The expression compiler (as given in the constructor).
AString formatStringStripped
The resulting format string passed to ALib formatters.
ExpressionFormatter(const String &formatString, Compiler *compiler, SPFormatter formatter=nullptr, character separatorChar='@')
SPFormatter stdFormatter
The formatter to use (as given in the constructor).
void Format(AString &target, expressions::Scope &scope)
const AString originalFormatString
The original format string. Used only for exception information.
std::vector< Expression > expressions
The expression functions to receive the format data.
static SPFormatter DEFAULT
Utility types of camp ALib Expressions.
containers::SharedPtr< format::Formatter > SPFormatter
Definition formatter.hpp:41
strings::util::StringEscaperStandard StringEscaperStandard
Type alias in namespace #"%alib".
Definition escaper.hpp:184
strings::TEscape< character > Escape
Type alias in namespace #"%alib".
Definition format.hpp:531
constexpr const String EMPTY_STRING
An empty string of the default character type.
Definition string.hpp:2227
lang::integer integer
Type alias in namespace #"%alib".
Definition integers.hpp:149
boxing::Box Box
Type alias in namespace #"%alib".
Definition box.hpp:1128
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
exceptions::Exception Exception
Type alias in namespace #"%alib".
LocalString< 256 > String256
Type alias name for #"TLocalString;TLocalString<character,256>".
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace #"%alib".
characters::character character
Type alias in namespace #"%alib".