ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
escaper.cpp
1namespace alib::strings::util {
2
3//==================================================================================================
4// ==== StringEscaper
5//==================================================================================================
6#if ALIB_MONOMEM
7int StringEscaper::EscapeTokens( StringVectorMA& result, const String& value, const String& delimiters ) const {
8 const integer oldSize= result.Size();
9 Tokenizer tknzr( value, delimiters.CharAtStart() );
10 tknzr.TrimChars.Reset();
11 while(tknzr.HasNext())
12 result.Add( tknzr.Next() );
13
14 return int(result.Size() - oldSize);
15}
16#endif // if ALIB_MONOMEM
17
18//==================================================================================================
19// ==== StringEscaperStandard
20//==================================================================================================
22 Substring parser(src);
23 parser.Trim();
24 if( parser.Length() > 1 && parser.CharAtStart() == '"' && parser.CharAtEnd() == '"') {
25 parser.ConsumeChar <NC>();
26 parser.ConsumeCharFromEnd<NC>();
27 }
28 bool lastWasSlash= false;
29
30 while( parser.IsNotEmpty() ) {
31 character c= parser.ConsumeChar<NC>();
32
33 if( lastWasSlash ) {
34 lastWasSlash= false;
35 character escChr= c == '\\' ? '\\' :
36 c == '"' ? '"' :
37 c == 'n' ? '\n' :
38 c == 'r' ? '\r' :
39 c == 't' ? '\t' :
40 c == 'a' ? '\a' :
41 c == 'b' ? '\b' :
42 c == 'v' ? '\v' :
43 c == 'f' ? '\f' :
44 c == 'e' ? '\033' :
45 c;
46
47 dest._<NC>(escChr);
48 continue;
49 }
50
51 if( c== '\\' ) {
52 lastWasSlash= true;
53 continue;
54 }
55
56 dest._<NC>(c);
57 }
58
59 return dest;
60}
61
63 const String& delimiters ) const {
64 Substring parser(src);
65 bool needsQuotes= parser.CharAtStart() == ' '
66 || parser.CharAtStart() == '\t'
67 || parser.CharAtEnd() == ' '
68 || parser.CharAtEnd() == '\t';
69 if (!needsQuotes)
70 for( character c : delimiters )
71 if( parser.IndexOf(c) >= 0 ) {
72 needsQuotes= true;
73 break;
74 }
75
76 if ( needsQuotes )
77 dest._<NC>('"');
78
79 while( parser.IsNotEmpty() ) {
80 character c= parser.ConsumeChar();
81
82 switch(c) {
83 case '"' : dest._<NC>(needsQuotes ? "\\\"" : "\"");
84 break;
85 case '\\' : dest._<NC>("\\\\"); break;
86 case '\r' : dest._<NC>("\\r" ); break;
87 case '\n' : dest._<NC>("\\n" ); break;
88 case '\t' : dest._<NC>("\\t" ); break;
89 case '\a' : dest._<NC>("\\a" ); break;
90 case '\b' : dest._<NC>("\\b" ); break;
91 case '\v' : dest._<NC>("\\v" ); break;
92 case '\f' : dest._<NC>("\\f" ); break;
93 case '\033' : dest._<NC>("\\e" ); break;
94 default : dest._<NC>(c); break;
95 } }
96
97 if ( needsQuotes )
98 dest._('"');
99
100 return dest;
101}
102
103
104#if ALIB_MONOMEM
105int StringEscaperStandard::EscapeTokens( StringVectorMA& result, const String& src, const String& delimiters ) const {
106 String2K buf;
107 const integer oldSize= result.Size();
108 Tokenizer tknzr( src, delimiters.CharAtStart() );
109 tknzr.TrimChars.Reset();
110 while(tknzr.HasNext()) {
111 Escape( tknzr.Next(), buf.Reset(), delimiters );
112 result.Add( buf );
113 }
114 return int(result.Size() - oldSize);
115}
116
117int StringEscaperStandard::UnescapeTokens( StringVectorMA& result, const String& value, const String& delimiters ) const {
118 const integer oldSize= result.Size();
120 Substring src( value );
121
122 // tokenize
123 bool inQuote= false;
124 bool lastWasSlash= false;
125 integer idx= 0;
126 while( idx < src.Length() ) {
127 character c= src.CharAt<NC>( idx++ );
128
129 if( lastWasSlash ) {
130 lastWasSlash= false;
131 continue;
132 }
133
134 if( c== '\\' ) {
135 lastWasSlash= true;
136 continue;
137 }
138
139 if( c== '"' && (idx==1 || inQuote) ) {
140 inQuote= !inQuote;
141 continue;
142 }
143
144 if( !inQuote && delimiters.IndexOf(c) >= 0 ) {
145 Substring tok= src.Substring<NC>( 0, idx - 1 );
146 Unescape( tok, tempBuf );
147 result.Add( tempBuf );
148 tempBuf.Reset();
149 src.ConsumeChars( idx );
150 src.TrimStart();
151 idx= 0;
152 } }
153
154 if ( src.IsNotEmpty() ) {
155 Unescape( src, tempBuf );
156 result.Add( tempBuf );
157 }
158
159 return int (result.Size() - oldSize);
160}
161#endif // if ALIB_MONOMEM
162
163
164
165} // namespace [alib::util::strings]
TAString & _(const TAppendable &src)
void DbgDisableBufferReplacementWarning()
Definition tastring.hpp:236
constexpr integer Length() const
Definition string.hpp:300
TChar CharAtStart() const
Definition string.hpp:417
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.hpp:799
TChar CharAt(integer idx) const
Definition string.hpp:399
constexpr bool IsNotEmpty() const
Definition string.hpp:353
TChar CharAtEnd() const
Definition string.hpp:436
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.hpp:368
TSubstring & TrimStart(const TCString< TChar > &whiteSpaces=CStringConstantsTraits< TChar >::DefaultWhitespaces())
Definition substring.hpp:69
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)
Definition vector.hpp:81
integer Size() const noexcept
Definition vector.hpp:76
TSubstring< TChar > & Next(lang::Whitespaces trimming=lang::Whitespaces::Trim, TChar newDelim='\0')
Definition tokenizer.cpp:4
TLocalString< TChar, 8 > TrimChars
Definition tokenizer.hpp:68
strings::util::TTokenizer< character > Tokenizer
Type alias in namespace #"%alib".
strings::util::TStringVector< character, MonoAllocator > StringVectorMA
Type alias in namespace #"%alib".
Definition vector.hpp:107
strings::TEscape< character > Escape
Type alias in namespace #"%alib".
Definition format.hpp:531
lang::integer integer
Type alias in namespace #"%alib".
Definition integers.hpp:149
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
strings::TSubstring< character > Substring
Type alias in namespace #"%alib".
LocalString< 2048 > String2K
Type alias name for #"TLocalString;TLocalString<character,2048>".
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace #"%alib".
characters::character character
Type alias in namespace #"%alib".
LocalString< 512 > String512
Type alias name for #"TLocalString;TLocalString<character,512>".
virtual AString & Unescape(const String &src, AString &dest) const override
Definition escaper.cpp:21
virtual int UnescapeTokens(StringVectorMA &result, const String &src, const String &delimiters) const override
Definition escaper.cpp:117
virtual AString & Escape(const String &src, AString &dest, const String &delimiters) const override
Definition escaper.cpp:62
virtual int EscapeTokens(StringVectorMA &result, const String &src, const String &delimiters) const override
Definition escaper.cpp:105
virtual int EscapeTokens(StringVectorMA &result, const String &src, const String &delimiters) const
Definition escaper.cpp:7