TrailingAsteriskMatcher.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. /**
  12.  * Matcher for simple patterns ending with an asterisk, e.g. "Makefile.*"
  13.  */
  14. public class TrailingAsteriskMatcher extends NameMatcher {

  15.     TrailingAsteriskMatcher(String pattern, Character pathSeparator, boolean dirOnly) {
  16.         super(pattern, pathSeparator, dirOnly, true);

  17.         if (subPattern.charAt(subPattern.length() - 1) != '*')
  18.             throw new IllegalArgumentException(
  19.                     "Pattern must have trailing asterisk: " + pattern); //$NON-NLS-1$
  20.     }

  21.     /** {@inheritDoc} */
  22.     @Override
  23.     public boolean matches(String segment, int startIncl, int endExcl) {
  24.         // faster local access, same as in string.indexOf()
  25.         String s = subPattern;
  26.         // we don't need to count '*' character itself
  27.         int subLenth = s.length() - 1;
  28.         // simple /*/ pattern
  29.         if (subLenth == 0)
  30.             return true;

  31.         if (subLenth > (endExcl - startIncl))
  32.             return false;

  33.         for (int i = 0; i < subLenth; i++) {
  34.             char c1 = s.charAt(i);
  35.             char c2 = segment.charAt(i + startIncl);
  36.             if (c1 != c2)
  37.                 return false;
  38.         }
  39.         return true;
  40.     }

  41. }