File tree Expand file tree Collapse file tree 1 file changed +5
-7
lines changed Expand file tree Collapse file tree 1 file changed +5
-7
lines changed Original file line number Diff line number Diff line change @@ -37,14 +37,15 @@ impl<'a> Specifier<'a> {
37
37
let mut fragment_start: Option < usize > = None ;
38
38
39
39
let mut prev = specifier. chars ( ) . next ( ) . unwrap ( ) ;
40
- let mut escaped_indexes = vec ! [ ] ;
40
+ // Optimize for the common case: most specifiers don't have escaped characters
41
+ let mut escaped_indexes: Option < Vec < usize > > = None ;
41
42
for ( i, c) in specifier. char_indices ( ) . skip ( skip) {
42
43
if c == '?' && query_start. is_none ( ) {
43
44
query_start = Some ( i) ;
44
45
}
45
46
if c == '#' {
46
47
if prev == '\0' {
47
- escaped_indexes. push ( i - 1 ) ;
48
+ escaped_indexes. get_or_insert_with ( Vec :: new ) . push ( i - 1 ) ;
48
49
} else {
49
50
fragment_start = Some ( i) ;
50
51
break ;
@@ -63,17 +64,14 @@ impl<'a> Specifier<'a> {
63
64
_ => ( specifier, None , None ) ,
64
65
} ;
65
66
66
- let path = if escaped_indexes. is_empty ( ) {
67
- Cow :: Borrowed ( path)
68
- } else {
69
- // Remove the `\0` characters for a legal path.
67
+ let path = escaped_indexes. map_or ( Cow :: Borrowed ( path) , |escaped_indexes| {
70
68
Cow :: Owned (
71
69
path. chars ( )
72
70
. enumerate ( )
73
71
. filter_map ( |( i, c) | ( !escaped_indexes. contains ( & i) ) . then_some ( c) )
74
72
. collect :: < String > ( ) ,
75
73
)
76
- } ;
74
+ } ) ;
77
75
78
76
( path, query, fragment)
79
77
}
You can’t perform that action at this time.
0 commit comments