File tree 2 files changed +33
-6
lines changed
2 files changed +33
-6
lines changed Original file line number Diff line number Diff line change @@ -202,7 +202,17 @@ impl SourceMap {
202
202
203
203
/// Creates a new source_file.
204
204
/// This does not ensure that only one SourceFile exists per file name.
205
- pub fn new_source_file ( & self , filename : FileName , src : String ) -> Lrc < SourceFile > {
205
+ pub fn new_source_file ( & self , filename : FileName , mut src : String ) -> Lrc < SourceFile > {
206
+ remove_bom ( & mut src) ;
207
+
208
+ self . new_source_file_from ( filename, Lrc :: new ( src) )
209
+ }
210
+
211
+ /// Creates a new source_file.
212
+ /// This does not ensure that only one SourceFile exists per file name.
213
+ ///
214
+ /// `src` should not have UTF8 BOM
215
+ pub fn new_source_file_from ( & self , filename : FileName , src : Lrc < String > ) -> Lrc < SourceFile > {
206
216
// The path is used to determine the directory for loading submodules and
207
217
// include files, so it must be before remapping.
208
218
// Note that filename may not be a valid path, eg it may be `<anon>` etc,
@@ -224,7 +234,7 @@ impl SourceMap {
224
234
225
235
let start_pos = self . next_start_pos ( src. len ( ) ) ;
226
236
227
- let source_file = Lrc :: new ( SourceFile :: new (
237
+ let source_file = Lrc :: new ( SourceFile :: new_from (
228
238
filename,
229
239
was_remapped,
230
240
unmapped_path,
Original file line number Diff line number Diff line change @@ -869,15 +869,32 @@ impl SourceFile {
869
869
unmapped_path : FileName ,
870
870
mut src : String ,
871
871
start_pos : BytePos ,
872
+ ) -> SourceFile {
873
+ remove_bom ( & mut src) ;
874
+
875
+ Self :: new_from (
876
+ name,
877
+ name_was_remapped,
878
+ unmapped_path,
879
+ Lrc :: new ( src) ,
880
+ start_pos,
881
+ )
882
+ }
883
+
884
+ /// `src` should not have UTF8 BOM
885
+ pub fn new_from (
886
+ name : FileName ,
887
+ name_was_remapped : bool ,
888
+ unmapped_path : FileName ,
889
+ src : Lrc < String > ,
890
+ start_pos : BytePos ,
872
891
) -> SourceFile {
873
892
debug_assert_ne ! (
874
893
start_pos,
875
894
BytePos :: DUMMY ,
876
895
"BytePos::DUMMY is reserved and `SourceFile` should not use it"
877
896
) ;
878
897
879
- remove_bom ( & mut src) ;
880
-
881
898
let src_hash = {
882
899
let mut hasher: StableHasher = StableHasher :: new ( ) ;
883
900
hasher. write ( src. as_bytes ( ) ) ;
@@ -898,7 +915,7 @@ impl SourceFile {
898
915
name_was_remapped,
899
916
unmapped_path : Some ( unmapped_path) ,
900
917
crate_of_origin : 0 ,
901
- src : Lrc :: new ( src ) ,
918
+ src,
902
919
src_hash,
903
920
start_pos,
904
921
end_pos : Pos :: from_usize ( end_pos) ,
@@ -988,7 +1005,7 @@ impl SourceFile {
988
1005
}
989
1006
990
1007
/// Remove utf-8 BOM if any.
991
- fn remove_bom ( src : & mut String ) {
1008
+ pub ( super ) fn remove_bom ( src : & mut String ) {
992
1009
if src. starts_with ( '\u{feff}' ) {
993
1010
src. drain ( ..3 ) ;
994
1011
}
You can’t perform that action at this time.
0 commit comments