3
3
import static java .lang .String .valueOf ;
4
4
5
5
import java .util .ArrayList ;
6
+ import java .util .HashMap ;
6
7
import java .util .List ;
8
+ import java .util .Map ;
7
9
import java .util .regex .Matcher ;
8
10
import java .util .regex .Pattern ;
9
11
@@ -18,6 +20,19 @@ public static class Builder {
18
20
private StringBuilder suffixes = new StringBuilder ();
19
21
private int modifiers = Pattern .MULTILINE ;
20
22
23
+ private static final Map <Character , Integer > SYMBOL_MAP ;
24
+
25
+ static {
26
+ SYMBOL_MAP = new HashMap <>();
27
+ SYMBOL_MAP .put ('d' , Pattern .UNIX_LINES );
28
+ SYMBOL_MAP .put ('i' , Pattern .CASE_INSENSITIVE );
29
+ SYMBOL_MAP .put ('x' , Pattern .COMMENTS );
30
+ SYMBOL_MAP .put ('m' , Pattern .MULTILINE );
31
+ SYMBOL_MAP .put ('s' , Pattern .DOTALL );
32
+ SYMBOL_MAP .put ('u' , Pattern .UNICODE_CASE );
33
+ SYMBOL_MAP .put ('U' , Pattern .UNICODE_CHARACTER_CLASS );
34
+ }
35
+
21
36
/**
22
37
* Package private. Use {@link #regex()} to build a new one
23
38
*
@@ -172,7 +187,7 @@ public Builder find(final String value) {
172
187
public Builder maybe (final String pValue ) {
173
188
return this .then (pValue ).add ("?" );
174
189
}
175
-
190
+
176
191
/**
177
192
* Add a regex to the expression that might appear once (or not)
178
193
* Example:
@@ -367,60 +382,16 @@ public Builder range(final String... pArgs) {
367
382
}
368
383
369
384
public Builder addModifier (final char pModifier ) {
370
- switch (pModifier ) {
371
- case 'd' :
372
- modifiers |= Pattern .UNIX_LINES ;
373
- break ;
374
- case 'i' :
375
- modifiers |= Pattern .CASE_INSENSITIVE ;
376
- break ;
377
- case 'x' :
378
- modifiers |= Pattern .COMMENTS ;
379
- break ;
380
- case 'm' :
381
- modifiers |= Pattern .MULTILINE ;
382
- break ;
383
- case 's' :
384
- modifiers |= Pattern .DOTALL ;
385
- break ;
386
- case 'u' :
387
- modifiers |= Pattern .UNICODE_CASE ;
388
- break ;
389
- case 'U' :
390
- modifiers |= Pattern .UNICODE_CHARACTER_CLASS ;
391
- break ;
392
- default :
393
- break ;
385
+ if (SYMBOL_MAP .containsKey (pModifier )) {
386
+ modifiers |= SYMBOL_MAP .get (pModifier );
394
387
}
395
388
396
389
return this ;
397
390
}
398
391
399
392
public Builder removeModifier (final char pModifier ) {
400
- switch (pModifier ) {
401
- case 'd' :
402
- modifiers &= ~Pattern .UNIX_LINES ;
403
- break ;
404
- case 'i' :
405
- modifiers &= ~Pattern .CASE_INSENSITIVE ;
406
- break ;
407
- case 'x' :
408
- modifiers &= ~Pattern .COMMENTS ;
409
- break ;
410
- case 'm' :
411
- modifiers &= ~Pattern .MULTILINE ;
412
- break ;
413
- case 's' :
414
- modifiers &= ~Pattern .DOTALL ;
415
- break ;
416
- case 'u' :
417
- modifiers &= ~Pattern .UNICODE_CASE ;
418
- break ;
419
- case 'U' :
420
- modifiers &= ~Pattern .UNICODE_CHARACTER_CLASS ;
421
- break ;
422
- default :
423
- break ;
393
+ if (SYMBOL_MAP .containsKey (pModifier )) {
394
+ modifiers &= ~SYMBOL_MAP .get (pModifier );
424
395
}
425
396
426
397
return this ;
@@ -578,7 +549,7 @@ public Builder or(final String pValue) {
578
549
}
579
550
return this ;
580
551
}
581
-
552
+
582
553
/**
583
554
* Adds an alternative expression to be matched
584
555
* based on an array of values
@@ -750,7 +721,7 @@ public String getText(final String toTest, final int group) {
750
721
751
722
/**
752
723
* Extract exact group from string and add it to list
753
- *
724
+ *
754
725
* Example:
755
726
* String text = "SampleHelloWorldString";
756
727
* VerbalExpression regex = regex().capt().oneOf("Hello", "World").endCapt().maybe("String").build();
0 commit comments