wstrings.inc 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. Procedure fpc_widestr_Unique(Var S : WideString); [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. If Pointer(S)=Nil then
  605. exit;
  606. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  607. begin
  608. L:=PWideRec(Pointer(S)-WideFirstOff)^.len;
  609. SNew:=NewWideString (L);
  610. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  611. PWideRec(SNew-WideFirstOff)^.len:=L;
  612. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  613. Pointer(S):=SNew;
  614. end;
  615. end;
  616. {$ifdef interncopy}
  617. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : Longint) : WideString;compilerproc;
  618. {$else}
  619. Function Copy (Const S : WideString; Index,Size : Longint) : WideString;
  620. {$endif}
  621. var
  622. ResultAddress : Pointer;
  623. begin
  624. ResultAddress:=Nil;
  625. dec(index);
  626. if Index < 0 then
  627. Index := 0;
  628. { Check Size. Accounts for Zero-length S, the double check is needed because
  629. Size can be maxint and will get <0 when adding index }
  630. if (Size>Length(S)) or
  631. (Index+Size>Length(S)) then
  632. Size:=Length(S)-Index;
  633. If Size>0 then
  634. begin
  635. If Index<0 Then
  636. Index:=0;
  637. ResultAddress:=Pointer(NewWideString (Size));
  638. if ResultAddress<>Nil then
  639. begin
  640. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  641. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size;
  642. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  643. end;
  644. end;
  645. {$ifdef interncopy}
  646. Pointer(fpc_widestr_Copy):=ResultAddress;
  647. {$else}
  648. Pointer(Copy):=ResultAddress;
  649. {$endif}
  650. end;
  651. Function Pos (Const Substr : WideString; Const Source : WideString) : Longint;
  652. var
  653. i,MaxLen : StrLenInt;
  654. pc : pwidechar;
  655. begin
  656. Pos:=0;
  657. if Length(SubStr)>0 then
  658. begin
  659. MaxLen:=Length(source)-Length(SubStr);
  660. i:=0;
  661. pc:=@source[1];
  662. while (i<=MaxLen) do
  663. begin
  664. inc(i);
  665. if (SubStr[1]=pc^) and
  666. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  667. begin
  668. Pos:=i;
  669. exit;
  670. end;
  671. inc(pc);
  672. end;
  673. end;
  674. end;
  675. { Faster version for a widechar alone }
  676. Function Pos (c : WideChar; Const s : WideString) : Longint;
  677. var
  678. i: longint;
  679. pc : pwidechar;
  680. begin
  681. pc:=@s[1];
  682. for i:=1 to length(s) do
  683. begin
  684. if pc^=c then
  685. begin
  686. pos:=i;
  687. exit;
  688. end;
  689. inc(pc);
  690. end;
  691. pos:=0;
  692. end;
  693. { Faster version for a char alone. Must be implemented because }
  694. { pos(c: char; const s: shortstring) also exists, so otherwise }
  695. { using pos(char,pchar) will always call the shortstring version }
  696. { (exact match for first argument), also with $h+ (JM) }
  697. Function Pos (c : Char; Const s : WideString) : Longint;
  698. var
  699. i: longint;
  700. wc : widechar;
  701. pc : pwidechar;
  702. begin
  703. wc:=c;
  704. pc:=@s[1];
  705. for i:=1 to length(s) do
  706. begin
  707. if pc^=wc then
  708. begin
  709. pos:=i;
  710. exit;
  711. end;
  712. inc(pc);
  713. end;
  714. pos:=0;
  715. end;
  716. Procedure Delete (Var S : WideString; Index,Size: Longint);
  717. Var
  718. LS : Longint;
  719. begin
  720. If Length(S)=0 then
  721. exit;
  722. if index<=0 then
  723. exit;
  724. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len;
  725. if (Index<=LS) and (Size>0) then
  726. begin
  727. UniqueString (S);
  728. if Size+Index>LS then
  729. Size:=LS-Index+1;
  730. if Index+Size<=LS then
  731. begin
  732. Dec(Index);
  733. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  734. end;
  735. Setlength(s,LS-Size);
  736. end;
  737. end;
  738. Procedure Insert (Const Source : WideString; Var S : WideString; Index : Longint);
  739. var
  740. Temp : WideString;
  741. LS : Longint;
  742. begin
  743. If Length(Source)=0 then
  744. exit;
  745. if index <= 0 then
  746. index := 1;
  747. Ls:=Length(S);
  748. if index > LS then
  749. index := LS+1;
  750. Dec(Index);
  751. Pointer(Temp) := NewWideString(Length(Source)+LS);
  752. SetLength(Temp,Length(Source)+LS);
  753. If Index>0 then
  754. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  755. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  756. If (LS-Index)>0 then
  757. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  758. S:=Temp;
  759. end;
  760. Procedure SetString (Var S : WideString; Buf : PWideChar; Len : Longint);
  761. var
  762. BufLen: longint;
  763. begin
  764. SetLength(S,Len);
  765. If (Buf<>Nil) and (Len>0) then
  766. begin
  767. BufLen := IndexWord(Buf^, Len+1, 0);
  768. If (BufLen>0) and (BufLen < Len) then
  769. Len := BufLen;
  770. Move (Buf[0],S[1],Len*sizeof(WideChar));
  771. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  772. end;
  773. end;
  774. Procedure SetString (Var S : WideString; Buf : PChar; Len : Longint);
  775. var
  776. BufLen: longint;
  777. begin
  778. SetLength(S,Len);
  779. If (Buf<>Nil) and (Len>0) then
  780. begin
  781. BufLen := IndexByte(Buf^, Len+1, 0);
  782. If (BufLen>0) and (BufLen < Len) then
  783. Len := BufLen;
  784. Ansi2WideMoveProc(Buf,PWideChar(S),Len);
  785. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  786. end;
  787. end;
  788. Function fpc_Val_Real_WideStr(Const S : WideString; Var Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  789. Var
  790. SS : String;
  791. begin
  792. fpc_Val_Real_WideStr := 0;
  793. if length(S) > 255 then
  794. code := 256
  795. else
  796. begin
  797. SS := S;
  798. Val(SS,fpc_Val_Real_WideStr,code);
  799. end;
  800. end;
  801. Function fpc_Val_UInt_WideStr (Const S : WideString; Var Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  802. Var
  803. SS : ShortString;
  804. begin
  805. fpc_Val_UInt_WideStr := 0;
  806. if length(S) > 255 then
  807. code := 256
  808. else
  809. begin
  810. SS := S;
  811. Val(SS,fpc_Val_UInt_WideStr,code);
  812. end;
  813. end;
  814. Function fpc_Val_SInt_WideStr (DestSize: longint; Const S : WideString; Var Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  815. Var
  816. SS : ShortString;
  817. begin
  818. fpc_Val_SInt_WideStr:=0;
  819. if length(S)>255 then
  820. code:=256
  821. else
  822. begin
  823. SS := S;
  824. fpc_Val_SInt_WideStr := fpc_Val_SInt_ShortStr(DestSize,SS,Code);
  825. end;
  826. end;
  827. Function fpc_Val_qword_WideStr (Const S : WideString; Var Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  828. Var
  829. SS : ShortString;
  830. begin
  831. fpc_Val_qword_WideStr:=0;
  832. if length(S)>255 then
  833. code:=256
  834. else
  835. begin
  836. SS := S;
  837. Val(SS,fpc_Val_qword_WideStr,Code);
  838. end;
  839. end;
  840. Function fpc_Val_int64_WideStr (Const S : WideString; Var Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  841. Var
  842. SS : ShortString;
  843. begin
  844. fpc_Val_int64_WideStr:=0;
  845. if length(S)>255 then
  846. code:=256
  847. else
  848. begin
  849. SS := S;
  850. Val(SS,fpc_Val_int64_WideStr,Code);
  851. end;
  852. end;
  853. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : longint;var s : WideString);[public,alias:'FPC_WIDESTR_FLOAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  854. var
  855. ss : shortstring;
  856. begin
  857. str_real(len,fr,d,treal_type(rt),ss);
  858. s:=ss;
  859. end;
  860. Procedure fpc_WideStr_Longword(C : Longword;Len : Longint; Var S : WideString);[Public,Alias : 'FPC_WIDESTR_CARDINAL']; {$ifdef hascompilerproc} compilerproc; {$endif}
  861. Var
  862. SS : ShortString;
  863. begin
  864. str(C:Len,SS);
  865. S:=SS;
  866. end;
  867. Procedure fpc_WideStr_Longint(L : Longint; Len : Longint; Var S : WideString);[Public,Alias : 'FPC_WIDESTR_LONGINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  868. Var
  869. SS : ShortString;
  870. begin
  871. Str (L:Len,SS);
  872. S:=SS;
  873. end;
  874. {
  875. $Log$
  876. Revision 1.29 2003-05-01 08:05:23 florian
  877. * started to make the rtl 64 bit save by introducing SizeInt and SizeUInt (similar to size_t of C)
  878. Revision 1.28 2002/12/29 16:59:17 peter
  879. * implemented some more conversions
  880. Revision 1.27 2002/12/15 22:33:12 peter
  881. * SetString(WideString,[PChar|PWideChar],Len) added
  882. Revision 1.26 2002/12/14 19:16:45 sg
  883. * Ported improvements from the AnsiString equivalents to NewWideString and
  884. fpc_WideStr_SetLength
  885. Revision 1.25 2002/12/07 14:35:34 carl
  886. - avoid warnings (add typecast)
  887. Revision 1.24 2002/10/10 16:08:50 florian
  888. + several widestring/pwidechar related helpers added
  889. Revision 1.23 2002/10/02 18:21:52 peter
  890. * Copy() changed to internal function calling compilerprocs
  891. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  892. new copy functions
  893. Revision 1.22 2002/09/26 21:50:38 florian
  894. + some WideString<->AnsiString conversion functions added
  895. Revision 1.21 2002/09/14 11:20:50 carl
  896. * Delphi compatibility fix (with string routines)
  897. Revision 1.20 2002/09/07 21:16:45 carl
  898. * cardinal -> longword
  899. Revision 1.19 2002/09/07 15:07:46 peter
  900. * old logs removed and tabs fixed
  901. Revision 1.18 2002/07/29 21:28:17 florian
  902. * several fixes to get further with linux/ppc system unit compilation
  903. Revision 1.17 2002/04/26 15:19:05 peter
  904. * use saveregisters for incr routines, saves also problems with
  905. the optimizer
  906. Revision 1.16 2002/04/25 20:14:57 peter
  907. * updated compilerprocs
  908. * incr ref count has now a value argument instead of var
  909. }