1
+ #![ expect( dead_code) ] // (removed later in this PR)
2
+
3
+ use std:: fmt;
4
+
1
5
const COMPILETEST_DIRECTIVE_PREFIX : & str = "//@" ;
2
6
3
7
/// If the given line begins with the appropriate comment prefix for a directive,
@@ -28,7 +32,10 @@ pub(crate) fn line_directive<'line>(
28
32
raw_directive = after_comment;
29
33
} ;
30
34
31
- Some ( DirectiveLine { line_number, revision, raw_directive } )
35
+ // The directive name ends at the first occurrence of colon, space, or end-of-string.
36
+ let name = raw_directive. split ( [ ':' , ' ' ] ) . next ( ) . expect ( "split is never empty" ) ;
37
+
38
+ Some ( DirectiveLine { line_number, revision, raw_directive, name } )
32
39
}
33
40
34
41
/// The (partly) broken-down contents of a line containing a test directive,
@@ -39,10 +46,12 @@ pub(crate) fn line_directive<'line>(
39
46
/// ```text
40
47
/// //@ compile-flags: -O
41
48
/// ^^^^^^^^^^^^^^^^^ raw_directive
49
+ /// ^^^^^^^^^^^^^ name
42
50
///
43
51
/// //@ [foo] compile-flags: -O
44
52
/// ^^^ revision
45
53
/// ^^^^^^^^^^^^^^^^^ raw_directive
54
+ /// ^^^^^^^^^^^^^ name
46
55
/// ```
47
56
pub ( crate ) struct DirectiveLine < ' ln > {
48
57
pub ( crate ) line_number : usize ,
@@ -58,10 +67,44 @@ pub(crate) struct DirectiveLine<'ln> {
58
67
/// This is "raw" because the directive's name and colon-separated value
59
68
/// (if present) have not yet been extracted or checked.
60
69
pub ( crate ) raw_directive : & ' ln str ,
70
+
71
+ /// Name of the directive.
72
+ ///
73
+ /// Invariant: `self.raw_directive.starts_with(self.name)`
74
+ pub ( crate ) name : & ' ln str ,
61
75
}
62
76
63
77
impl < ' ln > DirectiveLine < ' ln > {
64
78
pub ( crate ) fn applies_to_test_revision ( & self , test_revision : Option < & str > ) -> bool {
65
79
self . revision . is_none ( ) || self . revision == test_revision
66
80
}
81
+
82
+ /// Helper method used by `value_after_colon` and `remark_after_space`.
83
+ /// Don't call this directly.
84
+ fn rest_after_separator ( & self , separator : u8 ) -> Option < & ' ln str > {
85
+ let n = self . name . len ( ) ;
86
+ if self . raw_directive . as_bytes ( ) . get ( n) != Some ( & separator) {
87
+ return None ;
88
+ }
89
+
90
+ Some ( & self . raw_directive [ n + 1 ..] )
91
+ }
92
+
93
+ /// If this directive uses `name: value` syntax, returns the part after
94
+ /// the colon character.
95
+ pub ( crate ) fn value_after_colon ( & self ) -> Option < & ' ln str > {
96
+ self . rest_after_separator ( b':' )
97
+ }
98
+
99
+ /// If this directive uses `name remark` syntax, returns the part after
100
+ /// the separating space.
101
+ pub ( crate ) fn remark_after_space ( & self ) -> Option < & ' ln str > {
102
+ self . rest_after_separator ( b' ' )
103
+ }
104
+
105
+ /// Allows callers to print `raw_directive` if necessary,
106
+ /// without accessing the field directly.
107
+ pub ( crate ) fn display ( & self ) -> impl fmt:: Display {
108
+ self . raw_directive
109
+ }
67
110
}
0 commit comments