wstrings.inc 29 KB

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