Consider only escaping backslash for regular expressions in ignore rules

While checking if we should consider an ignore rule without '[]'
brackets as a regular expression, check if the backslash escapes one of
the glob special characters '?', '*', '[', '\\'. If not, backslash is
not a part of a regex and should be treated literally.

Bug: 463581
Change-Id: I85208c7f85246fbf6c5029ce3c8b7bb8f4dbd947
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
This commit is contained in:
Andrey Loskutov 2015-07-13 23:54:57 +02:00 committed by Matthias Sohn
parent f8564c7601
commit f19b1f2d07
2 changed files with 30 additions and 2 deletions

View File

@ -835,6 +835,16 @@ public void testEscapedBackslash() throws Exception {
assertMatch("a\\\\b", "a\\b", true);
}
@Test
public void testNotEscapingBackslash() throws Exception {
assertMatch("\\out", "\\out", true);
assertMatch("\\out", "a/\\out", true);
assertMatch("c:\\/", "c:\\/", true);
assertMatch("c:\\/", "a/c:\\/", true);
assertMatch("c:\\tmp", "c:\\tmp", true);
assertMatch("c:\\tmp", "a/c:\\tmp", true);
}
@Test
public void testMultipleEscapedCharacters1() throws Exception {
assertMatch("\\]a?c\\*\\[d\\?\\]", "]abc*[d?]", true);

View File

@ -142,9 +142,27 @@ private static boolean isComplexWildcard(String pattern) {
if (idx2 > idx1)
return true;
}
// required to match escaped backslashes '\\\\'
if (pattern.indexOf('?') != -1 || pattern.indexOf('\\') != -1)
if (pattern.indexOf('?') != -1) {
return true;
} else {
// check if the backslash escapes one of the glob special characters
// if not, backslash is not part of a regex and treated literally
int backSlash = pattern.indexOf('\\');
if (backSlash >= 0) {
int nextIdx = backSlash + 1;
if (pattern.length() == nextIdx) {
return false;
}
char nextChar = pattern.charAt(nextIdx);
if (nextChar == '?' || nextChar == '*' || nextChar == '['
// required to match escaped backslashes '\\\\'
|| nextChar == '\\') {
return true;
} else {
return false;
}
}
}
return false;
}