wstrings.inc 28 KB

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