ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
propertyformatter.cpp
1namespace alib::format {
2
4 const TCallbackTable& propertyTable,
5 SPFormatter formatter,
6 character ESCCharacter )
7: stdFormatter ( formatter )
8, propertyFormatString( customFormatStringx ) {
9 if(!formatter.Get())
11
13
14 integer parsePos= 0;
15 while(parsePos < formatString.Length() ) {
16 // find next parse position
17 integer parsePosCopy= parsePos;
18 if( (parsePos= formatString.IndexOf( ESCCharacter, parsePosCopy ) ) < 0 )
19 break;
20
21 String identifier;
22 integer endPos= parsePos+ 1;
23 if( endPos < formatString.Length() ) {
24 // double Escape character? -> replace to single!
25 if( formatString[endPos] == ESCCharacter ) {
26 formatString.Delete( endPos, 1 );
27 ++parsePos;
28 continue;
29 }
30
31 // find end of Identifier
32 while( endPos < formatString.Length() && isalpha( formatString[endPos] ) )
33 ++endPos;
34
35 identifier= formatString.Substring<NC>( parsePos + 1, endPos - parsePos - 1 );
36 }
37
38 // not found
39 if( identifier.IsEmpty() )
40 continue;
41
42 // add callback
43 auto entryIt= propertyTable.begin();
44 while( entryIt != propertyTable.end() ) {
45 if( Substring(identifier).ConsumePartOf<lang::Case::Ignore>( entryIt->Name,
46 entryIt->MinimumRecognitionLength
47 ) == identifier.Length() )
48 {
49 // remove identifier from format string
50 formatString.Delete( parsePos, endPos - parsePos + (formatString[endPos] == ESCCharacter ? 1 : 0) );
51
52 // store callback
53 callBacks.emplace_back( &*entryIt );
54 ++parsePos;
55 break;
56 }
57 ++entryIt;
58 }
59
60 // identifier not found?
61 if( entryIt == propertyTable.end() ) {
62 Exception e( ALIB_CALLER_NULLED, FMTExceptions::UnknownPropertyInFormatString,
63 ESCCharacter, identifier, customFormatStringx );
64 for( auto& row : propertyTable )
65 e.Back().Add( ESCCharacter, row.Name, ", " );
66 e.Back().back()= '.'; // replace the last comma
67
68 throw e;
69} } }
70
71
72void PropertyFormatter::Format( AString& target, const Box& src ) {
73 // string buffers
74 std::vector<String> heapStrings;
75 String128 localString;
77
78 // collect boxes
79 BoxesMA& results= stdFormatter->GetArgContainer();
80 results.Add( formatString );
81 for( auto& entry : callBacks ) {
82 Box argument= entry->Callback( src, localString );
83 if( localString.IsNotEmpty() ) {
84 heapStrings.emplace_back( localString );
85 argument= heapStrings.back();
86 }
87
88 results.Add ( argument );
89 localString.Reset();
90 }
91
92 // invoke ALib default formatter
93 try
94 {
95 stdFormatter->FormatArgs( target, results );
96 }
97 catch(Exception& e)
98 {
100 throw;
101 }
102
103 results.clear();
104}
105
106
107} // namespace [alib::format]
#define ALIB_CALLER_NULLED
TBoxes & Add()
Definition boxes.hpp:54
Exception & Add(const lang::CallerInfo &ci, TEnum type, TArgs &&... args)
static SPFormatter DEFAULT
TCallbackResultTable callBacks
The callback functions to receive the format data.
AString formatString
The resulting format string passed to ALib formatters.
SPFormatter stdFormatter
The formatter (as given in the constructor).
PropertyFormatter(const String customFormatString, const TCallbackTable &propertyTable, SPFormatter formatter=nullptr, character ESCCharacter='@')
std::vector< IdentifierEntry > TCallbackTable
Type definition of the callback table.
AString propertyFormatString
The original format string. Used only for exception information.
void Format(AString &target, const Box &src)
void DbgDisableBufferReplacementWarning()
Definition tastring.hpp:236
constexpr bool IsNotEmpty() const
Definition string.hpp:353
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< 128 > String128
Type alias name for #"TLocalString;TLocalString<character,128>".
boxing::TBoxes< MonoAllocator > BoxesMA
Type alias in namespace #"%alib".
Definition boxes.hpp:192
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace #"%alib".
characters::character character
Type alias in namespace #"%alib".