Home | History | Annotate | Download | only in gn
      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.
      4 
      5 #ifndef TOOLS_GN_PATTERN_H_
      6 #define TOOLS_GN_PATTERN_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "tools/gn/value.h"
     12 
     13 extern const char kPattern_Help[];
     14 
     15 class Pattern {
     16  public:
     17   struct Subrange {
     18     enum Type {
     19       LITERAL,  // Matches exactly the contents of the string.
     20       ANYTHING,  // * (zero or more chars).
     21       PATH_BOUNDARY  // '/' or beginning of string.
     22     };
     23 
     24     Subrange(Type t, const std::string& l = std::string())
     25         : type(t),
     26           literal(l) {
     27     }
     28 
     29     // Returns the minimum number of chars that this subrange requires.
     30     size_t MinSize() const {
     31       switch (type) {
     32         case LITERAL:
     33           return literal.size();
     34         case ANYTHING:
     35           return 0;
     36         case PATH_BOUNDARY:
     37           return 0;  // Can match beginning or end of string, which is 0 len.
     38         default:
     39           return 0;
     40       }
     41     }
     42 
     43     Type type;
     44 
     45     // When type == LITERAL this is the text to match.
     46     std::string literal;
     47   };
     48 
     49   Pattern(const std::string& s);
     50   ~Pattern();
     51 
     52   // Returns true if the current pattern matches the given string.
     53   bool MatchesString(const std::string& s) const;
     54 
     55  private:
     56   // allow_implicit_path_boundary determines if a path boundary should accept
     57   // matches at the beginning or end of the string.
     58   bool RecursiveMatch(const std::string& s,
     59                       size_t begin_char,
     60                       size_t subrange_index,
     61                       bool allow_implicit_path_boundary) const;
     62 
     63   std::vector<Subrange> subranges_;
     64 
     65   // Set to true when the subranges are "*foo" ("ANYTHING" followed by a
     66   // literal). This covers most patterns so we optimize for this.
     67   bool is_suffix_;
     68 };
     69 
     70 class PatternList {
     71  public:
     72   PatternList();
     73   ~PatternList();
     74 
     75   bool is_empty() const { return patterns_.empty(); }
     76 
     77   // Initializes the pattern list from a give list of pattern strings. Sets
     78   // |*err| on failure.
     79   void SetFromValue(const Value& v, Err* err);
     80 
     81   bool MatchesString(const std::string& s) const;
     82   bool MatchesValue(const Value& v) const;
     83 
     84  private:
     85   std::vector<Pattern> patterns_;
     86 };
     87 
     88 #endif  // TOOLS_GN_PATTERN_H_
     89