wstrings.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2001 by Florian Klaempfl,
  5. member of the Free Pascal development team.
  6. This file implements support routines for WideStrings with FPC
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {
  14. This file contains the implementation of the WideString type,
  15. and all things that are needed for it.
  16. WideString is defined as a 'silent' pwidechar :
  17. a pwidechar that points to :
  18. @-12 : Longint for maximum size;
  19. @-8 : Longint for size;
  20. @-4 : Longint for reference count;
  21. @ : String + Terminating #0;
  22. Pwidechar(Widestring) is a valid typecast.
  23. So WS[i] is converted to the address @WS+i-1.
  24. Constants should be assigned a reference count of -1
  25. Meaning that they can't be disposed of.
  26. }
  27. Type
  28. PWideRec = ^TWideRec;
  29. TWideRec = Packed Record
  30. Maxlen,
  31. len,
  32. ref : Longint;
  33. First : WideChar;
  34. end;
  35. Const
  36. WideRecLen = SizeOf(TWideRec);
  37. WideFirstOff = SizeOf(TWideRec)-sizeof(WideChar);
  38. {
  39. Default WideChar <-> Char conversion is to only convert the
  40. lower 127 chars, all others are translated to spaces.
  41. These routines can be overwritten for the Current Locale
  42. }
  43. procedure Wide2AnsiMove(source:pwidechar;dest:pchar;len:longint);
  44. var
  45. i : longint;
  46. begin
  47. for i:=1to len do
  48. begin
  49. if word(source^)<128 then
  50. dest^:=char(word(source^))
  51. else
  52. dest^:=' ';
  53. inc(dest);
  54. inc(source);
  55. end;
  56. end;
  57. procedure Ansi2WideMove(source:pchar;dest:pwidechar;len:longint);
  58. var
  59. i : longint;
  60. begin
  61. for i:=1to len do
  62. begin
  63. if byte(source^)<128 then
  64. dest^:=widechar(byte(source^))
  65. else
  66. dest^:=' ';
  67. inc(dest);
  68. inc(source);
  69. end;
  70. end;
  71. Type
  72. TWide2AnsiMove=procedure(source:pwidechar;dest:pchar;len:longint);
  73. TAnsi2WideMove=procedure(source:pchar;dest:pwidechar;len:longint);
  74. Const
  75. Wide2AnsiMoveProc:TWide2AnsiMove=@Wide2AnsiMove;
  76. Ansi2WideMoveProc:TAnsi2WideMove=@Ansi2WideMove;
  77. (*
  78. Procedure UniqueWideString(Var S : WideString); [Public,Alias : 'FPC_WIDESTR_UNIQUE'];
  79. {
  80. Make sure reference count of S is 1,
  81. using copy-on-write semantics.
  82. }
  83. begin
  84. end;
  85. *)
  86. {****************************************************************************
  87. Internal functions, not in interface.
  88. ****************************************************************************}
  89. {$ifdef WideStrDebug}
  90. Procedure DumpWideRec(S : Pointer);
  91. begin
  92. If S=Nil then
  93. Writeln ('String is nil')
  94. Else
  95. Begin
  96. With PWideRec(S-WideFirstOff)^ do
  97. begin
  98. Write ('(Maxlen: ',maxlen);
  99. Write (' Len:',len);
  100. Writeln (' Ref: ',ref,')');
  101. end;
  102. end;
  103. end;
  104. {$endif}
  105. Function NewWideString(Len : Longint) : Pointer;
  106. {
  107. Allocate a new WideString on the heap.
  108. initialize it to zero length and reference count 1.
  109. }
  110. Var
  111. P : Pointer;
  112. begin
  113. { Also add +1 for a terminating zero }
  114. GetMem(P,Len+Len+WideRecLen);
  115. If P<>Nil then
  116. begin
  117. PWideRec(P)^.Maxlen:=Len; { Maximal length }
  118. PWideRec(P)^.Len:=0; { Initial length }
  119. PWideRec(P)^.Ref:=1; { Set reference count }
  120. PWideRec(P)^.First:=#0; { Terminating #0 }
  121. inc(p,WideFirstOff); { Points to string now }
  122. end;
  123. NewWideString:=P;
  124. end;
  125. Procedure DisposeWideString(Var S : Pointer);
  126. {
  127. Deallocates a WideString From the heap.
  128. }
  129. begin
  130. If S=Nil then
  131. exit;
  132. Dec (Longint(S),WideFirstOff);
  133. FreeMem (S);
  134. S:=Nil;
  135. end;
  136. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);saveregisters;[Public,Alias:'FPC_WIDESTR_DECR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  137. {
  138. Decreases the ReferenceCount of a non constant widestring;
  139. If the reference count is zero, deallocate the string;
  140. }
  141. Type
  142. plongint = ^longint;
  143. Var
  144. l : plongint;
  145. Begin
  146. { Zero string }
  147. If S=Nil then exit;
  148. { check for constant strings ...}
  149. l:=@PWIDEREC(S-WideFirstOff)^.Ref;
  150. If l^<0 then exit;
  151. { declocked does a MT safe dec and returns true, if the counter is 0 }
  152. If declocked(l^) then
  153. { Ref count dropped to zero }
  154. DisposeWideString (S); { Remove...}
  155. { this pointer is not valid anymore, so set it to zero }
  156. S:=nil;
  157. end;
  158. {$ifdef hascompilerproc}
  159. { alias for internal use }
  160. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);saveregisters;[external name 'FPC_WIDESTR_DECR_REF'];
  161. {$endif compilerproc}
  162. {$ifdef hascompilerproc}
  163. Procedure fpc_WideStr_Incr_Ref (S : Pointer);saveregisters;[Public,Alias:'FPC_WIDESTR_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  164. {$else}
  165. Procedure fpc_WideStr_Incr_Ref (Var S : Pointer);saveregisters;[Public,Alias:'FPC_WIDESTR_INCR_REF'];
  166. {$endif compilerproc}
  167. Begin
  168. If S=Nil then
  169. exit;
  170. { Let's be paranoid : Constant string ??}
  171. If PWideRec(S-WideFirstOff)^.Ref<0 then exit;
  172. inclocked(PWideRec(S-WideFirstOff)^.Ref);
  173. end;
  174. {$ifdef hascompilerproc}
  175. { alias for internal use }
  176. Procedure fpc_WideStr_Incr_Ref (S : Pointer);saveregisters;[external name 'FPC_WIDESTR_INCR_REF'];
  177. {$endif compilerproc}
  178. function fpc_WideStr_To_ShortStr (high_of_res: longint;const S2 : WideString): shortstring;[Public, alias: 'FPC_WIDESTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  179. {
  180. Converts a WideString to a ShortString;
  181. }
  182. Var
  183. Size : Longint;
  184. begin
  185. if S2='' then
  186. fpc_WideStr_To_ShortStr:=''
  187. else
  188. begin
  189. Size:=Length(S2);
  190. If Size>high_of_res then
  191. Size:=high_of_res;
  192. Wide2AnsiMoveProc(PWideChar(S2),PChar(@fpc_WideStr_To_ShortStr[1]),Size);
  193. byte(fpc_WideStr_To_ShortStr[0]):=Size;
  194. end;
  195. end;
  196. Function fpc_ShortStr_To_WideStr (Const S2 : ShortString): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  197. {
  198. Converts a ShortString to a WideString;
  199. }
  200. Var
  201. Size : Longint;
  202. begin
  203. Size:=Length(S2);
  204. Setlength (fpc_ShortStr_To_WideStr,Size);
  205. if Size>0 then
  206. begin
  207. Ansi2WideMoveProc(PChar(@S2[1]),PWideChar(Pointer(fpc_ShortStr_To_WideStr)),Size);
  208. { Terminating Zero }
  209. PWideChar(Pointer(fpc_ShortStr_To_WideStr)+Size*sizeof(WideChar))^:=#0;
  210. end;
  211. end;
  212. { old style helper }
  213. {$ifndef hascompilerproc}
  214. Procedure fpc_ShortStr_To_WideStr (Var S1 : Pointer; Const S2 : ShortString);[Public, alias: 'FPC_SHORTSTR_TO_WIDESTR'];
  215. begin
  216. s1 := pointer(fpc_ShortStr_To_WideStr(s2));
  217. end;
  218. {$endif hascompilerproc}
  219. Function fpc_WideStr_To_AnsiStr (const S2 : WideString): AnsiString; {$ifdef hascompilerproc} compilerproc; {$endif}
  220. {
  221. Converts a WideString to an AnsiString
  222. }
  223. Var
  224. Size : Longint;
  225. begin
  226. if s2='' then
  227. exit;
  228. Size:=Length(WideString(S2));
  229. Setlength (fpc_WideStr_To_AnsiStr,Size);
  230. if Size>0 then
  231. begin
  232. Wide2AnsiMoveProc(PWideChar(Pointer(S2)),PChar(Pointer(fpc_WideStr_To_AnsiStr)),Size);
  233. { Terminating Zero }
  234. PChar(Pointer(fpc_WideStr_To_AnsiStr)+Size)^:=#0;
  235. end;
  236. end;
  237. { old style helper }
  238. {$ifndef hascompilerproc}
  239. Procedure fpc_WideStr_To_AnsiStr (Var S1 : Pointer;const S2 : WideString);[Public, alias: 'FPC_WIDESTR_TO_ANSISTR'];
  240. begin
  241. s1 := pointer(fpc_WideStr_To_AnsiStr(s2));
  242. end;
  243. {$endif hascompilerproc}
  244. Function fpc_AnsiStr_To_WideStr (Const S2 : AnsiString): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  245. {
  246. Converts an AnsiString to a WideString;
  247. }
  248. Var
  249. Size : Longint;
  250. begin
  251. if s2='' then
  252. exit;
  253. Size:=Length(S2);
  254. Setlength (fpc_AnsiStr_To_WideStr,Size);
  255. if Size>0 then
  256. begin
  257. Ansi2WideMoveProc(PChar(S2),PWideChar(Pointer(fpc_AnsiStr_To_WideStr)),Size);
  258. { Terminating Zero }
  259. PWideChar(Pointer(fpc_AnsiStr_To_WideStr)+Size*sizeof(WideChar))^:=#0;
  260. end;
  261. end;
  262. { old style helper }
  263. {$ifndef hascompilerproc}
  264. Procedure fpc_AnsiStr_To_WideStr (Var S1 : Pointer; Const S2 : AnsiString);[Public, alias: 'FPC_ANSISTR_TO_WIDESTR'];
  265. begin
  266. s1 := pointer(fpc_AnsiStr_To_WideStr(s2));
  267. end;
  268. {$endif hascompilerproc}
  269. { checked against the ansistring routine, 2001-05-27 (FK) }
  270. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_WIDESTR_ASSIGN']; {$ifdef hascompilerproc} compilerproc; {$endif}
  271. {
  272. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  273. }
  274. begin
  275. If S2<>nil then
  276. If PWideRec(S2-WideFirstOff)^.Ref>0 then
  277. Inc(PWideRec(S2-WideFirstOff)^.ref);
  278. { Decrease the reference count on the old S1 }
  279. fpc_widestr_decr_ref (S1);
  280. { And finally, have S1 pointing to S2 (or its copy) }
  281. S1:=S2;
  282. end;
  283. {$ifdef hascompilerproc}
  284. { alias for internal use }
  285. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_WIDESTR_ASSIGN'];
  286. {$endif hascompilerproc}
  287. { checked against the ansistring routine, 2001-05-27 (FK) }
  288. {$ifdef hascompilerproc}
  289. function fpc_WideStr_Concat (const S1,S2 : WideString): WideString; compilerproc;
  290. var
  291. S3: WideString absolute result;
  292. {$else hascompilerproc}
  293. Procedure fpc_WideStr_Concat (S1,S2 : WideString;var S3 : WideString);[Public, alias: 'FPC_WIDESTR_CONCAT'];
  294. {$endif hascompilerproc}
  295. {
  296. Concatenates 2 WideStrings : S1+S2.
  297. Result Goes to S3;
  298. }
  299. Var
  300. Size,Location : Longint;
  301. begin
  302. { only assign if s1 or s2 is empty }
  303. if (S1='') then
  304. S3 := S2
  305. else
  306. if (S2='') then
  307. S3 := S1
  308. else
  309. begin
  310. { create new result }
  311. Size:=Length(S2);
  312. Location:=Length(S1);
  313. SetLength (S3,Size+Location);
  314. Move (S1[1],S3[1],Location*sizeof(WideChar));
  315. Move (S2[1],S3[location+1],(Size+1)*sizeof(WideChar));
  316. end;
  317. end;
  318. Function fpc_Char_To_WideStr(const c : Char): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  319. {
  320. Converts a Char to a WideString;
  321. }
  322. begin
  323. if c = #0 then
  324. { result is automatically set to '' }
  325. exit;
  326. Setlength (fpc_Char_To_WideStr,1);
  327. fpc_Char_To_WideStr[1]:=c;
  328. { Terminating Zero }
  329. PWideChar(Pointer(fpc_Char_To_WideStr)+sizeof(WideChar))^:=#0;
  330. end;
  331. { old style helper }
  332. {$ifndef hascompilerproc}
  333. Procedure fpc_Char_To_WideStr(var S1 : Pointer; c : Char);[Public, alias: 'FPC_CHAR_TO_WIDESTR'];
  334. begin
  335. s1 := pointer(fpc_Char_To_WideStr(c));
  336. end;
  337. {$endif hascompilerproc}
  338. Function fpc_PChar_To_WideStr(const p : pchar): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  339. Var
  340. L : Longint;
  341. begin
  342. if (not assigned(p)) or (p[0]=#0) Then
  343. { result is automatically set to '' }
  344. exit;
  345. l:=IndexChar(p^,-1,#0);
  346. SetLength(fpc_PChar_To_WideStr,L);
  347. Ansi2WideMoveProc(P,PWideChar(Pointer(fpc_PChar_To_WideStr)),l);
  348. end;
  349. { old style helper }
  350. {$ifndef hascompilerproc}
  351. Procedure fpc_PChar_To_WideStr(var a : WideString;p : pchar);[Public,Alias : 'FPC_PCHAR_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  352. begin
  353. pointer(a) := pointer(fpc_PChar_To_WideStr(p));
  354. end;
  355. {$endif hascompilerproc}
  356. Function fpc_CharArray_To_WideStr(const arr: array of char): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  357. var
  358. i : longint;
  359. begin
  360. if arr[0]=#0 Then
  361. { result is automatically set to '' }
  362. exit;
  363. i:=IndexChar(arr,high(arr)+1,#0);
  364. if i = -1 then
  365. i := high(arr)+1;
  366. SetLength(fpc_CharArray_To_WideStr,i);
  367. Ansi2WideMoveProc (pchar(@arr),PWideChar(Pointer(fpc_CharArray_To_WideStr)),i);
  368. end;
  369. { old style helper }
  370. {$ifndef hascompilerproc}
  371. Procedure fpc_CharArray_To_WideStr(var a : WideString; p: pointer; len: longint); [Public,Alias : 'FPC_CHARARRAY_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  372. var
  373. src: pchar;
  374. i: longint;
  375. begin
  376. src := pchar(p);
  377. if src[0]=#0 Then
  378. begin
  379. pointer(a) := nil;
  380. exit;
  381. end;
  382. i:=IndexChar(src^,len,#0);
  383. if i = -1 then
  384. i := len;
  385. pointer(a) := NewWideString(i);
  386. Ansi2WideMoveProc (src,PWideChar(Pointer(@a[1])),i);
  387. end;
  388. {$endif not hascompilerproc}
  389. {$ifdef hascompilerproc}
  390. { inside the compiler, the resulttype is modified to that of the actual }
  391. { chararray we're converting to (JM) }
  392. function fpc_widestr_to_chararray(arraysize: longint; const src: WideString): fpc_big_chararray;[public,alias: 'FPC_WIDESTR_TO_CHARARRAY']; compilerproc;
  393. var
  394. len: longint;
  395. begin
  396. len := length(src);
  397. if len > arraysize then
  398. len := arraysize;
  399. { make sure we don't dereference src if it can be nil (JM) }
  400. if len > 0 then
  401. wide2ansimoveproc(pwidechar(@src[1]),pchar(@fpc_widestr_to_chararray[0]),len);
  402. fillchar(fpc_widestr_to_chararray[len],arraysize-len,0);
  403. end;
  404. {$endif hascompilerproc}
  405. Function fpc_WideStr_Compare(const S1,S2 : WideString): Longint;[Public,Alias : 'FPC_WIDESTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  406. {
  407. Compares 2 WideStrings;
  408. The result is
  409. <0 if S1<S2
  410. 0 if S1=S2
  411. >0 if S1>S2
  412. }
  413. Var
  414. MaxI,Temp : Longint;
  415. begin
  416. if pointer(S1)=pointer(S2) then
  417. begin
  418. fpc_WideStr_Compare:=0;
  419. exit;
  420. end;
  421. Maxi:=Length(S1);
  422. temp:=Length(S2);
  423. If MaxI>Temp then
  424. MaxI:=Temp;
  425. Temp:=CompareWord(S1[1],S2[1],MaxI);
  426. if temp=0 then
  427. temp:=Length(S1)-Length(S2);
  428. fpc_WideStr_Compare:=Temp;
  429. end;
  430. Procedure fpc_WideStr_CheckZero(p : pointer);[Public,Alias : 'FPC_WIDESTR_CHECKZERO']; {$ifdef hascompilerproc} compilerproc; {$endif}
  431. begin
  432. if p=nil then
  433. HandleErrorFrame(201,get_frame);
  434. end;
  435. Procedure fpc_WideStr_CheckRange(len,index : longint);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
  436. begin
  437. if (index>len) or (Index<1) then
  438. HandleErrorFrame(201,get_frame);
  439. end;
  440. {$ifndef INTERNSETLENGTH}
  441. Procedure SetLength (Var S : WideString; l : Longint);
  442. {$else INTERNSETLENGTH}
  443. Procedure fpc_WideStr_SetLength (Var S : WideString; l : Longint);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  444. {$endif INTERNSETLENGTH}
  445. {
  446. Sets The length of string S to L.
  447. Makes sure S is unique, and contains enough room.
  448. }
  449. Var
  450. Temp : Pointer;
  451. begin
  452. if (l>0) then
  453. begin
  454. if Pointer(S)=nil then
  455. begin
  456. { Need a complete new string...}
  457. Pointer(s):=NewWideString(l);
  458. end
  459. else
  460. If (PWideRec(Pointer(S)-WideFirstOff)^.Maxlen < L) or
  461. (PWideRec(Pointer(S)-WideFirstOff)^.Ref <> 1) then
  462. begin
  463. { Reallocation is needed... }
  464. Temp:=Pointer(NewWideString(L));
  465. if Length(S)>0 then
  466. Move(Pointer(S)^,Temp^,L*sizeof(WideChar));
  467. fpc_WideStr_decr_ref(Pointer(S));
  468. Pointer(S):=Temp;
  469. end;
  470. { Force nil termination in case it gets shorter }
  471. PWideChar(Pointer(S)+l*sizeof(WideChar))^:=#0;
  472. PWideRec(Pointer(S)-WideFirstOff)^.Len:=l;
  473. end
  474. else
  475. begin
  476. { Length=0 }
  477. if Pointer(S)<>nil then
  478. fpc_WideStr_decr_ref (Pointer(S));
  479. Pointer(S):=Nil;
  480. end;
  481. end;
  482. {*****************************************************************************
  483. Public functions, In interface.
  484. *****************************************************************************}
  485. {$ifndef INTERNLENGTH}
  486. Function Length (Const S : WideString) : Longint;
  487. {
  488. Returns the length of an WideString.
  489. Takes in acount that zero strings are NIL;
  490. }
  491. begin
  492. If Pointer(S)=Nil then
  493. Length:=0
  494. else
  495. Length:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  496. end;
  497. {$endif INTERNLENGTH}
  498. { overloaded version of UniqueString for interface }
  499. procedure UniqueString(Var S : WideString); [external name 'FPC_WIDESTR_UNIQUE'];
  500. Procedure fpc_widestr_Unique(Var S : WideString); [Public,Alias : 'FPC_WIDESTR_UNIQUE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  501. {
  502. Make sure reference count of S is 1,
  503. using copy-on-write semantics.
  504. }
  505. Var
  506. SNew : Pointer;
  507. L : Longint;
  508. begin
  509. If Pointer(S)=Nil then
  510. exit;
  511. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  512. begin
  513. L:=PWideRec(Pointer(S)-WideFirstOff)^.len;
  514. SNew:=NewWideString (L);
  515. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  516. PWideRec(SNew-WideFirstOff)^.len:=L;
  517. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  518. Pointer(S):=SNew;
  519. end;
  520. end;
  521. Function Copy (Const S : WideString; Index,Size : Longint) : WideString;
  522. var
  523. ResultAddress : Pointer;
  524. begin
  525. ResultAddress:=Nil;
  526. dec(index);
  527. if Index < 0 then
  528. Index := 0;
  529. { Check Size. Accounts for Zero-length S, the double check is needed because
  530. Size can be maxint and will get <0 when adding index }
  531. if (Size>Length(S)) or
  532. (Index+Size>Length(S)) then
  533. Size:=Length(S)-Index;
  534. If Size>0 then
  535. begin
  536. If Index<0 Then
  537. Index:=0;
  538. ResultAddress:=Pointer(NewWideString (Size));
  539. if ResultAddress<>Nil then
  540. begin
  541. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  542. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size;
  543. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  544. end;
  545. end;
  546. Pointer(Copy):=ResultAddress;
  547. end;
  548. Function Pos (Const Substr : WideString; Const Source : WideString) : Longint;
  549. var
  550. i,MaxLen : StrLenInt;
  551. pc : pwidechar;
  552. begin
  553. Pos:=0;
  554. if Length(SubStr)>0 then
  555. begin
  556. MaxLen:=Length(source)-Length(SubStr);
  557. i:=0;
  558. pc:=@source[1];
  559. while (i<=MaxLen) do
  560. begin
  561. inc(i);
  562. if (SubStr[1]=pc^) and
  563. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  564. begin
  565. Pos:=i;
  566. exit;
  567. end;
  568. inc(pc);
  569. end;
  570. end;
  571. end;
  572. { Faster version for a widechar alone }
  573. Function Pos (c : WideChar; Const s : WideString) : Longint;
  574. var
  575. i: longint;
  576. pc : pwidechar;
  577. begin
  578. pc:=@s[1];
  579. for i:=1 to length(s) do
  580. begin
  581. if pc^=c then
  582. begin
  583. pos:=i;
  584. exit;
  585. end;
  586. inc(pc);
  587. end;
  588. pos:=0;
  589. end;
  590. { Faster version for a char alone. Must be implemented because }
  591. { pos(c: char; const s: shortstring) also exists, so otherwise }
  592. { using pos(char,pchar) will always call the shortstring version }
  593. { (exact match for first argument), also with $h+ (JM) }
  594. Function Pos (c : Char; Const s : WideString) : Longint;
  595. var
  596. i: longint;
  597. wc : widechar;
  598. pc : pwidechar;
  599. begin
  600. wc:=c;
  601. pc:=@s[1];
  602. for i:=1 to length(s) do
  603. begin
  604. if pc^=wc then
  605. begin
  606. pos:=i;
  607. exit;
  608. end;
  609. inc(pc);
  610. end;
  611. pos:=0;
  612. end;
  613. Procedure Delete (Var S : WideString; Index,Size: Longint);
  614. Var
  615. LS : Longint;
  616. begin
  617. If Length(S)=0 then
  618. exit;
  619. if index<=0 then
  620. exit;
  621. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  622. if (Index<=LS) and (Size>0) then
  623. begin
  624. UniqueString (S);
  625. if Size+Index>LS then
  626. Size:=LS-Index+1;
  627. if Index+Size<=LS then
  628. begin
  629. Dec(Index);
  630. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  631. end;
  632. Setlength(s,LS-Size);
  633. end;
  634. end;
  635. Procedure Insert (Const Source : WideString; Var S : WideString; Index : Longint);
  636. var
  637. Temp : WideString;
  638. LS : Longint;
  639. begin
  640. If Length(Source)=0 then
  641. exit;
  642. if index <= 0 then
  643. index := 1;
  644. Ls:=Length(S);
  645. if index > LS then
  646. index := LS+1;
  647. Dec(Index);
  648. Pointer(Temp) := NewWideString(Length(Source)+LS);
  649. SetLength(Temp,Length(Source)+LS);
  650. If Index>0 then
  651. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  652. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  653. If (LS-Index)>0 then
  654. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  655. S:=Temp;
  656. end;
  657. {!!!:Procedure SetString (Var S : WideString; Buf : PWideChar; Len : Longint);
  658. begin
  659. SetLength(S,Len);
  660. Move (Buf[0],S[1],Len*2);
  661. end;}
  662. Function fpc_Val_Real_WideStr(Const S : WideString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  663. Var
  664. SS : String;
  665. begin
  666. fpc_Val_Real_WideStr := 0;
  667. if length(S) > 255 then
  668. code := 256
  669. else
  670. begin
  671. SS := S;
  672. Val(SS,fpc_Val_Real_WideStr,code);
  673. end;
  674. end;
  675. Function fpc_Val_UInt_WideStr (Const S : WideString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  676. Var
  677. SS : ShortString;
  678. begin
  679. fpc_Val_UInt_WideStr := 0;
  680. if length(S) > 255 then
  681. code := 256
  682. else
  683. begin
  684. SS := S;
  685. Val(SS,fpc_Val_UInt_WideStr,code);
  686. end;
  687. end;
  688. Function fpc_Val_SInt_WideStr (DestSize: longint; Const S : WideString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  689. Var
  690. SS : ShortString;
  691. begin
  692. fpc_Val_SInt_WideStr:=0;
  693. if length(S)>255 then
  694. code:=256
  695. else
  696. begin
  697. SS := S;
  698. fpc_Val_SInt_WideStr := fpc_Val_SInt_ShortStr(DestSize,SS,Code);
  699. end;
  700. end;
  701. Function fpc_Val_qword_WideStr (Const S : WideString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  702. Var
  703. SS : ShortString;
  704. begin
  705. fpc_Val_qword_WideStr:=0;
  706. if length(S)>255 then
  707. code:=256
  708. else
  709. begin
  710. SS := S;
  711. Val(SS,fpc_Val_qword_WideStr,Code);
  712. end;
  713. end;
  714. Function fpc_Val_int64_WideStr (Const S : WideString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  715. Var
  716. SS : ShortString;
  717. begin
  718. fpc_Val_int64_WideStr:=0;
  719. if length(S)>255 then
  720. code:=256
  721. else
  722. begin
  723. SS := S;
  724. Val(SS,fpc_Val_int64_WideStr,Code);
  725. end;
  726. end;
  727. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : longint;var s : WideString);[public,alias:'FPC_WIDESTR_FLOAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  728. var
  729. ss : shortstring;
  730. begin
  731. str_real(len,fr,d,treal_type(rt),ss);
  732. s:=ss;
  733. end;
  734. Procedure fpc_WideStr_Longword(C : Longword;Len : Longint; Var S : WideString);[Public,Alias : 'FPC_WIDESTR_CARDINAL']; {$ifdef hascompilerproc} compilerproc; {$endif}
  735. Var
  736. SS : ShortString;
  737. begin
  738. str(C:Len,SS);
  739. S:=SS;
  740. end;
  741. Procedure fpc_WideStr_Longint(L : Longint; Len : Longint; Var S : WideString);[Public,Alias : 'FPC_WIDESTR_LONGINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  742. Var
  743. SS : ShortString;
  744. begin
  745. Str (L:Len,SS);
  746. S:=SS;
  747. end;
  748. {
  749. $Log$
  750. Revision 1.21 2002-09-14 11:20:50 carl
  751. * Delphi compatibility fix (with string routines)
  752. Revision 1.20 2002/09/07 21:16:45 carl
  753. * cardinal -> longword
  754. Revision 1.19 2002/09/07 15:07:46 peter
  755. * old logs removed and tabs fixed
  756. Revision 1.18 2002/07/29 21:28:17 florian
  757. * several fixes to get further with linux/ppc system unit compilation
  758. Revision 1.17 2002/04/26 15:19:05 peter
  759. * use saveregisters for incr routines, saves also problems with
  760. the optimizer
  761. Revision 1.16 2002/04/25 20:14:57 peter
  762. * updated compilerprocs
  763. * incr ref count has now a value argument instead of var
  764. }