4
4
* // Is missing the 4
5
5
* String digits = "123567890";
6
6
* ```
7
+ *
8
+ * @kind problem
7
9
*/
8
10
9
11
import java
@@ -74,22 +76,14 @@ string getSubsequentChar(string first) {
74
76
if ( first .isLowercase ( ) ) then (
75
77
areConsecutiveCharsLower ( first , result )
76
78
) else exists ( string secondLower |
77
- areConsecutiveCharsLower ( first .toLowerCase ( ) , secondLower )
78
- and result = secondLower .toUpperCase ( )
79
+ areConsecutiveCharsLower ( first .toLowerCase ( ) , secondLower )
80
+ and result = secondLower .toUpperCase ( )
79
81
)
80
82
}
81
83
82
- predicate isAllowedLastChar ( string last ) {
83
- last = [
84
- "f" , // Last hex digit
85
- "z" ,
86
- "0" ,
87
- "9"
88
- ]
89
- }
90
-
91
84
predicate hasSequenceStart ( IncompleteSequenceExpr expr ) {
92
- // Check first 3 chars, otherwise causes too much negatives, e.g. for "delete", "no", ...
85
+ // Check first 3 chars, otherwise causes a lot of false positives in the middle
86
+ // of arbitrary strings
93
87
exists ( string first , string second , string third |
94
88
first = expr .charAt ( 0 )
95
89
and second = expr .charAt ( 1 )
@@ -98,56 +92,35 @@ predicate hasSequenceStart(IncompleteSequenceExpr expr) {
98
92
areConsecutiveChars ( first , second )
99
93
and areConsecutiveChars ( second , third )
100
94
)
101
- // Exclude words containing three or more consecutive chars
102
- and not expr .getString ( ) .toLowerCase ( ) .matches ( [
103
- "default%" ,
104
- "stub%"
105
- ] )
106
95
}
107
96
108
97
abstract class IncompleteSequenceExpr extends Expr {
109
98
abstract string getString ( ) ;
110
99
abstract string charAt ( int index ) ;
111
- abstract int length ( ) ;
112
100
113
101
string getMissingChar ( int index ) {
114
- exists ( string previousCorrect , string lastCorrect |
102
+ exists ( string previousCorrect , string lastCorrect , string incorrect |
115
103
previousCorrect = charAt ( index - 2 )
116
104
and lastCorrect = charAt ( index - 1 )
105
+ and incorrect = charAt ( index )
117
106
// Make sure that there is a consecutive char in front, otherwise
118
107
// sequence which then later has other chars causes false positives
119
- // e.g. "abc-15-15 " would find matches for "15" (-> "12")
108
+ // e.g. "abc-15" would find matches for "15" (-> "12")
120
109
and areConsecutiveChars ( previousCorrect , lastCorrect )
121
110
and result = getSubsequentChar ( lastCorrect )
122
111
|
123
- exists ( string incorrect |
124
- incorrect = charAt ( index )
125
- |
126
- // Verify that both chars are of the same type; ignore somthing like "12.0", "ABC-123"
127
- areSameType ( lastCorrect , incorrect )
128
- and not areConsecutiveChars ( lastCorrect , incorrect )
129
- and (
130
- index = length ( ) - 1
131
- // Char was skipped, e.g. "abcd_f"
132
- or areConsecutiveChars ( result , incorrect )
133
- // Duplicate char
134
- or (
135
- index <= length ( ) - 1 // At least one more char
136
- and lastCorrect = incorrect
137
- and areConsecutiveChars ( lastCorrect , charAt ( index + 1 ) )
138
- )
112
+ // Verify that both chars are of the same type; ignore something like "12.0", "ABC-123"
113
+ areSameType ( lastCorrect , incorrect )
114
+ and not areConsecutiveChars ( lastCorrect , incorrect )
115
+ and (
116
+ // Char was skipped, e.g. "abcd_f"
117
+ areConsecutiveChars ( result , incorrect )
118
+ // Duplicate char
119
+ or (
120
+ lastCorrect = incorrect
121
+ and areConsecutiveChars ( lastCorrect , charAt ( index + 1 ) )
139
122
)
140
123
)
141
- or (
142
- (
143
- // Last char
144
- index = length ( )
145
- // Or next char has different type
146
- or not areSameType ( lastCorrect , charAt ( index ) )
147
- )
148
- // Verify that sequence is not allowed to end with `lastCorrect`
149
- and not isAllowedLastChar ( lastCorrect .toLowerCase ( ) )
150
- )
151
124
)
152
125
}
153
126
}
@@ -162,11 +135,6 @@ class IncompleteStringLiteralExpr extends IncompleteSequenceExpr, StringLiteral
162
135
string charAt ( int index ) {
163
136
result = getString ( ) .charAt ( index )
164
137
}
165
-
166
- override
167
- int length ( ) {
168
- result = getString ( ) .length ( )
169
- }
170
138
}
171
139
172
140
class IncompleteCharArrayExpr extends IncompleteSequenceExpr , ArrayInit {
@@ -179,16 +147,11 @@ class IncompleteCharArrayExpr extends IncompleteSequenceExpr, ArrayInit {
179
147
string charAt ( int index ) {
180
148
result = getInit ( index ) .( CharacterLiteral ) .getValue ( )
181
149
}
182
-
183
- override
184
- int length ( ) {
185
- // Count elements of initializer
186
- result = count ( getAnInit ( ) )
187
- }
188
150
}
189
151
190
- from IncompleteSequenceExpr incompleteSeq , int index
152
+ from IncompleteSequenceExpr incompleteSeq , int index , string missingChar
191
153
where
192
154
hasSequenceStart ( incompleteSeq )
155
+ and missingChar = incompleteSeq .getMissingChar ( index )
193
156
and not incompleteSeq .getEnclosingCallable ( ) .getDeclaringType ( ) instanceof TestClass
194
- select incompleteSeq , "Sequence of chars is incomplete, is missing '" + incompleteSeq . getMissingChar ( index ) + "' at index " + index
157
+ select incompleteSeq , "Sequence of chars is incomplete, is missing '" + missingChar + "' at index " + index
0 commit comments