OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 library glob.utils; | |
6 | |
7 import 'package:path/path.dart' as p; | |
8 | |
9 /// A range from [min] to [max], inclusive. | |
10 class Range { | |
11 /// The minimum value included by the range. | |
12 final int min; | |
13 | |
14 /// The maximum value included by the range. | |
15 final int max; | |
16 | |
17 /// Whether this range covers only a single number. | |
18 bool get isSingleton => min == max; | |
19 | |
20 Range(this.min, this.max); | |
21 | |
22 /// Returns a range that covers only [value]. | |
23 Range.singleton(int value) | |
24 : this(value, value); | |
25 | |
26 /// Whether [this] contains [value]. | |
27 bool contains(int value) => value >= min && value <= max; | |
28 | |
29 bool operator==(Object other) => other is Range && | |
30 other.min == min && other.max == max; | |
31 | |
32 int get hashCode => 3 * min + 7 * max; | |
33 } | |
34 | |
35 /// An implementation of [Match] constructed by [Glob]s. | |
36 class GlobMatch implements Match { | |
37 final String input; | |
38 final Pattern pattern; | |
39 final int start = 0; | |
40 | |
41 int get end => input.length; | |
42 int get groupCount => 0; | |
43 | |
44 GlobMatch(this.input, this.pattern); | |
45 | |
46 String operator [](int group) => this.group(group); | |
47 | |
48 String group(int group) { | |
49 if (group != 0) throw new RangeError.range(group, 0, 0); | |
50 return input; | |
51 } | |
52 | |
53 List<String> groups(List<int> groupIndices) => | |
54 groupIndices.map((index) => group(index)).toList(); | |
55 } | |
56 | |
57 final _quote = new RegExp(r"[+*?{}|[\]\\().^$-]"); | |
58 | |
59 /// Returns [contents] with characters that are meaningful in regular | |
60 /// expressions backslash-escaped. | |
61 String regExpQuote(String contents) => | |
62 contents.replaceAllMapped(_quote, (char) => "\\${char[0]}"); | |
63 | |
64 /// Returns [path] with all its separators replaced with forward slashes. | |
65 /// | |
66 /// This is useful when converting from Windows paths to globs. | |
67 String separatorToForwardSlash(String path) { | |
68 if (p.style != p.Style.windows) return path; | |
69 return path.replaceAll('\\', '/'); | |
70 } | |
71 | |
72 /// Returns [path] which follows [context] converted to the POSIX format that | |
73 /// globs match against. | |
74 String toPosixPath(p.Context context, String path) { | |
75 if (context.style == p.Style.windows) return path.replaceAll('\\', '/'); | |
76 if (context.style == p.Style.url) return Uri.decodeFull(path); | |
77 return path; | |
78 } | |
OLD | NEW |