1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef TOOLS_GN_PATTERN_H_
6 #define TOOLS_GN_PATTERN_H_
11 #include "tools/gn/value.h"
17 LITERAL, // Matches exactly the contents of the string.
18 ANYTHING, // * (zero or more chars).
19 PATH_BOUNDARY // '/' or beginning of string.
22 Subrange(Type t, const std::string& l = std::string())
27 // Returns the minimum number of chars that this subrange requires.
28 size_t MinSize() const {
31 return literal.size();
35 return 0; // Can match beginning or end of string, which is 0 len.
43 // When type == LITERAL this is the text to match.
47 Pattern(const std::string& s);
50 // Returns true if the current pattern matches the given string.
51 bool MatchesString(const std::string& s) const;
54 // allow_implicit_path_boundary determines if a path boundary should accept
55 // matches at the beginning or end of the string.
56 bool RecursiveMatch(const std::string& s,
58 size_t subrange_index,
59 bool allow_implicit_path_boundary) const;
61 std::vector<Subrange> subranges_;
63 // Set to true when the subranges are "*foo" ("ANYTHING" followed by a
64 // literal). This covers most patterns so we optimize for this.
73 bool is_empty() const { return patterns_.empty(); }
75 void Append(const Pattern& pattern);
77 // Initializes the pattern list from a give list of pattern strings. Sets
79 void SetFromValue(const Value& v, Err* err);
81 bool MatchesString(const std::string& s) const;
82 bool MatchesValue(const Value& v) const;
85 std::vector<Pattern> patterns_;
88 #endif // TOOLS_GN_PATTERN_H_