1
- /// TODO make trait of string
2
- /// Split string by prefix, including the prefix in the result.
3
- pub ( crate ) fn split_prefix_inclusive < ' a > ( string : & ' a str , prefix : & str ) -> Vec < & ' a str > {
4
- let matches = string. match_indices ( prefix) . map ( |( idx, _) | idx) ;
5
- let mut start = 0 ;
6
- let mut substrings = Vec :: new ( ) ;
7
- for idx in matches {
8
- if idx != start {
9
- substrings. push ( & string[ start..idx] ) ;
10
- start = idx;
1
+ pub ( crate ) trait SplitPrefixInclusive {
2
+ fn split_prefix_inclusive < ' a > ( & ' a self , prefix : & str ) -> Vec < & ' a str > ;
3
+ }
4
+
5
+ impl SplitPrefixInclusive for str {
6
+ /// Split string by prefix, including the prefix in the result.
7
+ fn split_prefix_inclusive < ' a > ( & ' a self , prefix : & str ) -> Vec < & ' a str > {
8
+ let matches = self . match_indices ( prefix) . map ( |( idx, _) | idx) ;
9
+ let mut start = 0 ;
10
+ let mut substrings = Vec :: new ( ) ;
11
+ for idx in matches {
12
+ if idx != start {
13
+ substrings. push ( & self [ start..idx] ) ;
14
+ start = idx;
15
+ }
11
16
}
12
- }
13
- substrings. push ( & string[ start..] ) ;
17
+ substrings. push ( & self [ start..] ) ;
14
18
15
- substrings
19
+ substrings
20
+ }
16
21
}
17
22
18
23
/// Finds the file name from a diff. The diff is expected to be of the form
@@ -33,16 +38,16 @@ mod tests {
33
38
fn test_split_prefix_inclusive ( ) {
34
39
let string = include_str ! ( "../tests/data/example_1.diff" ) ;
35
40
let pattern = "diff --git " ;
36
- assert_eq ! ( split_prefix_inclusive( string , pattern) . len( ) , 5 ) ;
41
+ assert_eq ! ( string . split_prefix_inclusive( pattern) . len( ) , 5 ) ;
37
42
}
38
43
39
44
#[ test]
40
45
fn test_basic_split_prefix_inclusive ( ) {
41
46
let string = "x111x222x333" ;
42
47
let pattern = "x" ;
43
- assert_eq ! ( split_prefix_inclusive( string , pattern) . len( ) , 3 ) ;
48
+ assert_eq ! ( string . split_prefix_inclusive( pattern) . len( ) , 3 ) ;
44
49
assert_eq ! (
45
- split_prefix_inclusive( string , pattern) ,
50
+ string . split_prefix_inclusive( pattern) ,
46
51
& [ "x111" , "x222" , "x333" ]
47
52
) ;
48
53
}
@@ -51,9 +56,9 @@ mod tests {
51
56
fn test_basic_split_prefix_inclusive_2 ( ) {
52
57
let string = "x111\n x222\n x333" ;
53
58
let pattern = "\n x" ;
54
- assert_eq ! ( split_prefix_inclusive( string , pattern) . len( ) , 3 ) ;
59
+ assert_eq ! ( string . split_prefix_inclusive( pattern) . len( ) , 3 ) ;
55
60
assert_eq ! (
56
- split_prefix_inclusive( string , pattern) ,
61
+ string . split_prefix_inclusive( pattern) ,
57
62
& [ "x111" , "\n x222" , "\n x333" ]
58
63
) ;
59
64
}
0 commit comments