ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
expressionformatter.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !defined(ALIB_DOX)
10# if !defined (HPP_ALIB_EXPRESSIONS_UTIL_EXPRESSION_FORMATTER)
12# endif
13# if !defined(HPP_ALIB_LANG_FORMAT_EXCEPTIONS)
15# endif
16
17# if !defined (HPP_ALIB_EXPRESSIONS_COMPILER)
19# endif
20#endif // !defined(ALIB_DOX)
21
22namespace alib::expressions::util {
23
25 Compiler* pCompiler,
26 SPFormatter formatter,
27 character separatorChar )
28: compiler ( pCompiler )
29, stdFormatter ( formatter )
30, originalFormatString( pFormatString )
31{
32 // use ALib standard formatter, if no dedicated instance was given.
33 if(!formatter.get())
35
36 // parse format string
37 integer nonExprPortionStart= 0;
38 integer parsePos = 0;
39 while(parsePos < originalFormatString.Length() )
40 {
41 // has next parse position?
42 // Note: if bracket is found at the end of string, we just ignore this here. An according
43 // exception is thrown in formatter later.
44 if( ( parsePos= originalFormatString.IndexOf( A_CHAR('{'), parsePos ) ) < 0
45 || parsePos == originalFormatString.Length() - 1 )
46 {
47 formatStringStripped << originalFormatString.Substring( nonExprPortionStart );
48 break;
49 }
50
51 // double Escape character? -> ignore
52 ++parsePos;
53 if( originalFormatString[parsePos] == A_CHAR('{') )
54 {
55 ++parsePos;
56 continue;
57 }
58
59 // add current portion to format string
60 formatStringStripped << originalFormatString.Substring( nonExprPortionStart, parsePos - nonExprPortionStart );
61
62 // Either find separator character or closing bracket of placeholder
63 integer endPos= parsePos;
64 while( endPos < originalFormatString.Length() -1
65 && originalFormatString[endPos] != separatorChar
66 && originalFormatString[endPos] != A_CHAR('}') )
67 ++endPos;
68
69 // extract expression string and set start of non-expression portion
70 String expressionString= originalFormatString.Substring( parsePos, endPos - parsePos );
71 nonExprPortionStart= endPos;
72 if( originalFormatString[endPos] == separatorChar )
73 ++nonExprPortionStart;
74
75 // add expression
76 try
77 {
78 expressions.emplace_back( compiler->Compile( expressionString ) );
79 }
80 catch( Exception& e)
81 {
84 throw;
85 }
86 }
87}
88
89
91{
92 // evaluate expressions and collect boxes
93 auto& results= stdFormatter->Acquire(ALIB_CALLER_PRUNED);
94 results.Add( formatStringStripped );
95
96 try
97 {
98 for( size_t expressionNo= 0; expressionNo < expressions.size() ; ++expressionNo )
99 results.Add( expressions[expressionNo]->Evaluate( scope ) );
100 }
101 catch( Exception& e)
102 {
103 stdFormatter->Release();
105 expressions.size() + 1, originalFormatString );
106 throw;
107 }
108
109 try
110 {
111 stdFormatter->FormatArgs( target );
112 }
113 catch(Exception& e)
114 {
115 stdFormatter->Release();
118 throw;
119 }
120
121 stdFormatter->Release();
122}
123
124} // namespace [alib::expressions::util]
virtual ALIB_API SPExpression Compile(const String &expressionString)
Definition compiler.cpp:251
ALIB_API ExpressionFormatter(const String formatString, Compiler *compiler, SPFormatter formatter=nullptr, character separatorChar='@')
ALIB_API void Format(AString &target, expressions::Scope &scope)
Exception & Add(const NCString &file, int line, const NCString &func, TEnum type, TArgs &&... args)
static SPFormatter GetDefault()
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.hpp:889
constexpr integer Length() const
Definition string.hpp:357
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.hpp:314
#define ALIB_CALLER_NULLED
Definition alib.hpp:846
#define A_CHAR(STR)
#define ALIB_CALLER_PRUNED
Definition alib.hpp:845
std::shared_ptr< lang::format::Formatter > SPFormatter
characters::character character
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286