8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- /*!
12
- * Support for matching file paths against Unix shell style patterns.
13
- *
14
- * The `glob` and `glob_with` functions, in concert with the `Paths`
15
- * type, allow querying the filesystem for all files that match a particular
16
- * pattern - just like the libc `glob` function (for an example see the `glob`
17
- * documentation). The methods on the `Pattern` type provide functionality
18
- * for checking if individual paths match a particular pattern - in a similar
19
- * manner to the libc `fnmatch` function
20
- *
21
- * For consistency across platforms, and for Windows support, this module
22
- * is implemented entirely in Rust rather than deferring to the libc
23
- * `glob`/`fnmatch` functions.
24
- */
11
+ //! Support for matching file paths against Unix shell style patterns.
12
+ //!
13
+ //! The `glob` and `glob_with` functions, in concert with the `Paths`
14
+ //! type, allow querying the filesystem for all files that match a particular
15
+ //! pattern - just like the libc `glob` function (for an example see the `glob`
16
+ //! documentation). The methods on the `Pattern` type provide functionality
17
+ //! for checking if individual paths match a particular pattern - in a similar
18
+ //! manner to the libc `fnmatch` function
19
+ //! For consistency across platforms, and for Windows support, this module
20
+ //! is implemented entirely in Rust rather than deferring to the libc
21
+ //! `glob`/`fnmatch` functions.
25
22
26
23
#![ crate_name = "glob" ]
27
24
#![ experimental]
@@ -37,18 +34,15 @@ use std::io::fs;
37
34
use std:: path:: is_sep;
38
35
use std:: string:: String ;
39
36
40
- /**
41
- * An iterator that yields Paths from the filesystem that match a particular
42
- * pattern - see the `glob` function for more details.
43
- */
37
+ /// An iterator that yields Paths from the filesystem that match a particular
38
+ /// pattern - see the `glob` function for more details.
44
39
pub struct Paths {
45
40
dir_patterns : Vec < Pattern > ,
46
41
require_dir : bool ,
47
42
options : MatchOptions ,
48
43
todo : Vec < ( Path , uint ) > ,
49
44
}
50
45
51
- ///
52
46
/// Return an iterator that produces all the Paths that match the given pattern,
53
47
/// which may be absolute or relative to the current working directory.
54
48
///
@@ -80,17 +74,15 @@ pub fn glob(pattern: &str) -> Paths {
80
74
glob_with ( pattern, MatchOptions :: new ( ) )
81
75
}
82
76
83
- /**
84
- * Return an iterator that produces all the Paths that match the given pattern,
85
- * which may be absolute or relative to the current working directory.
86
- *
87
- * This function accepts Unix shell style patterns as described by `Pattern::new(..)`.
88
- * The options given are passed through unchanged to `Pattern::matches_with(..)` with
89
- * the exception that `require_literal_separator` is always set to `true` regardless of the
90
- * value passed to this function.
91
- *
92
- * Paths are yielded in alphabetical order, as absolute paths.
93
- */
77
+ /// Return an iterator that produces all the Paths that match the given pattern,
78
+ /// which may be absolute or relative to the current working directory.
79
+ ///
80
+ /// This function accepts Unix shell style patterns as described by `Pattern::new(..)`.
81
+ /// The options given are passed through unchanged to `Pattern::matches_with(..)` with
82
+ /// the exception that `require_literal_separator` is always set to `true` regardless of the
83
+ /// value passed to this function.
84
+ ///
85
+ /// Paths are yielded in alphabetical order, as absolute paths.
94
86
pub fn glob_with ( pattern : & str , options : MatchOptions ) -> Paths {
95
87
#[ cfg( windows) ]
96
88
fn check_windows_verbatim ( p : & Path ) -> bool { path:: windows:: is_verbatim ( p) }
@@ -190,9 +182,7 @@ fn list_dir_sorted(path: &Path) -> Option<Vec<Path>> {
190
182
}
191
183
}
192
184
193
- /**
194
- * A compiled Unix shell style pattern.
195
- */
185
+ /// A compiled Unix shell style pattern.
196
186
#[ deriving( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Default ) ]
197
187
pub struct Pattern {
198
188
tokens : Vec < PatternToken > ,
@@ -222,25 +212,23 @@ enum MatchResult {
222
212
223
213
impl Pattern {
224
214
225
- /**
226
- * This function compiles Unix shell style patterns: `?` matches any single
227
- * character, `*` matches any (possibly empty) sequence of characters and
228
- * `[...]` matches any character inside the brackets, unless the first
229
- * character is `!` in which case it matches any character except those
230
- * between the `!` and the `]`. Character sequences can also specify ranges
231
- * of characters, as ordered by Unicode, so e.g. `[0-9]` specifies any
232
- * character between 0 and 9 inclusive.
233
- *
234
- * The metacharacters `?`, `*`, `[`, `]` can be matched by using brackets
235
- * (e.g. `[?]`). When a `]` occurs immediately following `[` or `[!` then
236
- * it is interpreted as being part of, rather then ending, the character
237
- * set, so `]` and NOT `]` can be matched by `[]]` and `[!]]` respectively.
238
- * The `-` character can be specified inside a character sequence pattern by
239
- * placing it at the start or the end, e.g. `[abc-]`.
240
- *
241
- * When a `[` does not have a closing `]` before the end of the string then
242
- * the `[` will be treated literally.
243
- */
215
+ /// This function compiles Unix shell style patterns: `?` matches any single
216
+ /// character, `*` matches any (possibly empty) sequence of characters and
217
+ /// `[...]` matches any character inside the brackets, unless the first
218
+ /// character is `!` in which case it matches any character except those
219
+ /// between the `!` and the `]`. Character sequences can also specify ranges
220
+ /// of characters, as ordered by Unicode, so e.g. `[0-9]` specifies any
221
+ /// character between 0 and 9 inclusive.
222
+ ///
223
+ /// The metacharacters `?`, `*`, `[`, `]` can be matched by using brackets
224
+ /// (e.g. `[?]`). When a `]` occurs immediately following `[` or `[!` then
225
+ /// it is interpreted as being part of, rather then ending, the character
226
+ /// set, so `]` and NOT `]` can be matched by `[]]` and `[!]]` respectively.
227
+ /// The `-` character can be specified inside a character sequence pattern by
228
+ /// placing it at the start or the end, e.g. `[abc-]`.
229
+ ///
230
+ /// When a `[` does not have a closing `]` before the end of the string then
231
+ /// the `[` will be treated literally.
244
232
pub fn new ( pattern : & str ) -> Pattern {
245
233
246
234
let chars = pattern. chars ( ) . collect :: < Vec < _ > > ( ) ;
@@ -300,11 +288,9 @@ impl Pattern {
300
288
Pattern { tokens : tokens }
301
289
}
302
290
303
- /**
304
- * Escape metacharacters within the given string by surrounding them in
305
- * brackets. The resulting string will, when compiled into a `Pattern`,
306
- * match the input string and nothing else.
307
- */
291
+ /// Escape metacharacters within the given string by surrounding them in
292
+ /// brackets. The resulting string will, when compiled into a `Pattern`,
293
+ /// match the input string and nothing else.
308
294
pub fn escape ( s : & str ) -> String {
309
295
let mut escaped = String :: new ( ) ;
310
296
for c in s. chars ( ) {
@@ -323,46 +309,38 @@ impl Pattern {
323
309
escaped
324
310
}
325
311
326
- /**
327
- * Return if the given `str` matches this `Pattern` using the default
328
- * match options (i.e. `MatchOptions::new()`).
329
- *
330
- * # Example
331
- *
332
- * ```rust
333
- * use glob::Pattern;
334
- *
335
- * assert!(Pattern::new("c?t").matches("cat"));
336
- * assert!(Pattern::new("k[!e]tteh").matches("kitteh"));
337
- * assert!(Pattern::new("d*g").matches("doog"));
338
- * ```
339
- */
312
+ /// Return if the given `str` matches this `Pattern` using the default
313
+ /// match options (i.e. `MatchOptions::new()`).
314
+ ///
315
+ /// # Example
316
+ ///
317
+ /// ```rust
318
+ /// use glob::Pattern;
319
+ ///
320
+ /// assert!(Pattern::new("c?t").matches("cat"));
321
+ /// assert!(Pattern::new("k[!e]tteh").matches("kitteh"));
322
+ /// assert!(Pattern::new("d*g").matches("doog"));
323
+ /// ```
340
324
pub fn matches ( & self , str : & str ) -> bool {
341
325
self . matches_with ( str, MatchOptions :: new ( ) )
342
326
}
343
327
344
- /**
345
- * Return if the given `Path`, when converted to a `str`, matches this `Pattern`
346
- * using the default match options (i.e. `MatchOptions::new()`).
347
- */
328
+ /// Return if the given `Path`, when converted to a `str`, matches this `Pattern`
329
+ /// using the default match options (i.e. `MatchOptions::new()`).
348
330
pub fn matches_path ( & self , path : & Path ) -> bool {
349
331
// FIXME (#9639): This needs to handle non-utf8 paths
350
332
path. as_str ( ) . map_or ( false , |s| {
351
333
self . matches ( s)
352
334
} )
353
335
}
354
336
355
- /**
356
- * Return if the given `str` matches this `Pattern` using the specified match options.
357
- */
337
+ /// Return if the given `str` matches this `Pattern` using the specified match options.
358
338
pub fn matches_with ( & self , str : & str , options : MatchOptions ) -> bool {
359
339
self . matches_from ( None , str, 0 , options) == Match
360
340
}
361
341
362
- /**
363
- * Return if the given `Path`, when converted to a `str`, matches this `Pattern`
364
- * using the specified match options.
365
- */
342
+ /// Return if the given `Path`, when converted to a `str`, matches this `Pattern`
343
+ /// using the specified match options.
366
344
pub fn matches_path_with ( & self , path : & Path , options : MatchOptions ) -> bool {
367
345
// FIXME (#9639): This needs to handle non-utf8 paths
368
346
path. as_str ( ) . map_or ( false , |s| {
@@ -588,50 +566,40 @@ fn chars_eq(a: char, b: char, case_sensitive: bool) -> bool {
588
566
}
589
567
}
590
568
591
- /**
592
- * Configuration options to modify the behaviour of `Pattern::matches_with(..)`
593
- */
569
+
570
+ /// Configuration options to modify the behaviour of `Pattern::matches_with(..)`
594
571
#[ deriving( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Default ) ]
595
572
pub struct MatchOptions {
596
-
597
- /**
598
- * Whether or not patterns should be matched in a case-sensitive manner. This
599
- * currently only considers upper/lower case relationships between ASCII characters,
600
- * but in future this might be extended to work with Unicode.
601
- */
573
+ /// Whether or not patterns should be matched in a case-sensitive manner. This
574
+ /// currently only considers upper/lower case relationships between ASCII characters,
575
+ /// but in future this might be extended to work with Unicode.
602
576
pub case_sensitive : bool ,
603
577
604
- /**
605
- * If this is true then path-component separator characters (e.g. `/` on Posix)
606
- * must be matched by a literal `/`, rather than by `*` or `?` or `[...]`
607
- */
578
+ /// If this is true then path-component separator characters (e.g. `/` on Posix)
579
+ /// must be matched by a literal `/`, rather than by `*` or `?` or `[...]`
608
580
pub require_literal_separator : bool ,
609
581
610
- /**
611
- * If this is true then paths that contain components that start with a `.` will
612
- * not match unless the `.` appears literally in the pattern: `*`, `?` or `[...]`
613
- * will not match. This is useful because such files are conventionally considered
614
- * hidden on Unix systems and it might be desirable to skip them when listing files.
615
- */
582
+ /// If this is true then paths that contain components that start with a `.` will
583
+ /// not match unless the `.` appears literally in the pattern: `*`, `?` or `[...]`
584
+ /// will not match. This is useful because such files are conventionally considered
585
+ /// hidden on Unix systems and it might be desirable to skip them when listing files.
616
586
pub require_literal_leading_dot : bool
617
587
}
618
588
619
589
impl MatchOptions {
620
590
621
- /**
622
- * Constructs a new `MatchOptions` with default field values. This is used
623
- * when calling functions that do not take an explicit `MatchOptions` parameter.
624
- *
625
- * This function always returns this value:
626
- *
627
- * ```rust,ignore
628
- * MatchOptions {
629
- * case_sensitive: true,
630
- * require_literal_separator: false.
631
- * require_literal_leading_dot: false
632
- * }
633
- * ```
634
- */
591
+ /// Constructs a new `MatchOptions` with default field values. This is used
592
+ /// when calling functions that do not take an explicit `MatchOptions` parameter.
593
+ ///
594
+ /// This function always returns this value:
595
+ ///
596
+ /// ```rust,ignore
597
+ /// MatchOptions {
598
+ /// case_sensitive: true,
599
+ /// require_literal_separator: false.
600
+ /// require_literal_leading_dot: false
601
+ /// }
602
+ /// ```
635
603
pub fn new ( ) -> MatchOptions {
636
604
MatchOptions {
637
605
case_sensitive : true ,
0 commit comments