123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770 |
- {
- $Id$
- This file is part of the Free Pascal run time library.
- Copyright (c) 1993,97 by Michael Van Canneyt,
- member of the Free Pascal development team.
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- { ---------------------------------------------------------------------
- This file implements AnsiStrings for FPC
- ---------------------------------------------------------------------}
- {
- This file contains the implementation of the LongString type,
- and all things that are needed for it.
- AnsiSTring is defined as a 'silent' pchar :
- a pchar that points to :
- @-12 : Longint for maximum size;
- @-8 : Longint for size;
- @-4 : Longint for reference count;
- @ : String + Terminating #0;
- Pchar(Ansistring) is a valid typecast.
- So AS[i] is converted to the address @AS+i-1.
- Constants should be assigned a reference count of -1
- Meaning that they can't be disposed of.
- }
- // Type shortstring=string;
- Function NewAnsiString (Len : Longint) : Pointer; forward;
- Procedure DisposeAnsiString (Var S : Pointer); forward;
- Procedure Decr_Ansi_Ref (Var S : Pointer); forward;
- Procedure Incr_Ansi_Ref (Var S : Pointer); forward;
- Procedure AssignAnsiString (Var S1 : Pointer; S2 : Pointer); forward;
- Procedure Ansi_String_Concat (Var S1 : Pointer; Var S2 : Pointer); forward;
- Procedure Ansi_ShortString_Concat (Var S1: AnsiString; Var S2 : ShortString); forward;
- Procedure Ansi_To_ShortString (Var S1 : ShortString; S2 : Pointer; maxlen : longint); forward;
- Procedure Short_To_AnsiString (Var S1 : Pointer; Const S2 : ShortString); forward;
- Function AnsiCompare (Var S1,S2 : Pointer): Longint; forward;
- Function AnsiCompare (var S1 : Pointer; Var S2 : ShortString): Longint; forward;
- Procedure SetCharAtIndex (Var S : AnsiString; Index : Longint; C : CHar); forward;
- {$PACKRECORDS 1}
- Type TAnsiRec = Record
- Maxlen, len, ref : Longint;
- First : Char;
- end;
- PAnsiRec = ^TAnsiRec;
- Const AnsiRecLen = SizeOf(TAnsiRec);
- FirstOff = SizeOf(TAnsiRec)-1;
- { ---------------------------------------------------------------------
- Internal functions, not in interface.
- ---------------------------------------------------------------------}
- Procedure DumpAnsiRec(S : Pointer);
- begin
- If S=Nil then
- Writeln ('String is nil')
- Else
- Begin
- With PAnsiRec(S-Firstoff)^ do
- begin
- Writeln ('Maxlen : ',maxlen);
- Writeln ('Len : ',len);
- Writeln ('Ref : ',ref);
- end;
- end;
- end;
- Function NewAnsiString(Len : Longint) : Pointer;
- {
- Allocate a new AnsiString on the heap.
- initialize it to zero length and reference count 1.
- }
- Var P : Pointer;
- begin
- GetMem(P,Len+AnsiRecLen);
- If P<>Nil then
- begin
- PAnsiRec(P)^.Maxlen:=Len; { Maximal length }
- PAnsiRec(P)^.Len:=0; { Initial length }
- PAnsiRec(P)^.Ref:=1; { Set reference count }
- PAnsiRec(P)^.First:=#0; { Terminating #0 }
- P:=P+FirstOff; { Points to string now }
- end;
- NewAnsiString:=P;
- end;
- Procedure DisposeAnsiString(Var S : Pointer);
- {
- Deallocates a AnsiString From the heap.
- }
- begin
- // Writeln ('In disposeAnsiSTring');
- If S=Nil
- then exit;
- Dec (Longint(S),FirstOff);
- FreeMem (S,PAnsiRec(S)^.Maxlen+AnsiRecLen);
- S:=Nil;
- end;
- Procedure Decr_Ansi_Ref (Var S : Pointer);
- [Public,Alias : 'FPC_DECR_ANSI_REF'];
- {
- Decreases the ReferenceCount of a non constant ansistring;
- If the reference count is zero, deallocate the string;
- }
- Type plongint = ^longint;
- Var l : plongint;
- Begin
- // dumpansirec(s);
- { Zero string }
- If S=Nil then
- exit;
- { check for constant strings ...}
- l:=S-FirstOff+8;
- If l^<0 then exit;
- Dec(l^);
- // dumpansirec(s);
- If l^=0 then
- { Ref count dropped to zero }
- begin
- // Writeln ('Calling disposestring');
- DisposeAnsiString (S); { Remove...}
- end
- end;
- Procedure Incr_Ansi_Ref (Var S : Pointer);
- [Public,Alias : 'FPC_INCR_ANSI_REF'];
- Begin
- If S=Nil then
- exit;
- { Let's be paranoid : Constant string ??}
- If PAnsiRec(S-FirstOff)^.Ref<0 then exit;
- Inc(PAnsiRec(S-FirstOff)^.Ref);
- end;
- Procedure UniqueAnsiString (Var S : AnsiString); [Public,Alias : 'FPC_UNIQUE_ANSISTRING'];
- {
- Make sure reference count of S is 1,
- using copy-on-write semantics.
- }
- Var SNew : Pointer;
- begin
- If Pointer(S)=Nil
- then exit;
- if PAnsiRec(Pointer(S)-Firstoff)^.Ref>1 then
- begin
- SNew:=NewAnsiString (PAnsiRec(Pointer(S)-FirstOff)^.len);
- Move (Pointer(S)^,SNew^,PAnsiRec(Pointer(S)-FirstOff)^.len+1);
- PAnsiRec(SNew-8)^.len:=PAnsiRec(Pointer(S)-FirstOff)^.len;
- Decr_Ansi_Ref (Pointer(S)); { Thread safe }
- Pointer(S):=SNew;
- end;
- end;
- Procedure AssignAnsiString (Var S1 : Pointer;S2 : Pointer);
- [Public, Alias : 'FPC_ASSIGN_ANSI_STRING'];
- {
- Assigns S2 to S1 (S1:=S2), taking in account reference counts.
- If S2 is a constant string, a new S1 is allocated on the heap.
- }
- Var Temp : Pointer;
- begin
- If S2<>nil then
- begin
- If PAnsiRec(S2-FirstOff)^.Ref<0 then
- begin
- { S2 is a constant string, Create new string with copy. }
- Temp:=Pointer(NewAnsiString(PansiRec(S2-FirstOff)^.Len));
- Move (S2^,Temp^,PAnsiRec(S2-FirstOff)^.len+1);
- PAnsiRec(Temp-FirstOff)^.Len:=PAnsiRec(S2-FirstOff)^.len;
- end
- else
- begin
- Inc(PAnsiRec(S2-FirstOff)^.ref);
- Temp:=S2;
- end;
- end;
- { Decrease the reference count on the old S1 }
- Decr_Ansi_Ref (S1);
- { And finally, have S1 pointing to S2 (or its copy) }
- S1:=Temp;
- end;
- Procedure Ansi_String_Concat (Var S1 : Pointer; Var S2 : Pointer);
- {
- Concatenates 2 AnsiStrings : S1+S2.
- Result Goes to S1;
- }
- Var Size,Location : Longint;
- begin
- if S2=Nil then exit;
- if (S1=Nil) then
- AssignAnsiString(S1,S2)
- else
- begin
- Size:=PAnsiRec(Pointer(S2)-FirstOff)^.Len;
- Location:=Length(AnsiString(S1));
- { Setlength takes case of uniqueness
- and allocated memory. We need to use length,
- to take into account possibility of S1=Nil }
- //!! SetLength (S1,Size+Location);
- Move (Pointer(S2)^,Pointer(Pointer(S1)+location)^,Size+1);
- end;
- end;
- Procedure Ansi_ShortString_Concat (Var S1: AnsiString; Var S2 : ShortString);
- {
- Concatenates a Ansi with a short string; : S2 + S2
- }
- Var Size,Location : Longint;
- begin
- Size:=byte(S2[0]);
- Location:=Length(S1);
- If Size=0 then exit;
- { Setlength takes case of uniqueness
- and alllocated memory. We need to use length,
- to take into account possibility of S1=Nil }
- SetLength (S1,Size+Length(S1));
- Move (S2[1],Pointer(Pointer(S1)+Location)^,Size);
- PByte( Pointer(S1)+length(S1) )^:=0; { Terminating Zero }
- end;
- Procedure Ansi_To_ShortString (Var S1 : ShortString;S2 : Pointer; Maxlen : Longint);
- [Public, alias: 'FPC_ANSI2SHORT'];
- {
- Converts a AnsiString to a ShortString;
- }
- Var Size : Longint;
- begin
- Size:=PAnsiRec(S2-FirstOff)^.Len;
- If Size>maxlen then Size:=maxlen;
- Move (S2^,S1[1],Size);
- byte(S1[0]):=Size;
- end;
- Procedure Short_To_AnsiString (Var S1 : Pointer; Const S2 : ShortString);
- [Public, alias: 'FPC_SHORT2ANSI'];
- {
- Converts a ShortString to a AnsiString;
- }
- Var Size : Longint;
- begin
- Size:=Byte(S2[0]);
- Setlength (AnsiString(S1),Size);
- Move (S2[1],Pointer(S1)^,Size);
- { Terminating Zero }
- PByte(Pointer(S1)+Size)^:=0;
- end;
- Procedure PChar2Ansi(var a : ansistring;p : pchar);[Public,Alias : 'FPC_PCHAR_TO_ANSISTRING'];
- begin
- //!!!!!!!!! needs to be fixed (FK)
- if p[0]=#0 Then
- Pointer(a):=nil
- else
- Pointer(a):=p;
- end;
- { the compiler generates inline code for that
- Const EmptyChar : char = #0;
- Function Ansi2pchar (S : Pointer) : Pchar; [Alias : 'FPC_ANSI2PCHAR'];
- begin
- If S<>Nil then
- Ansi2Pchar:=S
- else
- Ansi2Pchar:=@emptychar;
- end;
- }
- { stupid solution, could be done with public,name in later versions }
- {$ASMMODE DIRECT}
- procedure dummy;assembler;
- asm
- .globl FPC_EMPTYCHAR
- FPC_EMPTYCHAR:
- .byte 0
- end;
- {$ASMMODE ATT}
- Function AnsiCompare (Var S1,S2 : Pointer): Longint;
- {
- Compares 2 AnsiStrings;
- The result is
- <0 if S1<S2
- 0 if S1=S2
- >0 if S1>S2
- }
- Var i,MaxI,Temp : Longint;
- begin
- Temp:=0;
- i:=0;
- MaxI:=Length(AnsiString(S1));
- if MaxI>Length(AnsiString(S2)) then MaxI:=Length(AnsiString(S2));
- While (i<MaxI) and (Temp=0) do
- begin
- Temp:= PByte(S1+I)^ - PByte(S2+i)^;
- inc(i);
- end;
- if temp=0 then temp:=Length(AnsiString(S1))-Length(AnsiString(S2));
- AnsiCompare:=Temp;
- end;
- Function AnsiCompare (Var S1 : Pointer; Var S2 : ShortString): Longint;
- {
- Compares a AnsiString with a ShortString;
- The result is
- <0 if S1<S2
- 0 if S1=S2
- >0 if S1>S2
- }
- Var i,MaxI,Temp : Longint;
- begin
- Temp:=0;
- i:=0;
- MaxI:=Length(AnsiString(S1));
- if MaxI>byte(S2[0]) then MaxI:=Byte(S2[0]);
- While (i<MaxI) and (Temp=0) do
- begin
- Temp:= PByte(S1+I)^ - Byte(S2[i+1]);
- inc(i);
- end;
- AnsiCompare:=Temp;
- end;
- { Not used, can be removed. }
- Procedure SetCharAtIndex (Var S : AnsiString; Index : Longint; C : CHar);
- begin
- if Index<=Length(S) then
- begin
- UniqueAnsiString(S);
- Pbyte(Pointer(S)+index-1)^:=Byte(C);
- end;
- end;
- { ---------------------------------------------------------------------
- Public functions, In interface.
- ---------------------------------------------------------------------}
- Function Length (Const S : AnsiString) : Longint;
- {
- Returns the length of an AnsiString.
- Takes in acount that zero strings are NIL;
- }
- begin
- If Pointer(S)=Nil then
- Length:=0
- else
- Length:=PAnsiRec(Pointer(S)-FirstOff)^.Len;
- end;
- Procedure SetLength (Var S : AnsiString; l : Longint);
- {
- Sets The length of string S to L.
- Makes sure S is unique, and contains enough room.
- }
- Var Temp : Pointer;
- begin
- If (Pointer(S)=Nil) and (l>0) then
- begin
- { Need a complete new string...}
- // S:=NewAnsiString(l);
- PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
- PAnsiRec(Pointer(S)-FirstOff)^.Len:=l;
- PByte (Pointer(S)+l)^:=0;
- end
- else if l>0 then
- begin
- If (PAnsiRec(Pointer(S)-FirstOff)^.Maxlen < L) or
- (PAnsiRec(Pointer(S)-FirstOff)^.Ref <> 1) then
- begin
- { Reallocation is needed... }
- Temp:=Pointer(NewAnsiString(L));
- if Length(S)>0 then
- Move (Pointer(S)^,Temp^,Length(S)+1);
- Decr_Ansi_ref (Pointer(S));
- Pointer(S):=Temp;
- end;
- PAnsiRec(Pointer(S)-FirstOff)^.Len:=l
- end
- else
- { Length=0 }
- begin
- Decr_Ansi_Ref (Pointer(S));
- Pointer(S):=Nil;
- end;
- end;
- Function Copy (Const S : AnsiString; Index,Size : Longint) : AnsiString;
- var ResultAddress : Pointer;
- begin
- ResultAddress:=Nil;
- dec(index);
- { Check Size. Accounts for Zero-length S }
- if Length(S)<Index+Size then
- Size:=Length(S)-Index;
- If Size>0 then
- begin
- ResultAddress:=Pointer(NewAnsiString (Size));
- if ResultAddress<>Nil then
- begin
- Move (Pointer(Pointer(S)+index)^,ResultAddress^,Size);
- PAnsiRec(ResultAddress-FirstOff)^.Len:=Size;
- PByte(ResultAddress+Size)^:=0;
- end;
- end;
- Copy:=AnsiString(ResultAddress);
- end;
- Function Pos (Const Substr : AnsiString; Const Source : AnsiString) : Longint;
- var i,j : longint;
- e : boolean;
- s,se : Pointer;
- begin
- i := 0;
- j := 0;
- e := true;
- if Plongint(substr)^=0 then e := false;
- while (e) and (i <= length (Source) - length (substr)) do
- begin
- inc (i);
- S:=Pointer(copy(Source,i,length(Substr)));
- Se:=pointer(substr);
- if AnsiCompare(se,S)=0 then
- begin
- j := i;
- e := false;
- end;
- DisposeAnsiString(S);
- end;
- pos := j;
- end;
- Procedure Val (Const S : AnsiString; var R : real; Var Code : Integer);
- Var SS : String;
- begin
- Ansi_To_ShortString (SS,Pointer(S),255);
- Val(SS,R,Code);
- end;
- {
- Procedure Val (var S : AnsiString; var D : Double; Var Code : Integer);
- Var SS : ShortString;
- begin
- Ansi_To_ShortString (SS,S,255);
- Val(SS,D,Code);
- end;
- }
- Procedure Val (Const S : AnsiString; var E : Extended; Code : Integer);
- Var SS : ShortString;
- begin
- Ansi_To_ShortString (SS,Pointer(S),255);
- Val(SS,E,Code);
- end;
- Procedure Val (Const S : AnsiString; var C : Cardinal; Code : Integer);
- Var SS : ShortString;
- begin
- Ansi_To_ShortString (SS,Pointer(S),255);
- Val(SS,C,Code);
- end;
- Procedure Val (Const S : AnsiString; var L : Longint; Var Code : Integer);
- Var SS : ShortString;
- begin
- Ansi_To_ShortString (SS,Pointer(S),255);
- Val(SS,L,Code);
- end;
- Procedure Val (Const S : AnsiString; var W : Word; Var Code : Integer);
- Var SS : ShortString;
- begin
- Ansi_To_ShortString (SS,Pointer(S),255);
- Val(SS,W,Code);
- end;
- Procedure Val (Const S : AnsiString; var I : Integer; Var Code : Integer);
- Var SS : ShortString;
- begin
- Ansi_To_ShortString (SS,Pointer(S),255);
- Val(SS,I,Code);
- end;
- Procedure Val (Const S : AnsiString; var B : Byte; Var Code : Integer);
- Var SS : ShortString;
- begin
- Ansi_To_ShortString (SS,Pointer(S),255);
- Val(SS,B,Code);
- end;
- Procedure Val (Const S : AnsiString; var SI : ShortInt; Var Code : Integer);
- Var SS : ShortString;
- begin
- Ansi_To_ShortString (SS,Pointer(S),255);
- Val(SS,SI,Code);
- end;
- {
- Procedure Str (Const R : Real;Len,fr : Longint; Const S : AnsiString);
- Var SS : ShortString;
- begin
- {int_Str_Real (R,Len,fr,SS);}
- Short_To_AnsiString (Pointer(S),SS);
- end;
- {
- Procedure Str (Var D : Double;Len,fr: Longint; Var S : AnsiString);
- Var SS : ShortString;
- begin
- {int_Str_Double (D,Len,fr,SS);}
- Short_To_AnsiString (S,SS);
- end;
- }
- Procedure Str (E : Extended;Lenf,Fr: Longint; Var S : AnsiString);
- Var SS : ShortString;
- begin
- {int_Str_Extended (E,Len,fr,SS);}
- Short_To_AnsiString (S,SS);
- end;
- Procedure Str (C : Cardinal;Len : Longint; Var S : AnsiString);
- begin
- end;
- Procedure Str (L : Longint; Len : Longint; Var S : AnsiString);
- Var SS : ShortString;
- begin
- {int_Str_Longint (L,Len,fr,SS);}
- Short_To_AnsiString (S,SS);
- end;
- Procedure Str (Var W : Word;Len : Longint; Var S : AnsiString);
- begin
- end;
- Procedure Str (Var I : Integer;Len : Longint; Var S : AnsiString);
- begin
- end;
- Procedure Str (Var B : Byte; Len : Longint; Var S : AnsiString);
- begin
- end;
- Procedure Str (Var SI : ShortInt; Len : Longint; Var S : AnsiString);
- begin
- end;
- }
- Procedure Delete (Var S : AnsiString; Index,Size: Longint);
- begin
- if index<=0 then
- begin
- Size:=Size+index-1;
- index:=1;
- end;
- if (Index<=length(s)) and (Size>0) then
- begin
- UniqueAnsiString (S);
- if Size+Index>Length(S) then
- Size:=Length(s)-Index+1;
- Setlength(s,Length(s)-Size);
- if Index<=Length(s) then
- Move(Pointer(Pointer(S)+Index+Size-1)^,
- Pointer(Pointer(s)+Index-1)^,Length(s)-Index+2)
- else
- Pbyte(Pointer(S)+Length(S))^:=0;
- end;
- end;
- Procedure Insert (Const Source : AnsiString; Var S : AnsiString; Index : Longint);
- var s3,s4,s5 : Pointer;
-
- begin
- If Length(Source)=0 then exit;
- if index <= 0 then index := 1;
- s3 := Pointer(copy(s,index,length(s)));
- if index > Length(s) then
- index := Length(S)+1;
- SetLength(s,index - 1);
- s4 := Pointer ( NewAnsiString(PansiRec(Pointer(Source)-Firstoff)^.len) );
- S5:=Pointer(Source);
- Ansi_String_Concat(s4,s5);
- if S4<>Nil then
- Ansi_String_Concat(S4,s3);
- Ansi_String_Concat(Pointer(S),S4);
- Decr_ansi_ref (S3);
- Decr_ansi_ref (S4);
- end;
- {
- $Log$
- Revision 1.18 1998-09-28 14:02:34 michael
- + AnsiString changes
- Revision 1.17 1998/09/27 22:44:50 florian
- * small fixes
- * made UniqueAnsistring public
- * ...
- Revision 1.16 1998/09/20 17:49:08 florian
- * some ansistring fixes
- Revision 1.15 1998/09/19 08:33:17 florian
- * some internal procedures take now an pointer instead of a
- ansistring
- Revision 1.14 1998/09/14 10:48:14 peter
- * FPC_ names
- * Heap manager is now system independent
- Revision 1.13 1998/08/23 20:58:51 florian
- + rtti for objects and classes
- + TObject.GetClassName implemented
- Revision 1.12 1998/08/22 09:32:12 michael
- + minor fixes typos, and ansi2pchar
- Revision 1.11 1998/08/08 12:28:10 florian
- * a lot small fixes to the extended data type work
- Revision 1.10 1998/07/29 21:44:34 michael
- + Implemented reading/writing of ansistrings
- Revision 1.9 1998/07/20 23:36:56 michael
- changes for ansistrings
- Revision 1.8 1998/07/13 21:19:09 florian
- * some problems with ansi string support fixed
- Revision 1.7 1998/07/06 14:29:08 michael
- + Added Public,Alias directives for some calls
- Revision 1.6 1998/06/25 08:41:44 florian
- * better rtti
- Revision 1.5 1998/06/12 07:39:13 michael
- + Added aliases for Incr/Decr ref.
- Revision 1.4 1998/06/08 19:35:02 michael
- Some changes to integrate in system unit
- Revision 1.3 1998/06/08 12:38:22 michael
- Implemented rtti, inserted ansistrings again
- Revision 1.2 1998/05/12 10:42:44 peter
- * moved getopts to inc/, all supported OS's need argc,argv exported
- + strpas, strlen are now exported in the systemunit
- * removed logs
- * removed $ifdef ver_above
- }
|