* Ansi2UnicodeMove:

- Avoid calling Utf8ToUnicode with only three parameters,
  as this is not reliable if the ansitring contains embedded #0 chars.
  Use explicit high(SizeUInt) or allocated length explicitly.
  - Change destLen type to SizeUInt.
  * UpperAnsiString, LowerAnsiString:
  Also avoid call to Utf8ToUnicode with only three parameters.
  Change type of slen and ulen local variables to SizeUInt.
  * CompareStrAnsiString:
  Fix sign of return value if l2=0 (was returning negative value)

git-svn-id: trunk@38972 -
This commit is contained in:
pierre 2018-05-11 12:12:37 +00:00
parent b557bc5627
commit dfcb34aef2

View File

@ -303,7 +303,7 @@ end;
procedure Ansi2UnicodeMove(source:PAnsiChar; cp:TSystemCodePage; var dest:UnicodeString; len:SizeInt); procedure Ansi2UnicodeMove(source:PAnsiChar; cp:TSystemCodePage; var dest:UnicodeString; len:SizeInt);
var var
locMap : punicodemap; locMap : punicodemap;
destLen : SizeInt; destLen : SizeUInt;
begin begin
if (len<=0) then if (len<=0) then
begin begin
@ -313,12 +313,12 @@ begin
if (cp=CP_UTF8) then if (cp=CP_UTF8) then
begin begin
destLen:=Utf8ToUnicode(nil,source,len); destLen:=Utf8ToUnicode(nil,high(SizeUint),source,len);
if destLen > 0 then if destLen > 0 then
SetLength(dest,destLen-1) SetLength(dest,destLen-1)
else else
SetLength(dest,0); SetLength(dest,0);
Utf8ToUnicode(@dest[1],source,len); Utf8ToUnicode(@dest[1],destLen,source,len);
exit; exit;
end; end;
if (cp=CP_UTF16) then if (cp=CP_UTF16) then
@ -521,13 +521,12 @@ end;
function UpperAnsiString(const s : ansistring) : ansistring; function UpperAnsiString(const s : ansistring) : ansistring;
var var
p : PAnsiChar; p : PAnsiChar;
i, slen, i,resindex : SizeInt;
resindex : SizeInt;
mblen : SizeInt; mblen : SizeInt;
us,usl : UnicodeString; us,usl : UnicodeString;
locMap : punicodemap; locMap : punicodemap;
ulen,k, ulen,slen : SizeUint;
aalen,ai : SizeInt; k,aalen,ai : SizeInt;
aa : array[0..8] of AnsiChar; aa : array[0..8] of AnsiChar;
begin begin
if (Length(s)=0) then if (Length(s)=0) then
@ -535,14 +534,14 @@ begin
if (DefaultSystemCodePage=CP_UTF8) then if (DefaultSystemCodePage=CP_UTF8) then
begin begin
//convert to UnicodeString,uppercase,convert back to utf8 //convert to UnicodeString,uppercase,convert back to utf8
ulen:=Utf8ToUnicode(nil,@s[1],Length(s)); ulen:=Utf8ToUnicode(nil,high(SizeUint),@s[1],Length(s));
if ulen>0 then if ulen>0 then
SetLength(us,ulen-1); SetLength(us,ulen-1);
Utf8ToUnicode(@us[1],@s[1],Length(s)); Utf8ToUnicode(@us[1],ulen,@s[1],Length(s));
us:=UpperUnicodeString(us); us:=UpperUnicodeString(us);
ulen:=Length(us); ulen:=Length(us);
slen:=UnicodeToUtf8(nil,0,@us[1],ulen); slen:=UnicodeToUtf8(nil,high(SizeUInt),@us[1],ulen);
SetLength(Result,slen); SetLength(Result,slen);
UnicodeToUtf8(@Result[1],slen,@us[1],ulen); UnicodeToUtf8(@Result[1],slen,@us[1],ulen);
exit; exit;
@ -588,13 +587,12 @@ end;
function LowerAnsiString(const s : ansistring) : ansistring; function LowerAnsiString(const s : ansistring) : ansistring;
var var
p : PAnsiChar; p : PAnsiChar;
i, slen, i,resindex : SizeInt;
resindex : SizeInt;
mblen : SizeInt; mblen : SizeInt;
us,usl : UnicodeString; us,usl : UnicodeString;
locMap : punicodemap; locMap : punicodemap;
ulen,k, k,aalen,ai : SizeInt;
aalen,ai : SizeInt; slen, ulen : SizeUInt;
aa : array[0..8] of AnsiChar; aa : array[0..8] of AnsiChar;
begin begin
if (Length(s)=0) then if (Length(s)=0) then
@ -602,14 +600,14 @@ begin
if (DefaultSystemCodePage=CP_UTF8) then if (DefaultSystemCodePage=CP_UTF8) then
begin begin
//convert to UnicodeString,lowercase,convert back to utf8 //convert to UnicodeString,lowercase,convert back to utf8
ulen:=Utf8ToUnicode(nil,@s[1],Length(s)); ulen:=Utf8ToUnicode(nil,high(SizeUInt),@s[1],Length(s));
if ulen>0 then if ulen>0 then
SetLength(us,ulen-1); SetLength(us,ulen-1);
Utf8ToUnicode(@us[1],@s[1],Length(s)); Utf8ToUnicode(@us[1],ulen,@s[1],Length(s));
us:=LowerUnicodeString(us); us:=LowerUnicodeString(us);
ulen:=Length(us); ulen:=Length(us);
slen:=UnicodeToUtf8(nil,0,@us[1],ulen); slen:=UnicodeToUtf8(nil,high(SizeUInt),@us[1],ulen);
SetLength(Result,slen); SetLength(Result,slen);
UnicodeToUtf8(@Result[1],slen,@us[1],ulen); UnicodeToUtf8(@Result[1],slen,@us[1],ulen);
exit; exit;
@ -735,13 +733,8 @@ begin
exit(0); exit(0);
l1:=Length(S1); l1:=Length(S1);
l2:=Length(S2); l2:=Length(S2);
if (l1=0) then begin if (l1=0) or (l2=0) then
if (l2=0) then exit(l1-l2);
exit(0);
exit(-l2);
end;
if (l2=0) then
exit(-l1);
Result := InternalCompareStrAnsiString(@S1[1],@S2[1],l1,l2); Result := InternalCompareStrAnsiString(@S1[1],@S2[1],l1,l2);
end; end;