wstrings.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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:=1 to 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:=1 to 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. function WideCharToString(S : PWideChar) : AnsiString;
  486. begin
  487. result:=WideCharLenToString(s,Length(WideString(s)));
  488. end;
  489. function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : LongInt) : PWideChar;
  490. begin
  491. if Length(Src)<DestSize then
  492. Ansi2WideMoveProc(PChar(Src),Dest,Length(Src))
  493. else
  494. Ansi2WideMoveProc(PChar(Src),Dest,DestSize);
  495. end;
  496. function WideCharLenToString(S : PWideChar;Len : LongInt) : AnsiString;
  497. begin
  498. SetLength(result,Len);
  499. Wide2AnsiMove(S,PChar(result),Len);
  500. end;
  501. procedure WideCharLenToStrVar(Src : PWideChar;Len : LongInt;var Dest : AnsiString);
  502. begin
  503. Dest:=WideCharLenToString(Src,Len);
  504. end;
  505. procedure WideCharToStrVar(S : PWideChar;var Dest : AnsiString);
  506. begin
  507. Dest:=WideCharToString(S);
  508. end;
  509. {$ifndef INTERNLENGTH}
  510. Function Length (Const S : WideString) : Longint;
  511. {
  512. Returns the length of an WideString.
  513. Takes in acount that zero strings are NIL;
  514. }
  515. begin
  516. If Pointer(S)=Nil then
  517. Length:=0
  518. else
  519. Length:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  520. end;
  521. {$endif INTERNLENGTH}
  522. { overloaded version of UniqueString for interface }
  523. procedure UniqueString(Var S : WideString); [external name 'FPC_WIDESTR_UNIQUE'];
  524. Procedure fpc_widestr_Unique(Var S : WideString); [Public,Alias : 'FPC_WIDESTR_UNIQUE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  525. {
  526. Make sure reference count of S is 1,
  527. using copy-on-write semantics.
  528. }
  529. Var
  530. SNew : Pointer;
  531. L : Longint;
  532. begin
  533. If Pointer(S)=Nil then
  534. exit;
  535. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  536. begin
  537. L:=PWideRec(Pointer(S)-WideFirstOff)^.len;
  538. SNew:=NewWideString (L);
  539. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  540. PWideRec(SNew-WideFirstOff)^.len:=L;
  541. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  542. Pointer(S):=SNew;
  543. end;
  544. end;
  545. {$ifdef interncopy}
  546. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : Longint) : WideString;compilerproc;
  547. {$else}
  548. Function Copy (Const S : WideString; Index,Size : Longint) : WideString;
  549. {$endif}
  550. var
  551. ResultAddress : Pointer;
  552. begin
  553. ResultAddress:=Nil;
  554. dec(index);
  555. if Index < 0 then
  556. Index := 0;
  557. { Check Size. Accounts for Zero-length S, the double check is needed because
  558. Size can be maxint and will get <0 when adding index }
  559. if (Size>Length(S)) or
  560. (Index+Size>Length(S)) then
  561. Size:=Length(S)-Index;
  562. If Size>0 then
  563. begin
  564. If Index<0 Then
  565. Index:=0;
  566. ResultAddress:=Pointer(NewWideString (Size));
  567. if ResultAddress<>Nil then
  568. begin
  569. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  570. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size;
  571. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  572. end;
  573. end;
  574. {$ifdef interncopy}
  575. Pointer(fpc_widestr_Copy):=ResultAddress;
  576. {$else}
  577. Pointer(Copy):=ResultAddress;
  578. {$endif}
  579. end;
  580. Function Pos (Const Substr : WideString; Const Source : WideString) : Longint;
  581. var
  582. i,MaxLen : StrLenInt;
  583. pc : pwidechar;
  584. begin
  585. Pos:=0;
  586. if Length(SubStr)>0 then
  587. begin
  588. MaxLen:=Length(source)-Length(SubStr);
  589. i:=0;
  590. pc:=@source[1];
  591. while (i<=MaxLen) do
  592. begin
  593. inc(i);
  594. if (SubStr[1]=pc^) and
  595. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  596. begin
  597. Pos:=i;
  598. exit;
  599. end;
  600. inc(pc);
  601. end;
  602. end;
  603. end;
  604. { Faster version for a widechar alone }
  605. Function Pos (c : WideChar; Const s : WideString) : Longint;
  606. var
  607. i: longint;
  608. pc : pwidechar;
  609. begin
  610. pc:=@s[1];
  611. for i:=1 to length(s) do
  612. begin
  613. if pc^=c then
  614. begin
  615. pos:=i;
  616. exit;
  617. end;
  618. inc(pc);
  619. end;
  620. pos:=0;
  621. end;
  622. { Faster version for a char alone. Must be implemented because }
  623. { pos(c: char; const s: shortstring) also exists, so otherwise }
  624. { using pos(char,pchar) will always call the shortstring version }
  625. { (exact match for first argument), also with $h+ (JM) }
  626. Function Pos (c : Char; Const s : WideString) : Longint;
  627. var
  628. i: longint;
  629. wc : widechar;
  630. pc : pwidechar;
  631. begin
  632. wc:=c;
  633. pc:=@s[1];
  634. for i:=1 to length(s) do
  635. begin
  636. if pc^=wc then
  637. begin
  638. pos:=i;
  639. exit;
  640. end;
  641. inc(pc);
  642. end;
  643. pos:=0;
  644. end;
  645. Procedure Delete (Var S : WideString; Index,Size: Longint);
  646. Var
  647. LS : Longint;
  648. begin
  649. If Length(S)=0 then
  650. exit;
  651. if index<=0 then
  652. exit;
  653. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  654. if (Index<=LS) and (Size>0) then
  655. begin
  656. UniqueString (S);
  657. if Size+Index>LS then
  658. Size:=LS-Index+1;
  659. if Index+Size<=LS then
  660. begin
  661. Dec(Index);
  662. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  663. end;
  664. Setlength(s,LS-Size);
  665. end;
  666. end;
  667. Procedure Insert (Const Source : WideString; Var S : WideString; Index : Longint);
  668. var
  669. Temp : WideString;
  670. LS : Longint;
  671. begin
  672. If Length(Source)=0 then
  673. exit;
  674. if index <= 0 then
  675. index := 1;
  676. Ls:=Length(S);
  677. if index > LS then
  678. index := LS+1;
  679. Dec(Index);
  680. Pointer(Temp) := NewWideString(Length(Source)+LS);
  681. SetLength(Temp,Length(Source)+LS);
  682. If Index>0 then
  683. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  684. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  685. If (LS-Index)>0 then
  686. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  687. S:=Temp;
  688. end;
  689. {!!!:Procedure SetString (Var S : WideString; Buf : PWideChar; Len : Longint);
  690. begin
  691. SetLength(S,Len);
  692. Move (Buf[0],S[1],Len*2);
  693. end;}
  694. Function fpc_Val_Real_WideStr(Const S : WideString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  695. Var
  696. SS : String;
  697. begin
  698. fpc_Val_Real_WideStr := 0;
  699. if length(S) > 255 then
  700. code := 256
  701. else
  702. begin
  703. SS := S;
  704. Val(SS,fpc_Val_Real_WideStr,code);
  705. end;
  706. end;
  707. Function fpc_Val_UInt_WideStr (Const S : WideString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  708. Var
  709. SS : ShortString;
  710. begin
  711. fpc_Val_UInt_WideStr := 0;
  712. if length(S) > 255 then
  713. code := 256
  714. else
  715. begin
  716. SS := S;
  717. Val(SS,fpc_Val_UInt_WideStr,code);
  718. end;
  719. end;
  720. Function fpc_Val_SInt_WideStr (DestSize: longint; Const S : WideString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  721. Var
  722. SS : ShortString;
  723. begin
  724. fpc_Val_SInt_WideStr:=0;
  725. if length(S)>255 then
  726. code:=256
  727. else
  728. begin
  729. SS := S;
  730. fpc_Val_SInt_WideStr := fpc_Val_SInt_ShortStr(DestSize,SS,Code);
  731. end;
  732. end;
  733. Function fpc_Val_qword_WideStr (Const S : WideString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  734. Var
  735. SS : ShortString;
  736. begin
  737. fpc_Val_qword_WideStr:=0;
  738. if length(S)>255 then
  739. code:=256
  740. else
  741. begin
  742. SS := S;
  743. Val(SS,fpc_Val_qword_WideStr,Code);
  744. end;
  745. end;
  746. Function fpc_Val_int64_WideStr (Const S : WideString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  747. Var
  748. SS : ShortString;
  749. begin
  750. fpc_Val_int64_WideStr:=0;
  751. if length(S)>255 then
  752. code:=256
  753. else
  754. begin
  755. SS := S;
  756. Val(SS,fpc_Val_int64_WideStr,Code);
  757. end;
  758. end;
  759. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : longint;var s : WideString);[public,alias:'FPC_WIDESTR_FLOAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  760. var
  761. ss : shortstring;
  762. begin
  763. str_real(len,fr,d,treal_type(rt),ss);
  764. s:=ss;
  765. end;
  766. Procedure fpc_WideStr_Longword(C : Longword;Len : Longint; Var S : WideString);[Public,Alias : 'FPC_WIDESTR_CARDINAL']; {$ifdef hascompilerproc} compilerproc; {$endif}
  767. Var
  768. SS : ShortString;
  769. begin
  770. str(C:Len,SS);
  771. S:=SS;
  772. end;
  773. Procedure fpc_WideStr_Longint(L : Longint; Len : Longint; Var S : WideString);[Public,Alias : 'FPC_WIDESTR_LONGINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  774. Var
  775. SS : ShortString;
  776. begin
  777. Str (L:Len,SS);
  778. S:=SS;
  779. end;
  780. {
  781. $Log$
  782. Revision 1.23 2002-10-02 18:21:52 peter
  783. * Copy() changed to internal function calling compilerprocs
  784. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  785. new copy functions
  786. Revision 1.22 2002/09/26 21:50:38 florian
  787. + some WideString<->AnsiString conversion functions added
  788. Revision 1.21 2002/09/14 11:20:50 carl
  789. * Delphi compatibility fix (with string routines)
  790. Revision 1.20 2002/09/07 21:16:45 carl
  791. * cardinal -> longword
  792. Revision 1.19 2002/09/07 15:07:46 peter
  793. * old logs removed and tabs fixed
  794. Revision 1.18 2002/07/29 21:28:17 florian
  795. * several fixes to get further with linux/ppc system unit compilation
  796. Revision 1.17 2002/04/26 15:19:05 peter
  797. * use saveregisters for incr routines, saves also problems with
  798. the optimizer
  799. Revision 1.16 2002/04/25 20:14:57 peter
  800. * updated compilerprocs
  801. * incr ref count has now a value argument instead of var
  802. }