wstrings.inc 29 KB

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