Skip to content

Commit f4da0f4

Browse files
make common prefix calculation more robust
1 parent 205f9eb commit f4da0f4

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

sqldev/src/main/java/org/utplsql/sqldev/model/PrefixTools.xtend

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import java.util.List
2121
class PrefixTools {
2222
def static int findMinLength(String[] arr, int n) {
2323
var int min = Integer.MAX_VALUE
24-
for (var int i = 0; i <= (n - 1); i++) {
24+
for (var int i = 0; i < n; i++) {
2525
if ({
2626
val _rdIndx_arr = i
2727
arr.get(_rdIndx_arr)
@@ -36,13 +36,16 @@ class PrefixTools {
3636
}
3737

3838
def static boolean allContainsPrefix(String[] arr, int n, String str, int start, int end) {
39-
for (var int i = 0; i <= (n - 1); i++) {
39+
for (var int i = 0; i < n; i++) {
4040
var String arr_i = {
4141
val _rdIndx_arr = i
4242
arr.get(_rdIndx_arr)
4343
}
44-
for (var int j = start; j <= end; j++)
45-
if(arr_i.charAt(j) !== str.charAt(j)) return false
44+
for (var int j = start; j <= end; j++) {
45+
if (arr_i.charAt(j) !== str.charAt(j)) {
46+
return false
47+
}
48+
}
4649
}
4750
return true
4851
}
@@ -63,22 +66,26 @@ class PrefixTools {
6366
}
6467
return prefix
6568
}
66-
69+
6770
def static String commonPrefix(List<String> list) {
68-
if (list.size === 0) {
69-
return ""
70-
} else if (list.size === 1) {
71-
val pos = list.get(0).lastIndexOf(".");
72-
if (pos > 0) {
73-
return list.get(0).substring(0, pos + 1)
74-
} else {
71+
try {
72+
if (list.size === 0) {
7573
return ""
74+
} else if (list.size === 1) {
75+
val pos = list.get(0).lastIndexOf(".");
76+
if (pos > 0) {
77+
return list.get(0).substring(0, pos + 1)
78+
} else {
79+
return ""
80+
}
81+
} else {
82+
var String[] testArray = newArrayOfSize(list.size)
83+
var prefix = commonPrefix(list.toArray(testArray), list.size)
84+
return prefix
7685
}
77-
} else {
78-
var String[] testArray = newArrayOfSize(list.size)
79-
var prefix = commonPrefix(list.toArray(testArray), list.size)
80-
return prefix
86+
} catch (Exception e) {
87+
return ""
8188
}
8289
}
83-
90+
8491
}

sqldev/src/test/java/org/utplsql/sqldev/test/PrefixToolsTest.xtend

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,19 @@ class PrefixToolsTest {
2525
val expected = ""
2626
Assert.assertEquals(expected, actual)
2727
}
28+
29+
@Test
30+
def void twoOverlapLeft() {
31+
val actual = PrefixTools.commonPrefix(#["a.b.c", "a.b.c.d"])
32+
val expected = ""
33+
Assert.assertEquals(expected, actual)
34+
}
35+
36+
@Test
37+
def void twoOverlapRight() {
38+
val actual = PrefixTools.commonPrefix(#["a.b.c.d", "a.b.c"])
39+
val expected = ""
40+
Assert.assertEquals(expected, actual)
41+
}
42+
2843
}

0 commit comments

Comments
 (0)