-
Notifications
You must be signed in to change notification settings - Fork 1
/
Builder.h
1090 lines (888 loc) · 31.3 KB
/
Builder.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ///////////////////////////////////////////////////////////////// //
// *SRL-C++ 11 Library
// *Copyright(c) 2019 Mbadiwe Nnaemeka Ronald
// *Github Repository <https://github.com/ron4fun>
// *Distributed under the MIT software license, see the accompanying file LICENSE
// *or visit http ://www.opensource.org/licenses/mit-license.php.
// *Acknowledgements:
// ** //
// *Thanks to Simple Regex (https://simple-regex.com/) for his creative
// *development of this library in Javascript
// ////////////////////////////////////////////////////// ///////////////
#ifndef BUILDER_H
#define BUILDER_H
#pragma warning(disable:4996) // _CRT_SECURE_NO_WARNING
#include <map>
#include <stdexcept>
#include <regex>
#include <iostream>
#include <functional>
#include "Exceptions\NotImplementedException.h"
#include "Exceptions\BuilderException.h"
#include "Exceptions\ImplementationException.h"
#include "Exceptions\SyntaxException.h"
#include "Utils.h"
using namespace std;
using namespace SRLExceptions;
namespace SRLBuilder
{
const string NON_LITERAL_CHARACTERS = "[]\\^$.|?*+-(){}"; // "[\\^$.|?*+- ()/";
const int METHOD_TYPE_BEGIN = 0b00001;
const int METHOD_TYPE_CHARACTER = 0b00010;
const int METHOD_TYPE_GROUP = 0b00100;
const int METHOD_TYPE_QUANTIFIER = 0b01000;
const int METHOD_TYPE_ANCHOR = 0b10000;
const int METHOD_TYPE_UNKNOWN = 0b11111;
const int METHOD_TYPES_ALLOWED_FOR_CHARACTERS = METHOD_TYPE_BEGIN | METHOD_TYPE_ANCHOR | METHOD_TYPE_GROUP | METHOD_TYPE_QUANTIFIER | METHOD_TYPE_CHARACTER;
const map<string, mapperStruct> simpleMapper = {
{"startsWith", mapperStruct("^", METHOD_TYPE_ANCHOR, METHOD_TYPE_BEGIN)},
{ "mustEnd", mapperStruct("$", METHOD_TYPE_ANCHOR, METHOD_TYPE_CHARACTER | METHOD_TYPE_QUANTIFIER | METHOD_TYPE_GROUP) },
{ "onceOrMore", mapperStruct("+", METHOD_TYPE_QUANTIFIER, METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP) },
{ "neverOrMore", mapperStruct("*", METHOD_TYPE_QUANTIFIER, METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP) },
{ "any", mapperStruct(".", METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS) },
{ "backslash", mapperStruct(R"(\\)", METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS) },
{ "tab", mapperStruct(R"(\t)", METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS) },
{ "verticalTab", mapperStruct(R"(\v)", METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS) },
{ "newLine", mapperStruct(R"(\n)", METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS) },
{ "carriageReturn", mapperStruct(R"(\r)", METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS) },
{ "whitespace", mapperStruct(R"(\s)", METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS) },
{ "noWhitespace", mapperStruct(R"(\S)", METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS) },
{ "anyCharacter", mapperStruct(R"(\w)", METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS) },
{ "noCharacter", mapperStruct(R"(\W)", METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS) },
{ "word", mapperStruct(R"(\b)", METHOD_TYPE_CHARACTER, METHOD_TYPE_BEGIN) },
{ "nonWord", mapperStruct(R"(\B)", METHOD_TYPE_CHARACTER, METHOD_TYPE_BEGIN) },
}; // end
class Builder
{
//==================================================================
// Friends of class
//==================================================================
//
friend ostream &operator<<(ostream &output, Builder &value)
{
output << value.getRawRegex();
return output; // enables cout << a << b << c;
} // end function &operator<<
public:
Builder(){}
~Builder () {
if (_result != nullptr) {
delete _result;
_result = nullptr;
} // end if
} // end destructor
Builder(Builder &value)
{
_regEx = value._regEx;
_modifiers = value._modifiers;
_lastMethodType = value._lastMethodType;
_group = value._group;
_implodeString = value._implodeString;
_captureNames = value._captureNames;
_clearResult();
} // end cctor
//==================================================================
// to string operator
//==================================================================
//
explicit operator string() const
{
return getRawRegex();
} // end function operator string()
//
//==================================================================
// assignment operator
//==================================================================
//
const Builder &operator=(Builder &value)
{
if (*this != value)
{
_regEx = value._regEx;
_modifiers = value._modifiers;
_lastMethodType = value._lastMethodType;
_group = value._group;
_implodeString = value._implodeString;
_captureNames = value._captureNames;
_clearResult();
} // end if
return *this;
} // end function assignment operator
//==================================================================
// operator==
//==================================================================
//
bool operator==(Builder &value) const
{
return _regEx == value._regEx && _modifiers == value._modifiers &&
_lastMethodType == value._lastMethodType && _group == value._group &&
_implodeString == value._implodeString;
} // end function operator==
//==================================================================
// operator!=
//==================================================================
//
bool operator!=(Builder &value) const
{
return !(*this == value);
} // end function operator!=
/**********************************************************/
/* CHARACTERS */
/**********************************************************/
/**
* Add raw Regular Expression to current expression.
*
* @param {string} regularExpression
* @throws {BuilderException}
* @return {Builder}
*/
Builder& raw(const string ®ularExpression) {
_lastMethodType = METHOD_TYPE_UNKNOWN;
add(regularExpression);
if (!isValid()) {
_revertLast();
throw BuilderException("Adding raw would invalidate this regular expression. Reverted.");
} // end if
return *this;
} // end function raw;
/**
* Literally match one of these characters.
*
* @param {string} chars
* @return {Builder}
*/
Builder& oneOf(const string &chars) {
string temp;
_validateAndAddMethodType(METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
for (register size_t i = 0;i < chars.size(); i++) {
temp += escape(chars[i]);
} //end if
return add(Utils::format<char *>("[%s]", 1, temp.c_str()));
} // end function oneOf
/**
* Literally match a character that is not one of these characters.
*
* @param {string} chars
* @return {Builder}
*/
Builder& noneOf(const string &chars) {
string temp;
_validateAndAddMethodType(METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
for (register size_t i = 0; i < chars.size(); i++) {
temp += escape(chars[i]);
} //end if
return add(Utils::format<char *>("[^%s]", 1, temp.c_str()));
} // end function noneOf
/**
* Literally match all of these characters in that order.
*
* @param {string} chars One or more characters
* @return {Builder}
*/
Builder& literally(const string &chars) {
string temp;
_validateAndAddMethodType(METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
for (register size_t i = 0;i < chars.size(); i++) {
temp += escape(chars[i]);
} //end if
return add(Utils::format<char *>("(?:%s)", 1, temp.c_str())); // "(? : %s)"
} // end function literally
/**
* Match any digit (in given span). Default will be a digit between 0 and 9.
*
* @param {number} min
* @param {number} max
* @return {Builder}
*/
Builder& digit(const int min = 0, const int max = 9) {
_validateAndAddMethodType(METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return add(Utils::format<int>("[%d-%d]", 2, min, max));
} // end function digit
/**
* Match any number (in given span). Default will be a number between 0 and 9.
*
* @param {number} min
* @param {number} max
* @return {Builder}
*/
Builder& number(const int min = 0, const int max = 9) { return digit(min, max); } // end function number
/**
* Match any non-digit character (in given span). Default will be any character not between 0 and 9.
*
* @return {Builder}
*/
Builder& noDigit() {
_validateAndAddMethodType(METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return add("[^0-9]");
} // end function noDigit
/**
* Match any uppercase letter (between A to Z).
*
* @param {char} min
* @param {char} max
* @return {Builder}
*/
Builder& uppercaseLetter(const char min = 'A', const char max = 'Z') {
_validateAndAddMethodType(METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return add(Utils::format<char>("[%c-%c]", 2, min, max));
} // end function uppercaseLetter
/**
* Match any lowercase letter (between a to z).
* @param {char} min
* @param {char} max
* @return {Builder}
*/
Builder& letter(const char min = 'a', const char max = 'z') {
_validateAndAddMethodType(METHOD_TYPE_CHARACTER, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return add(Utils::format<char>("[%c-%c]", 2, min, max));
} // end function letter
/**********************************************************/
/* GROUPS */
/**********************************************************/
/**
* Match any of these conditions.
*
* @param {Closure|Builder|string} conditions Anonymous function with its Builder as first parameter.
* @return {Builder}
*/
Builder& anyOf(const string &conditions) {
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(?:%s)", "|"), conditions);
} // end function anyOf
Builder& anyOf(Builder &conditions) {
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(?:%s)", "|"), conditions);
} // end function anyOf
Builder& anyOf(function<void (Builder&)> func)
{
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(?:%s)", "|"), func);
} // end function anyOf
/**
* Match any of these conditions.
*
* @param {Closure|Builder|string} conditions Anonymous function with its Builder as first parameter.
* @return {Builder}
*/
Builder& eitherOf(const string &conditions) {return anyOf(conditions);} // end function eitherOf
Builder& eitherOf(Builder &conditions) { return anyOf(conditions); } // end function eitherOf
Builder& eitherOf(function<void (Builder&)> func) { return anyOf(func);} // end function eitherOf
/**
* Match all of these conditions, but in a non capture group.
*
* @param {Closure|Builder|string} conditions Anonymous function with its Builder as a first parameter.
* @return {Builder}
*/
Builder& group(const string &conditions) {
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(?:%s)"), conditions);
} // end function group
Builder& group(Builder &conditions) {
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(?:%s)"), conditions);
} // end function group
Builder& group(function<void (Builder&)> func)
{
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(?:%s)"), func);
} // end function group
/**
* Match all of these conditions, Basically reverts back to the default mode, if coming from anyOf, etc.
*
* @param {Closure|Builder|string} conditions
* @return {Builder}
*/
Builder& and(const string &conditions) {
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder(), conditions);
} // end function and
Builder& and(Builder &conditions) {
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder(), conditions);
} // end function and
Builder& and(function<void (Builder&)> func)
{
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder(), func);
} // end function and
/**
* Positive lookahead. Match the previous condition only if followed by given conditions.
*
* @param {Closure|Builder|string} condition Anonymous function with its Builder as a first parameter.
* @return {Builder}
*/
Builder& ifFollowedBy(const string &conditions) {
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(?=%s)"), conditions);
} // end function ifFollowedBy
Builder& ifFollowedBy(Builder &conditions) {
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(?=%s)"), conditions);
} // end function ifFollowedBy
Builder& ifFollowedBy(function<void (Builder&)> func)
{
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(?=%s)"), func);
} // end function ifFollowedBy
/**
* Negative lookahead. Match the previous condition only if NOT followed by given conditions.
*
* @param {Closure|Builder|string} condition Anonymous function with its Builder as a first parameter.
* @return {Builder}
*/
Builder& ifNotFollowedBy(const string &conditions) {
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(?!%s)"), conditions);
} // end function ifNotFollowedBy
Builder& ifNotFollowedBy(Builder &conditions) {
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(?!%s)"), conditions);
} // end function ifNotFollowedBy
Builder& ifNotFollowedBy(function<void (Builder&)> func)
{
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(?!%s)"), func);
} // end function ifNotFollowedBy
/**
* Create capture group of given conditions.
*
* @param {Closure|Builder|string} condition Anonymous function with its Builder as a first parameter.
* @param {String} name
* @return {Builder}
*/
Builder& capture(const string &conditions, const string &name = "") {
if (!name.empty()) _captureNames.push_back(name);
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(%s)"), conditions);
} // end function capture
Builder& capture(Builder &conditions, const string &name = "") {
if (!name.empty()) _captureNames.push_back(name);
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(%s)"), conditions);
} // end function capture
Builder& capture(function<void(Builder&)> func, const string &name = "") {
if (!name.empty()) _captureNames.push_back(name);
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder()._extends("(%s)"), func);
} // end function capture
/**********************************************************/
/* QUANTIFIERS */
/**********************************************************/
/**
* Make the last or given condition optional.
*
* @param {null|Closure|Builder|string} conditions Anonymous function with its Builder as a first parameter.
* @return {Builder}
*/
Builder& optional(const string &conditions="") {
_validateAndAddMethodType(METHOD_TYPE_QUANTIFIER, METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP);
if (conditions.empty()) {
return add("?");
} // end if
return _addClosure(Builder()._extends("(?:%s)?"), conditions);
} // end function optional
Builder& optional(Builder &conditions) {
_validateAndAddMethodType(METHOD_TYPE_QUANTIFIER, METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP);
return _addClosure(Builder()._extends("(?:%s)?"), conditions);
} // end function optional
Builder& optional(function<void (Builder&)> func)
{
_validateAndAddMethodType(METHOD_TYPE_QUANTIFIER, METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP);
return _addClosure(Builder()._extends("(?:%s)?"), func);
} // end function optional
/**
* Previous match must occur so often.
*
* @param {number} min
* @param {number} max
* @return {Builder}
*/
Builder& between(const int min, const int max) {
_validateAndAddMethodType(METHOD_TYPE_QUANTIFIER, METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP);
return add(Utils::format<int>("{%d,%d}", 2, min, max));
} // end function between
/**
* Previous match must occur at least this often.
*
* @param {number} min
* @return {Builder}
*/
Builder& atLeast(const int min) {
_validateAndAddMethodType(METHOD_TYPE_QUANTIFIER, METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP);
return add(Utils::format<int>("{%d,}", 1, min));
} // end function atLeast
/**
* Previous match must occur exactly once.
*
* @return {Builder}
*/
Builder& once() {
return exactly(1);
} // end function once
/**
* Previous match must occur exactly twice.
*
* @return {Builder}
*/
Builder& twice() {
return exactly(2);
} // end function twice
/**
* Previous match must occur exactly this often.
*
* @param {number} count
* @return {Builder}
*/
Builder& exactly(const int count) {
_validateAndAddMethodType(METHOD_TYPE_QUANTIFIER, METHOD_TYPE_CHARACTER | METHOD_TYPE_GROUP);
return add(Utils::format<int>("{%d}", 1, count));
} // end function exactly
/**
* Match less chars instead of more (lazy).
*
* @return {Builder}
* @throws {ImplementationException}
*/
Builder& lazy() {
const string chars = "+*}?";
const string raw = getRawRegex();
const string last = Utils::substr(raw, -1);
const int lastMethodType = _lastMethodType;
_lastMethodType = METHOD_TYPE_QUANTIFIER;
if (!Utils::contains(chars, last))
{
if (last == ")" && Utils::contains(chars, Utils::substr(raw, -2, 1)))
{
const string target = lastMethodType == METHOD_TYPE_GROUP ? Utils::slice(_revertLast(), 0, -1) + "?)" : "?";
return add(target);
} // end if
throw ImplementationException("Cannot apply laziness at this point. Only applicable after quantifier.");
} // end if
return add("?");
} // end function lazy
/**
* Match up to the given condition.
*
* @param {Closure|Builder|string} toCondition
* @return {Builder}
*/
Builder& until(const string &toCondition) {
lazy();
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder(), toCondition);
} // end function until
Builder& until(Builder &toCondition) {
lazy();
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder(), toCondition);
} // end function until
Builder& until(function<void (Builder&)> func)
{
lazy();
_validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS);
return _addClosure(Builder(), func);
} // end function until
/**********************************************************/
/* MODIFIER MAPPER */
/**********************************************************/
/**
* Add a specific unique modifier. This will ignore all modifiers already set.
*
* @param {int} modifier
* @return {Builder}
*/
Builder& addUniqueModifier(const regex_constants::syntax_option_type modifier) {
_clearResult();
if (!Utils::contains(_modifiers, modifier)) {
_modifiers.push_back(modifier);
} // end if
return *this;
} // end function addUniqueModifier
Builder& ECMAScript() {
return addUniqueModifier(regex_constants::ECMAScript);
} // end function ECMAScript
Builder& caseInsensitive() {
return addUniqueModifier(regex_constants::icase);
} // end function caseInsensitive
/**********************************************************/
/* SIMPLE MAPPER */
/**********************************************************/
Builder& startsWith() {
return _addFromMapper("startsWith");
} // end function startsWith
Builder& beginWith() { return startsWith(); } // end function beginWith
Builder& mustEnd() {
return _addFromMapper("mustEnd");
} // end function mustEnd
Builder& onceOrMore() {
return _addFromMapper("onceOrMore");
} // end function onceOrMore
Builder& neverOrMore() {
return _addFromMapper("neverOrMore");
} // end function neverOrMore
Builder& any() {
return _addFromMapper("any");
} // end function nonWord
Builder& backslash() {
return _addFromMapper("backslash");
} // end function backslash
Builder& tab() {
return _addFromMapper("tab");
} // end function tab
Builder& verticalTab() {
return _addFromMapper("verticalTab");
} // end function verticalTab
Builder& newLine() {
return _addFromMapper("newLine");
} // end function newLine
Builder& whitespace() {
return _addFromMapper("whitespace");
} // end function whitespace
Builder& noWhitespace() {
return _addFromMapper("noWhitespace");
} // end function noWhitespace
Builder& anyCharacter() {
return _addFromMapper("anyCharacter");
} // end function anyCharacter
Builder& noCharacter() {
return _addFromMapper("noCharacter");
} // end function noCharacter
Builder& word() {
return _addFromMapper("word");
} // end function word
Builder& nonWord() {
return _addFromMapper("nonWord");
} // end function nonWord
/**********************************************************/
/* INTERNAL METHODS */
/**********************************************************/
/**
* Escape specific character.
*
* @param {char} character
* @return {string}
*/
string escape(const char character) {
string temp;
if (Utils::contains(NON_LITERAL_CHARACTERS, character)) temp += "\\";
temp += character;
return temp;
} // end function escape
/**
* Get the raw regular expression string.
*
* @return string
*/
string getRawRegex() const {
string temp = Utils::join(_regEx, _implodeString.c_str());
return Utils::replace(_group, "%s", temp);
} // end function getRawRegex
/**
* Get the union of all set modifiers.
*
* @return {int}
*/
regex_constants::syntax_option_type getModifiers() {
regex_constants::syntax_option_type value = _modifiers[0];
for (register size_t i = 1; i < _modifiers.size(); i++) value |= _modifiers[i];
return value;
} // end function getModifiers
/**
* Remove specific flag.
*
* @param {int} flag
* @return {Builder}
*/
Builder& removeModifier(const regex_constants::syntax_option_type flag) {
_modifiers = Utils::remove(_modifiers, flag);
_clearResult();
return *this;
} // end function removeModifier
/**
* Add condition to the expression query.
*
* @param {string} condition
* @return {Builder}
*/
Builder& add(const string &condition) {
_clearResult(); // Reset result to make up a new one.
_regEx.push_back(condition);
return *this;
} // end function add
/**
* Build and return the resulting RegExp object. This will apply all the modifiers.
*
* @return {RegExp}
* @throws {SyntaxException}
*/
regex get() {
if (isValid()) {
return *_result;
} // end if
throw SyntaxException("Generated expression seems to be invalid.");
} // end function get
/**
* Validate regular expression.
*
* @return {boolean}
*/
bool isValid() {
if (_result != nullptr) {
return true;
} // end if
else {
try {
if (_modifiers.empty())
_result = new regex(getRawRegex().c_str());
else
_result = new regex(getRawRegex().c_str(), getModifiers());
return true;
} // end try
catch (exception &) {
return false;
} // end catch
} // end else
} // end function isValid
/**
* Clone a new builder object.
*
* @return {Builder}
*/
Builder clone() const {
Builder clone = Builder();
// Copy deeply
clone._regEx = vector<string>(_regEx);
clone._modifiers = _modifiers;
clone._lastMethodType = _lastMethodType;
clone._group = _group;
return clone;
} // end function clone
/**
* Check if the target string contains any substring that matches the regex pattern.
*
* @param {string} target
* @return {boolean}
*/
bool isMatchContained(const string &target) {
return regex_search(target, get());
} // end function isMatchContained
/**
* Check if the target string matches the pattern completely.
*
* @param {string} target
* @return {boolean}
*/
bool isExactMatch(const string &target) {
return regex_match(target, get());
} // end function isExactMatch
/**
* Check if the target string matches the pattern completely or partly.
*
* @param {string} target
* @return {boolean}
*/
bool isMatching(const string &target) {
return isMatchContained(target) || isExactMatch(target);
} // end function isExactMatch
/**
* Search and replace patterns within a string of text.
*
* @param {string} target
* @param {string} replacement_pattern_str
* @return {string}
*/
string replace(const string &target, const string &replacement_pattern_str) {
regex r = get();
return regex_replace(target, r, replacement_pattern_str);
} // end function replace
/**
* Get the first substring contained in the target string that matches the regex pattern.
*
* @param {string} target
* @return {vector<string>}
*/
vector<string> getMatch(const string &target) {
smatch sm;
vector<string> result;
regex r = get();
bool b = regex_search(target, sm, r);
if (b == false) return vector<string>();
for (register size_t i = 0; i < sm.size(); i++) {
result.push_back(sm[i]);
} // end for
return result;
} // end function getMatch
/**
* Get all matches.
* @param {string} target
* @return {vector<Dictionary>}
*/
vector<Dictionary> getMatches(const string &target) {
vector<Dictionary> result;
vector<string> arr;
smatch sm;
regex r = get();
size_t last_pos = 0;
string::const_iterator searchStart( target.cbegin() );
while ( regex_search(searchStart, target.cend(), sm, r) )
{
for (register size_t i = 0; i < sm.size(); i++) {
arr.push_back(sm[i]);
} // end for
result.push_back(_mapCaptureIndexToName(arr));
arr.clear(); // empty array
searchStart = sm.suffix().first;
last_pos = (target.size() - sm.suffix().length());
} // end while
return result;
} // end function getMatches
protected:
/**
* Clears the regex variable if any.
*/
void _clearResult() {
if (_result != nullptr) {
delete _result;
_result = nullptr;
} // end if
} // end function _clearResult
/**
* Map capture index to name.
* Then to resolve to return: ["0": "aa ", "1": "aa", [captureName]: "aa" ]
*
* @param {vector<string>} result
* @return {Dictionary}
*/
Dictionary _mapCaptureIndexToName(const vector<string> &arr) const
{
Dictionary result;
vector<string> _arr = Utils::slice(arr, 1);
vector<string> names = _captureNames;
size_t count = 0;
for (register size_t i = 0; i < _arr.size(); ++i)
{
string current = _arr[i];
if (i < names.size() && !(names[i].empty())) {
result[names[i]] = current;
} // end if
else {
char buf[50];
result[itoa((int)count, buf, 10)] = current;
count += 1;
} // end else
} // end for
return result;
} // end function _mapCaptureIndexToName
/**
* Validate method call. This will throw an exception if the called method makes no sense at this point.
* Will add the current type as the last method type.
*
* @param {number} type
* @param {number} allowed
* @param {string} methodName
*/
void _validateAndAddMethodType(const int type, const int allowed, const string &methodName="") {
if (allowed & _lastMethodType) {
_lastMethodType = type;
return;
} // end if
string message;
switch (_lastMethodType)
{
case METHOD_TYPE_BEGIN:
message = "at the beginning";
break;
case METHOD_TYPE_CHARACTER:
message = "after a literal character";
break;
case METHOD_TYPE_GROUP:
message = "after a group";
break;
case METHOD_TYPE_QUANTIFIER:
message = "after a quantifier";
break;
case METHOD_TYPE_ANCHOR:
message = "after an anchor";
break;
default:
message = "here";
break;
}
throw ImplementationException(string("Method ") + methodName + string(" is not allowed ") + message);
}
/**
* Add the value form simple mapper to the regular expression.
*