forked from eclipse-jdt/eclipse.jdt.core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharOperation.java
4400 lines (4227 loc) · 120 KB
/
CharOperation.java
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
/*******************************************************************************
* Copyright (c) 2000, 2020 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Luiz-Otavio Zorzella <zorzella at gmail dot com> - Improve CamelCase algorithm
* Gábor Kövesdán - Contribution for Bug 350000 - [content assist] Include non-prefix matches in auto-complete suggestions
* Stefan Xenos <sxenos@gmail.com> (Google) - Bug 501283 - Lots of hash collisions during indexing
*******************************************************************************/
package org.eclipse.jdt.core.compiler;
import java.util.Arrays;
import java.util.List;
import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
/**
* This class is a collection of helper methods to manipulate char arrays.
*
* @since 2.1
* @noinstantiate This class is not intended to be instantiated by clients.
*/
public final class CharOperation {
/**
* Constant for an empty char array
*/
public static final char[] NO_CHAR = new char[0];
/**
* Constant for an empty char array with two dimensions.
*/
public static final char[][] NO_CHAR_CHAR = new char[0][];
/**
* Constant for an empty String array.
* @since 3.1
*/
public static final String[] NO_STRINGS = new String[0];
/**
* Constant for all Prefix
* @since 3.14
*/
public static final char[] ALL_PREFIX = new char[] {'*'};
/**
* Constant for comma
* @since 3.14
*/
public static final char[] COMMA_SEPARATOR = new char[] {','};
private static final int[] EMPTY_REGIONS = new int[0];
/**
* Answers a new array with appending the suffix character at the end of the array.
* <br>
* <br>
* For example:<br>
* <ol>
* <li><pre>
* array = { 'a', 'b' }
* suffix = 'c'
* => result = { 'a', 'b' , 'c' }
* </pre>
* </li>
* <li><pre>
* array = null
* suffix = 'c'
* => result = { 'c' }
* </pre></li>
* </ol>
*
* @param array the array that is concatenated with the suffix character
* @param suffix the suffix character
* @return the new array
*/
public static final char[] append(char[] array, char suffix) {
if (array == null)
return new char[] { suffix };
int length = array.length;
System.arraycopy(array, 0, array = new char[length + 1], 0, length);
array[length] = suffix;
return array;
}
/**
* Answers a new array with appending the sub-array at the end of the array.
* <br>
* <br>
* For example:<br>
* <ol>
* <li><pre>
* array = { 'a', 'b' }
* suffix = { 'c', 'd' }
* => result = { 'a', 'b' , 'c' , d' }
* </pre>
* </li>
* <li><pre>
* array = null
* suffix = { 'c' }
* => result = { 'c' }
* </pre></li>
* </ol>
*
* @param target the array that is concatenated with the suffix array.
* @param suffix the array that will be concatenated to the target
* @return the new array
* @throws NullPointerException if the target array is null
* @since 3.11
*/
public static final char[] append(char[] target, char[] suffix) {
if(suffix == null || suffix.length == 0)
return target;
int targetLength = target.length;
int subLength = suffix.length;
int newTargetLength = targetLength + subLength;
if (newTargetLength > targetLength) {
System.arraycopy(target, 0, target = new char[newTargetLength], 0, targetLength);
}
System.arraycopy(suffix, 0, target, targetLength, subLength);
return target;
}
/**
* Append the given sub-array to the target array starting at the given index in the target array.
* The start of the sub-array is inclusive, the end is exclusive.
* Answers a new target array if it needs to grow, otherwise answers the same target array.
* <br>
* For example:<br>
* <ol>
* <li><pre>
* target = { 'a', 'b', '0' }
* index = 2
* array = { 'c', 'd' }
* start = 0
* end = 1
* => result = { 'a', 'b' , 'c' }
* </pre>
* </li>
* <li><pre>
* target = { 'a', 'b' }
* index = 2
* array = { 'c', 'd' }
* start = 0
* end = 1
* => result = { 'a', 'b' , 'c', '0', '0' , '0' } (new array)
* </pre></li>
* <li><pre>
* target = { 'a', 'b', 'c' }
* index = 1
* array = { 'c', 'd', 'e', 'f' }
* start = 1
* end = 4
* => result = { 'a', 'd' , 'e', 'f', '0', '0', '0', '0' } (new array)
* </pre></li>
* </ol>
*
* @param target the given target
* @param index the given index
* @param array the given array
* @param start the given start index
* @param end the given end index
*
* @return the new array
* @throws NullPointerException if the target array is null
*/
public static final char[] append(char[] target, int index, char[] array, int start, int end) {
int targetLength = target.length;
int subLength = end-start;
int newTargetLength = subLength+index;
if (newTargetLength > targetLength) {
System.arraycopy(target, 0, target = new char[newTargetLength*2], 0, index);
}
System.arraycopy(array, start, target, index, subLength);
return target;
}
/**
* Answers a new array with prepending the prefix character at the start of the array.
* <br>
* <br>
* For example:<br>
* <ol>
* <li><pre>
* prefix = 'c'
* array = { 'a', 'b' }
* => result = { 'c' , 'a', 'b' }
* </pre>
* </li>
* <li><pre>
* prefix = 'c'
* array = null
* => result = { 'c' }
* </pre></li>
* </ol>
*
* @param array the array that is concatenated with the prefix character
* @param prefix the prefix character
* @return the new array
* @since 3.14
*/
public static final char[] prepend(char prefix, char[] array) {
if (array == null)
return new char[] { prefix };
int length = array.length;
System.arraycopy(array, 0, array = new char[length + 1], 1, length);
array[0] = prefix;
return array;
}
/**
* Answers the concatenation of the two arrays. It answers null if the two arrays are null.
* If the first array is null, then the second array is returned.
* If the second array is null, then the first array is returned.
* <br>
* <br>
* For example:
* <ol>
* <li><pre>
* first = null
* second = null
* => result = null
* </pre>
* </li>
* <li><pre>
* first = { { ' a' } }
* second = null
* => result = { { ' a' } }
* </pre>
* </li>
* <li><pre>
* first = null
* second = { { ' a' } }
* => result = { { ' a' } }
* </pre>
* </li>
* <li><pre>
* first = { { ' b' } }
* second = { { ' a' } }
* => result = { { ' b' }, { ' a' } }
* </pre>
* </li>
* </ol>
*
* @param first the first array to concatenate
* @param second the second array to concatenate
* @return the concatenation of the two arrays, or null if the two arrays are null.
*/
public static final char[][] arrayConcat(char[][] first, char[][] second) {
if (first == null)
return second;
if (second == null)
return first;
int length1 = first.length;
int length2 = second.length;
char[][] result = new char[length1 + length2][];
System.arraycopy(first, 0, result, 0, length1);
System.arraycopy(second, 0, result, length1, length2);
return result;
}
/**
* Answers true if the pattern matches the given name using CamelCase rules, or
* false otherwise. char[] CamelCase matching does NOT accept explicit wild-cards
* '*' and '?' and is inherently case sensitive.
* <p>
* CamelCase denotes the convention of writing compound names without spaces,
* and capitalizing every term. This function recognizes both upper and lower
* CamelCase, depending whether the leading character is capitalized or not.
* The leading part of an upper CamelCase pattern is assumed to contain a
* sequence of capitals which are appearing in the matching name; e.g. 'NPE' will
* match 'NullPointerException', but not 'NewPerfData'. A lower CamelCase pattern
* uses a lowercase first character. In Java, type names follow the upper
* CamelCase convention, whereas method or field names follow the lower
* CamelCase convention.
* <p>
* The pattern may contain lowercase characters, which will be matched in a case
* sensitive way. These characters must appear in sequence in the name.
* For instance, 'NPExcep' will match 'NullPointerException', but not
* 'NullPointerExCEPTION' or 'NuPoEx' will match 'NullPointerException', but not
* 'NoPointerException'.
* <p>
* Digit characters are treated in a special way. They can be used in the pattern
* but are not always considered as leading character. For instance, both
* 'UTF16DSS' and 'UTFDSS' patterns will match 'UTF16DocumentScannerSupport'.
* <p>
* Using this method allows matching names to have more parts than the specified
* pattern (see {@link #camelCaseMatch(char[], char[], boolean)}).<br>
* For instance, 'HM' , 'HaMa' and 'HMap' patterns will match 'HashMap',
* 'HatMapper' <b>and also</b> 'HashMapEntry'.
* <p>
* Examples:
* <ol>
* <li> pattern = "NPE".toCharArray()
* name = "NullPointerException".toCharArray()
* result => true</li>
* <li> pattern = "NPE".toCharArray()
* name = "NoPermissionException".toCharArray()
* result => true</li>
* <li> pattern = "NuPoEx".toCharArray()
* name = "NullPointerException".toCharArray()
* result => true</li>
* <li> pattern = "NuPoEx".toCharArray()
* name = "NoPermissionException".toCharArray()
* result => false</li>
* <li> pattern = "npe".toCharArray()
* name = "NullPointerException".toCharArray()
* result => false</li>
* <li> pattern = "IPL3".toCharArray()
* name = "IPerspectiveListener3".toCharArray()
* result => true</li>
* <li> pattern = "HM".toCharArray()
* name = "HashMapEntry".toCharArray()
* result => true</li>
* </ol>
*
* @param pattern the given pattern
* @param name the given name
* @return true if the pattern matches the given name, false otherwise
* @since 3.2
*/
public static final boolean camelCaseMatch(char[] pattern, char[] name) {
if (pattern == null)
return true; // null pattern is equivalent to '*'
if (name == null)
return false; // null name cannot match
return camelCaseMatch(pattern, 0, pattern.length, name, 0, name.length, false/*not the same count of parts*/);
}
/**
* Answers true if the pattern matches the given name using CamelCase rules, or
* false otherwise. char[] CamelCase matching does NOT accept explicit wild-cards
* '*' and '?' and is inherently case sensitive.
* <p>
* CamelCase denotes the convention of writing compound names without spaces,
* and capitalizing every term. This function recognizes both upper and lower
* CamelCase, depending whether the leading character is capitalized or not.
* The leading part of an upper CamelCase pattern is assumed to contain a
* sequence of capitals which are appearing in the matching name; e.g. 'NPE' will
* match 'NullPointerException', but not 'NewPerfData'. A lower CamelCase pattern
* uses a lowercase first character. In Java, type names follow the upper
* CamelCase convention, whereas method or field names follow the lower
* CamelCase convention.
* <p>
* The pattern may contain lowercase characters, which will be matched in a case
* sensitive way. These characters must appear in sequence in the name.
* For instance, 'NPExcep' will match 'NullPointerException', but not
* 'NullPointerExCEPTION' or 'NuPoEx' will match 'NullPointerException', but not
* 'NoPointerException'.
* <p>
* Digit characters are treated in a special way. They can be used in the pattern
* but are not always considered as leading character. For instance, both
* 'UTF16DSS' and 'UTFDSS' patterns will match 'UTF16DocumentScannerSupport'.
* <p>
* CamelCase can be restricted to match only the same count of parts. When this
* restriction is specified the given pattern and the given name must have <b>exactly</b>
* the same number of parts (i.e. the same number of uppercase characters).<br>
* For instance, 'HM' , 'HaMa' and 'HMap' patterns will match 'HashMap' and
* 'HatMapper' <b>but not</b> 'HashMapEntry'.
* <p>
* Examples:<ol>
* <li> pattern = "NPE".toCharArray()
* name = "NullPointerException".toCharArray()
* result => true</li>
* <li> pattern = "NPE".toCharArray()
* name = "NoPermissionException".toCharArray()
* result => true</li>
* <li> pattern = "NuPoEx".toCharArray()
* name = "NullPointerException".toCharArray()
* result => true</li>
* <li> pattern = "NuPoEx".toCharArray()
* name = "NoPermissionException".toCharArray()
* result => false</li>
* <li> pattern = "npe".toCharArray()
* name = "NullPointerException".toCharArray()
* result => false</li>
* <li> pattern = "IPL3".toCharArray()
* name = "IPerspectiveListener3".toCharArray()
* result => true</li>
* <li> pattern = "HM".toCharArray()
* name = "HashMapEntry".toCharArray()
* result => (samePartCount == false)</li>
* </ol>
*
* @param pattern the given pattern
* @param name the given name
* @param samePartCount flag telling whether the pattern and the name should
* have the same count of parts or not.<br>
* For example:
* <ul>
* <li>'HM' type string pattern will match 'HashMap' and 'HtmlMapper' types,
* but not 'HashMapEntry'</li>
* <li>'HMap' type string pattern will still match previous 'HashMap' and
* 'HtmlMapper' types, but not 'HighMagnitude'</li>
* </ul>
* @return true if the pattern matches the given name, false otherwise
* @since 3.4
*/
public static final boolean camelCaseMatch(char[] pattern, char[] name, boolean samePartCount) {
if (pattern == null)
return true; // null pattern is equivalent to '*'
if (name == null)
return false; // null name cannot match
return camelCaseMatch(pattern, 0, pattern.length, name, 0, name.length, samePartCount);
}
/**
* Answers true if a sub-pattern matches the sub-part of the given name using
* CamelCase rules, or false otherwise. char[] CamelCase matching does NOT
* accept explicit wild-cards '*' and '?' and is inherently case sensitive.
* Can match only subset of name/pattern, considering end positions as non-inclusive.
* The sub-pattern is defined by the patternStart and patternEnd positions.
* <p>
* CamelCase denotes the convention of writing compound names without spaces,
* and capitalizing every term. This function recognizes both upper and lower
* CamelCase, depending whether the leading character is capitalized or not.
* The leading part of an upper CamelCase pattern is assumed to contain a
* sequence of capitals which are appearing in the matching name; e.g. 'NPE' will
* match 'NullPointerException', but not 'NewPerfData'. A lower CamelCase pattern
* uses a lowercase first character. In Java, type names follow the upper
* CamelCase convention, whereas method or field names follow the lower
* CamelCase convention.
* <p>
* The pattern may contain lowercase characters, which will be matched in a case
* sensitive way. These characters must appear in sequence in the name.
* For instance, 'NPExcep' will match 'NullPointerException', but not
* 'NullPointerExCEPTION' or 'NuPoEx' will match 'NullPointerException', but not
* 'NoPointerException'.
* <p>
* Digit characters are treated in a special way. They can be used in the pattern
* but are not always considered as leading character. For instance, both
* 'UTF16DSS' and 'UTFDSS' patterns will match 'UTF16DocumentScannerSupport'.
* <p>
* Digit characters are treated in a special way. They can be used in the pattern
* but are not always considered as leading character. For instance, both
* 'UTF16DSS' and 'UTFDSS' patterns will match 'UTF16DocumentScannerSupport'.
* <p>
* Using this method allows matching names to have more parts than the specified
* pattern (see {@link #camelCaseMatch(char[], int, int, char[], int, int, boolean)}).<br>
* For instance, 'HM' , 'HaMa' and 'HMap' patterns will match 'HashMap',
* 'HatMapper' <b>and also</b> 'HashMapEntry'.
* <p>
* Examples:
* <ol>
* <li> pattern = "NPE".toCharArray()
* patternStart = 0
* patternEnd = 3
* name = "NullPointerException".toCharArray()
* nameStart = 0
* nameEnd = 20
* result => true</li>
* <li> pattern = "NPE".toCharArray()
* patternStart = 0
* patternEnd = 3
* name = "NoPermissionException".toCharArray()
* nameStart = 0
* nameEnd = 21
* result => true</li>
* <li> pattern = "NuPoEx".toCharArray()
* patternStart = 0
* patternEnd = 6
* name = "NullPointerException".toCharArray()
* nameStart = 0
* nameEnd = 20
* result => true</li>
* <li> pattern = "NuPoEx".toCharArray()
* patternStart = 0
* patternEnd = 6
* name = "NoPermissionException".toCharArray()
* nameStart = 0
* nameEnd = 21
* result => false</li>
* <li> pattern = "npe".toCharArray()
* patternStart = 0
* patternEnd = 3
* name = "NullPointerException".toCharArray()
* nameStart = 0
* nameEnd = 20
* result => false</li>
* <li> pattern = "IPL3".toCharArray()
* patternStart = 0
* patternEnd = 4
* name = "IPerspectiveListener3".toCharArray()
* nameStart = 0
* nameEnd = 21
* result => true</li>
* <li> pattern = "HM".toCharArray()
* patternStart = 0
* patternEnd = 2
* name = "HashMapEntry".toCharArray()
* nameStart = 0
* nameEnd = 12
* result => true</li>
* </ol>
*
* @param pattern the given pattern
* @param patternStart the start index of the pattern, inclusive
* @param patternEnd the end index of the pattern, exclusive
* @param name the given name
* @param nameStart the start index of the name, inclusive
* @param nameEnd the end index of the name, exclusive
* @return true if a sub-pattern matches the sub-part of the given name, false otherwise
* @since 3.2
*/
public static final boolean camelCaseMatch(char[] pattern, int patternStart, int patternEnd, char[] name, int nameStart, int nameEnd) {
return camelCaseMatch(pattern, patternStart, patternEnd, name, nameStart, nameEnd, false/*not the same count of parts*/);
}
/**
* Answers true if a sub-pattern matches the sub-part of the given name using
* CamelCase rules, or false otherwise. char[] CamelCase matching does NOT
* accept explicit wild-cards '*' and '?' and is inherently case sensitive.
* Can match only subset of name/pattern, considering end positions as
* non-inclusive. The sub-pattern is defined by the patternStart and patternEnd
* positions.
* <p>
* CamelCase denotes the convention of writing compound names without spaces,
* and capitalizing every term. This function recognizes both upper and lower
* CamelCase, depending whether the leading character is capitalized or not.
* The leading part of an upper CamelCase pattern is assumed to contain
* a sequence of capitals which are appearing in the matching name; e.g. 'NPE' will
* match 'NullPointerException', but not 'NewPerfData'. A lower CamelCase pattern
* uses a lowercase first character. In Java, type names follow the upper
* CamelCase convention, whereas method or field names follow the lower
* CamelCase convention.
* <p>
* The pattern may contain lowercase characters, which will be matched in a case
* sensitive way. These characters must appear in sequence in the name.
* For instance, 'NPExcep' will match 'NullPointerException', but not
* 'NullPointerExCEPTION' or 'NuPoEx' will match 'NullPointerException', but not
* 'NoPointerException'.
* <p>
* Digit characters are treated in a special way. They can be used in the pattern
* but are not always considered as leading character. For instance, both
* 'UTF16DSS' and 'UTFDSS' patterns will match 'UTF16DocumentScannerSupport'.
* <p>
* CamelCase can be restricted to match only the same count of parts. When this
* restriction is specified the given pattern and the given name must have <b>exactly</b>
* the same number of parts (i.e. the same number of uppercase characters).<br>
* For instance, 'HM' , 'HaMa' and 'HMap' patterns will match 'HashMap' and
* 'HatMapper' <b>but not</b> 'HashMapEntry'.
* <p>
* Examples:
* <ol>
* <li> pattern = "NPE".toCharArray()
* patternStart = 0
* patternEnd = 3
* name = "NullPointerException".toCharArray()
* nameStart = 0
* nameEnd = 20
* result => true</li>
* <li> pattern = "NPE".toCharArray()
* patternStart = 0
* patternEnd = 3
* name = "NoPermissionException".toCharArray()
* nameStart = 0
* nameEnd = 21
* result => true</li>
* <li> pattern = "NuPoEx".toCharArray()
* patternStart = 0
* patternEnd = 6
* name = "NullPointerException".toCharArray()
* nameStart = 0
* nameEnd = 20
* result => true</li>
* <li> pattern = "NuPoEx".toCharArray()
* patternStart = 0
* patternEnd = 6
* name = "NoPermissionException".toCharArray()
* nameStart = 0
* nameEnd = 21
* result => false</li>
* <li> pattern = "npe".toCharArray()
* patternStart = 0
* patternEnd = 3
* name = "NullPointerException".toCharArray()
* nameStart = 0
* nameEnd = 20
* result => false</li>
* <li> pattern = "IPL3".toCharArray()
* patternStart = 0
* patternEnd = 4
* name = "IPerspectiveListener3".toCharArray()
* nameStart = 0
* nameEnd = 21
* result => true</li>
* <li> pattern = "HM".toCharArray()
* patternStart = 0
* patternEnd = 2
* name = "HashMapEntry".toCharArray()
* nameStart = 0
* nameEnd = 12
* result => (samePartCount == false)</li>
* </ol>
*
* @param pattern the given pattern
* @param patternStart the start index of the pattern, inclusive
* @param patternEnd the end index of the pattern, exclusive
* @param name the given name
* @param nameStart the start index of the name, inclusive
* @param nameEnd the end index of the name, exclusive
* @param samePartCount flag telling whether the pattern and the name should
* have the same count of parts or not.<br>
* For example:
* <ul>
* <li>'HM' type string pattern will match 'HashMap' and 'HtmlMapper' types,
* but not 'HashMapEntry'</li>
* <li>'HMap' type string pattern will still match previous 'HashMap' and
* 'HtmlMapper' types, but not 'HighMagnitude'</li>
* </ul>
* @return true if a sub-pattern matches the sub-part of the given name, false otherwise
* @since 3.4
*/
public static final boolean camelCaseMatch(char[] pattern, int patternStart, int patternEnd, char[] name, int nameStart, int nameEnd, boolean samePartCount) {
/* !!!!!!!!!! WARNING !!!!!!!!!!
* The algorithm implemented in this method has been heavily used in
* StringOperation#getCamelCaseMatchingRegions(String, int, int, String, int, int, boolean)
* method.
*
* So, if any change needs to be applied in the current algorithm,
* do NOT forget to also apply the same change in the StringOperation method!
*/
if (name == null)
return false; // null name cannot match
if (pattern == null)
return true; // null pattern is equivalent to '*'
if (patternEnd < 0) patternEnd = pattern.length;
if (nameEnd < 0) nameEnd = name.length;
if (patternEnd <= patternStart) return nameEnd <= nameStart;
if (nameEnd <= nameStart) return false;
// check first pattern char
if (name[nameStart] != pattern[patternStart]) {
// first char must strictly match (upper/lower)
return false;
}
char patternChar, nameChar;
int iPattern = patternStart;
int iName = nameStart;
// Main loop is on pattern characters
while (true) {
iPattern++;
iName++;
if (iPattern == patternEnd) { // we have exhausted pattern...
// it's a match if the name can have additional parts (i.e. uppercase characters) or is also exhausted
if (!samePartCount || iName == nameEnd) return true;
// otherwise it's a match only if the name has no more uppercase characters
while (true) {
if (iName == nameEnd) {
// we have exhausted the name, so it's a match
return true;
}
nameChar = name[iName];
// test if the name character is uppercase
if (nameChar < ScannerHelper.MAX_OBVIOUS) {
if ((ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[nameChar] & ScannerHelper.C_UPPER_LETTER) != 0) {
return false;
}
}
else if (!Character.isJavaIdentifierPart(nameChar) || Character.isUpperCase(nameChar)) {
return false;
}
iName++;
}
}
if (iName == nameEnd){
// We have exhausted the name (and not the pattern), so it's not a match
return false;
}
// For as long as we're exactly matching, bring it on (even if it's a lower case character)
if ((patternChar = pattern[iPattern]) == name[iName]) {
continue;
}
// If characters are not equals, then it's not a match if patternChar is lowercase
if (patternChar < ScannerHelper.MAX_OBVIOUS) {
if ((ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[patternChar] & (ScannerHelper.C_UPPER_LETTER | ScannerHelper.C_DIGIT)) == 0) {
return false;
}
}
else if (Character.isJavaIdentifierPart(patternChar) && !Character.isUpperCase(patternChar) && !Character.isDigit(patternChar)) {
return false;
}
// patternChar is uppercase, so let's find the next uppercase in name
while (true) {
if (iName == nameEnd){
// We have exhausted name (and not pattern), so it's not a match
return false;
}
nameChar = name[iName];
if (nameChar < ScannerHelper.MAX_OBVIOUS) {
int charNature = ScannerHelper.OBVIOUS_IDENT_CHAR_NATURES[nameChar];
if ((charNature & (ScannerHelper.C_LOWER_LETTER | ScannerHelper.C_SPECIAL)) != 0) {
// nameChar is lowercase
iName++;
} else if ((charNature & ScannerHelper.C_DIGIT) != 0) {
// nameChar is digit => break if the digit is current pattern character otherwise consume it
if (patternChar == nameChar) break;
iName++;
// nameChar is uppercase...
} else if (patternChar != nameChar) {
//.. and it does not match patternChar, so it's not a match
return false;
} else {
//.. and it matched patternChar. Back to the big loop
break;
}
}
// Same tests for non-obvious characters
else if (Character.isJavaIdentifierPart(nameChar) && !Character.isUpperCase(nameChar)) {
iName++;
} else if (Character.isDigit(nameChar)) {
if (patternChar == nameChar) break;
iName++;
} else if (patternChar != nameChar) {
return false;
} else {
break;
}
}
// At this point, either name has been exhausted, or it is at an uppercase letter.
// Since pattern is also at an uppercase letter
}
}
/**
* Answers true if the characters of the pattern are contained in the
* name as a subword, in a case-insensitive way.
*
* @param pattern the given pattern
* @param name the given name
* @return true if the pattern matches the given name, false otherwise
* @since 3.21
*/
public static final boolean subWordMatch(char[] pattern, char[] name) {
if (name == null)
return false; // null name cannot match
if (pattern == null)
return true; // null pattern is equivalent to '*'
int[] matchingRegions = getSubWordMatchingRegions(new String(pattern), new String(name));
return matchingRegions != null;
}
/**
* Answers all the regions in a given name matching a subword pattern.
* <p>
* Each of these regions is made of its starting index and its length in the given
* name. They are all concatenated in a single array of <code>int</code>
* which therefore always has an even length.
* <p>
* Note that each region is disjointed from the following one.<br>
* E.g. if the regions are <code>{ start1, length1, start2, length2 }</code>,
* then <code>start1+length1</code> will always be smaller than
* <code>start2</code>.
* <p>
* Examples:
* <ol>
* <li><pre>
* pattern = "linkedmap"
* name = LinkedHashMap
* result: { 0, 6, 10, 3 }
* </pre></li>
* </ol>
*
* @see CharOperation#subWordMatch(char[], char[])
* for more details on the subword behavior
*
* @param pattern the given pattern
* @param name the given name
* @return an array of <code>int</code> having two slots per returned
* regions (first one is the starting index of the region and the second
* one the length of the region).<br>
* Note that it may be <code>null</code> if the given name does not match
* the pattern
* @since 3.21
*/
public static final int[] getSubWordMatchingRegions(String pattern, String name) {
if (name == null)
return null; // null name cannot match
if (pattern == null) {
// null pattern cannot match any region
// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=264816
return EMPTY_REGIONS;
}
return new SubwordMatcher(name).getMatchingRegions(pattern);
}
/**
* Answers true if the characters of the pattern are contained in the
* name as a substring, in a case-insensitive way.
*
* @param pattern the given pattern
* @param name the given name
* @return true if the pattern matches the given name, false otherwise
* @since 3.12
*/
public static final boolean substringMatch(String pattern, String name) {
if (pattern == null || pattern.length() == 0) {
return true;
}
if (name == null) {
return false;
}
return checkSubstringMatch(pattern.toCharArray(), name.toCharArray());
}
/**
* Answers true if the characters of the pattern are contained in the
* name as a substring, in a case-insensitive way.
*
* @param pattern the given pattern
* @param name the given name
* @return true if the pattern matches the given name, false otherwise
* @since 3.12
*/
public static final boolean substringMatch(char[] pattern, char[] name) {
if (pattern == null || pattern.length == 0) {
return true;
}
if (name == null) {
return false;
}
return checkSubstringMatch(pattern, name);
}
/**
* Internal substring matching method; called after the null and length
* checks are performed.
*
* @param pattern the given pattern
* @param name the given name
* @return true if the pattern matches the given name, false otherwise
*
* @see CharOperation#substringMatch(char[], char[])
*/
private static final boolean checkSubstringMatch(char[] pattern, char[] name) {
/* XXX: to be revised/enabled
// allow non-consecutive occurrence of pattern characters
if (pattern.length >= 3) {
int pidx = 0;
for (int nidx = 0; nidx < name.length; nidx++) {
if (Character.toLowerCase(name[nidx]) ==
Character.toLowerCase(pattern[pidx]))
pidx++;
if (pidx == pattern.length)
return true;
}
// for short patterns only allow consecutive occurrence
} else {
*/
// outer loop iterates on the characters of the name; trying to
// match at any possible position
outer: for (int nidx = 0; nidx < name.length - pattern.length + 1; nidx++) {
// inner loop iterates on pattern characters
for (int pidx = 0; pidx < pattern.length; pidx++) {
if (Character.toLowerCase(name[nidx + pidx]) !=
Character.toLowerCase(pattern[pidx])) {
// no match until parameter list; do not match parameter list
if ((name[nidx + pidx] == '(') || (name[nidx + pidx] == ':'))
return false;
continue outer;
}
if (pidx == pattern.length - 1)
return true;
}
}
// XXX: }
return false;
}
/**
* Returns the char arrays as an array of Strings
*
* @param charArrays the char array to convert
* @return the char arrays as an array of Strings or null if the given char arrays is null.
* @since 3.0
*/
public static String[] charArrayToStringArray(char[][] charArrays) {
if (charArrays == null)
return null;
int length = charArrays.length;
if (length == 0)
return NO_STRINGS;
String[] strings= new String[length];
for (int i= 0; i < length; i++)
strings[i]= new String(charArrays[i]);
return strings;
}
/**
* Returns the char array as a String
* @param charArray the char array to convert
* @return the char array as a String or null if the given char array is null.
* @since 3.0
*/
public static String charToString(char[] charArray) {
if (charArray == null) return null;
return new String(charArray);
}
/**
* Converts the given list of strings to an array of equal size,
* containing the individual strings converted to char[] each.
*
* @return an array of char[], representing the elements in the input list, or {@code null} if the list was {@code null}.
* @since 3.14
*/
public static char[][] toCharArrays(List<String> stringList) {
if (stringList == null)
return null;
char[][] result = new char[stringList.size()][];
for (int i = 0; i < result.length; i++)
result[i] = stringList.get(i).toCharArray();
return result;
}
/**
* Answers a new array adding the second array at the end of first array.
* It answers null if the first and second are null.
* If the first array is null, then a new array char[][] is created with second.
* If the second array is null, then the first array is returned.
* <br>
* <br>
* For example:
* <ol>
* <li><pre>
* first = null
* second = { 'a' }
* => result = { { ' a' } }
* </pre>
* <li><pre>
* first = { { ' a' } }
* second = null
* => result = { { ' a' } }
* </pre>
* </li>
* <li><pre>
* first = { { ' a' } }
* second = { ' b' }
* => result = { { ' a' } , { ' b' } }
* </pre>
* </li>
* </ol>
*
* @param first the first array to concatenate
* @param second the array to add at the end of the first array
* @return a new array adding the second array at the end of first array, or null if the two arrays are null.
*/
public static final char[][] arrayConcat(char[][] first, char[] second) {
if (second == null)
return first;
if (first == null)
return new char[][] { second };
int length = first.length;
char[][] result = new char[length + 1][];
System.arraycopy(first, 0, result, 0, length);
result[length] = second;
return result;
}
/**
* Compares the two char arrays lexicographically.
*
* Returns a negative integer if array1 lexicographically precedes the array2,
* a positive integer if this array1 lexicographically follows the array2, or
* zero if both arrays are equal.
*
* @param array1 the first given array
* @param array2 the second given array
* @return the returned value of the comparison between array1 and array2