diff --git a/rtl/inc/astrings.inc b/rtl/inc/astrings.inc index f20aff7fb4..c3c423f18a 100644 --- a/rtl/inc/astrings.inc +++ b/rtl/inc/astrings.inc @@ -116,21 +116,17 @@ Function NewAnsiString(Len : SizeInt) : Pointer; Allocate a new AnsiString on the heap. initialize it to zero length and reference count 1. } -Var - P : Pointer; begin { request a multiple of 16 because the heap manager alloctes anyways chunks of 16 bytes } - GetMem(P,Len+(AnsiFirstOff+sizeof(AnsiChar))); - If P<>Nil then - begin - PAnsiRec(P)^.Ref:=1; { Set reference count } - PAnsiRec(P)^.Len:=0; { Initial length } - PAnsiRec(P)^.CodePage:=DefaultSystemCodePage; - PAnsiRec(P)^.ElementSize:=SizeOf(AnsiChar); - inc(p,AnsiFirstOff); { Points to string now } - PAnsiChar(P)^:=#0; { Terminating #0 } - end; - NewAnsiString:=P; + Result:=GetMem(Len+(AnsiFirstOff+sizeof(AnsiChar))); + If Result=Nil then + Exit; + PAnsiRec(Result)^.Ref:=1; { Set reference count } + PAnsiRec(Result)^.Len:=0; { Initial length } + PAnsiRec(Result)^.CodePage:=DefaultSystemCodePage; + PAnsiRec(Result)^.ElementSize:=SizeOf(AnsiChar); + inc(Result,AnsiFirstOff); { Points to string now } + PAnsiChar(Result)^:=#0; { Terminating #0 } end; {$endif FPC_HAS_NEWANSISTR} @@ -650,44 +646,20 @@ Function fpc_AnsiStr_Compare(const S1,S2 : RawByteString): SizeInt;[Public,Alias >0 if S1>S2 } Var - MaxI,Temp : SizeInt; - cp1,cp2 : TSystemCodePage; - r1,r2 : RawByteString; + Len1,Len2,CmpLen : SizeInt; begin - if pointer(S1)=pointer(S2) then - begin - result:=0; - exit; - end; - if (pointer(S1)=nil) then - begin - result:=-Length(S2); - exit; - end; - if (pointer(S2)=nil) then - begin - result:=Length(S1); - exit; - end; - cp1:=TranslatePlaceholderCP(StringCodePage(S1)); - cp2:=TranslatePlaceholderCP(StringCodePage(S2)); - if cp1=cp2 then - begin - Maxi:=Length(S1); - temp:=Length(S2); - If MaxI>Temp then - MaxI:=Temp; - if MaxI>0 then - begin - result:=CompareByte(S1[1],S2[1],MaxI); - if result=0 then - result:=Length(S1)-Length(S2); - end - else - result:=Length(S1)-Length(S2); - end - else - Result:=fpc_utf8_compare(s1,s2); + if (pointer(S1)=pointer(S2)) or (pointer(S1)=nil) or (pointer(S2)=nil) then + exit(ord(pointer(S1)<>nil)-ord(pointer(S2)<>nil)); + if TranslatePlaceholderCP(PAnsiRec(Pointer(S1)-AnsiFirstOff)^.CodePage)<>TranslatePlaceholderCP(PAnsiRec(Pointer(S2)-AnsiFirstOff)^.CodePage) then + exit(fpc_utf8_compare(s1,s2)); + Len1:=PAnsiRec(Pointer(S1)-AnsiFirstOff)^.Len; + Len2:=PAnsiRec(Pointer(S2)-AnsiFirstOff)^.Len; + CmpLen:=Len1; + If Len1>Len2 then + CmpLen:=Len2; + result:=CompareByte(S1[1],S2[1],CmpLen); + if result=0 then + result:=Len1-Len2; end; {$endif FPC_HAS_ANSISTR_COMPARE} @@ -722,29 +694,17 @@ Function fpc_AnsiStr_Compare_equal(const S1,S2 : RawByteString): SizeInt;[Public <>0 if S1<>S2 } Var - MaxI,Temp : SizeInt; - cp1,cp2 : TSystemCodePage; + MaxI : SizeInt; begin - if pointer(S1)=pointer(S2) then - Exit(0); { don't compare strings if one of them is empty } - if (pointer(S1)=nil) or (pointer(S2)=Nil) then - Exit(-1); - cp1:=TranslatePlaceholderCP(StringCodePage(S1)); - cp2:=TranslatePlaceholderCP(StringCodePage(S2)); - if cp1=cp2 then - begin - Maxi:=Length(S1); - temp:=Length(S2); - Result := Maxi - temp; - if Result = 0 then - if MaxI>0 then - result:=CompareByte(S1[1],S2[1],MaxI); - end - else - begin - Result:=fpc_utf8_Compare_equal(S1,S2); - end; + if (pointer(S1)=pointer(S2)) or (pointer(S1)=nil) or (pointer(S2)=nil) then + Exit(ord(pointer(S1)<>pointer(S2))); + if TranslatePlaceholderCP(PAnsiRec(Pointer(S1)-AnsiFirstOff)^.CodePage)<>TranslatePlaceholderCP(PAnsiRec(Pointer(S2)-AnsiFirstOff)^.CodePage) then + Exit(fpc_utf8_Compare_equal(S1,S2)); + Maxi:=PAnsiRec(Pointer(S1)-AnsiFirstOff)^.Len; + Result:=Maxi-PAnsiRec(Pointer(S2)-AnsiFirstOff)^.Len; + if Result=0 then + result:=CompareByte(S1[1],S2[1],MaxI); end; {$endif FPC_HAS_ANSISTR_COMPARE_EQUAL} diff --git a/rtl/inc/ustrings.inc b/rtl/inc/ustrings.inc index be3ccd22d8..7edddd4e5c 100644 --- a/rtl/inc/ustrings.inc +++ b/rtl/inc/ustrings.inc @@ -194,22 +194,16 @@ Function NewUnicodeString(Len : SizeInt) : Pointer; Allocate a new UnicodeString on the heap. initialize it to zero length and reference count 1. } -Var - P : Pointer; begin - GetMem(P,Len*sizeof(UnicodeChar)+(UnicodeFirstOff+sizeof(UnicodeChar))); - If P<>Nil then - begin - PUnicodeRec(P)^.Len:=Len; { Initial length } - PUnicodeRec(P)^.Ref:=1; { Initial Refcount } - PUnicodeRec(P)^.CodePage:=DefaultUnicodeCodePage; - PUnicodeRec(P)^.ElementSize:=SizeOf(UnicodeChar); - inc(p,UnicodeFirstOff); { Points to string now } - PUnicodeChar(P)^:=#0; { Terminating #0 } - end - else + Result:=GetMem(Len*sizeof(UnicodeChar)+(UnicodeFirstOff+sizeof(UnicodeChar))); + If Result=Nil then UnicodeStringError; - NewUnicodeString:=P; + PUnicodeRec(Result)^.Len:=Len; { Initial length } + PUnicodeRec(Result)^.Ref:=1; { Initial Refcount } + PUnicodeRec(Result)^.CodePage:=DefaultUnicodeCodePage; + PUnicodeRec(Result)^.ElementSize:=SizeOf(UnicodeChar); + inc(Result,UnicodeFirstOff); { Points to string now } + PUnicodeChar(Result)^:=#0; { Terminating #0 } end; {$endif FPC_HAS_NEW_UNICODESTRING}