2
2
3
3
use crate :: event:: { Event , Kind , Point } ;
4
4
use crate :: util:: constant:: TAB_SIZE ;
5
- use alloc:: string:: String ;
5
+ use alloc:: { format , string:: String , vec } ;
6
6
use core:: str;
7
7
8
8
/// A range between two points.
@@ -24,20 +24,19 @@ impl<'a> Position<'a> {
24
24
///
25
25
/// This function panics if an enter event is given.
26
26
/// When `markdown-rs` is used, this function never panics.
27
- #[ cfg_attr( tarpaulin, ignore) ]
28
27
pub fn from_exit_event ( events : & ' a [ Event ] , index : usize ) -> Position < ' a > {
29
- let exit = & events[ index] ;
30
- debug_assert_eq ! ( exit. kind, Kind :: Exit , "expected `exit` event" ) ;
31
- let mut enter_index = index - 1 ;
28
+ debug_assert_eq ! ( events[ index] . kind, Kind :: Exit , "expected `exit` event" ) ;
29
+ let end = & events[ index] . point ;
30
+ let name = & events[ index] . name ;
31
+ let mut index = index - 1 ;
32
32
33
- while events[ enter_index ] . kind != Kind :: Enter || events[ enter_index ] . name != exit . name {
34
- enter_index -= 1 ;
33
+ while ! ( events[ index ] . kind == Kind :: Enter && events[ index ] . name == * name) {
34
+ index -= 1 ;
35
35
}
36
36
37
- Position {
38
- start : & events[ enter_index] . point ,
39
- end : & exit. point ,
40
- }
37
+ let start = & events[ index] . point ;
38
+
39
+ Position { start, end }
41
40
}
42
41
43
42
/// Turn a position into indices.
@@ -118,19 +117,9 @@ impl<'a> Slice<'a> {
118
117
/// Turn the slice into a `String`.
119
118
///
120
119
/// Supports virtual spaces.
121
- #[ cfg_attr( tarpaulin, ignore) ]
122
120
pub fn serialize ( & self ) -> String {
123
- debug_assert_eq ! ( self . after, 0 , "expected no trailing vs" ) ;
124
- // If the above ever starts erroring, handle the same as `self.before`
125
- // above but with `self.after`.
126
- // It’d currently be unused code.
127
- let mut string = String :: with_capacity ( self . len ( ) ) ;
128
- let mut index = self . before ;
129
- while index > 0 {
130
- string. push ( ' ' ) ;
131
- index -= 1 ;
132
- }
133
- string. push_str ( self . as_str ( ) ) ;
134
- string
121
+ let prefix = String :: from_utf8 ( vec ! [ b' ' ; self . before] ) . unwrap ( ) ;
122
+ let suffix = String :: from_utf8 ( vec ! [ b' ' ; self . after] ) . unwrap ( ) ;
123
+ format ! ( "{}{}{}" , prefix, self . as_str( ) , suffix)
135
124
}
136
125
}
0 commit comments