@@ -32,6 +32,7 @@ import (
32
32
33
33
"github.com/sergi/go-diff/diffmatchpatch"
34
34
stdcharset "golang.org/x/net/html/charset"
35
+ "golang.org/x/text/encoding"
35
36
"golang.org/x/text/transform"
36
37
)
37
38
@@ -883,35 +884,46 @@ parsingLoop:
883
884
884
885
}
885
886
886
- // FIXME : There are numerous issues with this:
887
+ // TODO : There are numerous issues with this:
887
888
// - we might want to consider detecting encoding while parsing but...
888
889
// - we're likely to fail to get the correct encoding here anyway as we won't have enough information
889
- // - and this doesn't really account for changes in encoding
890
- var buf bytes.Buffer
890
+ var diffLineTypeBuffers = make (map [DiffLineType ]* bytes.Buffer , 3 )
891
+ var diffLineTypeDecoders = make (map [DiffLineType ]* encoding.Decoder , 3 )
892
+ diffLineTypeBuffers [DiffLinePlain ] = new (bytes.Buffer )
893
+ diffLineTypeBuffers [DiffLineAdd ] = new (bytes.Buffer )
894
+ diffLineTypeBuffers [DiffLineDel ] = new (bytes.Buffer )
891
895
for _ , f := range diff .Files {
892
- buf .Reset ()
896
+ for _ , buffer := range diffLineTypeBuffers {
897
+ buffer .Reset ()
898
+ }
893
899
for _ , sec := range f .Sections {
894
900
for _ , l := range sec .Lines {
895
901
if l .Type == DiffLineSection {
896
902
continue
897
903
}
898
- buf .WriteString (l .Content [1 :])
899
- buf .WriteString ("\n " )
904
+ diffLineTypeBuffers [ l . Type ] .WriteString (l .Content [1 :])
905
+ diffLineTypeBuffers [ l . Type ] .WriteString ("\n " )
900
906
}
901
907
}
902
- charsetLabel , err := charset .DetectEncoding (buf .Bytes ())
903
- if charsetLabel != "UTF-8" && err == nil {
904
- encoding , _ := stdcharset .Lookup (charsetLabel )
905
- if encoding != nil {
906
- d := encoding .NewDecoder ()
907
- for _ , sec := range f .Sections {
908
- for _ , l := range sec .Lines {
909
- if l .Type == DiffLineSection {
910
- continue
911
- }
912
- if c , _ , err := transform .String (d , l .Content [1 :]); err == nil {
913
- l .Content = l .Content [0 :1 ] + c
914
- }
908
+ for lineType , buffer := range diffLineTypeBuffers {
909
+ diffLineTypeDecoders [lineType ] = nil
910
+ if buffer .Len () == 0 {
911
+ continue
912
+ }
913
+ charsetLabel , err := charset .DetectEncoding (buffer .Bytes ())
914
+ if charsetLabel != "UTF-8" && err == nil {
915
+ encoding , _ := stdcharset .Lookup (charsetLabel )
916
+ if encoding != nil {
917
+ diffLineTypeDecoders [lineType ] = encoding .NewDecoder ()
918
+ }
919
+ }
920
+ }
921
+ for _ , sec := range f .Sections {
922
+ for _ , l := range sec .Lines {
923
+ decoder := diffLineTypeDecoders [l .Type ]
924
+ if decoder != nil {
925
+ if c , _ , err := transform .String (decoder , l .Content [1 :]); err == nil {
926
+ l .Content = l .Content [0 :1 ] + c
915
927
}
916
928
}
917
929
}
0 commit comments