comparison src/org/tmatesoft/hg/internal/PathGlobMatcher.java @ 229:1ec6b327a6ac

Scope for status reworked: explicit files or a general matcher
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 31 May 2011 05:23:07 +0200
parents 4a948ec83980
children 981f9f50bb6c
comparison
equal deleted inserted replaced
228:fffe4f882248 229:1ec6b327a6ac
37 */ 37 */
38 public PathGlobMatcher(String... globPatterns) { 38 public PathGlobMatcher(String... globPatterns) {
39 String[] regexp = new String[globPatterns.length]; //deliberately let fail with NPE 39 String[] regexp = new String[globPatterns.length]; //deliberately let fail with NPE
40 int i = 0; 40 int i = 0;
41 for (String s : globPatterns) { 41 for (String s : globPatterns) {
42 regexp[i] = glob2regexp(s); 42 regexp[i++] = glob2regexp(s);
43 } 43 }
44 try { 44 try {
45 delegate = new PathRegexpMatcher(regexp); 45 delegate = new PathRegexpMatcher(regexp);
46 } catch (PatternSyntaxException ex) { 46 } catch (PatternSyntaxException ex) {
47 ex.printStackTrace(); 47 ex.printStackTrace();
51 51
52 52
53 // HgIgnore.glob2regex is similar, but IsIgnore solves slightly different task 53 // HgIgnore.glob2regex is similar, but IsIgnore solves slightly different task
54 // (need to match partial paths, e.g. for glob 'bin' shall match not only 'bin' folder, but also any path below it, 54 // (need to match partial paths, e.g. for glob 'bin' shall match not only 'bin' folder, but also any path below it,
55 // which is not generally the case 55 // which is not generally the case
56 private static String glob2regexp(String glob) { 56 private static String glob2regexp(String glob) { // FIXME TESTS NEEDED!!!
57 int end = glob.length() - 1; 57 int end = glob.length() - 1;
58 boolean needLineEndMatch = glob.charAt(end) != '*'; 58 if (glob.length() > 2 && glob.charAt(end) == '*' && glob.charAt(end - 1) == '.') {
59 while (end > 0 && glob.charAt(end) == '*') end--; // remove trailing * that are useless for Pattern.find() 59 end-=2;
60 }
61 boolean needLineEndMatch = true;//glob.charAt(end) != '*';
62 // while (end > 0 && glob.charAt(end) == '*') end--; // remove trailing * that are useless for Pattern.find()
60 StringBuilder sb = new StringBuilder(end*2); 63 StringBuilder sb = new StringBuilder(end*2);
61 if (glob.charAt(0) != '*') { 64 // if (glob.charAt(0) != '*') {
62 sb.append('^'); 65 sb.append('^');
63 } 66 // }
64 for (int i = 0; i <= end; i++) { 67 for (int i = 0; i <= end; i++) {
65 char ch = glob.charAt(i); 68 char ch = glob.charAt(i);
66 if (ch == '*') { 69 if (ch == '*') {
67 if (glob.charAt(i+1) == '*') { // i < end because we've stripped any trailing * earlier 70 if (i < end && glob.charAt(i+1) == '*') {
68 // any char, including path segment separator 71 // any char, including path segment separator
69 sb.append(".*?"); 72 sb.append(".*?");
70 i++; 73 i++;
74 if (i < end && glob.charAt(i+1) == '/') {
75 sb.append("/?");
76 i++;
77 }
71 } else { 78 } else {
72 // just path segments 79 // just path segments
73 sb.append("[^/]*?"); 80 sb.append("[^/]*?");
74 } 81 }
75 continue; 82 continue;