OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 @patch | 5 @patch |
6 class RegExp { | 6 class RegExp { |
7 @patch | 7 @patch |
8 factory RegExp(String source, | 8 factory RegExp(String source, |
9 {bool multiLine: false, bool caseSensitive: true}) { | 9 {bool multiLine: false, bool caseSensitive: true}) { |
10 _RegExpHashKey key = new _RegExpHashKey(source, multiLine, caseSensitive); | 10 _RegExpHashKey key = new _RegExpHashKey(source, multiLine, caseSensitive); |
11 _RegExpHashValue value = _cache[key]; | 11 _RegExpHashValue value = _cache[key]; |
12 | 12 |
13 if (value == null) { | 13 if (value == null) { |
14 if (_cache.length > _MAX_CACHE_SIZE) { | 14 if (_cache.length > _MAX_CACHE_SIZE) { |
15 _RegExpHashKey lastKey = _recentlyUsed.last; | 15 _RegExpHashKey lastKey = _recentlyUsed.last; |
16 lastKey.unlink(); | 16 _recentlyUsed.remove(lastKey); |
17 _cache.remove(lastKey); | 17 _cache.remove(lastKey); |
18 } | 18 } |
19 | 19 |
20 value = new _RegExpHashValue( | 20 value = new _RegExpHashValue( |
21 new _RegExp(source, | 21 new _RegExp(source, |
22 multiLine: multiLine, caseSensitive: caseSensitive), | 22 multiLine: multiLine, caseSensitive: caseSensitive), |
23 key); | 23 key); |
24 _cache[key] = value; | 24 _cache[key] = value; |
25 } else { | 25 } else { |
26 value.key.unlink(); | 26 value.key.unlink(); |
(...skipping 12 matching lines...) Expand all Loading... |
39 // elements using an LRU eviction strategy. | 39 // elements using an LRU eviction strategy. |
40 // TODO(zerny): Do not impose a fixed limit on the number of cached objects. | 40 // TODO(zerny): Do not impose a fixed limit on the number of cached objects. |
41 // Other possibilities could be limiting by the size of the regexp objects, | 41 // Other possibilities could be limiting by the size of the regexp objects, |
42 // or imposing a lower time bound for the most recent use under which a regexp | 42 // or imposing a lower time bound for the most recent use under which a regexp |
43 // may not be removed from the cache. | 43 // may not be removed from the cache. |
44 // TODO(zerny): Use self-sizing cache similar to _AccessorCache in | 44 // TODO(zerny): Use self-sizing cache similar to _AccessorCache in |
45 // mirrors_impl.dart. | 45 // mirrors_impl.dart. |
46 static const int _MAX_CACHE_SIZE = 256; | 46 static const int _MAX_CACHE_SIZE = 256; |
47 static final Map<_RegExpHashKey, _RegExpHashValue> _cache = | 47 static final Map<_RegExpHashKey, _RegExpHashValue> _cache = |
48 new HashMap<_RegExpHashKey, _RegExpHashValue>(); | 48 new HashMap<_RegExpHashKey, _RegExpHashValue>(); |
49 static final LinkedList<_RegExpHashKey> _recentlyUsed = | 49 static final internal.LinkedList<_RegExpHashKey> _recentlyUsed = |
50 new LinkedList<_RegExpHashKey>(); | 50 new internal.LinkedList<_RegExpHashKey>(); |
51 } | 51 } |
52 | 52 |
53 // Represents both a key in the regular expression cache as well as its | 53 // Represents both a key in the regular expression cache as well as its |
54 // corresponding entry in the LRU list. | 54 // corresponding entry in the LRU list. |
55 class _RegExpHashKey extends LinkedListEntry<_RegExpHashKey> { | 55 class _RegExpHashKey extends internal.LinkedListEntry<_RegExpHashKey> { |
56 final String pattern; | 56 final String pattern; |
57 final bool multiLine; | 57 final bool multiLine; |
58 final bool caseSensitive; | 58 final bool caseSensitive; |
59 | 59 |
60 _RegExpHashKey(this.pattern, this.multiLine, this.caseSensitive); | 60 _RegExpHashKey(this.pattern, this.multiLine, this.caseSensitive); |
61 | 61 |
62 int get hashCode => pattern.hashCode; | 62 int get hashCode => pattern.hashCode; |
63 bool operator ==(_RegExpHashKey that) { | 63 bool operator ==(_RegExpHashKey that) { |
64 return (this.pattern == that.pattern) && | 64 return (this.pattern == that.pattern) && |
65 (this.multiLine == that.multiLine) && | 65 (this.multiLine == that.multiLine) && |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 _nextIndex++; | 265 _nextIndex++; |
266 } | 266 } |
267 return true; | 267 return true; |
268 } | 268 } |
269 } | 269 } |
270 _current = null; | 270 _current = null; |
271 _re = null; | 271 _re = null; |
272 return false; | 272 return false; |
273 } | 273 } |
274 } | 274 } |
OLD | NEW |