-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_api.h
370 lines (303 loc) · 12.3 KB
/
string_api.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#ifndef STRINGAPI_H
#define STRINGAPI_H
#include <algorithm>
#include <cstring>
#include <fstream>
#include <initializer_list>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
namespace StringAPI
{
auto LoadFileContent( const char* FileName ) -> std::string
{
using it_type = std::istreambuf_iterator<char>;
std::ifstream Fin( FileName );
if( Fin ) return { it_type( Fin ), it_type( /*default_sentinel*/ ) };
return {};
}
struct Write
{
auto To( std::string_view Location )
{
using it_type = std::ostreambuf_iterator<char>;
std::ofstream Fout( Location.data(), std::ofstream::trunc );
if( Fout ) std::copy( Source.begin(), Source.end(), it_type( Fout ) );
}
std::string_view Source;
};
struct Search
{
auto In( std::string_view Source )
{
return std::search( Source.begin(), Source.end(), //
std::boyer_moore_searcher{ Pattern.begin(), Pattern.end() } );
}
std::string_view Pattern;
};
struct Trim
{
friend auto operator|( std::string_view Source, const Trim& Adaptor )
{
if( auto pos = Source.find_first_not_of( Adaptor.Exclude ); pos != Source.npos )
Source.remove_prefix( pos );
else
Source.remove_prefix( Source.length() );
if( auto pos = Source.find_last_not_of( Adaptor.Exclude ); pos != Source.npos )
Source.remove_suffix( Source.length() - pos - 1 );
return Source;
}
const char* Exclude;
};
struct After
{
friend auto operator|( std::string_view Source, const After& Adaptor )
{
auto Start = Search( Adaptor.LeftBound ).In( Source ) + Adaptor.LeftBound.length();
auto End = Source.end();
return Start < End ? std::string_view{ Start, End } : std::string_view{ End, 0 };
}
friend void operator|=( auto& Source, const After& Adaptor ) { Source = Source | Adaptor; }
std::string_view LeftBound;
};
struct Between
{
friend auto operator|( std::string_view Source, const Between& Adaptor )
{
auto Start = Search( Adaptor.LeftBound ).In( Source ) + Adaptor.LeftBound.length();
auto End = Search( Adaptor.RightBound ).In( { Start, Source.end() } );
return Start < End ? std::string_view{ Start, End } : std::string_view{ End, 0 };
}
friend void operator|=( auto& Source, const Between& Adaptor ) { Source = Source | Adaptor; }
std::string_view LeftBound, RightBound;
};
struct Count
{
auto In( std::string_view Source )
{
auto BMS = std::boyer_moore_searcher{ Pattern.begin(), Pattern.end() };
auto Count = -1;
auto NewFound = Source.begin();
for( auto NewFound = Source.begin(); //
NewFound != Source.end(); //
NewFound = std::search( Source.begin(), Source.end(), BMS ) )
{
++Count;
Source = std::string_view{ NewFound + Pattern.length(), Source.end() };
}
return Count;
}
std::string_view Pattern;
};
struct Split_
{
std::string_view BaseRange;
auto By( const char Delimiter )
{
auto RangeBegin = BaseRange.begin();
auto RangeEnd = BaseRange.end();
auto Result = std::vector<std::string_view>{};
Result.reserve( std::count( RangeBegin, RangeEnd, Delimiter ) + 1 );
while( RangeBegin != RangeEnd )
{
auto DelimiterPos = std::find( RangeBegin, RangeEnd, Delimiter );
Result.emplace_back( RangeBegin, DelimiterPos );
if( DelimiterPos == RangeEnd )
RangeBegin = RangeEnd;
else
RangeBegin = DelimiterPos + 1;
}
return Result;
}
};
struct Split
{
std::string_view BaseRange;
struct InternalItorSentinel
{};
struct InternalItor
{
std::string_view BaseRange;
const char Delimiter;
auto operator*()
{
auto DelimiterPos = std::find( BaseRange.begin(), BaseRange.end(), Delimiter );
return std::string_view{ BaseRange.begin(), DelimiterPos };
}
auto operator!=( InternalItorSentinel ) { return ! BaseRange.empty(); }
auto operator++()
{
auto DelimiterPos = std::find( BaseRange.begin(), BaseRange.end(), Delimiter );
if( DelimiterPos == BaseRange.end() )
BaseRange.remove_suffix( BaseRange.length() );
else
BaseRange = std::string_view{ DelimiterPos + 1, BaseRange.end() };
}
};
struct InternalRange
{
std::string_view BaseRange;
const char Delimiter;
auto begin() { return InternalItor{ BaseRange, Delimiter }; }
auto end() { return InternalItorSentinel{}; }
auto size()
{
return ! BaseRange.ends_with( Delimiter ) // ending delim adjustment
+ std::count( BaseRange.begin(), BaseRange.end(), Delimiter );
}
};
auto By( const char Delimiter ) { return InternalRange{ BaseRange, Delimiter }; };
};
struct SplitBetween
{
std::string_view LeftDelimiter, RightDelimiter;
struct InternalItorSentinel
{};
struct InternalItor
{
std::string_view BaseRange, LeftDelimiter, RightDelimiter;
auto operator*() { return BaseRange | Between( "", RightDelimiter ); }
auto operator!=( InternalItorSentinel ) { return ! BaseRange.empty(); }
auto operator++()
{
BaseRange |= After( RightDelimiter );
BaseRange |= After( LeftDelimiter );
if( ! BaseRange.contains( RightDelimiter ) ) BaseRange.remove_prefix( BaseRange.length() );
}
};
struct InternalRange
{
std::string_view BaseRange, LeftDelimiter, RightDelimiter;
auto begin() { return InternalItor{ BaseRange | After( LeftDelimiter ), LeftDelimiter, RightDelimiter }; }
auto end() { return InternalItorSentinel{}; }
};
friend auto operator|( std::string_view Source, const SplitBetween& Adaptor )
{
return InternalRange{ Source, Adaptor.LeftDelimiter, Adaptor.RightDelimiter };
}
};
template<typename Predicate>
struct DropIf
{
Predicate DropCondition;
template<typename AncestorRange>
struct InternalRange
{
AncestorRange BaseRange;
Predicate DropCondition;
auto begin()
{
auto First = BaseRange.begin();
while( ( First != BaseRange.end() ) && DropCondition( *First ) ) ++First;
return InternalItor{ *this, First };
}
auto end() { return BaseRange.end(); }
};
template<typename AncestorRange, typename AncestorItor>
struct InternalItor
{
AncestorRange EnclosingRange;
AncestorItor BaseItor;
auto operator*() { return *BaseItor; }
auto operator!=( const auto& RHS ) { return BaseItor != RHS; }
auto operator++()
{
++BaseItor;
while( ( BaseItor != EnclosingRange.end() ) && EnclosingRange.DropCondition( *BaseItor ) ) ++BaseItor;
}
};
friend auto operator|( auto SourceRange, const DropIf& Adaptor ) { return InternalRange{ SourceRange, Adaptor.DropCondition }; }
};
struct Take
{
int N;
template<typename AncestorRange>
struct InternalRange
{
AncestorRange BaseRange;
int N;
auto begin() { return InternalItor{ BaseRange.begin(), N }; }
auto end() { return BaseRange.end(); }
};
template<typename AncestorItor>
struct InternalItor
{
AncestorItor BaseItor;
int N;
auto operator*() { return *BaseItor; }
auto operator!=( const auto& RHS ) { return N > 0 && BaseItor != RHS; }
auto operator++()
{
if( N-- > 0 ) ++BaseItor;
}
};
friend auto operator|( auto SourceRange, const Take& Adaptor ) { return InternalRange{ SourceRange, Adaptor.N }; }
};
#define BatchReplace( ... ) \
BatchReplace_impl { __VA_ARGS__ }
using BatchReplaceSubstitutionPair = std::array<std::string_view, 2>;
struct BatchReplace_impl : std::vector<BatchReplaceSubstitutionPair>
{
BatchReplace_impl( const std::initializer_list<BatchReplaceSubstitutionPair>& IL ) : std::vector<BatchReplaceSubstitutionPair>{ IL }
{}
auto In( std::string_view Source ) const
{
auto Result = std::string{};
auto TotalLength = Source.length();
for( auto&& SubstitutionPair : *this )
TotalLength
+= ( SubstitutionPair[ 1 ].length() - SubstitutionPair[ 0 ].length() ) * Count( SubstitutionPair[ 0 ] ).In( Source );
Result.reserve( TotalLength );
auto SourceBegin = Source.begin();
auto SourceEnd = Source.end();
while( SourceBegin != SourceEnd )
{
auto TokenStart = std::find( SourceBegin, SourceEnd, '$' );
while( TokenStart != SourceEnd && //
std::string_view{ TokenStart, TokenStart + 2 } != "${" )
TokenStart = std::find( TokenStart + 1, SourceEnd, '$' );
Result.append( SourceBegin, TokenStart );
if( TokenStart == SourceEnd ) return Result;
auto TokenEnd = std::find( TokenStart + 2, SourceEnd, '}' );
if( TokenEnd == SourceEnd )
{
Result.append( TokenStart, SourceEnd );
return Result;
}
std::advance( TokenEnd, 1 );
auto ReplaceToken = std::string_view{ TokenStart, TokenEnd };
auto ReplaceStringItor = std::find_if( begin(), end(), //
[ = ]( const auto& E ) { return ReplaceToken == E[ 0 ]; } );
if( ReplaceStringItor != end() )
Result.append( ( *ReplaceStringItor )[ 1 ] );
else
Result.append( ReplaceToken );
SourceBegin = TokenEnd;
}
return Result;
}
friend auto operator|( std::string_view Source, const BatchReplace_impl& Adaptor ) { return Adaptor.In( Source ); }
};
auto operator+( const std::string& LHS, const std::string_view& RHS ) -> std::string //
{
return LHS + std::string{ RHS };
}
auto operator+( const std::string_view& LHS, const auto& RHS ) -> std::string //
{
return std::string{ LHS } + RHS;
}
namespace TestSuite
{
auto TestBatchReplace()
{
return BatchReplace( { "${item1}", " item 1" }, //
{ "${a b c}", "[ a b c ]" }, //
{ "${k}", "1234" }, //
{ "${unused}", "1231123" }, { "${last}", "!~LAST~!" }, )
.In( "this is ${item1}, not ${item 1}; lets see ${a b c}; its ${item1} again; ${k} can be replaced. and this is the ${last}" );
}
} // namespace TestSuite
} // namespace StringAPI
using StringAPI::operator+;
#endif