@@ -20,16 +20,16 @@ function detectFileEncodingWithBOM(fileName: string, buffer: Buffer) {
2020 type = 'utf-8' ;
2121 }
2222 else if ( buffer . slice ( 0 , 4 ) . equals ( new Buffer ( [ 255 , 254 , 0 , 0 ] ) ) ) {
23- type = 'UTF-32LE ' ;
23+ type = 'utf-32le ' ;
2424 }
2525 else if ( buffer . slice ( 0 , 2 ) . equals ( new Buffer ( [ 254 , 255 ] ) ) ) {
26- type = 'UTF-16BE ' ;
26+ type = 'utf-16be ' ;
2727 }
2828 else if ( buffer . slice ( 0 , 2 ) . equals ( new Buffer ( [ 255 , 254 ] ) ) ) {
2929 type = 'utf-16le' ;
3030 }
3131 else if ( buffer . slice ( 0 , 4 ) . equals ( new Buffer ( [ 0 , 0 , 254 , 255 ] ) ) ) {
32- type = 'UTF-32BE ' ;
32+ type = 'utf-32be ' ;
3333 }
3434 else {
3535 tl . debug ( 'Unable to detect File encoding using BOM' ) ;
@@ -40,41 +40,48 @@ function detectFileEncodingWithBOM(fileName: string, buffer: Buffer) {
4040
4141function detectFileEncodingWithoutBOM ( fileName : string , buffer : Buffer ) {
4242 tl . debug ( 'Detecting file encoding without BOM' ) ;
43+ if ( buffer . length < 4 ) {
44+ tl . debug ( 'Short file buffer error on file ' + fileName + '. length: ' + buffer . length ) ;
45+ }
46+
4347 var typeCode = 0 ;
4448 var type : string ;
45- for ( var index = 0 ; index < 4 ; index ++ ) {
49+ var codeForUtf8 = 0
50+ for ( var index = 0 ; index < 4 && index < buffer . length ; index ++ ) {
4651 typeCode = typeCode << 1 ;
4752 typeCode = typeCode | ( buffer [ index ] > 0 ? 1 : 0 ) ;
53+ codeForUtf8 = codeForUtf8 << 1 ;
54+ codeForUtf8 ++ ;
4855 }
4956 switch ( typeCode ) {
5057 case 1 :
51- type = 'UTF-32BE ' ;
58+ type = 'utf-32be ' ;
5259 break ;
5360 case 5 :
54- type = 'UTF-16BE ' ;
61+ type = 'utf-16be ' ;
5562 break ;
5663 case 8 :
57- type = 'UTF-32LE ' ;
64+ type = 'utf-32le ' ;
5865 break ;
5966 case 10 :
6067 type = 'utf-16le' ;
6168 break ;
62- case 15 :
63- type = 'utf-8' ;
64- break ;
6569 default :
66- return null ;
70+ if ( codeForUtf8 == typeCode ) {
71+ type = 'utf-8' ;
72+ }
73+ else {
74+ return null ;
75+ }
6776 }
6877 return new FileEncoding ( type , false ) ;
6978}
7079export function detectFileEncoding ( fileName : string , buffer : Buffer ) : FileEncoding {
71- if ( buffer . length < 4 ) {
72- tl . debug ( tl . loc ( 'ShortFileBufferError' , fileName ) )
73- throw Error ( tl . loc ( "CouldNotDetectEncoding" , fileName ) ) ;
74- }
80+
7581 var fileEncoding : FileEncoding = detectFileEncodingWithBOM ( fileName , buffer ) ;
76- if ( fileEncoding == null )
82+ if ( fileEncoding == null ) {
7783 fileEncoding = detectFileEncodingWithoutBOM ( fileName , buffer ) ;
84+ }
7885
7986 if ( fileEncoding == null ) {
8087 throw new Error ( tl . loc ( "CouldNotDetectEncoding" , fileName ) ) ;
0 commit comments