| OLD | NEW |
| 1 /* |
| 2 * Copyright 2015 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 1 #ifndef SkRecordPattern_DEFINED | 8 #ifndef SkRecordPattern_DEFINED |
| 2 #define SkRecordPattern_DEFINED | 9 #define SkRecordPattern_DEFINED |
| 3 | 10 |
| 4 #include "SkTLogic.h" | 11 #include "SkTLogic.h" |
| 5 | 12 |
| 6 namespace SkRecords { | 13 namespace SkRecords { |
| 7 | 14 |
| 8 // First, some matchers. These match a single command in the SkRecord, | 15 // First, some matchers. These match a single command in the SkRecord, |
| 9 // and may hang onto some data from it. If so, you can get the data by calling
.get(). | 16 // and may hang onto some data from it. If so, you can get the data by calling
.get(). |
| 10 | 17 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 // This is the main entry point to pattern matching, and so provides a couple of
extra API bits: | 109 // This is the main entry point to pattern matching, and so provides a couple of
extra API bits: |
| 103 // - search scans through the record to look for matches; | 110 // - search scans through the record to look for matches; |
| 104 // - first, second, and third return the data stored by their respective matche
rs in the pattern. | 111 // - first, second, and third return the data stored by their respective matche
rs in the pattern. |
| 105 // | 112 // |
| 106 // These Cons build lists analogously to Lisp's "cons". See Pattern# for the "l
ist" equivalent. | 113 // These Cons build lists analogously to Lisp's "cons". See Pattern# for the "l
ist" equivalent. |
| 107 template <typename Matcher, typename Pattern> | 114 template <typename Matcher, typename Pattern> |
| 108 class Cons { | 115 class Cons { |
| 109 public: | 116 public: |
| 110 // If this pattern matches the SkRecord starting at i, | 117 // If this pattern matches the SkRecord starting at i, |
| 111 // return the index just past the end of the pattern, otherwise return 0. | 118 // return the index just past the end of the pattern, otherwise return 0. |
| 112 SK_ALWAYS_INLINE unsigned match(SkRecord* record, unsigned i) { | 119 SK_ALWAYS_INLINE int match(SkRecord* record, int i) { |
| 113 i = this->matchHead(&fHead, record, i); | 120 i = this->matchHead(&fHead, record, i); |
| 114 return i == 0 ? 0 : fTail.match(record, i); | 121 return i == 0 ? 0 : fTail.match(record, i); |
| 115 } | 122 } |
| 116 | 123 |
| 117 // Starting from *end, walk through the SkRecord to find the first span matc
hing this pattern. | 124 // Starting from *end, walk through the SkRecord to find the first span matc
hing this pattern. |
| 118 // If there is no such span, return false. If there is, return true and set
[*begin, *end). | 125 // If there is no such span, return false. If there is, return true and set
[*begin, *end). |
| 119 SK_ALWAYS_INLINE bool search(SkRecord* record, unsigned* begin, unsigned* en
d) { | 126 SK_ALWAYS_INLINE bool search(SkRecord* record, int* begin, int* end) { |
| 120 for (*begin = *end; *begin < record->count(); ++(*begin)) { | 127 for (*begin = *end; *begin < record->count(); ++(*begin)) { |
| 121 *end = this->match(record, *begin); | 128 *end = this->match(record, *begin); |
| 122 if (*end != 0) { | 129 if (*end != 0) { |
| 123 return true; | 130 return true; |
| 124 } | 131 } |
| 125 } | 132 } |
| 126 return false; | 133 return false; |
| 127 } | 134 } |
| 128 | 135 |
| 129 // Once either match or search has succeeded, access the stored data of the
first, second, | 136 // Once either match or search has succeeded, access the stored data of the
first, second, |
| 130 // or third matcher in this pattern. Add as needed for longer patterns. | 137 // or third matcher in this pattern. Add as needed for longer patterns. |
| 131 // T is checked statically at compile time; no casting is involved. It's ju
st an API wart. | 138 // T is checked statically at compile time; no casting is involved. It's ju
st an API wart. |
| 132 template <typename T> T* first() { return fHead.get(); } | 139 template <typename T> T* first() { return fHead.get(); } |
| 133 template <typename T> T* second() { return fTail.fHead.get(); } | 140 template <typename T> T* second() { return fTail.fHead.get(); } |
| 134 template <typename T> T* third() { return fTail.fTail.fHead.get(); } | 141 template <typename T> T* third() { return fTail.fTail.fHead.get(); } |
| 135 template <typename T> T* fourth() { return fTail.fTail.fTail.fHead.get(); } | 142 template <typename T> T* fourth() { return fTail.fTail.fTail.fHead.get(); } |
| 136 | 143 |
| 137 private: | 144 private: |
| 138 // If head isn't a Star, try to match at i once. | 145 // If head isn't a Star, try to match at i once. |
| 139 template <typename T> | 146 template <typename T> |
| 140 unsigned matchHead(T*, SkRecord* record, unsigned i) { | 147 int matchHead(T*, SkRecord* record, int i) { |
| 141 if (i < record->count()) { | 148 if (i < record->count()) { |
| 142 if (record->mutate<bool>(i, fHead)) { | 149 if (record->mutate<bool>(i, fHead)) { |
| 143 return i+1; | 150 return i+1; |
| 144 } | 151 } |
| 145 } | 152 } |
| 146 return 0; | 153 return 0; |
| 147 } | 154 } |
| 148 | 155 |
| 149 // If head is a Star, walk i until it doesn't match. | 156 // If head is a Star, walk i until it doesn't match. |
| 150 template <typename T> | 157 template <typename T> |
| 151 unsigned matchHead(Star<T>*, SkRecord* record, unsigned i) { | 158 int matchHead(Star<T>*, SkRecord* record, int i) { |
| 152 while (i < record->count()) { | 159 while (i < record->count()) { |
| 153 if (!record->mutate<bool>(i, fHead)) { | 160 if (!record->mutate<bool>(i, fHead)) { |
| 154 return i; | 161 return i; |
| 155 } | 162 } |
| 156 i++; | 163 i++; |
| 157 } | 164 } |
| 158 return 0; | 165 return 0; |
| 159 } | 166 } |
| 160 | 167 |
| 161 Matcher fHead; | 168 Matcher fHead; |
| 162 Pattern fTail; | 169 Pattern fTail; |
| 163 | 170 |
| 164 // All Cons are friends with each other. This lets first, second, and third
work. | 171 // All Cons are friends with each other. This lets first, second, and third
work. |
| 165 template <typename, typename> friend class Cons; | 172 template <typename, typename> friend class Cons; |
| 166 }; | 173 }; |
| 167 | 174 |
| 168 // Nil is the end of every pattern Cons chain. | 175 // Nil is the end of every pattern Cons chain. |
| 169 struct Nil { | 176 struct Nil { |
| 170 // Bottoms out recursion down the fTail chain. Just return whatever i the f
ront decided on. | 177 // Bottoms out recursion down the fTail chain. Just return whatever i the f
ront decided on. |
| 171 unsigned match(SkRecord*, unsigned i) { return i; } | 178 int match(SkRecord*, int i) { return i; } |
| 172 }; | 179 }; |
| 173 | 180 |
| 174 // These Pattern# types are syntax sugar over Cons and Nil, just to help elimina
te some of the | 181 // These Pattern# types are syntax sugar over Cons and Nil, just to help elimina
te some of the |
| 175 // template noise. Use these if you can. Feel free to add more for longer patt
erns. | 182 // template noise. Use these if you can. Feel free to add more for longer patt
erns. |
| 176 // All types A, B, C, ... are Matchers. | 183 // All types A, B, C, ... are Matchers. |
| 177 template <typename A> | 184 template <typename A> |
| 178 struct Pattern1 : Cons<A, Nil> {}; | 185 struct Pattern1 : Cons<A, Nil> {}; |
| 179 | 186 |
| 180 template <typename A, typename B> | 187 template <typename A, typename B> |
| 181 struct Pattern2 : Cons<A, Pattern1<B> > {}; | 188 struct Pattern2 : Cons<A, Pattern1<B> > {}; |
| 182 | 189 |
| 183 template <typename A, typename B, typename C> | 190 template <typename A, typename B, typename C> |
| 184 struct Pattern3 : Cons<A, Pattern2<B, C> > {}; | 191 struct Pattern3 : Cons<A, Pattern2<B, C> > {}; |
| 185 | 192 |
| 186 template <typename A, typename B, typename C, typename D> | 193 template <typename A, typename B, typename C, typename D> |
| 187 struct Pattern4 : Cons<A, Pattern3<B, C, D> > {}; | 194 struct Pattern4 : Cons<A, Pattern3<B, C, D> > {}; |
| 188 | 195 |
| 189 template <typename A, typename B, typename C, typename D, typename E> | 196 template <typename A, typename B, typename C, typename D, typename E> |
| 190 struct Pattern5 : Cons<A, Pattern4<B, C, D, E> > {}; | 197 struct Pattern5 : Cons<A, Pattern4<B, C, D, E> > {}; |
| 191 | 198 |
| 192 template <typename A, typename B, typename C, typename D, typename E, typename F
> | 199 template <typename A, typename B, typename C, typename D, typename E, typename F
> |
| 193 struct Pattern6 : Cons<A, Pattern5<B, C, D, E, F> > {}; | 200 struct Pattern6 : Cons<A, Pattern5<B, C, D, E, F> > {}; |
| 194 | 201 |
| 195 template <typename A, typename B, typename C, typename D, typename E, typename F
, typename G> | 202 template <typename A, typename B, typename C, typename D, typename E, typename F
, typename G> |
| 196 struct Pattern7 : Cons<A, Pattern6<B, C, D, E, F, G> > {}; | 203 struct Pattern7 : Cons<A, Pattern6<B, C, D, E, F, G> > {}; |
| 197 | 204 |
| 198 } // namespace SkRecords | 205 } // namespace SkRecords |
| 199 | 206 |
| 200 #endif//SkRecordPattern_DEFINED | 207 #endif//SkRecordPattern_DEFINED |
| OLD | NEW |