1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114 |
- {
- $Id$
- This file is part of the Free Pascal run time library.
- Copyright (c) 1999-2001 by Florian Klaempfl,
- member of the Free Pascal development team.
- This file implements support routines for WideStrings with FPC
- 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 contains the implementation of the WideString type,
- and all things that are needed for it.
- WideString is defined as a 'silent' pwidechar :
- a pwidechar that points to :
- @-12 : StrLenInt for maximum size;
- @-8 : StrLenInt for size;
- @-4 : StrLenInt for reference count;
- @ : String + Terminating #0;
- Pwidechar(Widestring) is a valid typecast.
- So WS[i] is converted to the address @WS+i-1.
- Constants should be assigned a reference count of -1
- Meaning that they can't be disposed of.
- }
- Type
- PWideRec = ^TWideRec;
- TWideRec = Packed Record
- Maxlen,
- len,
- ref : StrLenInt;
- First : WideChar;
- end;
- Const
- WideRecLen = SizeOf(TWideRec);
- WideFirstOff = SizeOf(TWideRec)-sizeof(WideChar);
- {
- Default WideChar <-> Char conversion is to only convert the
- lower 127 chars, all others are translated to spaces.
- These routines can be overwritten for the Current Locale
- }
- procedure Wide2AnsiMove(source:pwidechar;dest:pchar;len:StrLenInt);
- var
- i : StrLenInt;
- begin
- for i:=1 to len do
- begin
- if word(source^)<128 then
- dest^:=char(word(source^))
- else
- dest^:=' ';
- inc(dest);
- inc(source);
- end;
- end;
- procedure Ansi2WideMove(source:pchar;dest:pwidechar;len:StrLenInt);
- var
- i : StrLenInt;
- begin
- for i:=1 to len do
- begin
- if byte(source^)<128 then
- dest^:=widechar(byte(source^))
- else
- dest^:=' ';
- inc(dest);
- inc(source);
- end;
- end;
- Const
- Wide2AnsiMoveProc:TWide2AnsiMove=@Wide2AnsiMove;
- Ansi2WideMoveProc:TAnsi2WideMove=@Ansi2WideMove;
- Procedure GetWideStringManager (Var Manager : TWideStringManager);
- begin
- Manager.Wide2AnsiMove:=Wide2AnsiMoveProc;
- Manager.Ansi2WideMove:=Ansi2WideMoveProc;
- end;
- Procedure SetWideStringManager (Const New : TWideStringManager; Var Old: TWideStringManager);
- begin
- GetWideStringManager(Old);
- SetWideStringManager(New);
- end;
- Procedure SetWideStringManager (Const New : TWideStringManager);
- begin
- Wide2AnsiMoveProc:=New.Wide2AnsiMove;
- Ansi2WideMoveProc:=New.Ansi2WideMove;
- end;
- (*
- Procedure UniqueWideString(Var S : WideString); [Public,Alias : 'FPC_WIDESTR_UNIQUE'];
- {
- Make sure reference count of S is 1,
- using copy-on-write semantics.
- }
- begin
- end;
- *)
- {****************************************************************************
- Internal functions, not in interface.
- ****************************************************************************}
- {$ifdef WideStrDebug}
- Procedure DumpWideRec(S : Pointer);
- begin
- If S=Nil then
- Writeln ('String is nil')
- Else
- Begin
- With PWideRec(S-WideFirstOff)^ do
- begin
- Write ('(Maxlen: ',maxlen);
- Write (' Len:',len);
- Writeln (' Ref: ',ref,')');
- end;
- end;
- end;
- {$endif}
- Function NewWideString(Len : StrLenInt) : Pointer;
- {
- Allocate a new WideString on the heap.
- initialize it to zero length and reference count 1.
- }
- Var
- P : Pointer;
- l : StrLenInt;
- begin
- { request a multiple of 16 because the heap manager alloctes anyways chunks of 16 bytes }
- L := (Len*sizeof(WideChar)+WideRecLen+15) and (not 15);
- GetMem(P,l);
- If P<>Nil then
- begin
- PWideRec(P)^.Maxlen:=(l-WideRecLen) div sizeof(WideChar); { Maximal length }
- PWideRec(P)^.Len:=0; { Initial length }
- PWideRec(P)^.Ref:=1; { Set reference count }
- PWideRec(P)^.First:=#0; { Terminating #0 }
- inc(p,WideFirstOff); { Points to string now }
- end;
- NewWideString:=P;
- end;
- Procedure DisposeWideString(Var S : Pointer);
- {
- Deallocates a WideString From the heap.
- }
- begin
- If S=Nil then
- exit;
- Dec (S,WideFirstOff);
- FreeMem (S);
- S:=Nil;
- end;
- Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);saveregisters;[Public,Alias:'FPC_WIDESTR_DECR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
- {
- Decreases the ReferenceCount of a non constant widestring;
- If the reference count is zero, deallocate the string;
- }
- Type
- pStrLenInt = ^StrLenInt;
- Var
- l : pStrLenInt;
- Begin
- { Zero string }
- If S=Nil then exit;
- { check for constant strings ...}
- l:=@PWIDEREC(S-WideFirstOff)^.Ref;
- If l^<0 then exit;
- { declocked does a MT safe dec and returns true, if the counter is 0 }
- If declocked(l^) then
- { Ref count dropped to zero }
- DisposeWideString (S); { Remove...}
- end;
- {$ifdef hascompilerproc}
- { alias for internal use }
- Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);saveregisters;[external name 'FPC_WIDESTR_DECR_REF'];
- {$endif compilerproc}
- {$ifdef hascompilerproc}
- Procedure fpc_WideStr_Incr_Ref (S : Pointer);saveregisters;[Public,Alias:'FPC_WIDESTR_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
- {$else}
- Procedure fpc_WideStr_Incr_Ref (Var S : Pointer);saveregisters;[Public,Alias:'FPC_WIDESTR_INCR_REF'];
- {$endif compilerproc}
- Begin
- If S=Nil then
- exit;
- { Let's be paranoid : Constant string ??}
- If PWideRec(S-WideFirstOff)^.Ref<0 then exit;
- inclocked(PWideRec(S-WideFirstOff)^.Ref);
- end;
- {$ifdef hascompilerproc}
- { alias for internal use }
- Procedure fpc_WideStr_Incr_Ref (S : Pointer);saveregisters;[external name 'FPC_WIDESTR_INCR_REF'];
- {$endif compilerproc}
- function fpc_WideStr_To_ShortStr (high_of_res: StrLenInt;const S2 : WideString): shortstring;[Public, alias: 'FPC_WIDESTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
- {
- Converts a WideString to a ShortString;
- }
- Var
- Size : StrLenInt;
- begin
- if S2='' then
- fpc_WideStr_To_ShortStr:=''
- else
- begin
- Size:=Length(S2);
- If Size>high_of_res then
- Size:=high_of_res;
- Wide2AnsiMoveProc(PWideChar(S2),PChar(@fpc_WideStr_To_ShortStr[1]),Size);
- byte(fpc_WideStr_To_ShortStr[0]):=byte(Size);
- end;
- end;
- Function fpc_ShortStr_To_WideStr (Const S2 : ShortString): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
- {
- Converts a ShortString to a WideString;
- }
- Var
- Size : StrLenInt;
- begin
- Size:=Length(S2);
- Setlength (fpc_ShortStr_To_WideStr,Size);
- if Size>0 then
- begin
- Ansi2WideMoveProc(PChar(@S2[1]),PWideChar(Pointer(fpc_ShortStr_To_WideStr)),Size);
- { Terminating Zero }
- PWideChar(Pointer(fpc_ShortStr_To_WideStr)+Size*sizeof(WideChar))^:=#0;
- end;
- end;
- { old style helper }
- {$ifndef hascompilerproc}
- Procedure fpc_ShortStr_To_WideStr (Var S1 : Pointer; Const S2 : ShortString);[Public, alias: 'FPC_SHORTSTR_TO_WIDESTR'];
- begin
- s1 := pointer(fpc_ShortStr_To_WideStr(s2));
- end;
- {$endif hascompilerproc}
- Function fpc_WideStr_To_AnsiStr (const S2 : WideString): AnsiString; {$ifdef hascompilerproc} compilerproc; {$endif}
- {
- Converts a WideString to an AnsiString
- }
- Var
- Size : StrLenInt;
- begin
- if s2='' then
- exit;
- Size:=Length(WideString(S2));
- Setlength (fpc_WideStr_To_AnsiStr,Size);
- if Size>0 then
- begin
- Wide2AnsiMoveProc(PWideChar(Pointer(S2)),PChar(Pointer(fpc_WideStr_To_AnsiStr)),Size);
- { Terminating Zero }
- PChar(Pointer(fpc_WideStr_To_AnsiStr)+Size)^:=#0;
- end;
- end;
- { old style helper }
- {$ifndef hascompilerproc}
- Procedure fpc_WideStr_To_AnsiStr (Var S1 : Pointer;const S2 : WideString);[Public, alias: 'FPC_WIDESTR_TO_ANSISTR'];
- begin
- s1 := pointer(fpc_WideStr_To_AnsiStr(s2));
- end;
- {$endif hascompilerproc}
- Function fpc_AnsiStr_To_WideStr (Const S2 : AnsiString): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
- {
- Converts an AnsiString to a WideString;
- }
- Var
- Size : StrLenInt;
- begin
- if s2='' then
- exit;
- Size:=Length(S2);
- Setlength (result,Size);
- if Size>0 then
- begin
- Ansi2WideMoveProc(PChar(S2),PWideChar(Pointer(result)),Size);
- { Terminating Zero }
- PWideChar(Pointer(result)+Size*sizeof(WideChar))^:=#0;
- end;
- end;
- { compilers with widestrings should have compiler procs }
- Function fpc_PWideChar_To_AnsiStr(const p : pwidechar): ansistring; compilerproc;
- var
- Size : StrLenInt;
- begin
- if p=nil then
- exit;
- Size := IndexWord(p^, -1, 0);
- Setlength (result,Size);
- if Size>0 then
- begin
- Wide2AnsiMoveProc(P,PChar(Pointer(result)),Size);
- { Terminating Zero }
- PChar(Pointer(result)+Size)^:=#0;
- end;
- end;
- Function fpc_PWideChar_To_WideStr(const p : pwidechar): widestring; compilerproc;
- var
- Size : StrLenInt;
- begin
- if p=nil then
- exit;
- Size := IndexWord(p^, -1, 0);
- Setlength (result,Size);
- if Size>0 then
- begin
- Move(p^,PWideChar(Pointer(result))^,Size*sizeof(WideChar));
- { Terminating Zero }
- PWideChar(Pointer(result)+Size*sizeof(WideChar))^:=#0;
- end;
- end;
- Function fpc_PWideChar_To_ShortStr(const p : pwidechar): shortstring; compilerproc;
- var
- Size : StrLenInt;
- begin
- if p=nil then
- begin
- fpc_PWideChar_To_ShortStr:='';
- exit;
- end;
- Size := IndexWord(p^, $7fffffff, 0);
- Setlength (result,Size+1);
- if Size>0 then
- begin
- If Size>255 then
- Size:=255;
- Wide2AnsiMoveProc(p,PChar(@result[1]),Size);
- byte(result[0]):=byte(Size);
- end;
- end;
- { old style helper }
- {$ifndef hascompilerproc}
- Procedure fpc_AnsiStr_To_WideStr (Var S1 : Pointer; Const S2 : AnsiString);[Public, alias: 'FPC_ANSISTR_TO_WIDESTR'];
- begin
- s1 := pointer(fpc_AnsiStr_To_WideStr(s2));
- end;
- {$endif hascompilerproc}
- { checked against the ansistring routine, 2001-05-27 (FK) }
- Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_WIDESTR_ASSIGN']; {$ifdef hascompilerproc} compilerproc; {$endif}
- {
- Assigns S2 to S1 (S1:=S2), taking in account reference counts.
- }
- begin
- If S2<>nil then
- If PWideRec(S2-WideFirstOff)^.Ref>0 then
- Inc(PWideRec(S2-WideFirstOff)^.ref);
- { Decrease the reference count on the old S1 }
- fpc_widestr_decr_ref (S1);
- { And finally, have S1 pointing to S2 (or its copy) }
- S1:=S2;
- end;
- {$ifdef hascompilerproc}
- { alias for internal use }
- Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_WIDESTR_ASSIGN'];
- {$endif hascompilerproc}
- { checked against the ansistring routine, 2001-05-27 (FK) }
- {$ifdef hascompilerproc}
- function fpc_WideStr_Concat (const S1,S2 : WideString): WideString; compilerproc;
- var
- S3: WideString absolute result;
- {$else hascompilerproc}
- Procedure fpc_WideStr_Concat (S1,S2 : WideString;var S3 : WideString);[Public, alias: 'FPC_WIDESTR_CONCAT'];
- {$endif hascompilerproc}
- {
- Concatenates 2 WideStrings : S1+S2.
- Result Goes to S3;
- }
- Var
- Size,Location : StrLenInt;
- begin
- { only assign if s1 or s2 is empty }
- if (S1='') then
- S3 := S2
- else
- if (S2='') then
- S3 := S1
- else
- begin
- { create new result }
- Size:=Length(S2);
- Location:=Length(S1);
- SetLength (S3,Size+Location);
- Move (S1[1],S3[1],Location*sizeof(WideChar));
- Move (S2[1],S3[location+1],(Size+1)*sizeof(WideChar));
- end;
- end;
- Function fpc_Char_To_WideStr(const c : Char): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
- {
- Converts a Char to a WideString;
- }
- begin
- if c = #0 then
- { result is automatically set to '' }
- exit;
- Setlength (fpc_Char_To_WideStr,1);
- fpc_Char_To_WideStr[1]:=c;
- { Terminating Zero }
- PWideChar(Pointer(fpc_Char_To_WideStr)+sizeof(WideChar))^:=#0;
- end;
- { old style helper }
- {$ifndef hascompilerproc}
- Procedure fpc_Char_To_WideStr(var S1 : Pointer; c : Char);[Public, alias: 'FPC_CHAR_TO_WIDESTR'];
- begin
- s1 := pointer(fpc_Char_To_WideStr(c));
- end;
- {$endif hascompilerproc}
- Function fpc_PChar_To_WideStr(const p : pchar): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
- Var
- L : StrLenInt;
- begin
- if (not assigned(p)) or (p[0]=#0) Then
- { result is automatically set to '' }
- exit;
- l:=IndexChar(p^,-1,#0);
- SetLength(fpc_PChar_To_WideStr,L);
- Ansi2WideMoveProc(P,PWideChar(Pointer(fpc_PChar_To_WideStr)),l);
- end;
- { old style helper }
- {$ifndef hascompilerproc}
- Procedure fpc_PChar_To_WideStr(var a : WideString;p : pchar);[Public,Alias : 'FPC_PCHAR_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
- begin
- pointer(a) := pointer(fpc_PChar_To_WideStr(p));
- end;
- {$endif hascompilerproc}
- Function fpc_CharArray_To_WideStr(const arr: array of char): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
- var
- i : StrLenInt;
- begin
- if arr[0]=#0 Then
- { result is automatically set to '' }
- exit;
- i:=IndexChar(arr,high(arr)+1,#0);
- if i = -1 then
- i := high(arr)+1;
- SetLength(fpc_CharArray_To_WideStr,i);
- Ansi2WideMoveProc (pchar(@arr),PWideChar(Pointer(fpc_CharArray_To_WideStr)),i);
- end;
- { old style helper }
- {$ifndef hascompilerproc}
- Procedure fpc_CharArray_To_WideStr(var a : WideString; p: pointer; len: StrLenInt); [Public,Alias : 'FPC_CHARARRAY_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
- var
- src: pchar;
- i: StrLenInt;
- begin
- src := pchar(p);
- if src[0]=#0 Then
- begin
- pointer(a) := nil;
- exit;
- end;
- i:=IndexChar(src^,len,#0);
- if i = -1 then
- i := len;
- pointer(a) := NewWideString(i);
- Ansi2WideMoveProc (src,PWideChar(Pointer(@a[1])),i);
- end;
- {$endif not hascompilerproc}
- {$ifdef hascompilerproc}
- { inside the compiler, the resulttype is modified to that of the actual }
- { chararray we're converting to (JM) }
- function fpc_widestr_to_chararray(arraysize: StrLenInt; const src: WideString): fpc_big_chararray;[public,alias: 'FPC_WIDESTR_TO_CHARARRAY']; compilerproc;
- var
- len: StrLenInt;
- begin
- len := length(src);
- if len > arraysize then
- len := arraysize;
- { make sure we don't dereference src if it can be nil (JM) }
- if len > 0 then
- wide2ansimoveproc(pwidechar(@src[1]),pchar(@fpc_widestr_to_chararray[0]),len);
- fillchar(fpc_widestr_to_chararray[len],arraysize-len,0);
- end;
- {$endif hascompilerproc}
- Function fpc_WideStr_Compare(const S1,S2 : WideString): StrLenInt;[Public,Alias : 'FPC_WIDESTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
- {
- Compares 2 WideStrings;
- The result is
- <0 if S1<S2
- 0 if S1=S2
- >0 if S1>S2
- }
- Var
- MaxI,Temp : StrLenInt;
- begin
- if pointer(S1)=pointer(S2) then
- begin
- fpc_WideStr_Compare:=0;
- exit;
- end;
- Maxi:=Length(S1);
- temp:=Length(S2);
- If MaxI>Temp then
- MaxI:=Temp;
- Temp:=CompareWord(S1[1],S2[1],MaxI);
- if temp=0 then
- temp:=Length(S1)-Length(S2);
- fpc_WideStr_Compare:=Temp;
- end;
- Procedure fpc_WideStr_CheckZero(p : pointer);[Public,Alias : 'FPC_WIDESTR_CHECKZERO']; {$ifdef hascompilerproc} compilerproc; {$endif}
- begin
- if p=nil then
- HandleErrorFrame(201,get_frame);
- end;
- Procedure fpc_WideStr_CheckRange(len,index : StrLenInt);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
- begin
- if (index>len) or (Index<1) then
- HandleErrorFrame(201,get_frame);
- end;
- {$ifndef INTERNSETLENGTH}
- Procedure SetLength (Var S : WideString; l : StrLenInt);
- {$else INTERNSETLENGTH}
- Procedure fpc_WideStr_SetLength (Var S : WideString; l : StrLenInt);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
- {$endif INTERNSETLENGTH}
- {
- Sets The length of string S to L.
- Makes sure S is unique, and contains enough room.
- }
- Var
- Temp : Pointer;
- movelen, NewLen: StrLenInt;
- begin
- if (l>0) then
- begin
- if Pointer(S)=nil then
- begin
- { Need a complete new string...}
- Pointer(s):=NewWideString(l);
- end
- else if (PWideRec(Pointer(S)-WideFirstOff)^.Ref = 1) then
- begin
- if (PWideRec(Pointer(S)-WideFirstOff)^.Maxlen < L) then
- begin
- Dec(Pointer(S),WideFirstOff);
- NewLen := (L*sizeof(WideChar)+WideRecLen+15) and (not 15);
- reallocmem(pointer(S), NewLen);
- PAnsiRec(S)^.MaxLen := (NewLen - WideRecLen) div sizeof(WideChar);
- Inc(Pointer(S), WideFirstOff);
- end;
- PWideRec(Pointer(S)-WideFirstOff)^.Len := L;
- PWord(Pointer(S)+L*sizeof(WideChar))^:=0;
- end
- else
- begin
- { Reallocation is needed... }
- Temp:=Pointer(NewWideString(L));
- if Length(S)>0 then
- begin
- if l < succ(length(s)) then
- movelen := l
- { also move terminating null }
- else movelen := succ(length(s));
- Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
- end;
- fpc_widestr_decr_ref(Pointer(S));
- Pointer(S):=Temp;
- end;
- { Force nil termination in case it gets shorter }
- PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
- PWideRec(Pointer(S)-FirstOff)^.Len:=l;
- end
- else
- begin
- { Length=0 }
- if Pointer(S)<>nil then
- fpc_widestr_decr_ref (Pointer(S));
- Pointer(S):=Nil;
- end;
- end;
- {*****************************************************************************
- Public functions, In interface.
- *****************************************************************************}
- function WideCharToString(S : PWideChar) : AnsiString;
- begin
- result:=WideCharLenToString(s,Length(WideString(s)));
- end;
- function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : StrLenInt) : PWideChar;
- begin
- if Length(Src)<DestSize then
- Ansi2WideMoveProc(PChar(Src),Dest,Length(Src))
- else
- Ansi2WideMoveProc(PChar(Src),Dest,DestSize);
- result:=Dest;
- end;
- function WideCharLenToString(S : PWideChar;Len : StrLenInt) : AnsiString;
- begin
- SetLength(result,Len);
- Wide2AnsiMove(S,PChar(result),Len);
- end;
- procedure WideCharLenToStrVar(Src : PWideChar;Len : StrLenInt;var Dest : AnsiString);
- begin
- Dest:=WideCharLenToString(Src,Len);
- end;
- procedure WideCharToStrVar(S : PWideChar;var Dest : AnsiString);
- begin
- Dest:=WideCharToString(S);
- end;
- {$ifndef INTERNLENGTH}
- Function Length (Const S : WideString) : StrLenInt;
- {
- Returns the length of an WideString.
- Takes in acount that zero strings are NIL;
- }
- begin
- If Pointer(S)=Nil then
- Length:=0
- else
- Length:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
- end;
- {$endif INTERNLENGTH}
- { overloaded version of UniqueString for interface }
- procedure UniqueString(Var S : WideString); [external name 'FPC_WIDESTR_UNIQUE'];
- Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; {$ifdef hascompilerproc} compilerproc; {$endif}
- {
- Make sure reference count of S is 1,
- using copy-on-write semantics.
- }
- Var
- SNew : Pointer;
- L : StrLenInt;
- begin
- pointer(result) := pointer(s);
- If Pointer(S)=Nil then
- exit;
- if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
- begin
- L:=PWideRec(Pointer(S)-WideFirstOff)^.len;
- SNew:=NewWideString (L);
- Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
- PWideRec(SNew-WideFirstOff)^.len:=L;
- fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
- pointer(S):=SNew;
- pointer(result):=SNew;
- end;
- end;
- {$ifdef interncopy}
- Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : StrLenInt) : WideString;compilerproc;
- {$else}
- Function Copy (Const S : WideString; Index,Size : StrLenInt) : WideString;
- {$endif}
- var
- ResultAddress : Pointer;
- begin
- ResultAddress:=Nil;
- dec(index);
- if Index < 0 then
- Index := 0;
- { Check Size. Accounts for Zero-length S, the double check is needed because
- Size can be maxint and will get <0 when adding index }
- if (Size>Length(S)) or
- (Index+Size>Length(S)) then
- Size:=Length(S)-Index;
- If Size>0 then
- begin
- If Index<0 Then
- Index:=0;
- ResultAddress:=Pointer(NewWideString (Size));
- if ResultAddress<>Nil then
- begin
- Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
- PWideRec(ResultAddress-WideFirstOff)^.Len:=Size;
- PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
- end;
- end;
- {$ifdef interncopy}
- Pointer(fpc_widestr_Copy):=ResultAddress;
- {$else}
- Pointer(Copy):=ResultAddress;
- {$endif}
- end;
- Function Pos (Const Substr : WideString; Const Source : WideString) : StrLenInt;
- var
- i,MaxLen : StrLenInt;
- pc : pwidechar;
- begin
- Pos:=0;
- if Length(SubStr)>0 then
- begin
- MaxLen:=Length(source)-Length(SubStr);
- i:=0;
- pc:=@source[1];
- while (i<=MaxLen) do
- begin
- inc(i);
- if (SubStr[1]=pc^) and
- (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
- begin
- Pos:=i;
- exit;
- end;
- inc(pc);
- end;
- end;
- end;
- { Faster version for a widechar alone }
- Function Pos (c : WideChar; Const s : WideString) : StrLenInt;
- var
- i: StrLenInt;
- pc : pwidechar;
- begin
- pc:=@s[1];
- for i:=1 to length(s) do
- begin
- if pc^=c then
- begin
- pos:=i;
- exit;
- end;
- inc(pc);
- end;
- pos:=0;
- end;
- { Faster version for a char alone. Must be implemented because }
- { pos(c: char; const s: shortstring) also exists, so otherwise }
- { using pos(char,pchar) will always call the shortstring version }
- { (exact match for first argument), also with $h+ (JM) }
- Function Pos (c : Char; Const s : WideString) : StrLenInt;
- var
- i: StrLenInt;
- wc : widechar;
- pc : pwidechar;
- begin
- wc:=c;
- pc:=@s[1];
- for i:=1 to length(s) do
- begin
- if pc^=wc then
- begin
- pos:=i;
- exit;
- end;
- inc(pc);
- end;
- pos:=0;
- end;
- Procedure Delete (Var S : WideString; Index,Size: StrLenInt);
- Var
- LS : StrLenInt;
- begin
- If Length(S)=0 then
- exit;
- if index<=0 then
- exit;
- LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
- if (Index<=LS) and (Size>0) then
- begin
- UniqueString (S);
- if Size+Index>LS then
- Size:=LS-Index+1;
- if Index+Size<=LS then
- begin
- Dec(Index);
- Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
- end;
- Setlength(s,LS-Size);
- end;
- end;
- Procedure Insert (Const Source : WideString; Var S : WideString; Index : StrLenInt);
- var
- Temp : WideString;
- LS : StrLenInt;
- begin
- If Length(Source)=0 then
- exit;
- if index <= 0 then
- index := 1;
- Ls:=Length(S);
- if index > LS then
- index := LS+1;
- Dec(Index);
- Pointer(Temp) := NewWideString(Length(Source)+LS);
- SetLength(Temp,Length(Source)+LS);
- If Index>0 then
- move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
- Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
- If (LS-Index)>0 then
- Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
- S:=Temp;
- end;
- Procedure SetString (Var S : WideString; Buf : PWideChar; Len : StrLenInt);
- var
- BufLen: StrLenInt;
- begin
- SetLength(S,Len);
- If (Buf<>Nil) and (Len>0) then
- begin
- BufLen := IndexWord(Buf^, Len+1, 0);
- If (BufLen>0) and (BufLen < Len) then
- Len := BufLen;
- Move (Buf[0],S[1],Len*sizeof(WideChar));
- PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
- end;
- end;
- Procedure SetString (Var S : WideString; Buf : PChar; Len : StrLenInt);
- var
- BufLen: StrLenInt;
- begin
- SetLength(S,Len);
- If (Buf<>Nil) and (Len>0) then
- begin
- BufLen := IndexByte(Buf^, Len+1, 0);
- If (BufLen>0) and (BufLen < Len) then
- Len := BufLen;
- Ansi2WideMoveProc(Buf,PWideChar(S),Len);
- PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
- end;
- end;
- Function fpc_Val_Real_WideStr(Const S : WideString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
- Var
- SS : String;
- begin
- fpc_Val_Real_WideStr := 0;
- if length(S) > 255 then
- code := 256
- else
- begin
- SS := S;
- Val(SS,fpc_Val_Real_WideStr,code);
- end;
- end;
- Function fpc_Val_UInt_WideStr (Const S : WideString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
- Var
- SS : ShortString;
- begin
- fpc_Val_UInt_WideStr := 0;
- if length(S) > 255 then
- code := 256
- else
- begin
- SS := S;
- Val(SS,fpc_Val_UInt_WideStr,code);
- end;
- end;
- Function fpc_Val_SInt_WideStr (DestSize: StrLenInt; Const S : WideString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
- Var
- SS : ShortString;
- begin
- fpc_Val_SInt_WideStr:=0;
- if length(S)>255 then
- code:=256
- else
- begin
- SS := S;
- fpc_Val_SInt_WideStr := fpc_Val_SInt_ShortStr(DestSize,SS,Code);
- end;
- end;
- {$ifndef CPU64}
- Function fpc_Val_qword_WideStr (Const S : WideString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
- Var
- SS : ShortString;
- begin
- fpc_Val_qword_WideStr:=0;
- if length(S)>255 then
- code:=256
- else
- begin
- SS := S;
- Val(SS,fpc_Val_qword_WideStr,Code);
- end;
- end;
- Function fpc_Val_int64_WideStr (Const S : WideString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
- Var
- SS : ShortString;
- begin
- fpc_Val_int64_WideStr:=0;
- if length(S)>255 then
- code:=256
- else
- begin
- SS := S;
- Val(SS,fpc_Val_int64_WideStr,Code);
- end;
- end;
- {$endif CPU64}
- procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : StrLenInt;var s : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
- var
- ss : shortstring;
- begin
- str_real(len,fr,d,treal_type(rt),ss);
- s:=ss;
- end;
- {$ifdef STR_USES_VALINT}
- Procedure fpc_WideStr_SInt(v : ValSint; Len : StrLenInt; Var S : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
- {$else}
- Procedure fpc_WideStr_Longint(v : Longint; Len : StrLenInt; Var S : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
- {$endif}
- Var
- SS : ShortString;
- begin
- Str (v:Len,SS);
- S:=SS;
- end;
- {$ifdef STR_USES_VALINT}
- Procedure fpc_WideStr_UInt(v : ValUInt;Len : StrLenInt; Var S : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
- {$else}
- Procedure fpc_WideStr_Longword(v : Longword;Len : StrLenInt; Var S : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
- {$endif}
- Var
- SS : ShortString;
- begin
- str(v:Len,SS);
- S:=SS;
- end;
- {$ifndef CPU64}
- Procedure fpc_WideStr_Int64(v : Int64; Len : StrLenInt; Var S : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
- Var
- SS : ShortString;
- begin
- Str (v:Len,SS);
- S:=SS;
- end;
- Procedure fpc_WideStr_Qword(v : Qword;Len : StrLenInt; Var S : WideString);{$ifdef hascompilerproc} compilerproc; {$endif}
- Var
- SS : ShortString;
- begin
- str(v:Len,SS);
- S:=SS;
- end;
- {$endif CPU64}
- {
- $Log$
- Revision 1.36 2004-04-29 18:59:43 peter
- * str() helpers now also use valint/valuint
- * int64/qword helpers disabled for cpu64
- Revision 1.35 2004/01/22 22:09:05 peter
- * finalize needs to reset to nil after decr_ref
- Revision 1.34 2003/11/29 17:27:05 michael
- + Added overloaded version of SetWideStringManager without old parameter
- Revision 1.33 2003/11/28 20:36:13 michael
- + Added WideStringManager
- Revision 1.32 2003/11/05 15:33:51 florian
- * made Index* usage consistent with astrings.inc
- Revision 1.31 2003/06/17 19:24:08 jonas
- * fixed conversion of fpc_*str_unique to compilerproc
- Revision 1.30 2003/06/17 16:38:53 jonas
- * fpc_{ansistr|widestr}_unique is now a function so it can be used as
- compilerproc
- Revision 1.29 2003/05/01 08:05:23 florian
- * started to make the rtl 64 bit save by introducing SizeInt and SizeUInt (similar to size_t of C)
- Revision 1.28 2002/12/29 16:59:17 peter
- * implemented some more conversions
- Revision 1.27 2002/12/15 22:33:12 peter
- * SetString(WideString,[PChar|PWideChar],Len) added
- Revision 1.26 2002/12/14 19:16:45 sg
- * Ported improvements from the AnsiString equivalents to NewWideString and
- fpc_WideStr_SetLength
- Revision 1.25 2002/12/07 14:35:34 carl
- - avoid warnings (add typecast)
- Revision 1.24 2002/10/10 16:08:50 florian
- + several widestring/pwidechar related helpers added
- Revision 1.23 2002/10/02 18:21:52 peter
- * Copy() changed to internal function calling compilerprocs
- * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
- new copy functions
- Revision 1.22 2002/09/26 21:50:38 florian
- + some WideString<->AnsiString conversion functions added
- Revision 1.21 2002/09/14 11:20:50 carl
- * Delphi compatibility fix (with string routines)
- Revision 1.20 2002/09/07 21:16:45 carl
- * cardinal -> longword
- Revision 1.19 2002/09/07 15:07:46 peter
- * old logs removed and tabs fixed
- Revision 1.18 2002/07/29 21:28:17 florian
- * several fixes to get further with linux/ppc system unit compilation
- Revision 1.17 2002/04/26 15:19:05 peter
- * use saveregisters for incr routines, saves also problems with
- the optimizer
- Revision 1.16 2002/04/25 20:14:57 peter
- * updated compilerprocs
- * incr ref count has now a value argument instead of var
- }
|