Signed-off-by: Florian Koeberle <florianskarten@xxxxxx> --- .../src/org/spearce/jgit/lib/FNMatchPattern.java | 81 ++++++++++++++++++++ 1 files changed, 81 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/FNMatchPattern.java diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/FNMatchPattern.java b/org.spearce.jgit/src/org/spearce/jgit/lib/FNMatchPattern.java new file mode 100644 index 0000000..005659e --- /dev/null +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/FNMatchPattern.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2008 Florian Köberle + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License, version 2, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + */ +package org.spearce.jgit.lib; + +import java.util.regex.Pattern; + +/** + * This class represents a pattern which should work like the fnmatch method. + * <code>new FNMatchPattern(exp).matches(input)</code> should do the same like + * <code>fnmatch(exp, input, 0) == 0</code> + * + * As this isn't a one to one code port, but written based on the documentation + * of fnmatch it can be that the behavior of this class differ in some corner + * cases from the behavior of the fnmatch function. + */ +public class FNMatchPattern { + + private final Pattern regexPattern; + + private static String toRegexString(String fnmatchPattern) { + final StringBuilder regexStringBuilder = new StringBuilder(); + char perviosCharacter = 0; + for (int i = 0; i < fnmatchPattern.length(); i++) { + final char c = fnmatchPattern.charAt(i); + switch (c) { + case '^': + if (perviosCharacter == '[') { + regexStringBuilder.append('!'); + } else { + regexStringBuilder.append("\\x5E"); + } + break; + case '.': + regexStringBuilder.append("\\x2E"); + break; + case '*': + regexStringBuilder.append(".*"); + break; + default: + regexStringBuilder.append(c); + } + perviosCharacter = c; + } + return regexStringBuilder.toString(); + } + + /** + * + * @param fnmatchPattern + * must be a valid fnmatch pattern string. + */ + public FNMatchPattern(String fnmatchPattern) { + final String regularExpression = toRegexString(fnmatchPattern); + this.regexPattern = Pattern.compile(regularExpression); + } + + /** + * + * @param stringToMatch + * the string to match. + * @return true when this pattern matches. + */ + public boolean matches(String stringToMatch) { + return regexPattern.matcher(stringToMatch).matches(); + } + +} -- 1.5.4.3 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html