// Copyright 2014 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.lib.util; import com.google.common.base.Joiner; import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; import com.google.devtools.common.options.Converter; import com.google.devtools.common.options.OptionsParsingException; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import javax.annotation.Nullable; /** * Handles options that specify list of included/excluded regex expressions. Validates whether * string is included in that filter. * *
String is considered to be included into the filter if it does not match any of the excluded * regex expressions and if it matches at least one included regex expression. */ @AutoCodec @Immutable public final class RegexFilter { // Null inclusion or exclusion pattern means those patterns are not used. @Nullable private final Pattern inclusionPattern; @Nullable private final Pattern exclusionPattern; private final int hashCode; /** * Converts from a colon-separated list of regex expressions with optional -/+ prefix into the * RegexFilter. Colons prefixed with backslash are considered to be part of regex definition and * not a delimiter between separate regex expressions. * *
Order of expressions is not important. Empty entries are ignored. '-' marks an excluded
* expression.
*/
public static class RegexFilterConverter implements Converter Null {@code inclusionPattern} or {@code exclusionPattern} means that inclusion or exclusion
* matching will not be applied, respectively.
*/
@AutoCodec.Instantiator
RegexFilter(@Nullable Pattern inclusionPattern, @Nullable Pattern exclusionPattern) {
this.inclusionPattern = inclusionPattern;
this.exclusionPattern = exclusionPattern;
this.hashCode =
Objects.hash(
inclusionPattern == null ? null : inclusionPattern.pattern(),
exclusionPattern == null ? null : exclusionPattern.pattern());
}
/** Creates new RegexFilter using provided inclusion and exclusion path lists. */
public RegexFilter(List