wstrings.inc 25 KB

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