| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 /** | |
| 6 * Pattern utilities for use with server.Router. | |
| 7 * | |
| 8 * Example: | |
| 9 * | |
| 10 * var router = new Router(server); | |
| 11 * router.filter(matchesAny(new UrlPattern(r'/(\w+)'), | |
| 12 * exclude: [new UrlPattern('/login')]), authFilter); | |
| 13 */ | |
| 14 library pattern; | |
| 15 | |
| 16 class _MultiPattern extends Pattern { | |
| 17 final Iterable<Pattern> include; | |
| 18 final Iterable<Pattern> exclude; | |
| 19 | |
| 20 _MultiPattern(Iterable<Pattern> this.include, | |
| 21 {Iterable<Pattern> this.exclude}); | |
| 22 | |
| 23 Iterable<Match> allMatches(String str) { | |
| 24 var _allMatches = []; | |
| 25 for (var pattern in include) { | |
| 26 var matches = pattern.allMatches(str); | |
| 27 if (_hasMatch(matches)) { | |
| 28 if (exclude != null) { | |
| 29 for (var excludePattern in exclude) { | |
| 30 if (_hasMatch(excludePattern.allMatches(str))) { | |
| 31 return []; | |
| 32 } | |
| 33 } | |
| 34 } | |
| 35 _allMatches.add(matches); | |
| 36 } | |
| 37 } | |
| 38 return _allMatches.expand((x) => x); | |
| 39 } | |
| 40 | |
| 41 Match matchAsPrefix(String string, [int start = 0]) { | |
| 42 throw new UnimplementedError('matchAsPrefix is not implemented'); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 /** | |
| 47 * Returns a [Pattern] that matches against every pattern in [include] and | |
| 48 * returns all the matches. If the input string matches against any pattern in | |
| 49 * [exclude] no matches are returned. | |
| 50 */ | |
| 51 Pattern matchAny(Iterable<Pattern> include, {Iterable<Pattern> exclude}) => | |
| 52 new _MultiPattern(include, exclude: exclude); | |
| 53 | |
| 54 /** | |
| 55 * Returns true if [pattern] has a single match in [str] that matches the whole | |
| 56 * string, not a substring. | |
| 57 */ | |
| 58 bool matchesFull(Pattern pattern, String str) { | |
| 59 var iter = pattern.allMatches(str).iterator; | |
| 60 if (iter.moveNext()) { | |
| 61 var match = iter.current; | |
| 62 return match.start == 0 && match.end == str.length && !iter.moveNext(); | |
| 63 } | |
| 64 return false; | |
| 65 } | |
| 66 | |
| 67 bool matchesPrefix(Pattern pattern, String str) { | |
| 68 Iterable<Match> matches = pattern.allMatches(str); | |
| 69 return !matches.isEmpty && matches.first.start == 0; | |
| 70 } | |
| 71 | |
| 72 /// return the tail | |
| 73 Match prefixMatch(Pattern pattern, String str) { | |
| 74 Iterable<Match> matches = pattern.allMatches(str); | |
| 75 if (!matches.isEmpty && matches.first.start == 0) { | |
| 76 return matches.first; | |
| 77 } | |
| 78 return null; | |
| 79 } | |
| 80 | |
| 81 bool _hasMatch(Iterable<Match> matches) => matches.iterator.moveNext(); | |
| OLD | NEW |