summaryrefslogtreecommitdiff
path: root/gn/tools/gn/pattern.h
blob: ef29c629668aac3efcc69c3cfbd12d661be4ddf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef TOOLS_GN_PATTERN_H_
#define TOOLS_GN_PATTERN_H_

#include <stddef.h>

#include <string>
#include <vector>

#include "tools/gn/value.h"

class Pattern {
 public:
  struct Subrange {
    enum Type {
      LITERAL,       // Matches exactly the contents of the string.
      ANYTHING,      // * (zero or more chars).
      PATH_BOUNDARY  // '/' or beginning of string.
    };

    explicit Subrange(Type t, const std::string& l = std::string())
        : type(t), literal(l) {}

    // Returns the minimum number of chars that this subrange requires.
    size_t MinSize() const {
      switch (type) {
        case LITERAL:
          return literal.size();
        case ANYTHING:
          return 0;
        case PATH_BOUNDARY:
          return 0;  // Can match beginning or end of string, which is 0 len.
        default:
          return 0;
      }
    }

    Type type;

    // When type == LITERAL this is the text to match.
    std::string literal;
  };

  explicit Pattern(const std::string& s);
  Pattern(const Pattern& other);
  ~Pattern();

  // Returns true if the current pattern matches the given string.
  bool MatchesString(const std::string& s) const;

 private:
  // allow_implicit_path_boundary determines if a path boundary should accept
  // matches at the beginning or end of the string.
  bool RecursiveMatch(const std::string& s,
                      size_t begin_char,
                      size_t subrange_index,
                      bool allow_implicit_path_boundary) const;

  std::vector<Subrange> subranges_;

  // Set to true when the subranges are "*foo" ("ANYTHING" followed by a
  // literal). This covers most patterns so we optimize for this.
  bool is_suffix_;
};

class PatternList {
 public:
  PatternList();
  PatternList(const PatternList& other);
  ~PatternList();

  bool is_empty() const { return patterns_.empty(); }

  void Append(const Pattern& pattern);

  // Initializes the pattern list from a give list of pattern strings. Sets
  // |*err| on failure.
  void SetFromValue(const Value& v, Err* err);

  bool MatchesString(const std::string& s) const;
  bool MatchesValue(const Value& v) const;

 private:
  std::vector<Pattern> patterns_;
};

#endif  // TOOLS_GN_PATTERN_H_