Skip to content

Commit 280d9fd

Browse files
Robert Di PardoRobert Di Pardo
authored andcommitted
Restore entity (d/en)coding for ANSI documents
Since this project came under new maintenance, the focus has been on the stability of x64 releases Back in v1.1, the (d/en)coding procedures queried the document's code page before every transformation. This was implemented by a pair of complementary overloaded functions Each function made a call to WideCharToMultiByte or MultiByteToWideChar, as appropriate. This became the source of access violations in 64-bit Notepad++, sometime after 7.7 was released: - https://fossil.2of4.net/npp_htmltag/tktview?name=c3c733e21f - https://community.notepad-plus-plus.org/topic/18488/htmltag-1-1-plugin-broken-since-npp-7-7 087872a resolved that issue -- at the cost of properly encoding multi-byte text. Preventing a crash was the only priority at the time, as v1.1 had been rendered completely unusable in 64-bit editors by a breaking change to the upstream ABI: - https://sourceforge.net/p/npp-plugins/discussion/725650/thread/08c8e87447 d2189a1 made another quick fix, one that preferred Unicode at the expense of ANSI: #3 This makes the encoding of text selections depend on the document's code page, the way it did before v1.2
1 parent eb36397 commit 280d9fd

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

src/LibNppPlugin/NppSimpleObjects.pas

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,12 @@ procedure TTextRange.SetText(const AValue: WideString);
391391
Chars: AnsiString;
392392
TxtRng: Integer;
393393
begin
394-
Chars := UTF8Encode(AValue);
394+
case FEditor.SendMessage(SCI_GETCODEPAGE) of
395+
SC_CP_UTF8:
396+
Chars := UTF8Encode(AValue)
397+
else
398+
Chars := AValue;
399+
end;
395400
TxtRng := System.Length(Chars);
396401
FEditor.SendMessage(SCI_SETTARGETSTART, FStartPos);
397402
FEditor.SendMessage(SCI_SETTARGETEND, FEndPos);
@@ -568,7 +573,12 @@ function TSelection.GetText: WideString;
568573
begin
569574
Chars := AnsiString(StringOfChar(#0, Self.GetLength + 1));
570575
FEditor.SendMessage(SCI_GETSELTEXT, 0, PAnsiChar(Chars));
571-
Result := WideString(UTF8Decode(Chars));
576+
case FEditor.SendMessage(SCI_GETCODEPAGE) of
577+
SC_CP_UTF8:
578+
Result := WideString(UTF8Decode(Chars))
579+
else
580+
Result := WideString(UTF8Encode(Chars))
581+
end;
572582
end;
573583
{ ------------------------------------------------------------------------------------------------ }
574584
procedure TSelection.SetText(const AValue: WideString);
@@ -577,7 +587,12 @@ procedure TSelection.SetText(const AValue: WideString);
577587
NewLength, EndPos: Sci_Position;
578588
Reversed: boolean;
579589
begin
580-
Chars := UTF8Encode(AValue);
590+
case FEditor.SendMessage(SCI_GETCODEPAGE) of
591+
SC_CP_UTF8:
592+
Chars := UTF8Encode(AValue)
593+
else
594+
Chars := AValue;
595+
end;
581596
NewLength := System.Length(Chars) - 1;
582597
Reversed := (Self.Anchor > Self.GetCurrentPos);
583598
FEditor.SendMessage(SCI_REPLACESEL, 0, PAnsiChar(Chars));

0 commit comments

Comments
 (0)