wstrings.inc 25 KB

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