wstrings.inc 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by Florian Klaempfl,
  4. member of the Free Pascal development team.
  5. This file implements support routines for WideStrings/Unicode with FPC
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. This file contains the implementation of the WideString type,
  14. and all things that are needed for it.
  15. WideString is defined as a 'silent' pwidechar :
  16. a pwidechar that points to :
  17. @-8 : SizeInt for reference count;
  18. @-4 : SizeInt for size; size=number of bytes, not the number of chars. Divide or multiply
  19. with sizeof(WideChar) to convert. This is needed to be compatible with Delphi and
  20. Windows COM BSTR.
  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. {$ifdef FPC_WINLIKEWIDESTRING}
  31. Len : DWord;
  32. {$else FPC_WINLIKEWIDESTRING}
  33. Ref : SizeInt;
  34. Len : SizeInt;
  35. {$endif FPC_WINLIKEWIDESTRING}
  36. First : WideChar;
  37. end;
  38. Const
  39. WideRecLen = SizeOf(TWideRec);
  40. WideFirstOff = SizeOf(TWideRec)-sizeof(WideChar);
  41. {
  42. Default WideChar <-> Char conversion is to only convert the
  43. lower 127 chars, all others are translated to spaces.
  44. These routines can be overwritten for the Current Locale
  45. }
  46. procedure DefaultWide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
  47. var
  48. i : SizeInt;
  49. begin
  50. setlength(dest,len);
  51. for i:=1 to len do
  52. begin
  53. if word(source^)<256 then
  54. dest[i]:=char(word(source^))
  55. else
  56. dest[i]:='?';
  57. inc(source);
  58. end;
  59. end;
  60. procedure DefaultAnsi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
  61. var
  62. i : SizeInt;
  63. begin
  64. setlength(dest,len);
  65. for i:=1 to len do
  66. begin
  67. dest[i]:=widechar(byte(source^));
  68. inc(source);
  69. end;
  70. end;
  71. Procedure GetWideStringManager (Var Manager : TWideStringManager);
  72. begin
  73. manager:=widestringmanager;
  74. end;
  75. Procedure SetWideStringManager (Const New : TWideStringManager; Var Old: TWideStringManager);
  76. begin
  77. Old:=widestringmanager;
  78. widestringmanager:=New;
  79. end;
  80. Procedure SetWideStringManager (Const New : TWideStringManager);
  81. begin
  82. widestringmanager:=New;
  83. end;
  84. (*
  85. Procedure UniqueWideString(Var S : WideString); [Public,Alias : 'FPC_WIDESTR_UNIQUE'];
  86. {
  87. Make sure reference count of S is 1,
  88. using copy-on-write semantics.
  89. }
  90. begin
  91. end;
  92. *)
  93. {****************************************************************************
  94. Internal functions, not in interface.
  95. ****************************************************************************}
  96. procedure WideStringError;
  97. begin
  98. HandleErrorFrame(204,get_frame);
  99. end;
  100. {$ifdef WideStrDebug}
  101. Procedure DumpWideRec(S : Pointer);
  102. begin
  103. If S=Nil then
  104. Writeln ('String is nil')
  105. Else
  106. Begin
  107. With PWideRec(S-WideFirstOff)^ do
  108. begin
  109. Write ('(Len:',len);
  110. Writeln (' Ref: ',ref,')');
  111. end;
  112. end;
  113. end;
  114. {$endif}
  115. Function NewWideString(Len : SizeInt) : Pointer;
  116. {
  117. Allocate a new WideString on the heap.
  118. initialize it to zero length and reference count 1.
  119. }
  120. Var
  121. P : Pointer;
  122. begin
  123. {$ifdef MSWINDOWS}
  124. if winwidestringalloc then
  125. begin
  126. P:=SysAllocStringLen(nil,Len);
  127. if P=nil then
  128. WideStringError;
  129. end
  130. else
  131. {$endif MSWINDOWS}
  132. begin
  133. GetMem(P,Len*sizeof(WideChar)+WideRecLen);
  134. If P<>Nil then
  135. begin
  136. PWideRec(P)^.Len:=Len*2; { Initial length }
  137. {$ifndef FPC_WINLIKEWIDESTRING}
  138. PWideRec(P)^.Ref:=1; { Initial Refcount }
  139. {$endif FPC_WINLIKEWIDESTRING}
  140. PWideRec(P)^.First:=#0; { Terminating #0 }
  141. inc(p,WideFirstOff); { Points to string now }
  142. end
  143. else
  144. WideStringError;
  145. end;
  146. NewWideString:=P;
  147. end;
  148. Procedure DisposeWideString(Var S : Pointer);
  149. {
  150. Deallocates a WideString From the heap.
  151. }
  152. begin
  153. If S=Nil then
  154. exit;
  155. {$ifndef MSWINDOWS}
  156. Dec (S,WideFirstOff);
  157. Freemem(S);
  158. {$else MSWINDOWS}
  159. if winwidestringalloc then
  160. SysFreeString(S)
  161. else
  162. begin
  163. Dec (S,WideFirstOff);
  164. Freemem(S);
  165. end;
  166. {$endif MSWINDOWS}
  167. S:=Nil;
  168. end;
  169. {$ifdef FPC_WINLIKEWIDESTRING}
  170. var
  171. __data_start: byte; external name '__data_start__';
  172. __data_end: byte; external name '__data_end__';
  173. function IsWideStringConstant(S: pointer): boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  174. {
  175. Returns True if widestring is constant (located in .data section);
  176. }
  177. begin
  178. Result:=(S>=@__data_start) and (S<@__data_end);
  179. end;
  180. {$endif FPC_WINLIKEWIDESTRING}
  181. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);[Public,Alias:'FPC_WIDESTR_DECR_REF']; compilerproc;
  182. {
  183. Decreases the ReferenceCount of a non constant widestring;
  184. If the reference count is zero, deallocate the string;
  185. }
  186. Type
  187. pSizeInt = ^SizeInt;
  188. {$ifndef FPC_WINLIKEWIDESTRING}
  189. Var
  190. l : pSizeInt;
  191. {$endif FPC_WINLIKEWIDESTRING}
  192. Begin
  193. { Zero string }
  194. if S=Nil then
  195. exit;
  196. {$ifndef FPC_WINLIKEWIDESTRING}
  197. { check for constant strings ...}
  198. l:=@PWideRec(S-WideFirstOff)^.Ref;
  199. if l^<0 then
  200. exit;
  201. { declocked does a MT safe dec and returns true, if the counter is 0 }
  202. if declocked(l^) then
  203. { Ref count dropped to zero ...
  204. ... remove }
  205. {$else}
  206. if not IsWideStringConstant(S) then
  207. {$endif FPC_WINLIKEWIDESTRING}
  208. DisposeWideString(S);
  209. end;
  210. { alias for internal use }
  211. Procedure fpc_WideStr_Decr_Ref (Var S : Pointer);[external name 'FPC_WIDESTR_DECR_REF'];
  212. Procedure fpc_WideStr_Incr_Ref(Var S : Pointer);[Public,Alias:'FPC_WIDESTR_INCR_REF']; compilerproc;
  213. {$ifdef FPC_WINLIKEWIDESTRING}
  214. var
  215. p : pointer;
  216. {$endif FPC_WINLIKEWIDESTRING}
  217. Begin
  218. If S=Nil then
  219. exit;
  220. {$ifdef FPC_WINLIKEWIDESTRING}
  221. p:=NewWidestring(length(WideString(S)));
  222. move(s^,p^,(length(WideString(s))+1)*sizeof(widechar)); // double #0 too
  223. s:=p;
  224. {$else FPC_WINLIKEWIDESTRING}
  225. { Let's be paranoid : Constant string ??}
  226. If PWideRec(S-WideFirstOff)^.Ref<0 then
  227. exit;
  228. inclocked(PWideRec(S-WideFirstOff)^.Ref);
  229. {$endif FPC_WINLIKEWIDESTRING}
  230. end;
  231. { alias for internal use }
  232. Procedure fpc_WideStr_Incr_Ref (Var S : Pointer);[external name 'FPC_WIDESTR_INCR_REF'];
  233. function fpc_WideStr_To_ShortStr (high_of_res: SizeInt;const S2 : WideString): shortstring;[Public, alias: 'FPC_WIDESTR_TO_SHORTSTR']; compilerproc;
  234. {
  235. Converts a WideString to a ShortString;
  236. }
  237. Var
  238. Size : SizeInt;
  239. temp : ansistring;
  240. begin
  241. result:='';
  242. Size:=Length(S2);
  243. if Size>0 then
  244. begin
  245. If Size>high_of_res then
  246. Size:=high_of_res;
  247. widestringmanager.Wide2AnsiMoveProc(PWideChar(S2),temp,Size);
  248. result:=temp;
  249. end;
  250. end;
  251. Function fpc_ShortStr_To_WideStr (Const S2 : ShortString): WideString;compilerproc;
  252. {
  253. Converts a ShortString to a WideString;
  254. }
  255. Var
  256. Size : SizeInt;
  257. begin
  258. result:='';
  259. Size:=Length(S2);
  260. if Size>0 then
  261. begin
  262. widestringmanager.Ansi2WideMoveProc(PChar(@S2[1]),result,Size);
  263. { Terminating Zero }
  264. PWideChar(Pointer(fpc_ShortStr_To_WideStr)+Size*sizeof(WideChar))^:=#0;
  265. end;
  266. end;
  267. Function fpc_WideStr_To_AnsiStr (const S2 : WideString): AnsiString; compilerproc;
  268. {
  269. Converts a WideString to an AnsiString
  270. }
  271. Var
  272. Size : SizeInt;
  273. begin
  274. result:='';
  275. Size:=Length(S2);
  276. if Size>0 then
  277. widestringmanager.Wide2AnsiMoveProc(PWideChar(Pointer(S2)),result,Size);
  278. end;
  279. Function fpc_AnsiStr_To_WideStr (Const S2 : AnsiString): WideString; compilerproc;
  280. {
  281. Converts an AnsiString to a WideString;
  282. }
  283. Var
  284. Size : SizeInt;
  285. begin
  286. result:='';
  287. Size:=Length(S2);
  288. if Size>0 then
  289. widestringmanager.Ansi2WideMoveProc(PChar(S2),result,Size);
  290. end;
  291. Function fpc_PWideChar_To_AnsiStr(const p : pwidechar): ansistring; compilerproc;
  292. var
  293. Size : SizeInt;
  294. begin
  295. result:='';
  296. if p=nil then
  297. exit;
  298. Size := IndexWord(p^, -1, 0);
  299. if Size>0 then
  300. widestringmanager.Wide2AnsiMoveProc(P,result,Size);
  301. end;
  302. Function fpc_PWideChar_To_WideStr(const p : pwidechar): widestring; compilerproc;
  303. var
  304. Size : SizeInt;
  305. begin
  306. result:='';
  307. if p=nil then
  308. exit;
  309. Size := IndexWord(p^, -1, 0);
  310. Setlength(result,Size);
  311. if Size>0 then
  312. begin
  313. Move(p^,PWideChar(Pointer(result))^,Size*sizeof(WideChar));
  314. { Terminating Zero }
  315. PWideChar(Pointer(result)+Size*sizeof(WideChar))^:=#0;
  316. end;
  317. end;
  318. Function fpc_PWideChar_To_ShortStr(const p : pwidechar): shortstring; compilerproc;
  319. var
  320. Size : SizeInt;
  321. temp: ansistring;
  322. begin
  323. result:='';
  324. if p=nil then
  325. exit;
  326. Size := IndexWord(p^, $7fffffff, 0);
  327. if Size>0 then
  328. begin
  329. widestringmanager.Wide2AnsiMoveProc(p,temp,Size);
  330. result:=temp;
  331. end;
  332. end;
  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']; compilerproc;
  335. {
  336. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  337. }
  338. begin
  339. {$ifdef FPC_WINLIKEWIDESTRING}
  340. if S1=S2 then exit;
  341. if S2<>nil then
  342. begin
  343. if IsWideStringConstant(S1) then
  344. begin
  345. S1:=NewWidestring(length(WideString(S2)));
  346. move(s2^,s1^,(length(WideString(s1))+1)*sizeof(widechar));
  347. end
  348. else
  349. {$ifdef MSWINDOWS}
  350. if winwidestringalloc then
  351. begin
  352. if SysReAllocStringLen(S1, S2, Length(WideString(S2))) = 0 then
  353. WideStringError;
  354. end
  355. else
  356. {$endif MSWINDOWS}
  357. begin
  358. SetLength(WideString(S1),length(WideString(S2)));
  359. move(s2^,s1^,(length(WideString(s1))+1)*sizeof(widechar));
  360. end;
  361. end
  362. else
  363. begin
  364. { Free S1 }
  365. fpc_widestr_decr_ref (S1);
  366. S1:=nil;
  367. end;
  368. {$else FPC_WINLIKEWIDESTRING}
  369. If S2<>nil then
  370. If PWideRec(S2-WideFirstOff)^.Ref>0 then
  371. inclocked(PWideRec(S2-WideFirstOff)^.ref);
  372. { Decrease the reference count on the old S1 }
  373. fpc_widestr_decr_ref (S1);
  374. s1:=s2;
  375. {$endif FPC_WINLIKEWIDESTRING}
  376. end;
  377. { alias for internal use }
  378. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_WIDESTR_ASSIGN'];
  379. {$ifndef STR_CONCAT_PROCS}
  380. function fpc_WideStr_Concat (const S1,S2 : WideString): WideString; compilerproc;
  381. Var
  382. Size,Location : SizeInt;
  383. pc : pwidechar;
  384. begin
  385. { only assign if s1 or s2 is empty }
  386. if (S1='') then
  387. begin
  388. result:=s2;
  389. exit;
  390. end;
  391. if (S2='') then
  392. begin
  393. result:=s1;
  394. exit;
  395. end;
  396. Location:=Length(S1);
  397. Size:=length(S2);
  398. SetLength(result,Size+Location);
  399. pc:=pwidechar(result);
  400. Move(S1[1],pc^,Location*sizeof(WideChar));
  401. inc(pc,location);
  402. Move(S2[1],pc^,(Size+1)*sizeof(WideChar));
  403. end;
  404. function fpc_WideStr_Concat_multi (const sarr:array of Widestring): widestring; compilerproc;
  405. Var
  406. i : Longint;
  407. p : pointer;
  408. pc : pwidechar;
  409. Size,NewSize : SizeInt;
  410. begin
  411. { First calculate size of the result so we can do
  412. a single call to SetLength() }
  413. NewSize:=0;
  414. for i:=low(sarr) to high(sarr) do
  415. inc(Newsize,length(sarr[i]));
  416. SetLength(result,NewSize);
  417. pc:=pwidechar(result);
  418. for i:=low(sarr) to high(sarr) do
  419. begin
  420. p:=pointer(sarr[i]);
  421. if assigned(p) then
  422. begin
  423. Size:=length(widestring(p));
  424. Move(pwidechar(p)^,pc^,(Size+1)*sizeof(WideChar));
  425. inc(pc,size);
  426. end;
  427. end;
  428. end;
  429. {$else STR_CONCAT_PROCS}
  430. procedure fpc_WideStr_Concat (var DestS:Widestring;const S1,S2 : WideString); compilerproc;
  431. Var
  432. Size,Location : SizeInt;
  433. same : boolean;
  434. begin
  435. { only assign if s1 or s2 is empty }
  436. if (S1='') then
  437. begin
  438. DestS:=s2;
  439. exit;
  440. end;
  441. if (S2='') then
  442. begin
  443. DestS:=s1;
  444. exit;
  445. end;
  446. Location:=Length(S1);
  447. Size:=length(S2);
  448. { Use Pointer() typecasts to prevent extra conversion code }
  449. if Pointer(DestS)=Pointer(S1) then
  450. begin
  451. same:=Pointer(S1)=Pointer(S2);
  452. SetLength(DestS,Size+Location);
  453. if same then
  454. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size)*sizeof(WideChar))
  455. else
  456. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  457. end
  458. else if Pointer(DestS)=Pointer(S2) then
  459. begin
  460. SetLength(DestS,Size+Location);
  461. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  462. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(WideChar));
  463. end
  464. else
  465. begin
  466. DestS:='';
  467. SetLength(DestS,Size+Location);
  468. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(WideChar));
  469. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  470. end;
  471. end;
  472. procedure fpc_WideStr_Concat_multi (var DestS:Widestring;const sarr:array of Widestring); compilerproc;
  473. Var
  474. lowstart,i : Longint;
  475. p,pc : pointer;
  476. Size,NewLen,
  477. OldDestLen : SizeInt;
  478. destcopy : widestring;
  479. begin
  480. if high(sarr)=0 then
  481. begin
  482. DestS:='';
  483. exit;
  484. end;
  485. lowstart:=low(sarr);
  486. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  487. inc(lowstart);
  488. { Check for another reuse, then we can't use
  489. the append optimization }
  490. for i:=lowstart to high(sarr) do
  491. begin
  492. if Pointer(DestS)=Pointer(sarr[i]) then
  493. begin
  494. { if DestS is used somewhere in the middle of the expression,
  495. we need to make sure the original string still exists after
  496. we empty/modify DestS }
  497. destcopy:=dests;
  498. lowstart:=low(sarr);
  499. break;
  500. end;
  501. end;
  502. { Start with empty DestS if we start with concatting
  503. the first array element }
  504. if lowstart=low(sarr) then
  505. DestS:='';
  506. OldDestLen:=length(DestS);
  507. { Calculate size of the result so we can do
  508. a single call to SetLength() }
  509. NewLen:=0;
  510. for i:=low(sarr) to high(sarr) do
  511. inc(NewLen,length(sarr[i]));
  512. SetLength(DestS,NewLen);
  513. { Concat all strings, except the string we already
  514. copied in DestS }
  515. pc:=Pointer(DestS)+OldDestLen*sizeof(WideChar);
  516. for i:=lowstart to high(sarr) do
  517. begin
  518. p:=pointer(sarr[i]);
  519. if assigned(p) then
  520. begin
  521. Size:=length(widestring(p));
  522. Move(p^,pc^,(Size+1)*sizeof(WideChar));
  523. inc(pc,size*sizeof(WideChar));
  524. end;
  525. end;
  526. end;
  527. {$endif STR_CONCAT_PROCS}
  528. Function fpc_Char_To_WChar(const c : Char): WideChar; compilerproc;
  529. var
  530. w: widestring;
  531. begin
  532. widestringmanager.Ansi2WideMoveProc(@c, w, 1);
  533. fpc_Char_To_WChar:= w[1];
  534. end;
  535. Function fpc_Char_To_WideStr(const c : Char): WideString; compilerproc;
  536. {
  537. Converts a Char to a WideString;
  538. }
  539. begin
  540. Setlength(fpc_Char_To_WideStr,1);
  541. fpc_Char_To_WideStr[1]:=c;
  542. { Terminating Zero }
  543. PWideChar(Pointer(fpc_Char_To_WideStr)+sizeof(WideChar))^:=#0;
  544. end;
  545. Function fpc_WChar_To_Char(const c : WideChar): Char; compilerproc;
  546. {
  547. Converts a WideChar to a Char;
  548. }
  549. var
  550. s: ansistring;
  551. begin
  552. widestringmanager.Wide2AnsiMoveProc(@c, s, 1);
  553. if length(s)=1 then
  554. fpc_WChar_To_Char:= s[1]
  555. else
  556. fpc_WChar_To_Char:='?';
  557. end;
  558. Function fpc_WChar_To_WideStr(const c : WideChar): WideString; compilerproc;
  559. {
  560. Converts a WideChar to a WideString;
  561. }
  562. begin
  563. Setlength (fpc_WChar_To_WideStr,1);
  564. fpc_WChar_To_WideStr[1]:= c;
  565. end;
  566. Function fpc_WChar_To_AnsiStr(const c : WideChar): AnsiString; compilerproc;
  567. {
  568. Converts a WideChar to a AnsiString;
  569. }
  570. begin
  571. widestringmanager.Wide2AnsiMoveProc(@c, fpc_WChar_To_AnsiStr, 1);
  572. end;
  573. Function fpc_WChar_To_ShortStr(const c : WideChar): ShortString; compilerproc;
  574. {
  575. Converts a WideChar to a ShortString;
  576. }
  577. var
  578. s: ansistring;
  579. begin
  580. widestringmanager.Wide2AnsiMoveProc(@c, s, 1);
  581. fpc_WChar_To_ShortStr:= s;
  582. end;
  583. Function fpc_PChar_To_WideStr(const p : pchar): WideString; compilerproc;
  584. Var
  585. L : SizeInt;
  586. begin
  587. if (not assigned(p)) or (p[0]=#0) Then
  588. { result is automatically set to '' }
  589. exit;
  590. l:=IndexChar(p^,-1,#0);
  591. widestringmanager.Ansi2WideMoveProc(P,fpc_PChar_To_WideStr,l);
  592. end;
  593. Function fpc_CharArray_To_WideStr(const arr: array of char; zerobased: boolean = true): WideString; compilerproc;
  594. var
  595. i : SizeInt;
  596. begin
  597. if (zerobased) then
  598. begin
  599. if (arr[0]=#0) Then
  600. { result is automatically set to '' }
  601. exit;
  602. i:=IndexChar(arr,high(arr)+1,#0);
  603. if i = -1 then
  604. i := high(arr)+1;
  605. end
  606. else
  607. i := high(arr)+1;
  608. SetLength(fpc_CharArray_To_WideStr,i);
  609. widestringmanager.Ansi2WideMoveProc (pchar(@arr),fpc_CharArray_To_WideStr,i);
  610. end;
  611. function fpc_WideCharArray_To_ShortStr(const arr: array of widechar; zerobased: boolean = true): shortstring;[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  612. var
  613. l: longint;
  614. index: longint;
  615. len: byte;
  616. temp: ansistring;
  617. begin
  618. l := high(arr)+1;
  619. if l>=256 then
  620. l:=255
  621. else if l<0 then
  622. l:=0;
  623. if zerobased then
  624. begin
  625. index:=IndexWord(arr[0],l,0);
  626. if (index < 0) then
  627. len := l
  628. else
  629. len := index;
  630. end
  631. else
  632. len := l;
  633. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,len);
  634. fpc_WideCharArray_To_ShortStr := temp;
  635. end;
  636. Function fpc_WideCharArray_To_AnsiStr(const arr: array of widechar; zerobased: boolean = true): AnsiString; compilerproc;
  637. var
  638. i : SizeInt;
  639. begin
  640. if (zerobased) then
  641. begin
  642. i:=IndexWord(arr,high(arr)+1,0);
  643. if i = -1 then
  644. i := high(arr)+1;
  645. end
  646. else
  647. i := high(arr)+1;
  648. SetLength(fpc_WideCharArray_To_AnsiStr,i);
  649. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),fpc_WideCharArray_To_AnsiStr,i);
  650. end;
  651. Function fpc_WideCharArray_To_WideStr(const arr: array of widechar; zerobased: boolean = true): WideString; compilerproc;
  652. var
  653. i : SizeInt;
  654. begin
  655. if (zerobased) then
  656. begin
  657. i:=IndexWord(arr,high(arr)+1,0);
  658. if i = -1 then
  659. i := high(arr)+1;
  660. end
  661. else
  662. i := high(arr)+1;
  663. SetLength(fpc_WideCharArray_To_WideStr,i);
  664. Move(arr[0], Pointer(fpc_WideCharArray_To_WideStr)^,i*sizeof(WideChar));
  665. end;
  666. {$ifndef FPC_STRTOCHARARRAYPROC}
  667. { inside the compiler, the resulttype is modified to that of the actual }
  668. { chararray we're converting to (JM) }
  669. function fpc_widestr_to_chararray(arraysize: SizeInt; const src: WideString): fpc_big_chararray;[public,alias: 'FPC_WIDESTR_TO_CHARARRAY']; compilerproc;
  670. var
  671. len: SizeInt;
  672. temp: ansistring;
  673. begin
  674. len := length(src);
  675. { make sure we don't dereference src if it can be nil (JM) }
  676. if len > 0 then
  677. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,len);
  678. len := length(temp);
  679. if len > arraysize then
  680. len := arraysize;
  681. {$r-}
  682. move(temp[1],fpc_widestr_to_chararray[0],len);
  683. fillchar(fpc_widestr_to_chararray[len],arraysize-len,0);
  684. {$ifdef RangeCheckWasOn}
  685. {$r+}
  686. {$endif}
  687. end;
  688. { inside the compiler, the resulttype is modified to that of the actual }
  689. { widechararray we're converting to (JM) }
  690. function fpc_widestr_to_widechararray(arraysize: SizeInt; const src: WideString): fpc_big_widechararray;[public,alias: 'FPC_WIDESTR_TO_WIDECHARARRAY']; compilerproc;
  691. var
  692. len: SizeInt;
  693. begin
  694. len := length(src);
  695. if len > arraysize then
  696. len := arraysize;
  697. {$r-}
  698. { make sure we don't try to access element 1 of the ansistring if it's nil }
  699. if len > 0 then
  700. move(src[1],fpc_widestr_to_widechararray[0],len*SizeOf(WideChar));
  701. fillchar(fpc_widestr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  702. {$ifdef RangeCheckWasOn}
  703. {$r+}
  704. {$endif}
  705. end;
  706. { inside the compiler, the resulttype is modified to that of the actual }
  707. { chararray we're converting to (JM) }
  708. function fpc_ansistr_to_widechararray(arraysize: SizeInt; const src: AnsiString): fpc_big_widechararray;[public,alias: 'FPC_ANSISTR_TO_WIDECHARARRAY']; compilerproc;
  709. var
  710. len: SizeInt;
  711. temp: widestring;
  712. begin
  713. len := length(src);
  714. { make sure we don't dereference src if it can be nil (JM) }
  715. if len > 0 then
  716. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  717. len := length(temp);
  718. if len > arraysize then
  719. len := arraysize;
  720. {$r-}
  721. move(temp[1],fpc_ansistr_to_widechararray[0],len*sizeof(widechar));
  722. fillchar(fpc_ansistr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  723. {$ifdef RangeCheckWasOn}
  724. {$r+}
  725. {$endif}
  726. end;
  727. function fpc_shortstr_to_widechararray(arraysize: SizeInt; const src: ShortString): fpc_big_widechararray;[public,alias: 'FPC_SHORTSTR_TO_WIDECHARARRAY']; compilerproc;
  728. var
  729. len: longint;
  730. temp : widestring;
  731. begin
  732. len := length(src);
  733. { make sure we don't access char 1 if length is 0 (JM) }
  734. if len > 0 then
  735. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  736. len := length(temp);
  737. if len > arraysize then
  738. len := arraysize;
  739. {$r-}
  740. move(temp[1],fpc_shortstr_to_widechararray[0],len*sizeof(widechar));
  741. fillchar(fpc_shortstr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  742. {$ifdef RangeCheckWasOn}
  743. {$r+}
  744. {$endif}
  745. end;
  746. {$else ndef FPC_STRTOCHARARRAYPROC}
  747. procedure fpc_widestr_to_chararray(out res: array of char; const src: WideString); compilerproc;
  748. var
  749. len: SizeInt;
  750. temp: ansistring;
  751. begin
  752. len := length(src);
  753. { make sure we don't dereference src if it can be nil (JM) }
  754. if len > 0 then
  755. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,len);
  756. len := length(temp);
  757. if len > length(res) then
  758. len := length(res);
  759. {$r-}
  760. move(temp[1],res[0],len);
  761. fillchar(res[len],length(res)-len,0);
  762. {$ifdef RangeCheckWasOn}
  763. {$r+}
  764. {$endif}
  765. end;
  766. procedure fpc_widestr_to_widechararray(out res: array of widechar; const src: WideString); compilerproc;
  767. var
  768. len: SizeInt;
  769. begin
  770. len := length(src);
  771. if len > length(res) then
  772. len := length(res);
  773. {$r-}
  774. { make sure we don't try to access element 1 of the ansistring if it's nil }
  775. if len > 0 then
  776. move(src[1],res[0],len*SizeOf(WideChar));
  777. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  778. {$ifdef RangeCheckWasOn}
  779. {$r+}
  780. {$endif}
  781. end;
  782. procedure fpc_ansistr_to_widechararray(out res: array of widechar; const src: AnsiString); compilerproc;
  783. var
  784. len: SizeInt;
  785. temp: widestring;
  786. begin
  787. len := length(src);
  788. { make sure we don't dereference src if it can be nil (JM) }
  789. if len > 0 then
  790. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  791. len := length(temp);
  792. if len > length(res) then
  793. len := length(res);
  794. {$r-}
  795. move(temp[1],res[0],len*sizeof(widechar));
  796. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  797. {$ifdef RangeCheckWasOn}
  798. {$r+}
  799. {$endif}
  800. end;
  801. procedure fpc_shortstr_to_widechararray(out res: array of widechar; const src: ShortString); compilerproc;
  802. var
  803. len: longint;
  804. temp : widestring;
  805. begin
  806. len := length(src);
  807. { make sure we don't access char 1 if length is 0 (JM) }
  808. if len > 0 then
  809. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  810. len := length(temp);
  811. if len > length(res) then
  812. len := length(res);
  813. {$r-}
  814. move(temp[1],res[0],len*sizeof(widechar));
  815. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  816. {$ifdef RangeCheckWasOn}
  817. {$r+}
  818. {$endif}
  819. end;
  820. {$endif ndef FPC_STRTOCHARARRAYPROC}
  821. Function fpc_WideStr_Compare(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE']; compilerproc;
  822. {
  823. Compares 2 WideStrings;
  824. The result is
  825. <0 if S1<S2
  826. 0 if S1=S2
  827. >0 if S1>S2
  828. }
  829. Var
  830. MaxI,Temp : SizeInt;
  831. begin
  832. if pointer(S1)=pointer(S2) then
  833. begin
  834. fpc_WideStr_Compare:=0;
  835. exit;
  836. end;
  837. Maxi:=Length(S1);
  838. temp:=Length(S2);
  839. If MaxI>Temp then
  840. MaxI:=Temp;
  841. Temp:=CompareWord(S1[1],S2[1],MaxI);
  842. if temp=0 then
  843. temp:=Length(S1)-Length(S2);
  844. fpc_WideStr_Compare:=Temp;
  845. end;
  846. Function fpc_WideStr_Compare_Equal(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE_EQUAL']; compilerproc;
  847. {
  848. Compares 2 WideStrings for equality only;
  849. The result is
  850. 0 if S1=S2
  851. <>0 if S1<>S2
  852. }
  853. Var
  854. MaxI : SizeInt;
  855. begin
  856. if pointer(S1)=pointer(S2) then
  857. exit(0);
  858. Maxi:=Length(S1);
  859. If MaxI<>Length(S2) then
  860. exit(-1)
  861. else
  862. exit(CompareWord(S1[1],S2[1],MaxI));
  863. end;
  864. Procedure fpc_WideStr_CheckZero(p : pointer);[Public,Alias : 'FPC_WIDESTR_CHECKZERO']; compilerproc;
  865. begin
  866. if p=nil then
  867. HandleErrorFrame(201,get_frame);
  868. end;
  869. Procedure fpc_WideStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; compilerproc;
  870. begin
  871. if (index>len) or (Index<1) then
  872. HandleErrorFrame(201,get_frame);
  873. end;
  874. Procedure fpc_WideStr_SetLength(Var S : WideString; l : SizeInt);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; compilerproc;
  875. {
  876. Sets The length of string S to L.
  877. Makes sure S is unique, and contains enough room.
  878. }
  879. Var
  880. Temp : Pointer;
  881. movelen: SizeInt;
  882. begin
  883. if (l>0) then
  884. begin
  885. if Pointer(S)=nil then
  886. begin
  887. { Need a complete new string...}
  888. Pointer(s):=NewWideString(l);
  889. end
  890. { windows doesn't support reallocing widestrings, this code
  891. is anyways subject to be removed because widestrings shouldn't be
  892. ref. counted anymore (FK) }
  893. else
  894. if
  895. {$ifdef MSWINDOWS}
  896. not winwidestringalloc and
  897. {$endif MSWINDOWS}
  898. {$ifdef FPC_WINLIKEWIDESTRING}
  899. not IsWideStringConstant(pointer(S))
  900. {$else}
  901. (PWideRec(Pointer(S)-WideFirstOff)^.Ref = 1)
  902. {$endif FPC_WINLIKEWIDESTRING}
  903. then
  904. begin
  905. Dec(Pointer(S),WideFirstOff);
  906. if L*sizeof(WideChar)+WideRecLen>MemSize(Pointer(S)) then
  907. reallocmem(pointer(S), L*sizeof(WideChar)+WideRecLen);
  908. Inc(Pointer(S), WideFirstOff);
  909. end
  910. else
  911. begin
  912. { Reallocation is needed... }
  913. Temp:=Pointer(NewWideString(L));
  914. if Length(S)>0 then
  915. begin
  916. if l < succ(length(s)) then
  917. movelen := l
  918. { also move terminating null }
  919. else
  920. movelen := succ(length(s));
  921. Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
  922. end;
  923. fpc_widestr_decr_ref(Pointer(S));
  924. Pointer(S):=Temp;
  925. end;
  926. { Force nil termination in case it gets shorter }
  927. PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
  928. {$ifdef MSWINDOWS}
  929. if not winwidestringalloc then
  930. {$endif MSWINDOWS}
  931. PWideRec(Pointer(S)-WideFirstOff)^.Len:=l*sizeof(WideChar);
  932. end
  933. else
  934. begin
  935. { Length=0 }
  936. if Pointer(S)<>nil then
  937. fpc_widestr_decr_ref (Pointer(S));
  938. Pointer(S):=Nil;
  939. end;
  940. end;
  941. {*****************************************************************************
  942. Public functions, In interface.
  943. *****************************************************************************}
  944. function WideCharToString(S : PWideChar) : AnsiString;
  945. begin
  946. result:=WideCharLenToString(s,Length(WideString(s)));
  947. end;
  948. function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  949. var
  950. temp:widestring;
  951. begin
  952. widestringmanager.Ansi2WideMoveProc(PChar(Src),temp,Length(Src));
  953. if Length(temp)<DestSize then
  954. move(temp[1],Dest^,Length(temp)*SizeOf(WideChar))
  955. else
  956. move(temp[1],Dest^,(DestSize-1)*SizeOf(WideChar));
  957. Dest[DestSize-1]:=#0;
  958. result:=Dest;
  959. end;
  960. function WideCharLenToString(S : PWideChar;Len : SizeInt) : AnsiString;
  961. begin
  962. //SetLength(result,Len);
  963. widestringmanager.Wide2AnsiMoveproc(S,result,Len);
  964. end;
  965. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : AnsiString);
  966. begin
  967. Dest:=WideCharLenToString(Src,Len);
  968. end;
  969. procedure WideCharToStrVar(S : PWideChar;out Dest : AnsiString);
  970. begin
  971. Dest:=WideCharToString(S);
  972. end;
  973. Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; compilerproc;
  974. {$ifdef FPC_WINLIKEWIDESTRING}
  975. begin
  976. pointer(result) := pointer(s);
  977. end;
  978. {$else FPC_WINLIKEWIDESTRING}
  979. {
  980. Make sure reference count of S is 1,
  981. using copy-on-write semantics.
  982. }
  983. Var
  984. SNew : Pointer;
  985. L : SizeInt;
  986. begin
  987. pointer(result) := pointer(s);
  988. If Pointer(S)=Nil then
  989. exit;
  990. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  991. begin
  992. L:=PWideRec(Pointer(S)-WideFirstOff)^.len div sizeof(WideChar);
  993. SNew:=NewWideString (L);
  994. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  995. PWideRec(SNew-WideFirstOff)^.len:=L * sizeof(WideChar);
  996. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  997. pointer(S):=SNew;
  998. pointer(result):=SNew;
  999. end;
  1000. end;
  1001. {$endif FPC_WINLIKEWIDESTRING}
  1002. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : SizeInt) : WideString;compilerproc;
  1003. var
  1004. ResultAddress : Pointer;
  1005. begin
  1006. ResultAddress:=Nil;
  1007. dec(index);
  1008. if Index < 0 then
  1009. Index := 0;
  1010. { Check Size. Accounts for Zero-length S, the double check is needed because
  1011. Size can be maxint and will get <0 when adding index }
  1012. if (Size>Length(S)) or
  1013. (Index+Size>Length(S)) then
  1014. Size:=Length(S)-Index;
  1015. If Size>0 then
  1016. begin
  1017. If Index<0 Then
  1018. Index:=0;
  1019. ResultAddress:=Pointer(NewWideString (Size));
  1020. if ResultAddress<>Nil then
  1021. begin
  1022. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  1023. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size*sizeof(WideChar);
  1024. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  1025. end;
  1026. end;
  1027. Pointer(fpc_widestr_Copy):=ResultAddress;
  1028. end;
  1029. Function Pos (Const Substr : WideString; Const Source : WideString) : SizeInt;
  1030. var
  1031. i,MaxLen : SizeInt;
  1032. pc : pwidechar;
  1033. begin
  1034. Pos:=0;
  1035. if Length(SubStr)>0 then
  1036. begin
  1037. MaxLen:=Length(source)-Length(SubStr);
  1038. i:=0;
  1039. pc:=@source[1];
  1040. while (i<=MaxLen) do
  1041. begin
  1042. inc(i);
  1043. if (SubStr[1]=pc^) and
  1044. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  1045. begin
  1046. Pos:=i;
  1047. exit;
  1048. end;
  1049. inc(pc);
  1050. end;
  1051. end;
  1052. end;
  1053. { Faster version for a widechar alone }
  1054. Function Pos (c : WideChar; Const s : WideString) : SizeInt;
  1055. var
  1056. i: SizeInt;
  1057. pc : pwidechar;
  1058. begin
  1059. pc:=@s[1];
  1060. for i:=1 to length(s) do
  1061. begin
  1062. if pc^=c then
  1063. begin
  1064. pos:=i;
  1065. exit;
  1066. end;
  1067. inc(pc);
  1068. end;
  1069. pos:=0;
  1070. end;
  1071. Function Pos (c : WideChar; Const s : AnsiString) : SizeInt;
  1072. var
  1073. i: SizeInt;
  1074. pc : pchar;
  1075. begin
  1076. pc:=@s[1];
  1077. for i:=1 to length(s) do
  1078. begin
  1079. if widechar(pc^)=c then
  1080. begin
  1081. pos:=i;
  1082. exit;
  1083. end;
  1084. inc(pc);
  1085. end;
  1086. pos:=0;
  1087. end;
  1088. Function Pos (c : AnsiString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1089. begin
  1090. result:=Pos(WideString(c),s);
  1091. end;
  1092. Function Pos (c : ShortString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1093. begin
  1094. result:=Pos(WideString(c),s);
  1095. end;
  1096. Function Pos (c : WideString; Const s : AnsiString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1097. begin
  1098. result:=Pos(c,WideString(s));
  1099. end;
  1100. { Faster version for a char alone. Must be implemented because }
  1101. { pos(c: char; const s: shortstring) also exists, so otherwise }
  1102. { using pos(char,pchar) will always call the shortstring version }
  1103. { (exact match for first argument), also with $h+ (JM) }
  1104. Function Pos (c : Char; Const s : WideString) : SizeInt;
  1105. var
  1106. i: SizeInt;
  1107. wc : widechar;
  1108. pc : pwidechar;
  1109. begin
  1110. wc:=c;
  1111. pc:=@s[1];
  1112. for i:=1 to length(s) do
  1113. begin
  1114. if pc^=wc then
  1115. begin
  1116. pos:=i;
  1117. exit;
  1118. end;
  1119. inc(pc);
  1120. end;
  1121. pos:=0;
  1122. end;
  1123. Procedure Delete (Var S : WideString; Index,Size: SizeInt);
  1124. Var
  1125. LS : SizeInt;
  1126. begin
  1127. If Length(S)=0 then
  1128. exit;
  1129. if index<=0 then
  1130. exit;
  1131. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len div sizeof(WideChar);
  1132. if (Index<=LS) and (Size>0) then
  1133. begin
  1134. UniqueString (S);
  1135. if Size+Index>LS then
  1136. Size:=LS-Index+1;
  1137. if Index+Size<=LS then
  1138. begin
  1139. Dec(Index);
  1140. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  1141. end;
  1142. Setlength(s,LS-Size);
  1143. end;
  1144. end;
  1145. Procedure Insert (Const Source : WideString; Var S : WideString; Index : SizeInt);
  1146. var
  1147. Temp : WideString;
  1148. LS : SizeInt;
  1149. begin
  1150. If Length(Source)=0 then
  1151. exit;
  1152. if index <= 0 then
  1153. index := 1;
  1154. Ls:=Length(S);
  1155. if index > LS then
  1156. index := LS+1;
  1157. Dec(Index);
  1158. Pointer(Temp) := NewWideString(Length(Source)+LS);
  1159. SetLength(Temp,Length(Source)+LS);
  1160. If Index>0 then
  1161. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  1162. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  1163. If (LS-Index)>0 then
  1164. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  1165. S:=Temp;
  1166. end;
  1167. function UpCase(const s : WideString) : WideString;
  1168. begin
  1169. result:=widestringmanager.UpperWideStringProc(s);
  1170. end;
  1171. Procedure SetString (Out S : WideString; Buf : PWideChar; Len : SizeInt);
  1172. var
  1173. BufLen: SizeInt;
  1174. begin
  1175. SetLength(S,Len);
  1176. If (Buf<>Nil) and (Len>0) then
  1177. begin
  1178. BufLen := IndexWord(Buf^, Len+1, 0);
  1179. If (BufLen>0) and (BufLen < Len) then
  1180. Len := BufLen;
  1181. Move (Buf[0],S[1],Len*sizeof(WideChar));
  1182. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1183. end;
  1184. end;
  1185. Procedure SetString (Out S : WideString; Buf : PChar; Len : SizeInt);
  1186. var
  1187. BufLen: SizeInt;
  1188. begin
  1189. SetLength(S,Len);
  1190. If (Buf<>Nil) and (Len>0) then
  1191. begin
  1192. BufLen := IndexByte(Buf^, Len+1, 0);
  1193. If (BufLen>0) and (BufLen < Len) then
  1194. Len := BufLen;
  1195. widestringmanager.Ansi2WideMoveProc(Buf,S,Len);
  1196. //PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1197. end;
  1198. end;
  1199. Function fpc_Val_Real_WideStr(Const S : WideString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  1200. Var
  1201. SS : String;
  1202. begin
  1203. fpc_Val_Real_WideStr := 0;
  1204. if length(S) > 255 then
  1205. code := 256
  1206. else
  1207. begin
  1208. SS := S;
  1209. Val(SS,fpc_Val_Real_WideStr,code);
  1210. end;
  1211. end;
  1212. function fpc_val_enum_widestr(str2ordindex:pointer;const s:widestring;out code:valsint):longint;compilerproc;
  1213. var ss:shortstring;
  1214. begin
  1215. if length(s)>255 then
  1216. code:=256
  1217. else
  1218. begin
  1219. ss:=s;
  1220. val(ss,fpc_val_enum_widestr,code);
  1221. end;
  1222. end;
  1223. Function fpc_Val_Currency_WideStr(Const S : WideString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_WIDESTR']; compilerproc;
  1224. Var
  1225. SS : String;
  1226. begin
  1227. if length(S) > 255 then
  1228. begin
  1229. fpc_Val_Currency_WideStr:=0;
  1230. code := 256;
  1231. end
  1232. else
  1233. begin
  1234. SS := S;
  1235. Val(SS,fpc_Val_Currency_WideStr,code);
  1236. end;
  1237. end;
  1238. Function fpc_Val_UInt_WideStr (Const S : WideString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  1239. Var
  1240. SS : ShortString;
  1241. begin
  1242. fpc_Val_UInt_WideStr := 0;
  1243. if length(S) > 255 then
  1244. code := 256
  1245. else
  1246. begin
  1247. SS := S;
  1248. Val(SS,fpc_Val_UInt_WideStr,code);
  1249. end;
  1250. end;
  1251. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  1252. Var
  1253. SS : ShortString;
  1254. begin
  1255. fpc_Val_SInt_WideStr:=0;
  1256. if length(S)>255 then
  1257. code:=256
  1258. else
  1259. begin
  1260. SS := S;
  1261. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1262. end;
  1263. end;
  1264. {$ifndef CPU64}
  1265. Function fpc_Val_qword_WideStr (Const S : WideString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  1266. Var
  1267. SS : ShortString;
  1268. begin
  1269. fpc_Val_qword_WideStr:=0;
  1270. if length(S)>255 then
  1271. code:=256
  1272. else
  1273. begin
  1274. SS := S;
  1275. Val(SS,fpc_Val_qword_WideStr,Code);
  1276. end;
  1277. end;
  1278. Function fpc_Val_int64_WideStr (Const S : WideString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  1279. Var
  1280. SS : ShortString;
  1281. begin
  1282. fpc_Val_int64_WideStr:=0;
  1283. if length(S)>255 then
  1284. code:=256
  1285. else
  1286. begin
  1287. SS := S;
  1288. Val(SS,fpc_Val_int64_WideStr,Code);
  1289. end;
  1290. end;
  1291. {$endif CPU64}
  1292. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : WideString);compilerproc;
  1293. var
  1294. ss : shortstring;
  1295. begin
  1296. str_real(len,fr,d,treal_type(rt),ss);
  1297. s:=ss;
  1298. end;
  1299. procedure fpc_widestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:widestring);compilerproc;
  1300. var ss:shortstring;
  1301. begin
  1302. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1303. s:=ss;
  1304. end;
  1305. {$ifdef FPC_HAS_STR_CURRENCY}
  1306. procedure fpc_WideStr_Currency(c : Currency;len,fr : SizeInt;out s : WideString);compilerproc;
  1307. var
  1308. ss : shortstring;
  1309. begin
  1310. str(c:len:fr,ss);
  1311. s:=ss;
  1312. end;
  1313. {$endif FPC_HAS_STR_CURRENCY}
  1314. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; out S : WideString);compilerproc;
  1315. Var
  1316. SS : ShortString;
  1317. begin
  1318. Str (v:Len,SS);
  1319. S:=SS;
  1320. end;
  1321. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; out S : WideString);compilerproc;
  1322. Var
  1323. SS : ShortString;
  1324. begin
  1325. str(v:Len,SS);
  1326. S:=SS;
  1327. end;
  1328. {$ifndef CPU64}
  1329. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; out S : WideString);compilerproc;
  1330. Var
  1331. SS : ShortString;
  1332. begin
  1333. Str (v:Len,SS);
  1334. S:=SS;
  1335. end;
  1336. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; out S : WideString);compilerproc;
  1337. Var
  1338. SS : ShortString;
  1339. begin
  1340. str(v:Len,SS);
  1341. S:=SS;
  1342. end;
  1343. {$endif CPU64}
  1344. function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1345. begin
  1346. if assigned(Source) then
  1347. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1348. else
  1349. Result:=0;
  1350. end;
  1351. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt;
  1352. var
  1353. i,j : SizeUInt;
  1354. w : word;
  1355. begin
  1356. result:=0;
  1357. if source=nil then
  1358. exit;
  1359. i:=0;
  1360. j:=0;
  1361. if assigned(Dest) then
  1362. begin
  1363. while (i<SourceChars) and (j<MaxDestBytes) do
  1364. begin
  1365. w:=word(Source[i]);
  1366. case w of
  1367. 0..$7f:
  1368. begin
  1369. Dest[j]:=char(w);
  1370. inc(j);
  1371. end;
  1372. $80..$7ff:
  1373. begin
  1374. if j+1>=MaxDestBytes then
  1375. break;
  1376. Dest[j]:=char($c0 or (w shr 6));
  1377. Dest[j+1]:=char($80 or (w and $3f));
  1378. inc(j,2);
  1379. end;
  1380. else
  1381. begin
  1382. if j+2>=MaxDestBytes then
  1383. break;
  1384. Dest[j]:=char($e0 or (w shr 12));
  1385. Dest[j+1]:=char($80 or ((w shr 6)and $3f));
  1386. Dest[j+2]:=char($80 or (w and $3f));
  1387. inc(j,3);
  1388. end;
  1389. end;
  1390. inc(i);
  1391. end;
  1392. if j>MaxDestBytes-1 then
  1393. j:=MaxDestBytes-1;
  1394. Dest[j]:=#0;
  1395. end
  1396. else
  1397. begin
  1398. while i<SourceChars do
  1399. begin
  1400. case word(Source[i]) of
  1401. $0..$7f:
  1402. inc(j);
  1403. $80..$7ff:
  1404. inc(j,2);
  1405. else
  1406. inc(j,3);
  1407. end;
  1408. inc(i);
  1409. end;
  1410. end;
  1411. result:=j+1;
  1412. end;
  1413. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1414. begin
  1415. if assigned(Source) then
  1416. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1417. else
  1418. Result:=0;
  1419. end;
  1420. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1421. var
  1422. i,j : SizeUInt;
  1423. w: SizeUInt;
  1424. b : byte;
  1425. begin
  1426. if not assigned(Source) then
  1427. begin
  1428. result:=0;
  1429. exit;
  1430. end;
  1431. result:=SizeUInt(-1);
  1432. i:=0;
  1433. j:=0;
  1434. if assigned(Dest) then
  1435. begin
  1436. while (j<MaxDestChars) and (i<SourceBytes) do
  1437. begin
  1438. b:=byte(Source[i]);
  1439. w:=b;
  1440. inc(i);
  1441. // 2 or 3 bytes?
  1442. if b>=$80 then
  1443. begin
  1444. w:=b and $3f;
  1445. if i>=SourceBytes then
  1446. exit;
  1447. // 3 bytes?
  1448. if (b and $20)<>0 then
  1449. begin
  1450. b:=byte(Source[i]);
  1451. inc(i);
  1452. if i>=SourceBytes then
  1453. exit;
  1454. if (b and $c0)<>$80 then
  1455. exit;
  1456. w:=(w shl 6) or (b and $3f);
  1457. end;
  1458. b:=byte(Source[i]);
  1459. w:=(w shl 6) or (b and $3f);
  1460. if (b and $c0)<>$80 then
  1461. exit;
  1462. inc(i);
  1463. end;
  1464. Dest[j]:=WideChar(w);
  1465. inc(j);
  1466. end;
  1467. if j>=MaxDestChars then j:=MaxDestChars-1;
  1468. Dest[j]:=#0;
  1469. end
  1470. else
  1471. begin
  1472. while i<SourceBytes do
  1473. begin
  1474. b:=byte(Source[i]);
  1475. inc(i);
  1476. // 2 or 3 bytes?
  1477. if b>=$80 then
  1478. begin
  1479. if i>=SourceBytes then
  1480. exit;
  1481. // 3 bytes?
  1482. b := b and $3f;
  1483. if (b and $20)<>0 then
  1484. begin
  1485. b:=byte(Source[i]);
  1486. inc(i);
  1487. if i>=SourceBytes then
  1488. exit;
  1489. if (b and $c0)<>$80 then
  1490. exit;
  1491. end;
  1492. if (byte(Source[i]) and $c0)<>$80 then
  1493. exit;
  1494. inc(i);
  1495. end;
  1496. inc(j);
  1497. end;
  1498. end;
  1499. result:=j+1;
  1500. end;
  1501. function UTF8Encode(const s : WideString) : UTF8String;
  1502. var
  1503. i : SizeInt;
  1504. hs : UTF8String;
  1505. begin
  1506. result:='';
  1507. if s='' then
  1508. exit;
  1509. SetLength(hs,length(s)*3);
  1510. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  1511. if i>0 then
  1512. begin
  1513. SetLength(hs,i-1);
  1514. result:=hs;
  1515. end;
  1516. end;
  1517. function UTF8Decode(const s : UTF8String): WideString;
  1518. var
  1519. i : SizeInt;
  1520. hs : WideString;
  1521. begin
  1522. result:='';
  1523. if s='' then
  1524. exit;
  1525. SetLength(hs,length(s));
  1526. i:=Utf8ToUnicode(PWideChar(hs),length(hs)+1,pchar(s),length(s));
  1527. if i>0 then
  1528. begin
  1529. SetLength(hs,i-1);
  1530. result:=hs;
  1531. end;
  1532. end;
  1533. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  1534. begin
  1535. Result:=Utf8Encode(s);
  1536. end;
  1537. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1538. begin
  1539. Result:=Utf8Decode(s);
  1540. end;
  1541. function WideStringToUCS4String(const s : WideString) : UCS4String;
  1542. var
  1543. i : SizeInt;
  1544. begin
  1545. setlength(result,length(s)+1);
  1546. for i:=1 to length(s) do
  1547. result[i-1]:=UCS4Char(s[i]);
  1548. result[length(s)]:=UCS4Char(0);
  1549. end;
  1550. function UCS4StringToWideString(const s : UCS4String) : WideString;
  1551. var
  1552. i : SizeInt;
  1553. begin
  1554. setlength(result,length(s)-1);
  1555. for i:=1 to length(s)-1 do
  1556. result[i]:=WideChar(s[i-1]);
  1557. end;
  1558. procedure unimplementedwidestring;
  1559. begin
  1560. HandleErrorFrame(215,get_frame);
  1561. end;
  1562. {$warnings off}
  1563. function GenericWideCase(const s : WideString) : WideString;
  1564. begin
  1565. unimplementedwidestring;
  1566. end;
  1567. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  1568. begin
  1569. unimplementedwidestring;
  1570. end;
  1571. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  1572. begin
  1573. unimplementedwidestring;
  1574. end;
  1575. function CharLengthPChar(const Str: PChar): PtrInt;
  1576. begin
  1577. unimplementedwidestring;
  1578. end;
  1579. {$warnings on}
  1580. procedure initwidestringmanager;
  1581. begin
  1582. fillchar(widestringmanager,sizeof(widestringmanager),0);
  1583. {$ifndef HAS_WIDESTRINGMANAGER}
  1584. widestringmanager.Wide2AnsiMoveProc:=@defaultWide2AnsiMove;
  1585. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2WideMove;
  1586. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  1587. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  1588. {$endif HAS_WIDESTRINGMANAGER}
  1589. widestringmanager.CompareWideStringProc:=@CompareWideString;
  1590. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  1591. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  1592. end;