ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
escaper.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14// ====================================== Global Fragment ======================================
16#include "ALib.Strings.Vector.H"
17// =========================================== Module ==========================================
18#if ALIB_C20_MODULES
21#else
22# include "ALib.Strings.Escaper.H"
24#endif
25
26// ====================================== Implementation =======================================
27namespace alib::strings::util {
28
29// =================================================================================================
30// ==== StringEscaper
31// =================================================================================================
32#if ALIB_MONOMEM
33int StringEscaper::EscapeTokens( StringVectorMA& result, const String& value, const String& delimiters ) const
34{
35 const integer oldSize= result.Size();
36 Tokenizer tknzr( value, delimiters.CharAtStart() );
37 tknzr.TrimChars.Reset();
38 while(tknzr.HasNext())
39 result.Add( tknzr.Next() );
40
41 return int(result.Size() - oldSize);
42}
43#endif // if ALIB_MONOMEM
44
45// =================================================================================================
46// ==== StringEscaperStandard
47// =================================================================================================
49{
50 Substring parser(src);
51 parser.Trim();
52 if( parser.Length() > 1 && parser.CharAtStart() == '"' && parser.CharAtEnd() == '"')
53 {
54 parser.ConsumeChar <NC>();
55 parser.ConsumeCharFromEnd<NC>();
56 }
57 bool lastWasSlash= false;
58
59 while( parser.IsNotEmpty() )
60 {
61 character c= parser.ConsumeChar<NC>();
62
63 if( lastWasSlash )
64 {
65 lastWasSlash= false;
66 character escChr= c == '\\' ? '\\' :
67 c == '"' ? '"' :
68 c == 'n' ? '\n' :
69 c == 'r' ? '\r' :
70 c == 't' ? '\t' :
71 c == 'a' ? '\a' :
72 c == 'b' ? '\b' :
73 c == 'v' ? '\v' :
74 c == 'f' ? '\f' :
75 c == 'e' ? '\033' :
76 c;
77
78 dest._<NC>(escChr);
79 continue;
80 }
81
82 if( c== '\\' )
83 {
84 lastWasSlash= true;
85 continue;
86 }
87
88 dest._<NC>(c);
89 }
90
91 return dest;
92}
93
94AString& StringEscaperStandard::Escape( const String& src, AString& dest, const String& delimiters ) const
95{
96 Substring parser(src);
97 bool needsQuotes= parser.CharAtStart() == ' '
98 || parser.CharAtStart() == '\t'
99 || parser.CharAtEnd() == ' '
100 || parser.CharAtEnd() == '\t';
101 if (!needsQuotes)
102 for( character c : delimiters )
103 if( parser.IndexOf(c) >= 0 )
104 {
105 needsQuotes= true;
106 break;
107 }
108
109 if ( needsQuotes )
110 dest._<NC>('"');
111
112 while( parser.IsNotEmpty() )
113 {
114 character c= parser.ConsumeChar();
115
116 switch(c)
117 {
118 case '"' : dest._<NC>(needsQuotes ? "\\\"" : "\"");
119 break;
120 case '\\' : dest._<NC>("\\\\"); break;
121 case '\r' : dest._<NC>("\\r" ); break;
122 case '\n' : dest._<NC>("\\n" ); break;
123 case '\t' : dest._<NC>("\\t" ); break;
124 case '\a' : dest._<NC>("\\a" ); break;
125 case '\b' : dest._<NC>("\\b" ); break;
126 case '\v' : dest._<NC>("\\v" ); break;
127 case '\f' : dest._<NC>("\\f" ); break;
128 case '\033' : dest._<NC>("\\e" ); break;
129 default : dest._<NC>(c); break;
130 }
131 }
132
133 if ( needsQuotes )
134 dest._('"');
135
136 return dest;
137}
138
139
140#if ALIB_MONOMEM
141int StringEscaperStandard::EscapeTokens( StringVectorMA& result, const String& src, const String& delimiters ) const
142{
143 String2K buf;
144 const integer oldSize= result.Size();
145 Tokenizer tknzr( src, delimiters.CharAtStart() );
146 tknzr.TrimChars.Reset();
147 while(tknzr.HasNext())
148 {
149 Escape( tknzr.Next(), buf.Reset(), delimiters );
150 result.Add( buf );
151 }
152 return int(result.Size() - oldSize);
153}
154
155int StringEscaperStandard::UnescapeTokens( StringVectorMA& result, const String& value, const String& delimiters ) const
156{
157 const integer oldSize= result.Size();
159 Substring src( value );
160
161 // tokenize
162 bool inQuote= false;
163 bool lastWasSlash= false;
164 integer idx= 0;
165 while( idx < src.Length() )
166 {
167 character c= src.CharAt<NC>( idx++ );
168
169 if( lastWasSlash )
170 {
171 lastWasSlash= false;
172 continue;
173 }
174
175 if( c== '\\' )
176 {
177 lastWasSlash= true;
178 continue;
179 }
180
181 if( c== '"' && (idx==1 || inQuote) )
182 {
183 inQuote= !inQuote;
184 continue;
185 }
186
187 if( !inQuote && delimiters.IndexOf(c) >= 0 )
188 {
189 Substring tok= src.Substring<NC>( 0, idx - 1 );
190 Unescape( tok, tempBuf );
191 result.Add( tempBuf );
192 tempBuf.Reset();
193 src.ConsumeChars( idx );
194 src.TrimStart();
195 idx= 0;
196 }
197 }
198
199 if ( src.IsNotEmpty() )
200 {
201 Unescape( src, tempBuf );
202 result.Add( tempBuf );
203 }
204
205 return int (result.Size() - oldSize);
206}
207#endif // if ALIB_MONOMEM
208
209
210
211} // namespace [alib::util::strings]
TAString & _(const TAppendable &src)
void DbgDisableBufferReplacementWarning()
Definition tastring.inl:245
constexpr integer Length() const
Definition string.inl:318
TChar CharAtStart() const
Definition string.inl:440
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.inl:844
TChar CharAt(integer idx) const
Definition string.inl:421
constexpr bool IsNotEmpty() const
Definition string.inl:371
TChar CharAtEnd() const
Definition string.inl:460
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.inl:386
TSubstring & TrimStart(const TCString< TChar > &whiteSpaces=CStringConstantsTraits< TChar >::DefaultWhitespaces())
Definition substring.inl:76
bool ConsumeCharFromEnd(TChar consumable)
integer ConsumeChars(integer regionLength, TSubstring *target=nullptr)
TSubstring & Trim(const TCString< TChar > &whiteSpaces=CStringConstantsTraits< TChar >::DefaultWhitespaces())
integer Add(const strings::TString< TChar > &src)
ALIB_DLL TSubstring< TChar > & Next(lang::Whitespaces trimming=lang::Whitespaces::Trim, TChar newDelim='\0')
Definition tokenizer.cpp:26
TLocalString< TChar, 8 > TrimChars
Definition tokenizer.inl:70
strings::TEscape< character > Escape
Type alias in namespace alib.
Definition format.inl:536
LocalString< 512 > String512
Type alias name for TLocalString<character,512>.
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
strings::util::TTokenizer< character > Tokenizer
Type alias in namespace alib.
strings::util::TStringVector< character, MonoAllocator > StringVectorMA
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381
characters::character character
Type alias in namespace alib.
LocalString< 2048 > String2K
Type alias name for TLocalString<character,2048>.
strings::TSubstring< character > Substring
Type alias in namespace alib.
virtual ALIB_DLL AString & Unescape(const String &src, AString &dest) const override
Definition escaper.cpp:48
virtual ALIB_DLL int UnescapeTokens(StringVectorMA &result, const String &src, const String &delimiters) const override
Definition escaper.cpp:155
virtual ALIB_DLL AString & Escape(const String &src, AString &dest, const String &delimiters) const override
Definition escaper.cpp:94
virtual ALIB_DLL int EscapeTokens(StringVectorMA &result, const String &src, const String &delimiters) const override
Definition escaper.cpp:141
virtual ALIB_DLL int EscapeTokens(StringVectorMA &result, const String &src, const String &delimiters) const
Definition escaper.cpp:33