WildCardMatcher.java

  1. /*
  2.  * Copyright (C) 2014, Andrey Loskutov <loskutov@gmx.de> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.ignore.internal;

  11. import static org.eclipse.jgit.ignore.internal.Strings.convertGlob;

  12. import java.util.regex.Pattern;

  13. import org.eclipse.jgit.errors.InvalidPatternException;

  14. /**
  15.  * Matcher built from path segments containing wildcards. This matcher converts
  16.  * glob wildcards to Java {@link java.util.regex.Pattern}'s.
  17.  * <p>
  18.  * This class is immutable and thread safe.
  19.  */
  20. public class WildCardMatcher extends NameMatcher {

  21.     final Pattern p;

  22.     WildCardMatcher(String pattern, Character pathSeparator, boolean dirOnly)
  23.             throws InvalidPatternException {
  24.         super(pattern, pathSeparator, dirOnly, false);
  25.         p = convertGlob(subPattern);
  26.     }

  27.     /** {@inheritDoc} */
  28.     @Override
  29.     public boolean matches(String segment, int startIncl, int endExcl) {
  30.         return p.matcher(segment.substring(startIncl, endExcl)).matches();
  31.     }
  32. }