wstrings.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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);[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);[external name 'FPC_WIDESTR_DECR_REF'];
  161. {$endif compilerproc}
  162. Procedure fpc_WideStr_Incr_Ref (Var S : Pointer);[Public,Alias:'FPC_WIDESTR_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  163. Begin
  164. If S=Nil then
  165. exit;
  166. { Let's be paranoid : Constant string ??}
  167. If PWideRec(S-WideFirstOff)^.Ref<0 then exit;
  168. inclocked(PWideRec(S-WideFirstOff)^.Ref);
  169. end;
  170. Procedure fpc_WideStr_To_ShortStr (Var S1 : ShortString;S2 : Pointer);[Public, alias: 'FPC_WIDESTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  171. {
  172. Converts a WideString to a ShortString;
  173. }
  174. Var
  175. Size : Longint;
  176. begin
  177. if S2=nil then
  178. S1:=''
  179. else
  180. begin
  181. Size:=PWideRec(S2-FirstOff)^.Len;
  182. If Size>high(S1) then
  183. Size:=high(S1);
  184. Wide2AnsiMoveProc(PWideChar(S2),PChar(@S1[1]),Size);
  185. byte(S1[0]):=Size;
  186. end;
  187. end;
  188. Procedure fpc_ShortStr_To_WideStr (Var S1 : Pointer; Const S2 : ShortString);[Public, alias: 'FPC_SHORTSTR_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  189. {
  190. Converts a ShortString to a WideString;
  191. }
  192. Var
  193. Size : Longint;
  194. begin
  195. Size:=Length(S2);
  196. Setlength (WideString(S1),Size);
  197. if Size>0 then
  198. Ansi2WideMoveProc(PChar(@S2[1]),PWideChar(S1),Size);
  199. end;
  200. Procedure fpc_WideStr_To_AnsiStr (Var S1 : Pointer;S2 : Pointer);[Public, alias: 'FPC_WIDESTR_TO_ANSISTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  201. {
  202. Converts a WideString to an AnsiString
  203. }
  204. Var
  205. Size : Longint;
  206. begin
  207. if s2=nil then
  208. s1:=nil
  209. else
  210. begin
  211. Size:=Length(WideString(S2));
  212. Setlength (AnsiString(S1),Size);
  213. if Size>0 then
  214. begin
  215. Wide2AnsiMoveProc(PWideChar(S2),PChar(S1),Size);
  216. { Terminating Zero }
  217. PChar(S1+Size)^:=#0;
  218. end;
  219. end;
  220. end;
  221. Procedure fpc_AnsiStr_To_WideStr (Var S1 : Pointer; Const S2 : Pointer);[Public, alias: 'FPC_ANSISTR_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  222. {
  223. Converts an AnsiString to a WideString;
  224. }
  225. Var
  226. Size : Longint;
  227. begin
  228. if s2=nil then
  229. s1:=nil
  230. else
  231. begin
  232. Size:=Length(AnsiString(S2));
  233. Setlength (WideString(S1),Size);
  234. if Size>0 then
  235. begin
  236. Ansi2WideMoveProc(PChar(S2),PWideChar(S1),Size);
  237. { Terminating Zero }
  238. PWideChar(S1+Size*sizeof(WideChar))^:=#0;
  239. end;
  240. end;
  241. end;
  242. { checked against the ansistring routine, 2001-05-27 (FK) }
  243. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_WIDESTR_ASSIGN']; {$ifdef hascompilerproc} compilerproc; {$endif}
  244. {
  245. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  246. }
  247. begin
  248. If S2<>nil then
  249. If PWideRec(S2-WideFirstOff)^.Ref>0 then
  250. Inc(PWideRec(S2-WideFirstOff)^.ref);
  251. { Decrease the reference count on the old S1 }
  252. fpc_widestr_decr_ref (S1);
  253. { And finally, have S1 pointing to S2 (or its copy) }
  254. S1:=S2;
  255. end;
  256. {$ifdef hascompilerproc}
  257. { alias for internal use }
  258. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_WIDESTR_ASSIGN'];
  259. {$endif hascompilerproc}
  260. { checked against the ansistring routine, 2001-05-27 (FK) }
  261. Procedure fpc_WideStr_Concat (S1,S2 : Pointer;var S3 : Pointer);[Public, alias: 'FPC_WIDESTR_CONCAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  262. {
  263. Concatenates 2 WideStrings : S1+S2.
  264. Result Goes to S3;
  265. }
  266. Var
  267. Size,Location : Longint;
  268. begin
  269. { only assign if s1 or s2 is empty }
  270. if (S1=Nil) then
  271. fpc_WideStr_Assign(S3,S2)
  272. else
  273. if (S2=Nil) then
  274. fpc_WideStr_Assign(S3,S1)
  275. else
  276. begin
  277. { create new result }
  278. fpc_WideStr_Decr_Ref(S3);
  279. Size:=PWideRec(S2-WideFirstOff)^.Len;
  280. Location:=Length(WideString(S1));
  281. SetLength (WideString(S3),Size+Location);
  282. Move (S1^,S3^,Location*sizeof(WideChar));
  283. Move (S2^,(S3+location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  284. end;
  285. end;
  286. Procedure fpc_Char_To_WideStr(var S1 : Pointer; c : Char);[Public, alias: 'FPC_CHAR_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  287. {
  288. Converts a Char to a WideString;
  289. }
  290. begin
  291. Setlength (WideString(S1),1);
  292. PWideChar(S1)^:=c;
  293. { Terminating Zero }
  294. PWideChar(S1+sizeof(WideChar))^:=#0;
  295. end;
  296. Procedure fpc_PChar_To_WideStr(var a : widestring;p : pchar);[Public,Alias : 'FPC_PCHAR_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  297. Var
  298. L : Longint;
  299. begin
  300. if pointer(a)<>nil then
  301. begin
  302. fpc_WideStr_Decr_Ref(Pointer(a));
  303. pointer(a):=nil;
  304. end;
  305. if (not assigned(p)) or (p[0]=#0) Then
  306. Pointer(a):=nil
  307. else
  308. begin
  309. l:=IndexChar(p^,-1,#0);
  310. Pointer(a):=NewWidestring(L);
  311. SetLength(A,L);
  312. Ansi2WideMoveProc(P,PWideChar(A),L);
  313. end;
  314. end;
  315. Procedure fpc_CharArray_To_WideStr(var a : widestring;p : pchar;l:longint);[Public,Alias : 'FPC_CHARARRAY_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  316. var
  317. i : longint;
  318. begin
  319. if p[0]=#0 Then
  320. Pointer(a):=nil
  321. else
  322. begin
  323. i:=IndexChar(p^,L,#0);
  324. Pointer(a):=NewWidestring(i);
  325. SetLength(a,i);
  326. Ansi2WideMoveProc(P,PWideChar(A),i);
  327. end;
  328. end;
  329. Function fpc_WideStr_Compare(S1,S2 : Pointer): Longint;[Public,Alias : 'FPC_WIDESTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  330. {
  331. Compares 2 WideStrings;
  332. The result is
  333. <0 if S1<S2
  334. 0 if S1=S2
  335. >0 if S1>S2
  336. }
  337. Var
  338. MaxI,Temp : Longint;
  339. begin
  340. if S1=S2 then
  341. begin
  342. fpc_WideStr_Compare:=0;
  343. exit;
  344. end;
  345. Maxi:=Length(WideString(S1));
  346. temp:=Length(WideString(S2));
  347. If MaxI>Temp then
  348. MaxI:=Temp;
  349. Temp:=CompareWord(S1^,S2^,MaxI);
  350. if temp=0 then
  351. temp:=Length(WideString(S1))-Length(WideString(S2));
  352. fpc_WideStr_Compare:=Temp;
  353. end;
  354. Procedure fpc_WideStr_CheckZero(p : pointer);[Public,Alias : 'FPC_WIDESTR_CHECKZERO']; {$ifdef hascompilerproc} compilerproc; {$endif}
  355. begin
  356. if p=nil then
  357. HandleErrorFrame(201,get_frame);
  358. end;
  359. Procedure fpc_WideStr_CheckRange(len,index : longint);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
  360. begin
  361. if (index>len) or (Index<1) then
  362. HandleErrorFrame(201,get_frame);
  363. end;
  364. {$ifndef INTERNSETLENGTH}
  365. Procedure SetLength (Var S : WideString; l : Longint);
  366. {$else INTERNSETLENGTH}
  367. Procedure fpc_WideStr_SetLength (Var S : WideString; l : Longint);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  368. {$endif INTERNSETLENGTH}
  369. {
  370. Sets The length of string S to L.
  371. Makes sure S is unique, and contains enough room.
  372. }
  373. Var
  374. Temp : Pointer;
  375. begin
  376. if (l>0) then
  377. begin
  378. if Pointer(S)=nil then
  379. begin
  380. { Need a complete new string...}
  381. Pointer(s):=NewWideString(l);
  382. end
  383. else
  384. If (PWideRec(Pointer(S)-WideFirstOff)^.Maxlen < L) or
  385. (PWideRec(Pointer(S)-WideFirstOff)^.Ref <> 1) then
  386. begin
  387. { Reallocation is needed... }
  388. Temp:=Pointer(NewWideString(L));
  389. if Length(S)>0 then
  390. Move(Pointer(S)^,Temp^,L*sizeof(WideChar));
  391. fpc_WideStr_decr_ref(Pointer(S));
  392. Pointer(S):=Temp;
  393. end;
  394. { Force nil termination in case it gets shorter }
  395. PWideChar(Pointer(S)+l*sizeof(WideChar))^:=#0;
  396. PWideRec(Pointer(S)-WideFirstOff)^.Len:=l;
  397. end
  398. else
  399. begin
  400. { Length=0 }
  401. if Pointer(S)<>nil then
  402. fpc_WideStr_decr_ref (Pointer(S));
  403. Pointer(S):=Nil;
  404. end;
  405. end;
  406. {*****************************************************************************
  407. Public functions, In interface.
  408. *****************************************************************************}
  409. {$ifndef INTERNLENGTH}
  410. Function Length (Const S : WideString) : Longint;
  411. {
  412. Returns the length of an WideString.
  413. Takes in acount that zero strings are NIL;
  414. }
  415. begin
  416. If Pointer(S)=Nil then
  417. Length:=0
  418. else
  419. Length:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  420. end;
  421. {$endif INTERNLENGTH}
  422. { overloaded version of UniqueString for interface }
  423. procedure UniqueString(Var S : WideString); [external name 'FPC_WIDESTR_UNIQUE'];
  424. Procedure fpc_widestr_Unique(Var S : WideString); [Public,Alias : 'FPC_WIDESTR_UNIQUE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  425. {
  426. Make sure reference count of S is 1,
  427. using copy-on-write semantics.
  428. }
  429. Var
  430. SNew : Pointer;
  431. L : Longint;
  432. begin
  433. If Pointer(S)=Nil then
  434. exit;
  435. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  436. begin
  437. L:=PWideRec(Pointer(S)-WideFirstOff)^.len;
  438. SNew:=NewWideString (L);
  439. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  440. PWideRec(SNew-WideFirstOff)^.len:=L;
  441. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  442. Pointer(S):=SNew;
  443. end;
  444. end;
  445. Function Copy (Const S : WideString; Index,Size : Longint) : WideString;
  446. var
  447. ResultAddress : Pointer;
  448. begin
  449. ResultAddress:=Nil;
  450. dec(index);
  451. if Index < 0 then
  452. Index := 0;
  453. { Check Size. Accounts for Zero-length S, the double check is needed because
  454. Size can be maxint and will get <0 when adding index }
  455. if (Size>Length(S)) or
  456. (Index+Size>Length(S)) then
  457. Size:=Length(S)-Index;
  458. If Size>0 then
  459. begin
  460. If Index<0 Then
  461. Index:=0;
  462. ResultAddress:=Pointer(NewWideString (Size));
  463. if ResultAddress<>Nil then
  464. begin
  465. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  466. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size;
  467. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  468. end;
  469. end;
  470. Pointer(Copy):=ResultAddress;
  471. end;
  472. Function Pos (Const Substr : WideString; Const Source : WideString) : Longint;
  473. var
  474. i,MaxLen : StrLenInt;
  475. pc : pwidechar;
  476. begin
  477. Pos:=0;
  478. if Length(SubStr)>0 then
  479. begin
  480. MaxLen:=Length(source)-Length(SubStr);
  481. i:=0;
  482. pc:=@source[1];
  483. while (i<=MaxLen) do
  484. begin
  485. inc(i);
  486. if (SubStr[1]=pc^) and
  487. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  488. begin
  489. Pos:=i;
  490. exit;
  491. end;
  492. inc(pc);
  493. end;
  494. end;
  495. end;
  496. { Faster version for a widechar alone }
  497. Function Pos (c : WideChar; Const s : WideString) : Longint;
  498. var
  499. i: longint;
  500. pc : pwidechar;
  501. begin
  502. pc:=@s[1];
  503. for i:=1 to length(s) do
  504. begin
  505. if pc^=c then
  506. begin
  507. pos:=i;
  508. exit;
  509. end;
  510. inc(pc);
  511. end;
  512. pos:=0;
  513. end;
  514. { Faster version for a char alone. Must be implemented because }
  515. { pos(c: char; const s: shortstring) also exists, so otherwise }
  516. { using pos(char,pchar) will always call the shortstring version }
  517. { (exact match for first argument), also with $h+ (JM) }
  518. Function Pos (c : Char; Const s : WideString) : Longint;
  519. var
  520. i: longint;
  521. wc : widechar;
  522. pc : pwidechar;
  523. begin
  524. wc:=c;
  525. pc:=@s[1];
  526. for i:=1 to length(s) do
  527. begin
  528. if pc^=wc then
  529. begin
  530. pos:=i;
  531. exit;
  532. end;
  533. inc(pc);
  534. end;
  535. pos:=0;
  536. end;
  537. Procedure Delete (Var S : WideString; Index,Size: Longint);
  538. Var
  539. LS : Longint;
  540. begin
  541. If Length(S)=0 then
  542. exit;
  543. if index<=0 then
  544. begin
  545. inc(Size,index-1);
  546. index:=1;
  547. end;
  548. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  549. if (Index<=LS) and (Size>0) then
  550. begin
  551. UniqueString (S);
  552. if Size+Index>LS then
  553. Size:=LS-Index+1;
  554. if Index+Size<=LS then
  555. begin
  556. Dec(Index);
  557. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  558. end;
  559. Setlength(s,LS-Size);
  560. end;
  561. end;
  562. Procedure Insert (Const Source : WideString; Var S : WideString; Index : Longint);
  563. var
  564. Temp : WideString;
  565. LS : Longint;
  566. begin
  567. If Length(Source)=0 then
  568. exit;
  569. if index <= 0 then
  570. index := 1;
  571. Ls:=Length(S);
  572. if index > LS then
  573. index := LS+1;
  574. Dec(Index);
  575. Pointer(Temp) := NewWideString(Length(Source)+LS);
  576. SetLength(Temp,Length(Source)+LS);
  577. If Index>0 then
  578. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  579. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  580. If (LS-Index)>0 then
  581. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  582. S:=Temp;
  583. end;
  584. {!!!:Procedure SetString (Var S : WideString; Buf : PWideChar; Len : Longint);
  585. begin
  586. SetLength(S,Len);
  587. Move (Buf[0],S[1],Len*2);
  588. end;}
  589. Function fpc_Val_Real_WideStr(Const S : WideString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  590. Var
  591. SS : String;
  592. begin
  593. fpc_Val_Real_WideStr := 0;
  594. if length(S) > 255 then
  595. code := 256
  596. else
  597. begin
  598. SS := S;
  599. Val(SS,fpc_Val_Real_WideStr,code);
  600. end;
  601. end;
  602. Function fpc_Val_UInt_WideStr (Const S : WideString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  603. Var
  604. SS : ShortString;
  605. begin
  606. fpc_Val_UInt_WideStr := 0;
  607. if length(S) > 255 then
  608. code := 256
  609. else
  610. begin
  611. SS := S;
  612. Val(SS,fpc_Val_UInt_WideStr,code);
  613. end;
  614. end;
  615. Function fpc_Val_SInt_WideStr (DestSize: longint; Const S : WideString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  616. Var
  617. SS : ShortString;
  618. begin
  619. fpc_Val_SInt_WideStr:=0;
  620. if length(S)>255 then
  621. code:=256
  622. else
  623. begin
  624. SS := S;
  625. fpc_Val_SInt_WideStr := fpc_Val_SInt_ShortStr(DestSize,SS,Code);
  626. end;
  627. end;
  628. Function fpc_Val_UInt64_WideStr (Const S : WideString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  629. Var
  630. SS : ShortString;
  631. begin
  632. fpc_Val_UInt64_WideStr:=0;
  633. if length(S)>255 then
  634. code:=256
  635. else
  636. begin
  637. SS := S;
  638. Val(SS,fpc_Val_UInt64_WideStr,Code);
  639. end;
  640. end;
  641. Function fpc_Val_SInt64_WideStr (Const S : WideString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  642. Var
  643. SS : ShortString;
  644. begin
  645. fpc_Val_SInt64_WideStr:=0;
  646. if length(S)>255 then
  647. code:=256
  648. else
  649. begin
  650. SS := S;
  651. Val(SS,fpc_Val_SInt64_WideStr,Code);
  652. end;
  653. end;
  654. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : longint;var s : WideString);[public,alias:'FPC_WIDESTR_FLOAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  655. var
  656. ss : shortstring;
  657. begin
  658. str_real(len,fr,d,treal_type(rt),ss);
  659. s:=ss;
  660. end;
  661. Procedure fpc_WideStr_Cardinal(C : Cardinal;Len : Longint; Var S : WideString);[Public,Alias : 'FPC_WIDESTR_CARDINAL']; {$ifdef hascompilerproc} compilerproc; {$endif}
  662. Var
  663. SS : ShortString;
  664. begin
  665. str(C:Len,SS);
  666. S:=SS;
  667. end;
  668. Procedure fpc_WideStr_Longint(L : Longint; Len : Longint; Var S : WideString);[Public,Alias : 'FPC_WIDESTR_LONGINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  669. Var
  670. SS : ShortString;
  671. begin
  672. Str (L:Len,SS);
  673. S:=SS;
  674. end;
  675. {
  676. $Log$
  677. Revision 1.11 2001-08-01 15:00:11 jonas
  678. + "compproc" helpers
  679. * renamed several helpers so that their name is the same as their
  680. "public alias", which should facilitate the conversion of processor
  681. specific code in the code generator to processor independent code
  682. * some small fixes to the val_ansistring and val_widestring helpers
  683. (always immediately exit if the source string is longer than 255
  684. chars)
  685. * fixed fpc_dynarray_high and fpc_dynarray_length if the dynarray is
  686. still nil (used to crash, now return resp -1 and 0)
  687. Revision 1.10 2001/07/16 12:33:08 jonas
  688. * fixed wrong public alieases for val(widestring,...)
  689. Revision 1.9 2001/07/09 21:15:41 peter
  690. * Length made internal
  691. * Add array support for Length
  692. Revision 1.8 2001/07/08 21:00:18 peter
  693. * various widestring updates, it works now mostly without charset
  694. mapping supported
  695. Revision 1.7 2001/05/27 14:28:03 florian
  696. + some procedures added
  697. Revision 1.6 2000/11/06 23:17:15 peter
  698. * removed some warnings
  699. Revision 1.5 2000/11/06 20:34:24 peter
  700. * changed ver1_0 defines to temporary defs
  701. Revision 1.4 2000/10/21 18:20:17 florian
  702. * a lot of small changes:
  703. - setlength is internal
  704. - win32 graph unit extended
  705. ....
  706. Revision 1.3 2000/08/08 22:12:36 sg
  707. * Implemented WideString helper functions (but they are not tested yet
  708. due to the lack of full compiler support for WideString/WideChar!)
  709. Revision 1.2 2000/07/13 11:33:46 michael
  710. + removed logs
  711. }