From 8ca568fad2423479cd85324db89ef6a9d2c7a93d Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Wed, 19 Oct 2022 15:25:33 +0000 Subject: [PATCH] fpspreadsheet: Simplify ValidXMLText(). git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8573 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../source/common/fpsxmlcommon.pas | 25 ++++--------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/components/fpspreadsheet/source/common/fpsxmlcommon.pas b/components/fpspreadsheet/source/common/fpsxmlcommon.pas index 8e266c27b..d12339e9f 100644 --- a/components/fpspreadsheet/source/common/fpsxmlcommon.pas +++ b/components/fpspreadsheet/source/common/fpsxmlcommon.pas @@ -47,7 +47,7 @@ function GetNodeValue(ANode: TDOMNode): String; function LineEndingToBR(const AText: String): String; function UTF8TextToXMLText(AText: string; ProcessLineEndings: Boolean = false): string; function ValidXMLText(var AText: string; ReplaceSpecialChars: Boolean = true; - ProcessLineEndings: Boolean = false): Boolean; + ProcessLineEndings: Boolean = false; InvalidUTF8Replacement: String = #$E2#$8E#$95): Boolean; function XMLQuote(AText: String): String; procedure UnzipFile(AZipFileName, AZippedFile, ADestFolder: String); @@ -202,30 +202,15 @@ end; codes (e.g. '>' --> '>') @param ProcessLineEndings If TRUE line ending characters are replaced by their HTML entities. + @param InvalidUTF8Replacement UTF8-character inserted for a malformed UTF8 codepoint. @return FALSE if characters < #32 were replaced, TRUE otherwise. -------------------------------------------------------------------------------} function ValidXMLText(var AText: string; ReplaceSpecialChars: Boolean = true; - ProcessLineEndings: Boolean = false): Boolean; -const - BOX = #$E2#$8E#$95; -var - i: Integer; - P: PChar; + ProcessLineEndings: Boolean = false; + InvalidUTF8Replacement: String = #$E2#$8E#$95): Boolean; begin - Result := true; - - repeat - P := PChar(AText); - i := FindInvalidUTF8CodePoint(P, Length(AText), true); - if i >= 0 then - begin - Delete(AText, i+1, 1); - Insert(BOX, AText, i+1); - Result := false; - end; - until (i < 0); - + Result := ValidUTF8Text(AText, InvalidUTF8Replacement); if ReplaceSpecialChars then AText := UTF8TextToXMLText(AText, ProcessLineEndings); end;