LazUtils: Raise exception in UTF8To... functions, controlled by a global boolean. Issue #33686, patch from AlexeyT.

git-svn-id: trunk@59416 -
This commit is contained in:
juha 2018-11-01 08:39:09 +00:00
parent efdb5239d0
commit 07607b4bba
2 changed files with 20 additions and 20 deletions

View File

@ -14,11 +14,9 @@
function DBCSToUTF8(const s: string; CodeP: integer): string;
var
len: SizeInt;
Src: PChar;
Dest: PChar;
c: char;
l: Integer;
len, l: Integer;
Src, Dest: PChar;
c: char;
code: word;
begin
if s = '' then
@ -189,13 +187,10 @@ end;
function UTF8ToDBCS(const s: string; const UTF8CharConvFunc: TUnicodeToCharID): string;
var
len: integer;
Src: PChar;
Dest: PChar;
c: char;
len, i, CharLen: integer;
Src, Dest: PChar;
c: char;
Unicode: longword;
CharLen: integer;
i: integer;
begin
if s = '' then
begin
@ -232,7 +227,10 @@ begin
else
Dest^ := chr(i);
Inc(Dest);
end;
end
else
if ConvertEncodingFromUtf8RaisesException then
raise EConvertError.Create('Cannot convert UTF8 to DBCS code page');
end;
until false;
//SetLength(Result, Dest - PChar(Result));

View File

@ -31,6 +31,9 @@ uses
SysUtils, Classes, dos, LazUTF8
{$IFDEF EnableIconvEnc},iconvenc{$ENDIF};
var
ConvertEncodingFromUtf8RaisesException: boolean = False;
//encoding names
const
EncodingUTF8 = 'utf8';
@ -6998,16 +7001,12 @@ begin
end;
{$ENDIF}
function UTF8ToSingleByte(const s: string;
const UTF8CharConvFunc: TUnicodeToCharID): string;
function UTF8ToSingleByte(const s: string; const UTF8CharConvFunc: TUnicodeToCharID): string;
var
len: Integer;
Src: PChar;
Dest: PChar;
len, i, CharLen: Integer;
Src, Dest: PChar;
c: Char;
Unicode: LongWord;
CharLen: integer;
i: integer;
begin
if s='' then begin
Result:='';
@ -7033,7 +7032,10 @@ begin
if i>=0 then begin
Dest^:=chr(i);
inc(Dest);
end;
end
else
if ConvertEncodingFromUtf8RaisesException then
raise EConvertError.Create('Cannot convert UTF8 to single byte');
end;
end;
SetLength(Result,Dest-PChar(Result));