wstrings.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. l : Longint;
  113. begin
  114. { request a multiple of 16 because the heap manager alloctes anyways chunks of 16 bytes }
  115. L := (Len*sizeof(WideChar)+WideRecLen+15) and (not 15);
  116. GetMem(P,l);
  117. If P<>Nil then
  118. begin
  119. PWideRec(P)^.Maxlen:=(l-WideRecLen) div sizeof(WideChar); { Maximal length }
  120. PWideRec(P)^.Len:=0; { Initial length }
  121. PWideRec(P)^.Ref:=1; { Set reference count }
  122. PWideRec(P)^.First:=#0; { Terminating #0 }
  123. inc(p,WideFirstOff); { Points to string now }
  124. end;
  125. NewWideString:=P;
  126. end;
  127. Procedure DisposeWideString(Var S : Pointer);
  128. {
  129. Deallocates a WideString From the heap.
  130. }
  131. begin
  132. If S=Nil then
  133. exit;
  134. Dec (S,WideFirstOff);
  135. FreeMem (S);
  136. S:=Nil;
  137. end;
  138. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);saveregisters;[Public,Alias:'FPC_WIDESTR_DECR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  139. {
  140. Decreases the ReferenceCount of a non constant widestring;
  141. If the reference count is zero, deallocate the string;
  142. }
  143. Type
  144. plongint = ^longint;
  145. Var
  146. l : plongint;
  147. Begin
  148. { Zero string }
  149. If S=Nil then exit;
  150. { check for constant strings ...}
  151. l:=@PWIDEREC(S-WideFirstOff)^.Ref;
  152. If l^<0 then exit;
  153. { declocked does a MT safe dec and returns true, if the counter is 0 }
  154. If declocked(l^) then
  155. { Ref count dropped to zero }
  156. DisposeWideString (S); { Remove...}
  157. { this pointer is not valid anymore, so set it to zero }
  158. S:=nil;
  159. end;
  160. {$ifdef hascompilerproc}
  161. { alias for internal use }
  162. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);saveregisters;[external name 'FPC_WIDESTR_DECR_REF'];
  163. {$endif compilerproc}
  164. {$ifdef hascompilerproc}
  165. Procedure fpc_WideStr_Incr_Ref (S : Pointer);saveregisters;[Public,Alias:'FPC_WIDESTR_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  166. {$else}
  167. Procedure fpc_WideStr_Incr_Ref (Var S : Pointer);saveregisters;[Public,Alias:'FPC_WIDESTR_INCR_REF'];
  168. {$endif compilerproc}
  169. Begin
  170. If S=Nil then
  171. exit;
  172. { Let's be paranoid : Constant string ??}
  173. If PWideRec(S-WideFirstOff)^.Ref<0 then exit;
  174. inclocked(PWideRec(S-WideFirstOff)^.Ref);
  175. end;
  176. {$ifdef hascompilerproc}
  177. { alias for internal use }
  178. Procedure fpc_WideStr_Incr_Ref (S : Pointer);saveregisters;[external name 'FPC_WIDESTR_INCR_REF'];
  179. {$endif compilerproc}
  180. function fpc_WideStr_To_ShortStr (high_of_res: longint;const S2 : WideString): shortstring;[Public, alias: 'FPC_WIDESTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  181. {
  182. Converts a WideString to a ShortString;
  183. }
  184. Var
  185. Size : Longint;
  186. begin
  187. if S2='' then
  188. fpc_WideStr_To_ShortStr:=''
  189. else
  190. begin
  191. Size:=Length(S2);
  192. If Size>high_of_res then
  193. Size:=high_of_res;
  194. Wide2AnsiMoveProc(PWideChar(S2),PChar(@fpc_WideStr_To_ShortStr[1]),Size);
  195. byte(fpc_WideStr_To_ShortStr[0]):=byte(Size);
  196. end;
  197. end;
  198. Function fpc_ShortStr_To_WideStr (Const S2 : ShortString): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  199. {
  200. Converts a ShortString to a WideString;
  201. }
  202. Var
  203. Size : Longint;
  204. begin
  205. Size:=Length(S2);
  206. Setlength (fpc_ShortStr_To_WideStr,Size);
  207. if Size>0 then
  208. begin
  209. Ansi2WideMoveProc(PChar(@S2[1]),PWideChar(Pointer(fpc_ShortStr_To_WideStr)),Size);
  210. { Terminating Zero }
  211. PWideChar(Pointer(fpc_ShortStr_To_WideStr)+Size*sizeof(WideChar))^:=#0;
  212. end;
  213. end;
  214. { old style helper }
  215. {$ifndef hascompilerproc}
  216. Procedure fpc_ShortStr_To_WideStr (Var S1 : Pointer; Const S2 : ShortString);[Public, alias: 'FPC_SHORTSTR_TO_WIDESTR'];
  217. begin
  218. s1 := pointer(fpc_ShortStr_To_WideStr(s2));
  219. end;
  220. {$endif hascompilerproc}
  221. Function fpc_WideStr_To_AnsiStr (const S2 : WideString): AnsiString; {$ifdef hascompilerproc} compilerproc; {$endif}
  222. {
  223. Converts a WideString to an AnsiString
  224. }
  225. Var
  226. Size : Longint;
  227. begin
  228. if s2='' then
  229. exit;
  230. Size:=Length(WideString(S2));
  231. Setlength (fpc_WideStr_To_AnsiStr,Size);
  232. if Size>0 then
  233. begin
  234. Wide2AnsiMoveProc(PWideChar(Pointer(S2)),PChar(Pointer(fpc_WideStr_To_AnsiStr)),Size);
  235. { Terminating Zero }
  236. PChar(Pointer(fpc_WideStr_To_AnsiStr)+Size)^:=#0;
  237. end;
  238. end;
  239. { old style helper }
  240. {$ifndef hascompilerproc}
  241. Procedure fpc_WideStr_To_AnsiStr (Var S1 : Pointer;const S2 : WideString);[Public, alias: 'FPC_WIDESTR_TO_ANSISTR'];
  242. begin
  243. s1 := pointer(fpc_WideStr_To_AnsiStr(s2));
  244. end;
  245. {$endif hascompilerproc}
  246. Function fpc_AnsiStr_To_WideStr (Const S2 : AnsiString): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  247. {
  248. Converts an AnsiString to a WideString;
  249. }
  250. Var
  251. Size : Longint;
  252. begin
  253. if s2='' then
  254. exit;
  255. Size:=Length(S2);
  256. Setlength (result,Size);
  257. if Size>0 then
  258. begin
  259. Ansi2WideMoveProc(PChar(S2),PWideChar(Pointer(result)),Size);
  260. { Terminating Zero }
  261. PWideChar(Pointer(result)+Size*sizeof(WideChar))^:=#0;
  262. end;
  263. end;
  264. { compilers with widestrings should have compiler procs }
  265. Function fpc_PWideChar_To_AnsiStr(const p : pwidechar): ansistring; compilerproc;
  266. var
  267. Size : longint;
  268. begin
  269. if p=nil then
  270. exit;
  271. Size := IndexWord(p^, $7fffffff, 0);
  272. Setlength (result,Size);
  273. if Size>0 then
  274. begin
  275. Wide2AnsiMoveProc(P,PChar(Pointer(result)),Size);
  276. { Terminating Zero }
  277. PChar(Pointer(result)+Size)^:=#0;
  278. end;
  279. end;
  280. Function fpc_PWideChar_To_WideStr(const p : pwidechar): widestring; compilerproc;
  281. var
  282. Size : longint;
  283. begin
  284. if p=nil then
  285. exit;
  286. Size := IndexWord(p^, $7fffffff, 0);
  287. Setlength (result,Size);
  288. if Size>0 then
  289. begin
  290. Move(p^,PWideChar(Pointer(result))^,Size*sizeof(WideChar));
  291. { Terminating Zero }
  292. PWideChar(Pointer(result)+Size*sizeof(WideChar))^:=#0;
  293. end;
  294. end;
  295. Function fpc_PWideChar_To_ShortStr(const p : pwidechar): shortstring; compilerproc;
  296. var
  297. Size : longint;
  298. begin
  299. if p=nil then
  300. begin
  301. fpc_PWideChar_To_ShortStr:='';
  302. exit;
  303. end;
  304. Size := IndexWord(p^, $7fffffff, 0);
  305. Setlength (result,Size+1);
  306. if Size>0 then
  307. begin
  308. If Size>255 then
  309. Size:=255;
  310. Wide2AnsiMoveProc(p,PChar(@result[1]),Size);
  311. byte(result[0]):=byte(Size);
  312. end;
  313. end;
  314. { old style helper }
  315. {$ifndef hascompilerproc}
  316. Procedure fpc_AnsiStr_To_WideStr (Var S1 : Pointer; Const S2 : AnsiString);[Public, alias: 'FPC_ANSISTR_TO_WIDESTR'];
  317. begin
  318. s1 := pointer(fpc_AnsiStr_To_WideStr(s2));
  319. end;
  320. {$endif hascompilerproc}
  321. { checked against the ansistring routine, 2001-05-27 (FK) }
  322. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_WIDESTR_ASSIGN']; {$ifdef hascompilerproc} compilerproc; {$endif}
  323. {
  324. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  325. }
  326. begin
  327. If S2<>nil then
  328. If PWideRec(S2-WideFirstOff)^.Ref>0 then
  329. Inc(PWideRec(S2-WideFirstOff)^.ref);
  330. { Decrease the reference count on the old S1 }
  331. fpc_widestr_decr_ref (S1);
  332. { And finally, have S1 pointing to S2 (or its copy) }
  333. S1:=S2;
  334. end;
  335. {$ifdef hascompilerproc}
  336. { alias for internal use }
  337. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_WIDESTR_ASSIGN'];
  338. {$endif hascompilerproc}
  339. { checked against the ansistring routine, 2001-05-27 (FK) }
  340. {$ifdef hascompilerproc}
  341. function fpc_WideStr_Concat (const S1,S2 : WideString): WideString; compilerproc;
  342. var
  343. S3: WideString absolute result;
  344. {$else hascompilerproc}
  345. Procedure fpc_WideStr_Concat (S1,S2 : WideString;var S3 : WideString);[Public, alias: 'FPC_WIDESTR_CONCAT'];
  346. {$endif hascompilerproc}
  347. {
  348. Concatenates 2 WideStrings : S1+S2.
  349. Result Goes to S3;
  350. }
  351. Var
  352. Size,Location : Longint;
  353. begin
  354. { only assign if s1 or s2 is empty }
  355. if (S1='') then
  356. S3 := S2
  357. else
  358. if (S2='') then
  359. S3 := S1
  360. else
  361. begin
  362. { create new result }
  363. Size:=Length(S2);
  364. Location:=Length(S1);
  365. SetLength (S3,Size+Location);
  366. Move (S1[1],S3[1],Location*sizeof(WideChar));
  367. Move (S2[1],S3[location+1],(Size+1)*sizeof(WideChar));
  368. end;
  369. end;
  370. Function fpc_Char_To_WideStr(const c : Char): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  371. {
  372. Converts a Char to a WideString;
  373. }
  374. begin
  375. if c = #0 then
  376. { result is automatically set to '' }
  377. exit;
  378. Setlength (fpc_Char_To_WideStr,1);
  379. fpc_Char_To_WideStr[1]:=c;
  380. { Terminating Zero }
  381. PWideChar(Pointer(fpc_Char_To_WideStr)+sizeof(WideChar))^:=#0;
  382. end;
  383. { old style helper }
  384. {$ifndef hascompilerproc}
  385. Procedure fpc_Char_To_WideStr(var S1 : Pointer; c : Char);[Public, alias: 'FPC_CHAR_TO_WIDESTR'];
  386. begin
  387. s1 := pointer(fpc_Char_To_WideStr(c));
  388. end;
  389. {$endif hascompilerproc}
  390. Function fpc_PChar_To_WideStr(const p : pchar): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  391. Var
  392. L : Longint;
  393. begin
  394. if (not assigned(p)) or (p[0]=#0) Then
  395. { result is automatically set to '' }
  396. exit;
  397. l:=IndexChar(p^,-1,#0);
  398. SetLength(fpc_PChar_To_WideStr,L);
  399. Ansi2WideMoveProc(P,PWideChar(Pointer(fpc_PChar_To_WideStr)),l);
  400. end;
  401. { old style helper }
  402. {$ifndef hascompilerproc}
  403. Procedure fpc_PChar_To_WideStr(var a : WideString;p : pchar);[Public,Alias : 'FPC_PCHAR_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  404. begin
  405. pointer(a) := pointer(fpc_PChar_To_WideStr(p));
  406. end;
  407. {$endif hascompilerproc}
  408. Function fpc_CharArray_To_WideStr(const arr: array of char): WideString; {$ifdef hascompilerproc} compilerproc; {$endif}
  409. var
  410. i : longint;
  411. begin
  412. if arr[0]=#0 Then
  413. { result is automatically set to '' }
  414. exit;
  415. i:=IndexChar(arr,high(arr)+1,#0);
  416. if i = -1 then
  417. i := high(arr)+1;
  418. SetLength(fpc_CharArray_To_WideStr,i);
  419. Ansi2WideMoveProc (pchar(@arr),PWideChar(Pointer(fpc_CharArray_To_WideStr)),i);
  420. end;
  421. { old style helper }
  422. {$ifndef hascompilerproc}
  423. Procedure fpc_CharArray_To_WideStr(var a : WideString; p: pointer; len: longint); [Public,Alias : 'FPC_CHARARRAY_TO_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  424. var
  425. src: pchar;
  426. i: longint;
  427. begin
  428. src := pchar(p);
  429. if src[0]=#0 Then
  430. begin
  431. pointer(a) := nil;
  432. exit;
  433. end;
  434. i:=IndexChar(src^,len,#0);
  435. if i = -1 then
  436. i := len;
  437. pointer(a) := NewWideString(i);
  438. Ansi2WideMoveProc (src,PWideChar(Pointer(@a[1])),i);
  439. end;
  440. {$endif not hascompilerproc}
  441. {$ifdef hascompilerproc}
  442. { inside the compiler, the resulttype is modified to that of the actual }
  443. { chararray we're converting to (JM) }
  444. function fpc_widestr_to_chararray(arraysize: longint; const src: WideString): fpc_big_chararray;[public,alias: 'FPC_WIDESTR_TO_CHARARRAY']; compilerproc;
  445. var
  446. len: longint;
  447. begin
  448. len := length(src);
  449. if len > arraysize then
  450. len := arraysize;
  451. { make sure we don't dereference src if it can be nil (JM) }
  452. if len > 0 then
  453. wide2ansimoveproc(pwidechar(@src[1]),pchar(@fpc_widestr_to_chararray[0]),len);
  454. fillchar(fpc_widestr_to_chararray[len],arraysize-len,0);
  455. end;
  456. {$endif hascompilerproc}
  457. Function fpc_WideStr_Compare(const S1,S2 : WideString): Longint;[Public,Alias : 'FPC_WIDESTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  458. {
  459. Compares 2 WideStrings;
  460. The result is
  461. <0 if S1<S2
  462. 0 if S1=S2
  463. >0 if S1>S2
  464. }
  465. Var
  466. MaxI,Temp : Longint;
  467. begin
  468. if pointer(S1)=pointer(S2) then
  469. begin
  470. fpc_WideStr_Compare:=0;
  471. exit;
  472. end;
  473. Maxi:=Length(S1);
  474. temp:=Length(S2);
  475. If MaxI>Temp then
  476. MaxI:=Temp;
  477. Temp:=CompareWord(S1[1],S2[1],MaxI);
  478. if temp=0 then
  479. temp:=Length(S1)-Length(S2);
  480. fpc_WideStr_Compare:=Temp;
  481. end;
  482. Procedure fpc_WideStr_CheckZero(p : pointer);[Public,Alias : 'FPC_WIDESTR_CHECKZERO']; {$ifdef hascompilerproc} compilerproc; {$endif}
  483. begin
  484. if p=nil then
  485. HandleErrorFrame(201,get_frame);
  486. end;
  487. Procedure fpc_WideStr_CheckRange(len,index : longint);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; {$ifdef hascompilerproc} compilerproc; {$endif}
  488. begin
  489. if (index>len) or (Index<1) then
  490. HandleErrorFrame(201,get_frame);
  491. end;
  492. {$ifndef INTERNSETLENGTH}
  493. Procedure SetLength (Var S : WideString; l : Longint);
  494. {$else INTERNSETLENGTH}
  495. Procedure fpc_WideStr_SetLength (Var S : WideString; l : Longint);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  496. {$endif INTERNSETLENGTH}
  497. {
  498. Sets The length of string S to L.
  499. Makes sure S is unique, and contains enough room.
  500. }
  501. Var
  502. Temp : Pointer;
  503. movelen, NewLen: longint;
  504. begin
  505. if (l>0) then
  506. begin
  507. if Pointer(S)=nil then
  508. begin
  509. { Need a complete new string...}
  510. Pointer(s):=NewWideString(l);
  511. end
  512. else if (PWideRec(Pointer(S)-WideFirstOff)^.Ref = 1) then
  513. begin
  514. if (PWideRec(Pointer(S)-WideFirstOff)^.Maxlen < L) then
  515. begin
  516. Dec(Pointer(S),WideFirstOff);
  517. NewLen := (L*sizeof(WideChar)+WideRecLen+15) and (not 15);
  518. reallocmem(pointer(S), NewLen);
  519. PAnsiRec(S)^.MaxLen := (NewLen - WideRecLen) div sizeof(WideChar);
  520. Inc(Pointer(S), WideFirstOff);
  521. end;
  522. PWideRec(Pointer(S)-WideFirstOff)^.Len := L;
  523. PWord(Pointer(S)+L*sizeof(WideChar))^:=0;
  524. end
  525. else
  526. begin
  527. { Reallocation is needed... }
  528. Temp:=Pointer(NewWideString(L));
  529. if Length(S)>0 then
  530. begin
  531. if l < succ(length(s)) then
  532. movelen := l
  533. { also move terminating null }
  534. else movelen := succ(length(s));
  535. Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
  536. end;
  537. fpc_widestr_decr_ref(Pointer(S));
  538. Pointer(S):=Temp;
  539. end;
  540. { Force nil termination in case it gets shorter }
  541. PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
  542. PWideRec(Pointer(S)-FirstOff)^.Len:=l;
  543. end
  544. else
  545. begin
  546. { Length=0 }
  547. if Pointer(S)<>nil then
  548. fpc_widestr_decr_ref (Pointer(S));
  549. Pointer(S):=Nil;
  550. end;
  551. end;
  552. {*****************************************************************************
  553. Public functions, In interface.
  554. *****************************************************************************}
  555. function WideCharToString(S : PWideChar) : AnsiString;
  556. begin
  557. result:=WideCharLenToString(s,Length(WideString(s)));
  558. end;
  559. function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : LongInt) : PWideChar;
  560. begin
  561. if Length(Src)<DestSize then
  562. Ansi2WideMoveProc(PChar(Src),Dest,Length(Src))
  563. else
  564. Ansi2WideMoveProc(PChar(Src),Dest,DestSize);
  565. result:=Dest;
  566. end;
  567. function WideCharLenToString(S : PWideChar;Len : LongInt) : AnsiString;
  568. begin
  569. SetLength(result,Len);
  570. Wide2AnsiMove(S,PChar(result),Len);
  571. end;
  572. procedure WideCharLenToStrVar(Src : PWideChar;Len : LongInt;var Dest : AnsiString);
  573. begin
  574. Dest:=WideCharLenToString(Src,Len);
  575. end;
  576. procedure WideCharToStrVar(S : PWideChar;var Dest : AnsiString);
  577. begin
  578. Dest:=WideCharToString(S);
  579. end;
  580. {$ifndef INTERNLENGTH}
  581. Function Length (Const S : WideString) : Longint;
  582. {
  583. Returns the length of an WideString.
  584. Takes in acount that zero strings are NIL;
  585. }
  586. begin
  587. If Pointer(S)=Nil then
  588. Length:=0
  589. else
  590. Length:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  591. end;
  592. {$endif INTERNLENGTH}
  593. { overloaded version of UniqueString for interface }
  594. procedure UniqueString(Var S : WideString); [external name 'FPC_WIDESTR_UNIQUE'];
  595. Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  596. {
  597. Make sure reference count of S is 1,
  598. using copy-on-write semantics.
  599. }
  600. Var
  601. SNew : Pointer;
  602. L : Longint;
  603. begin
  604. pointer(result) := pointer(s);
  605. If Pointer(S)=Nil then
  606. exit;
  607. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  608. begin
  609. L:=PWideRec(Pointer(S)-WideFirstOff)^.len;
  610. SNew:=NewWideString (L);
  611. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  612. PWideRec(SNew-WideFirstOff)^.len:=L;
  613. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  614. pointer(S):=SNew;
  615. pointer(result):=SNew;
  616. end;
  617. end;
  618. {$ifdef interncopy}
  619. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : Longint) : WideString;compilerproc;
  620. {$else}
  621. Function Copy (Const S : WideString; Index,Size : Longint) : WideString;
  622. {$endif}
  623. var
  624. ResultAddress : Pointer;
  625. begin
  626. ResultAddress:=Nil;
  627. dec(index);
  628. if Index < 0 then
  629. Index := 0;
  630. { Check Size. Accounts for Zero-length S, the double check is needed because
  631. Size can be maxint and will get <0 when adding index }
  632. if (Size>Length(S)) or
  633. (Index+Size>Length(S)) then
  634. Size:=Length(S)-Index;
  635. If Size>0 then
  636. begin
  637. If Index<0 Then
  638. Index:=0;
  639. ResultAddress:=Pointer(NewWideString (Size));
  640. if ResultAddress<>Nil then
  641. begin
  642. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  643. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size;
  644. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  645. end;
  646. end;
  647. {$ifdef interncopy}
  648. Pointer(fpc_widestr_Copy):=ResultAddress;
  649. {$else}
  650. Pointer(Copy):=ResultAddress;
  651. {$endif}
  652. end;
  653. Function Pos (Const Substr : WideString; Const Source : WideString) : Longint;
  654. var
  655. i,MaxLen : StrLenInt;
  656. pc : pwidechar;
  657. begin
  658. Pos:=0;
  659. if Length(SubStr)>0 then
  660. begin
  661. MaxLen:=Length(source)-Length(SubStr);
  662. i:=0;
  663. pc:=@source[1];
  664. while (i<=MaxLen) do
  665. begin
  666. inc(i);
  667. if (SubStr[1]=pc^) and
  668. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  669. begin
  670. Pos:=i;
  671. exit;
  672. end;
  673. inc(pc);
  674. end;
  675. end;
  676. end;
  677. { Faster version for a widechar alone }
  678. Function Pos (c : WideChar; Const s : WideString) : Longint;
  679. var
  680. i: longint;
  681. pc : pwidechar;
  682. begin
  683. pc:=@s[1];
  684. for i:=1 to length(s) do
  685. begin
  686. if pc^=c then
  687. begin
  688. pos:=i;
  689. exit;
  690. end;
  691. inc(pc);
  692. end;
  693. pos:=0;
  694. end;
  695. { Faster version for a char alone. Must be implemented because }
  696. { pos(c: char; const s: shortstring) also exists, so otherwise }
  697. { using pos(char,pchar) will always call the shortstring version }
  698. { (exact match for first argument), also with $h+ (JM) }
  699. Function Pos (c : Char; Const s : WideString) : Longint;
  700. var
  701. i: longint;
  702. wc : widechar;
  703. pc : pwidechar;
  704. begin
  705. wc:=c;
  706. pc:=@s[1];
  707. for i:=1 to length(s) do
  708. begin
  709. if pc^=wc then
  710. begin
  711. pos:=i;
  712. exit;
  713. end;
  714. inc(pc);
  715. end;
  716. pos:=0;
  717. end;
  718. Procedure Delete (Var S : WideString; Index,Size: Longint);
  719. Var
  720. LS : Longint;
  721. begin
  722. If Length(S)=0 then
  723. exit;
  724. if index<=0 then
  725. exit;
  726. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  727. if (Index<=LS) and (Size>0) then
  728. begin
  729. UniqueString (S);
  730. if Size+Index>LS then
  731. Size:=LS-Index+1;
  732. if Index+Size<=LS then
  733. begin
  734. Dec(Index);
  735. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  736. end;
  737. Setlength(s,LS-Size);
  738. end;
  739. end;
  740. Procedure Insert (Const Source : WideString; Var S : WideString; Index : Longint);
  741. var
  742. Temp : WideString;
  743. LS : Longint;
  744. begin
  745. If Length(Source)=0 then
  746. exit;
  747. if index <= 0 then
  748. index := 1;
  749. Ls:=Length(S);
  750. if index > LS then
  751. index := LS+1;
  752. Dec(Index);
  753. Pointer(Temp) := NewWideString(Length(Source)+LS);
  754. SetLength(Temp,Length(Source)+LS);
  755. If Index>0 then
  756. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  757. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  758. If (LS-Index)>0 then
  759. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  760. S:=Temp;
  761. end;
  762. Procedure SetString (Var S : WideString; Buf : PWideChar; Len : Longint);
  763. var
  764. BufLen: longint;
  765. begin
  766. SetLength(S,Len);
  767. If (Buf<>Nil) and (Len>0) then
  768. begin
  769. BufLen := IndexWord(Buf^, Len+1, 0);
  770. If (BufLen>0) and (BufLen < Len) then
  771. Len := BufLen;
  772. Move (Buf[0],S[1],Len*sizeof(WideChar));
  773. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  774. end;
  775. end;
  776. Procedure SetString (Var S : WideString; Buf : PChar; Len : Longint);
  777. var
  778. BufLen: longint;
  779. begin
  780. SetLength(S,Len);
  781. If (Buf<>Nil) and (Len>0) then
  782. begin
  783. BufLen := IndexByte(Buf^, Len+1, 0);
  784. If (BufLen>0) and (BufLen < Len) then
  785. Len := BufLen;
  786. Ansi2WideMoveProc(Buf,PWideChar(S),Len);
  787. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  788. end;
  789. end;
  790. Function fpc_Val_Real_WideStr(Const S : WideString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  791. Var
  792. SS : String;
  793. begin
  794. fpc_Val_Real_WideStr := 0;
  795. if length(S) > 255 then
  796. code := 256
  797. else
  798. begin
  799. SS := S;
  800. Val(SS,fpc_Val_Real_WideStr,code);
  801. end;
  802. end;
  803. Function fpc_Val_UInt_WideStr (Const S : WideString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  804. Var
  805. SS : ShortString;
  806. begin
  807. fpc_Val_UInt_WideStr := 0;
  808. if length(S) > 255 then
  809. code := 256
  810. else
  811. begin
  812. SS := S;
  813. Val(SS,fpc_Val_UInt_WideStr,code);
  814. end;
  815. end;
  816. Function fpc_Val_SInt_WideStr (DestSize: longint; Const S : WideString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  817. Var
  818. SS : ShortString;
  819. begin
  820. fpc_Val_SInt_WideStr:=0;
  821. if length(S)>255 then
  822. code:=256
  823. else
  824. begin
  825. SS := S;
  826. fpc_Val_SInt_WideStr := fpc_Val_SInt_ShortStr(DestSize,SS,Code);
  827. end;
  828. end;
  829. Function fpc_Val_qword_WideStr (Const S : WideString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  830. Var
  831. SS : ShortString;
  832. begin
  833. fpc_Val_qword_WideStr:=0;
  834. if length(S)>255 then
  835. code:=256
  836. else
  837. begin
  838. SS := S;
  839. Val(SS,fpc_Val_qword_WideStr,Code);
  840. end;
  841. end;
  842. Function fpc_Val_int64_WideStr (Const S : WideString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  843. Var
  844. SS : ShortString;
  845. begin
  846. fpc_Val_int64_WideStr:=0;
  847. if length(S)>255 then
  848. code:=256
  849. else
  850. begin
  851. SS := S;
  852. Val(SS,fpc_Val_int64_WideStr,Code);
  853. end;
  854. end;
  855. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : longint;var s : WideString);[public,alias:'FPC_WIDESTR_FLOAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  856. var
  857. ss : shortstring;
  858. begin
  859. str_real(len,fr,d,treal_type(rt),ss);
  860. s:=ss;
  861. end;
  862. Procedure fpc_WideStr_Longword(C : Longword;Len : Longint; Var S : WideString);[Public,Alias : 'FPC_WIDESTR_CARDINAL']; {$ifdef hascompilerproc} compilerproc; {$endif}
  863. Var
  864. SS : ShortString;
  865. begin
  866. str(C:Len,SS);
  867. S:=SS;
  868. end;
  869. Procedure fpc_WideStr_Longint(L : Longint; Len : Longint; Var S : WideString);[Public,Alias : 'FPC_WIDESTR_LONGINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  870. Var
  871. SS : ShortString;
  872. begin
  873. Str (L:Len,SS);
  874. S:=SS;
  875. end;
  876. {
  877. $Log$
  878. Revision 1.31 2003-06-17 19:24:08 jonas
  879. * fixed conversion of fpc_*str_unique to compilerproc
  880. Revision 1.30 2003/06/17 16:38:53 jonas
  881. * fpc_{ansistr|widestr}_unique is now a function so it can be used as
  882. compilerproc
  883. Revision 1.29 2003/05/01 08:05:23 florian
  884. * started to make the rtl 64 bit save by introducing SizeInt and SizeUInt (similar to size_t of C)
  885. Revision 1.28 2002/12/29 16:59:17 peter
  886. * implemented some more conversions
  887. Revision 1.27 2002/12/15 22:33:12 peter
  888. * SetString(WideString,[PChar|PWideChar],Len) added
  889. Revision 1.26 2002/12/14 19:16:45 sg
  890. * Ported improvements from the AnsiString equivalents to NewWideString and
  891. fpc_WideStr_SetLength
  892. Revision 1.25 2002/12/07 14:35:34 carl
  893. - avoid warnings (add typecast)
  894. Revision 1.24 2002/10/10 16:08:50 florian
  895. + several widestring/pwidechar related helpers added
  896. Revision 1.23 2002/10/02 18:21:52 peter
  897. * Copy() changed to internal function calling compilerprocs
  898. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  899. new copy functions
  900. Revision 1.22 2002/09/26 21:50:38 florian
  901. + some WideString<->AnsiString conversion functions added
  902. Revision 1.21 2002/09/14 11:20:50 carl
  903. * Delphi compatibility fix (with string routines)
  904. Revision 1.20 2002/09/07 21:16:45 carl
  905. * cardinal -> longword
  906. Revision 1.19 2002/09/07 15:07:46 peter
  907. * old logs removed and tabs fixed
  908. Revision 1.18 2002/07/29 21:28:17 florian
  909. * several fixes to get further with linux/ppc system unit compilation
  910. Revision 1.17 2002/04/26 15:19:05 peter
  911. * use saveregisters for incr routines, saves also problems with
  912. the optimizer
  913. Revision 1.16 2002/04/25 20:14:57 peter
  914. * updated compilerprocs
  915. * incr ref count has now a value argument instead of var
  916. }