@@ -35,7 +35,7 @@ pub fn get_map<'a, S: StreamChunks<'a>>(
35
35
mappings. push ( mapping) ;
36
36
} ,
37
37
// on_source
38
- & mut |source_index, source : & str , source_content : Option < & str > | {
38
+ & mut |source_index, source, source_content| {
39
39
let source_index = source_index as usize ;
40
40
if sources. len ( ) <= source_index {
41
41
sources. resize ( source_index + 1 , Cow :: Borrowed ( "" ) ) ;
@@ -49,7 +49,7 @@ pub fn get_map<'a, S: StreamChunks<'a>>(
49
49
}
50
50
} ,
51
51
// on_name
52
- & mut |name_index, name : & str | {
52
+ & mut |name_index, name| {
53
53
let name_index = name_index as usize ;
54
54
if names. len ( ) <= name_index {
55
55
names. resize ( name_index + 1 , Cow :: Borrowed ( "" ) ) ;
@@ -75,10 +75,10 @@ pub trait StreamChunks<'a> {
75
75
}
76
76
77
77
/// [OnChunk] abstraction, see [webpack-sources onChunk](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
78
- pub type OnChunk < ' a , ' b > = & ' a mut dyn FnMut ( Option < & ' b str > , Mapping ) ;
78
+ pub type OnChunk < ' a , ' b > = & ' a mut dyn FnMut ( Option < Cow < ' b , str > > , Mapping ) ;
79
79
80
80
/// [OnSource] abstraction, see [webpack-sources onSource](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
81
- pub type OnSource < ' a , ' b > = & ' a mut dyn FnMut ( u32 , & str , Option < & ' b str > ) ;
81
+ pub type OnSource < ' a , ' b > = & ' a mut dyn FnMut ( u32 , Cow < ' b , str > , Option < & ' b str > ) ;
82
82
83
83
/// [OnName] abstraction, see [webpack-sources onName](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
84
84
pub type OnName < ' a , ' b > = & ' a mut dyn FnMut ( u32 , & ' b str ) ;
@@ -519,7 +519,7 @@ pub fn stream_chunks_of_raw_source<'a>(
519
519
let mut last_line = None ;
520
520
for l in split_into_lines ( source) {
521
521
on_chunk (
522
- Some ( l ) ,
522
+ Some ( Cow :: Borrowed ( l ) ) ,
523
523
Mapping {
524
524
generated_line : line,
525
525
generated_column : 0 ,
@@ -580,13 +580,13 @@ pub fn stream_chunks_of_source_map<'a>(
580
580
}
581
581
}
582
582
583
- fn get_source ( source_map : & SourceMap , source : & str ) -> String {
583
+ fn get_source < ' a > ( source_map : & SourceMap , source : & ' a str ) -> Cow < ' a , str > {
584
584
let source_root = source_map. source_root ( ) ;
585
585
match source_root {
586
- Some ( root) if root. is_empty ( ) => source . to_string ( ) ,
587
- Some ( root) if root. ends_with ( '/' ) => format ! ( "{}{}" , root, source) ,
588
- Some ( root) => format ! ( "{}/{}" , root, source) ,
589
- None => source . to_string ( ) ,
586
+ Some ( root) if root. is_empty ( ) => Cow :: Borrowed ( source ) ,
587
+ Some ( root) if root. ends_with ( '/' ) => Cow :: Owned ( format ! ( "{}{}" , root, source) ) ,
588
+ Some ( root) => Cow :: Owned ( format ! ( "{}/{}" , root, source) ) ,
589
+ None => Cow :: Borrowed ( source ) ,
590
590
}
591
591
}
592
592
@@ -604,7 +604,7 @@ fn stream_chunks_of_source_map_final<'a>(
604
604
for ( i, source) in source_map. sources ( ) . iter ( ) . enumerate ( ) {
605
605
on_source (
606
606
i as u32 ,
607
- & get_source ( source_map, source) ,
607
+ get_source ( source_map, source) ,
608
608
source_map. get_source_content ( i) ,
609
609
)
610
610
}
@@ -656,7 +656,7 @@ fn stream_chunks_of_source_map_full<'a>(
656
656
for ( i, source) in source_map. sources ( ) . iter ( ) . enumerate ( ) {
657
657
on_source (
658
658
i as u32 ,
659
- & get_source ( source_map, source) ,
659
+ get_source ( source_map, source) ,
660
660
source_map. get_source_content ( i) ,
661
661
)
662
662
}
@@ -684,7 +684,7 @@ fn stream_chunks_of_source_map_full<'a>(
684
684
let chunk = & source[ tracking_generated_index..current_generated_index] ;
685
685
if !chunk. is_empty ( ) {
686
686
on_chunk (
687
- Some ( chunk) ,
687
+ Some ( Cow :: Borrowed ( chunk) ) ,
688
688
Mapping {
689
689
generated_line : tracking_generated_line,
690
690
generated_column : tracking_generated_column,
@@ -710,7 +710,7 @@ fn stream_chunks_of_source_map_full<'a>(
710
710
let chunk =
711
711
& source[ tracking_generated_index..current_generated_index + 1 ] ;
712
712
on_chunk (
713
- Some ( chunk) ,
713
+ Some ( Cow :: Borrowed ( chunk) ) ,
714
714
Mapping {
715
715
generated_line : tracking_generated_line,
716
716
generated_column : tracking_generated_column,
@@ -732,7 +732,7 @@ fn stream_chunks_of_source_map_full<'a>(
732
732
if tracking_generated_index < source. len ( ) {
733
733
let chunk = & source[ tracking_generated_index..] ;
734
734
on_chunk (
735
- Some ( chunk) ,
735
+ Some ( Cow :: Borrowed ( chunk) ) ,
736
736
Mapping {
737
737
generated_line : tracking_generated_line,
738
738
generated_column : tracking_generated_column,
@@ -764,7 +764,7 @@ fn stream_chunks_of_source_map_lines_final<'a>(
764
764
for ( i, source) in source_map. sources ( ) . iter ( ) . enumerate ( ) {
765
765
on_source (
766
766
i as u32 ,
767
- & get_source ( source_map, source) ,
767
+ get_source ( source_map, source) ,
768
768
source_map. get_source_content ( i) ,
769
769
)
770
770
}
@@ -819,7 +819,7 @@ fn stream_chunks_of_source_map_lines_full<'a>(
819
819
for ( i, source) in source_map. sources ( ) . iter ( ) . enumerate ( ) {
820
820
on_source (
821
821
i as u32 ,
822
- & get_source ( source_map, source) ,
822
+ get_source ( source_map, source) ,
823
823
source_map. get_source_content ( i) ,
824
824
)
825
825
}
@@ -833,8 +833,9 @@ fn stream_chunks_of_source_map_lines_full<'a>(
833
833
}
834
834
while mapping. generated_line > current_generated_line {
835
835
if current_generated_line as usize <= lines. len ( ) {
836
+ let chunk = lines[ current_generated_line as usize - 1 ] ;
836
837
on_chunk (
837
- Some ( lines [ current_generated_line as usize - 1 ] ) ,
838
+ Some ( Cow :: Borrowed ( chunk ) ) ,
838
839
Mapping {
839
840
generated_line : current_generated_line,
840
841
generated_column : 0 ,
@@ -848,8 +849,9 @@ fn stream_chunks_of_source_map_lines_full<'a>(
848
849
. original
849
850
. filter ( |_| mapping. generated_line as usize <= lines. len ( ) )
850
851
{
852
+ let chunk = lines[ current_generated_line as usize - 1 ] ;
851
853
on_chunk (
852
- Some ( lines [ mapping . generated_line as usize - 1 ] ) ,
854
+ Some ( Cow :: Borrowed ( chunk ) ) ,
853
855
Mapping {
854
856
generated_line : mapping. generated_line ,
855
857
generated_column : 0 ,
@@ -868,8 +870,9 @@ fn stream_chunks_of_source_map_lines_full<'a>(
868
870
on_mapping ( mapping) ;
869
871
}
870
872
while current_generated_line as usize <= lines. len ( ) {
873
+ let chunk = lines[ current_generated_line as usize - 1 ] ;
871
874
on_chunk (
872
- Some ( lines [ current_generated_line as usize - 1 ] ) ,
875
+ Some ( Cow :: Borrowed ( chunk ) ) ,
873
876
Mapping {
874
877
generated_line : current_generated_line,
875
878
generated_column : 0 ,
@@ -900,24 +903,26 @@ struct SourceMapLineData<'a> {
900
903
901
904
#[ derive( Debug ) ]
902
905
struct SourceMapLineChunk < ' a > {
903
- content : & ' a str ,
904
- cached : OnceCell < WithIndices < & ' a str > > ,
906
+ content : Cow < ' a , str > ,
907
+ cached : OnceCell < WithIndices < Cow < ' a , str > > > ,
905
908
}
906
909
907
910
impl < ' a > SourceMapLineChunk < ' a > {
908
- pub fn new ( content : & ' a str ) -> Self {
911
+ pub fn new ( content : Cow < ' a , str > ) -> Self {
909
912
Self {
910
913
content,
911
914
cached : OnceCell :: new ( ) ,
912
915
}
913
916
}
914
917
915
918
pub fn substring ( & self , start_index : usize , end_index : usize ) -> & str {
916
- let cached = self . cached . get_or_init ( || WithIndices :: new ( self . content ) ) ;
919
+ let cached = self . cached . get_or_init ( || WithIndices :: new ( self . content . clone ( ) ) ) ;
917
920
cached. substring ( start_index, end_index)
918
921
}
919
922
}
920
923
924
+ type InnerSourceIndexValueMapping < ' a > = HashMap < i64 , ( Cow < ' a , str > , Option < & ' a str > ) > ;
925
+
921
926
#[ allow( clippy:: too_many_arguments) ]
922
927
pub fn stream_chunks_of_combined_source_map < ' a > (
923
928
source : & ' a str ,
@@ -933,7 +938,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
933
938
) -> GeneratedInfo {
934
939
let on_source = RefCell :: new ( on_source) ;
935
940
let inner_source: RefCell < Option < & str > > = RefCell :: new ( inner_source) ;
936
- let source_mapping: RefCell < HashMap < String , u32 > > =
941
+ let source_mapping: RefCell < HashMap < Cow < str > , u32 > > =
937
942
RefCell :: new ( HashMap :: default ( ) ) ;
938
943
let mut name_mapping: HashMap < & str , u32 > = HashMap :: default ( ) ;
939
944
let source_index_mapping: RefCell < HashMap < i64 , i64 > > =
@@ -945,9 +950,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
945
950
let inner_source_index: RefCell < i64 > = RefCell :: new ( -2 ) ;
946
951
let inner_source_index_mapping: RefCell < HashMap < i64 , i64 > > =
947
952
RefCell :: new ( HashMap :: default ( ) ) ;
948
- let inner_source_index_value_mapping: RefCell <
949
- HashMap < i64 , ( String , Option < & str > ) > ,
950
- > = RefCell :: new ( HashMap :: default ( ) ) ;
953
+ let inner_source_index_value_mapping: RefCell < InnerSourceIndexValueMapping > = RefCell :: new ( HashMap :: default ( ) ) ;
951
954
let inner_source_contents: RefCell < HashMap < i64 , Option < & str > > > =
952
955
RefCell :: new ( HashMap :: default ( ) ) ;
953
956
let inner_source_content_lines: InnerSourceContentLine =
@@ -1080,11 +1083,11 @@ pub fn stream_chunks_of_combined_source_map<'a>(
1080
1083
. unwrap_or ( ( "" . into ( ) , None ) ) ;
1081
1084
let mut source_mapping = source_mapping. borrow_mut ( ) ;
1082
1085
let mut global_index =
1083
- source_mapping. get ( & source. to_string ( ) ) . copied ( ) ;
1086
+ source_mapping. get ( & source) . copied ( ) ;
1084
1087
if global_index. is_none ( ) {
1085
1088
let len = source_mapping. len ( ) as u32 ;
1086
- source_mapping. insert ( source. to_string ( ) , len) ;
1087
- on_source. borrow_mut ( ) ( len, & source, source_content) ;
1089
+ source_mapping. insert ( source. clone ( ) , len) ;
1090
+ on_source. borrow_mut ( ) ( len, source, source_content) ;
1088
1091
global_index = Some ( len) ;
1089
1092
}
1090
1093
source_index = global_index. unwrap ( ) as i64 ;
@@ -1225,7 +1228,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
1225
1228
source_mapping. insert ( source. into ( ) , len) ;
1226
1229
on_source. borrow_mut ( ) (
1227
1230
len,
1228
- inner_source_name,
1231
+ Cow :: Borrowed ( inner_source_name) ,
1229
1232
* inner_source. borrow ( ) ,
1230
1233
) ;
1231
1234
global_index = Some ( len) ;
@@ -1360,7 +1363,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
1360
1363
inner_source_index_mapping. borrow_mut ( ) . insert ( i, -2 ) ;
1361
1364
inner_source_index_value_mapping
1362
1365
. borrow_mut ( )
1363
- . insert ( i, ( source. to_string ( ) , source_content) ) ;
1366
+ . insert ( i, ( source, source_content) ) ;
1364
1367
} ,
1365
1368
& mut |i, name| {
1366
1369
let i = i as i64 ;
@@ -1374,10 +1377,10 @@ pub fn stream_chunks_of_combined_source_map<'a>(
1374
1377
) ;
1375
1378
} else {
1376
1379
let mut source_mapping = source_mapping. borrow_mut ( ) ;
1377
- let mut global_index = source_mapping. get ( source) . copied ( ) ;
1380
+ let mut global_index = source_mapping. get ( & source) . copied ( ) ;
1378
1381
if global_index. is_none ( ) {
1379
1382
let len = source_mapping. len ( ) as u32 ;
1380
- source_mapping. insert ( source. to_string ( ) , len) ;
1383
+ source_mapping. insert ( source. clone ( ) , len) ;
1381
1384
on_source. borrow_mut ( ) ( len, source, source_content) ;
1382
1385
global_index = Some ( len) ;
1383
1386
}
0 commit comments