wustrings.inc 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  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. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  234. function fpc_WideStr_To_ShortStr (high_of_res: SizeInt;const S2 : WideString): shortstring;[Public, alias: 'FPC_WIDESTR_TO_SHORTSTR']; compilerproc;
  235. {
  236. Converts a WideString to a ShortString;
  237. }
  238. Var
  239. Size : SizeInt;
  240. temp : ansistring;
  241. begin
  242. result:='';
  243. Size:=Length(S2);
  244. if Size>0 then
  245. begin
  246. If Size>high_of_res then
  247. Size:=high_of_res;
  248. widestringmanager.Wide2AnsiMoveProc(PWideChar(S2),temp,Size);
  249. result:=temp;
  250. end;
  251. end;
  252. {$else FPC_STRTOSHORTSTRINGPROC}
  253. procedure fpc_WideStr_To_ShortStr (out res: ShortString;const S2 : WideString); [Public, alias: 'FPC_WIDESTR_TO_SHORTSTR'];compilerproc;
  254. {
  255. Converts a WideString to a ShortString;
  256. }
  257. Var
  258. Size : SizeInt;
  259. temp : ansistring;
  260. begin
  261. res:='';
  262. Size:=Length(S2);
  263. if Size>0 then
  264. begin
  265. If Size>high(res) then
  266. Size:=high(res);
  267. widestringmanager.Wide2AnsiMoveProc(PWideChar(S2),temp,Size);
  268. res:=temp;
  269. end;
  270. end;
  271. {$endif FPC_STRTOSHORTSTRINGPROC}
  272. Function fpc_ShortStr_To_WideStr (Const S2 : ShortString): WideString;compilerproc;
  273. {
  274. Converts a ShortString to a WideString;
  275. }
  276. Var
  277. Size : SizeInt;
  278. begin
  279. result:='';
  280. Size:=Length(S2);
  281. if Size>0 then
  282. begin
  283. widestringmanager.Ansi2WideMoveProc(PChar(@S2[1]),result,Size);
  284. { Terminating Zero }
  285. PWideChar(Pointer(fpc_ShortStr_To_WideStr)+Size*sizeof(WideChar))^:=#0;
  286. end;
  287. end;
  288. Function fpc_WideStr_To_AnsiStr (const S2 : WideString): AnsiString; compilerproc;
  289. {
  290. Converts a WideString to an AnsiString
  291. }
  292. Var
  293. Size : SizeInt;
  294. begin
  295. result:='';
  296. Size:=Length(S2);
  297. if Size>0 then
  298. widestringmanager.Wide2AnsiMoveProc(PWideChar(Pointer(S2)),result,Size);
  299. end;
  300. Function fpc_AnsiStr_To_WideStr (Const S2 : AnsiString): WideString; compilerproc;
  301. {
  302. Converts an AnsiString to a WideString;
  303. }
  304. Var
  305. Size : SizeInt;
  306. begin
  307. result:='';
  308. Size:=Length(S2);
  309. if Size>0 then
  310. widestringmanager.Ansi2WideMoveProc(PChar(S2),result,Size);
  311. end;
  312. Function fpc_PWideChar_To_AnsiStr(const p : pwidechar): ansistring; compilerproc;
  313. var
  314. Size : SizeInt;
  315. begin
  316. result:='';
  317. if p=nil then
  318. exit;
  319. Size := IndexWord(p^, -1, 0);
  320. if Size>0 then
  321. widestringmanager.Wide2AnsiMoveProc(P,result,Size);
  322. end;
  323. Function fpc_PWideChar_To_WideStr(const p : pwidechar): widestring; compilerproc;
  324. var
  325. Size : SizeInt;
  326. begin
  327. result:='';
  328. if p=nil then
  329. exit;
  330. Size := IndexWord(p^, -1, 0);
  331. Setlength(result,Size);
  332. if Size>0 then
  333. begin
  334. Move(p^,PWideChar(Pointer(result))^,Size*sizeof(WideChar));
  335. { Terminating Zero }
  336. PWideChar(Pointer(result)+Size*sizeof(WideChar))^:=#0;
  337. end;
  338. end;
  339. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  340. Function fpc_PWideChar_To_ShortStr(const p : pwidechar): shortstring; compilerproc;
  341. var
  342. Size : SizeInt;
  343. temp: ansistring;
  344. begin
  345. result:='';
  346. if p=nil then
  347. exit;
  348. Size := IndexWord(p^, $7fffffff, 0);
  349. if Size>0 then
  350. begin
  351. widestringmanager.Wide2AnsiMoveProc(p,temp,Size);
  352. result:=temp;
  353. end;
  354. end;
  355. {$else FPC_STRTOSHORTSTRINGPROC}
  356. procedure fpc_PWideChar_To_ShortStr(out res : shortstring;const p : pwidechar); compilerproc;
  357. var
  358. Size : SizeInt;
  359. temp: ansistring;
  360. begin
  361. res:='';
  362. if p=nil then
  363. exit;
  364. Size:=IndexWord(p^, high(PtrInt), 0);
  365. if Size>0 then
  366. begin
  367. widestringmanager.Wide2AnsiMoveProc(p,temp,Size);
  368. res:=temp;
  369. end;
  370. end;
  371. {$endif FPC_STRTOSHORTSTRINGPROC}
  372. { checked against the ansistring routine, 2001-05-27 (FK) }
  373. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_WIDESTR_ASSIGN']; compilerproc;
  374. {
  375. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  376. }
  377. begin
  378. {$ifdef FPC_WINLIKEWIDESTRING}
  379. if S1=S2 then exit;
  380. if S2<>nil then
  381. begin
  382. if IsWideStringConstant(S1) then
  383. begin
  384. S1:=NewWidestring(length(WideString(S2)));
  385. move(s2^,s1^,(length(WideString(s1))+1)*sizeof(widechar));
  386. end
  387. else
  388. {$ifdef MSWINDOWS}
  389. if winwidestringalloc then
  390. begin
  391. if SysReAllocStringLen(S1, S2, Length(WideString(S2))) = 0 then
  392. WideStringError;
  393. end
  394. else
  395. {$endif MSWINDOWS}
  396. begin
  397. SetLength(WideString(S1),length(WideString(S2)));
  398. move(s2^,s1^,(length(WideString(s1))+1)*sizeof(widechar));
  399. end;
  400. end
  401. else
  402. begin
  403. { Free S1 }
  404. fpc_widestr_decr_ref (S1);
  405. S1:=nil;
  406. end;
  407. {$else FPC_WINLIKEWIDESTRING}
  408. If S2<>nil then
  409. If PWideRec(S2-WideFirstOff)^.Ref>0 then
  410. inclocked(PWideRec(S2-WideFirstOff)^.ref);
  411. { Decrease the reference count on the old S1 }
  412. fpc_widestr_decr_ref (S1);
  413. s1:=s2;
  414. {$endif FPC_WINLIKEWIDESTRING}
  415. end;
  416. { alias for internal use }
  417. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_WIDESTR_ASSIGN'];
  418. {$ifndef STR_CONCAT_PROCS}
  419. function fpc_WideStr_Concat (const S1,S2 : WideString): WideString; compilerproc;
  420. Var
  421. Size,Location : SizeInt;
  422. pc : pwidechar;
  423. begin
  424. { only assign if s1 or s2 is empty }
  425. if (S1='') then
  426. begin
  427. result:=s2;
  428. exit;
  429. end;
  430. if (S2='') then
  431. begin
  432. result:=s1;
  433. exit;
  434. end;
  435. Location:=Length(S1);
  436. Size:=length(S2);
  437. SetLength(result,Size+Location);
  438. pc:=pwidechar(result);
  439. Move(S1[1],pc^,Location*sizeof(WideChar));
  440. inc(pc,location);
  441. Move(S2[1],pc^,(Size+1)*sizeof(WideChar));
  442. end;
  443. function fpc_WideStr_Concat_multi (const sarr:array of Widestring): widestring; compilerproc;
  444. Var
  445. i : Longint;
  446. p : pointer;
  447. pc : pwidechar;
  448. Size,NewSize : SizeInt;
  449. begin
  450. { First calculate size of the result so we can do
  451. a single call to SetLength() }
  452. NewSize:=0;
  453. for i:=low(sarr) to high(sarr) do
  454. inc(Newsize,length(sarr[i]));
  455. SetLength(result,NewSize);
  456. pc:=pwidechar(result);
  457. for i:=low(sarr) to high(sarr) do
  458. begin
  459. p:=pointer(sarr[i]);
  460. if assigned(p) then
  461. begin
  462. Size:=length(widestring(p));
  463. Move(pwidechar(p)^,pc^,(Size+1)*sizeof(WideChar));
  464. inc(pc,size);
  465. end;
  466. end;
  467. end;
  468. {$else STR_CONCAT_PROCS}
  469. procedure fpc_WideStr_Concat (var DestS:Widestring;const S1,S2 : WideString); compilerproc;
  470. Var
  471. Size,Location : SizeInt;
  472. same : boolean;
  473. begin
  474. { only assign if s1 or s2 is empty }
  475. if (S1='') then
  476. begin
  477. DestS:=s2;
  478. exit;
  479. end;
  480. if (S2='') then
  481. begin
  482. DestS:=s1;
  483. exit;
  484. end;
  485. Location:=Length(S1);
  486. Size:=length(S2);
  487. { Use Pointer() typecasts to prevent extra conversion code }
  488. if Pointer(DestS)=Pointer(S1) then
  489. begin
  490. same:=Pointer(S1)=Pointer(S2);
  491. SetLength(DestS,Size+Location);
  492. if same then
  493. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size)*sizeof(WideChar))
  494. else
  495. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  496. end
  497. else if Pointer(DestS)=Pointer(S2) then
  498. begin
  499. SetLength(DestS,Size+Location);
  500. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  501. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(WideChar));
  502. end
  503. else
  504. begin
  505. DestS:='';
  506. SetLength(DestS,Size+Location);
  507. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(WideChar));
  508. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  509. end;
  510. end;
  511. procedure fpc_WideStr_Concat_multi (var DestS:Widestring;const sarr:array of Widestring); compilerproc;
  512. Var
  513. lowstart,i : Longint;
  514. p,pc : pointer;
  515. Size,NewLen,
  516. OldDestLen : SizeInt;
  517. destcopy : pointer;
  518. begin
  519. if high(sarr)=0 then
  520. begin
  521. DestS:='';
  522. exit;
  523. end;
  524. destcopy:=nil;
  525. lowstart:=low(sarr);
  526. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  527. inc(lowstart);
  528. { Check for another reuse, then we can't use
  529. the append optimization }
  530. for i:=lowstart to high(sarr) do
  531. begin
  532. if Pointer(DestS)=Pointer(sarr[i]) then
  533. begin
  534. { if DestS is used somewhere in the middle of the expression,
  535. we need to make sure the original string still exists after
  536. we empty/modify DestS }
  537. destcopy:=pointer(dests);
  538. fpc_WideStr_Incr_Ref(destcopy);
  539. lowstart:=low(sarr);
  540. break;
  541. end;
  542. end;
  543. { Start with empty DestS if we start with concatting
  544. the first array element }
  545. if lowstart=low(sarr) then
  546. DestS:='';
  547. OldDestLen:=length(DestS);
  548. { Calculate size of the result so we can do
  549. a single call to SetLength() }
  550. NewLen:=0;
  551. for i:=low(sarr) to high(sarr) do
  552. inc(NewLen,length(sarr[i]));
  553. SetLength(DestS,NewLen);
  554. { Concat all strings, except the string we already
  555. copied in DestS }
  556. pc:=Pointer(DestS)+OldDestLen*sizeof(WideChar);
  557. for i:=lowstart to high(sarr) do
  558. begin
  559. p:=pointer(sarr[i]);
  560. if assigned(p) then
  561. begin
  562. Size:=length(widestring(p));
  563. Move(p^,pc^,(Size+1)*sizeof(WideChar));
  564. inc(pc,size*sizeof(WideChar));
  565. end;
  566. end;
  567. fpc_WideStr_Decr_Ref(destcopy);
  568. end;
  569. {$endif STR_CONCAT_PROCS}
  570. Function fpc_Char_To_WChar(const c : Char): WideChar; compilerproc;
  571. var
  572. w: widestring;
  573. begin
  574. widestringmanager.Ansi2WideMoveProc(@c, w, 1);
  575. fpc_Char_To_WChar:= w[1];
  576. end;
  577. Function fpc_Char_To_WideStr(const c : Char): WideString; compilerproc;
  578. {
  579. Converts a Char to a WideString;
  580. }
  581. begin
  582. Setlength(fpc_Char_To_WideStr,1);
  583. fpc_Char_To_WideStr[1]:=c;
  584. { Terminating Zero }
  585. PWideChar(Pointer(fpc_Char_To_WideStr)+sizeof(WideChar))^:=#0;
  586. end;
  587. Function fpc_WChar_To_Char(const c : WideChar): Char; compilerproc;
  588. {
  589. Converts a WideChar to a Char;
  590. }
  591. var
  592. s: ansistring;
  593. begin
  594. widestringmanager.Wide2AnsiMoveProc(@c, s, 1);
  595. if length(s)=1 then
  596. fpc_WChar_To_Char:= s[1]
  597. else
  598. fpc_WChar_To_Char:='?';
  599. end;
  600. Function fpc_WChar_To_WideStr(const c : WideChar): WideString; compilerproc;
  601. {
  602. Converts a WideChar to a WideString;
  603. }
  604. begin
  605. Setlength (fpc_WChar_To_WideStr,1);
  606. fpc_WChar_To_WideStr[1]:= c;
  607. end;
  608. Function fpc_WChar_To_AnsiStr(const c : WideChar): AnsiString; compilerproc;
  609. {
  610. Converts a WideChar to a AnsiString;
  611. }
  612. begin
  613. widestringmanager.Wide2AnsiMoveProc(@c, fpc_WChar_To_AnsiStr, 1);
  614. end;
  615. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  616. Function fpc_WChar_To_ShortStr(const c : WideChar): ShortString; compilerproc;
  617. {
  618. Converts a WideChar to a ShortString;
  619. }
  620. var
  621. s: ansistring;
  622. begin
  623. widestringmanager.Wide2AnsiMoveProc(@c, s, 1);
  624. fpc_WChar_To_ShortStr:= s;
  625. end;
  626. {$else FPC_STRTOSHORTSTRINGPROC}
  627. procedure fpc_WChar_To_ShortStr(out res : shortstring;const c : WideChar) compilerproc;
  628. {
  629. Converts a WideChar to a ShortString;
  630. }
  631. var
  632. s: ansistring;
  633. begin
  634. widestringmanager.Wide2AnsiMoveProc(@c,s,1);
  635. res:=s;
  636. end;
  637. {$endif FPC_STRTOSHORTSTRINGPROC}
  638. Function fpc_PChar_To_WideStr(const p : pchar): WideString; compilerproc;
  639. Var
  640. L : SizeInt;
  641. begin
  642. if (not assigned(p)) or (p[0]=#0) Then
  643. { result is automatically set to '' }
  644. exit;
  645. l:=IndexChar(p^,-1,#0);
  646. widestringmanager.Ansi2WideMoveProc(P,fpc_PChar_To_WideStr,l);
  647. end;
  648. Function fpc_CharArray_To_WideStr(const arr: array of char; zerobased: boolean = true): WideString; compilerproc;
  649. var
  650. i : SizeInt;
  651. begin
  652. if (zerobased) then
  653. begin
  654. if (arr[0]=#0) Then
  655. { result is automatically set to '' }
  656. exit;
  657. i:=IndexChar(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_CharArray_To_WideStr,i);
  664. widestringmanager.Ansi2WideMoveProc (pchar(@arr),fpc_CharArray_To_WideStr,i);
  665. end;
  666. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  667. function fpc_WideCharArray_To_ShortStr(const arr: array of widechar; zerobased: boolean = true): shortstring;[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  668. var
  669. l: longint;
  670. index: longint;
  671. len: byte;
  672. temp: ansistring;
  673. begin
  674. l := high(arr)+1;
  675. if l>=256 then
  676. l:=255
  677. else if l<0 then
  678. l:=0;
  679. if zerobased then
  680. begin
  681. index:=IndexWord(arr[0],l,0);
  682. if (index < 0) then
  683. len := l
  684. else
  685. len := index;
  686. end
  687. else
  688. len := l;
  689. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,len);
  690. fpc_WideCharArray_To_ShortStr := temp;
  691. end;
  692. {$else FPC_STRTOSHORTSTRINGPROC}
  693. procedure fpc_WideCharArray_To_ShortStr(out res : shortstring;const arr: array of widechar; zerobased: boolean = true);[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  694. var
  695. l: longint;
  696. index: ptrint;
  697. len: byte;
  698. temp: ansistring;
  699. begin
  700. l := high(arr)+1;
  701. if l>=high(res)+1 then
  702. l:=high(res)
  703. else if l<0 then
  704. l:=0;
  705. if zerobased then
  706. begin
  707. index:=IndexWord(arr[0],l,0);
  708. if index<0 then
  709. len:=l
  710. else
  711. len:=index;
  712. end
  713. else
  714. len:=l;
  715. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,len);
  716. res:=temp;
  717. end;
  718. {$endif FPC_STRTOSHORTSTRINGPROC}
  719. Function fpc_WideCharArray_To_AnsiStr(const arr: array of widechar; zerobased: boolean = true): AnsiString; compilerproc;
  720. var
  721. i : SizeInt;
  722. begin
  723. if (zerobased) then
  724. begin
  725. i:=IndexWord(arr,high(arr)+1,0);
  726. if i = -1 then
  727. i := high(arr)+1;
  728. end
  729. else
  730. i := high(arr)+1;
  731. SetLength(fpc_WideCharArray_To_AnsiStr,i);
  732. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),fpc_WideCharArray_To_AnsiStr,i);
  733. end;
  734. Function fpc_WideCharArray_To_WideStr(const arr: array of widechar; zerobased: boolean = true): WideString; compilerproc;
  735. var
  736. i : SizeInt;
  737. begin
  738. if (zerobased) then
  739. begin
  740. i:=IndexWord(arr,high(arr)+1,0);
  741. if i = -1 then
  742. i := high(arr)+1;
  743. end
  744. else
  745. i := high(arr)+1;
  746. SetLength(fpc_WideCharArray_To_WideStr,i);
  747. Move(arr[0], Pointer(fpc_WideCharArray_To_WideStr)^,i*sizeof(WideChar));
  748. end;
  749. {$ifndef FPC_STRTOCHARARRAYPROC}
  750. { inside the compiler, the resulttype is modified to that of the actual }
  751. { chararray we're converting to (JM) }
  752. function fpc_widestr_to_chararray(arraysize: SizeInt; const src: WideString): fpc_big_chararray;[public,alias: 'FPC_WIDESTR_TO_CHARARRAY']; compilerproc;
  753. var
  754. len: SizeInt;
  755. temp: ansistring;
  756. begin
  757. len := length(src);
  758. { make sure we don't dereference src if it can be nil (JM) }
  759. if len > 0 then
  760. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,len);
  761. len := length(temp);
  762. if len > arraysize then
  763. len := arraysize;
  764. {$r-}
  765. move(temp[1],fpc_widestr_to_chararray[0],len);
  766. fillchar(fpc_widestr_to_chararray[len],arraysize-len,0);
  767. {$ifdef RangeCheckWasOn}
  768. {$r+}
  769. {$endif}
  770. end;
  771. { inside the compiler, the resulttype is modified to that of the actual }
  772. { widechararray we're converting to (JM) }
  773. function fpc_widestr_to_widechararray(arraysize: SizeInt; const src: WideString): fpc_big_widechararray;[public,alias: 'FPC_WIDESTR_TO_WIDECHARARRAY']; compilerproc;
  774. var
  775. len: SizeInt;
  776. begin
  777. len := length(src);
  778. if len > arraysize then
  779. len := arraysize;
  780. {$r-}
  781. { make sure we don't try to access element 1 of the ansistring if it's nil }
  782. if len > 0 then
  783. move(src[1],fpc_widestr_to_widechararray[0],len*SizeOf(WideChar));
  784. fillchar(fpc_widestr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  785. {$ifdef RangeCheckWasOn}
  786. {$r+}
  787. {$endif}
  788. end;
  789. { inside the compiler, the resulttype is modified to that of the actual }
  790. { chararray we're converting to (JM) }
  791. function fpc_ansistr_to_widechararray(arraysize: SizeInt; const src: AnsiString): fpc_big_widechararray;[public,alias: 'FPC_ANSISTR_TO_WIDECHARARRAY']; compilerproc;
  792. var
  793. len: SizeInt;
  794. temp: widestring;
  795. begin
  796. len := length(src);
  797. { make sure we don't dereference src if it can be nil (JM) }
  798. if len > 0 then
  799. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  800. len := length(temp);
  801. if len > arraysize then
  802. len := arraysize;
  803. {$r-}
  804. move(temp[1],fpc_ansistr_to_widechararray[0],len*sizeof(widechar));
  805. fillchar(fpc_ansistr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  806. {$ifdef RangeCheckWasOn}
  807. {$r+}
  808. {$endif}
  809. end;
  810. function fpc_shortstr_to_widechararray(arraysize: SizeInt; const src: ShortString): fpc_big_widechararray;[public,alias: 'FPC_SHORTSTR_TO_WIDECHARARRAY']; compilerproc;
  811. var
  812. len: longint;
  813. temp : widestring;
  814. begin
  815. len := length(src);
  816. { make sure we don't access char 1 if length is 0 (JM) }
  817. if len > 0 then
  818. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  819. len := length(temp);
  820. if len > arraysize then
  821. len := arraysize;
  822. {$r-}
  823. move(temp[1],fpc_shortstr_to_widechararray[0],len*sizeof(widechar));
  824. fillchar(fpc_shortstr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  825. {$ifdef RangeCheckWasOn}
  826. {$r+}
  827. {$endif}
  828. end;
  829. {$else ndef FPC_STRTOCHARARRAYPROC}
  830. procedure fpc_widestr_to_chararray(out res: array of char; const src: WideString); compilerproc;
  831. var
  832. len: SizeInt;
  833. temp: ansistring;
  834. begin
  835. len := length(src);
  836. { make sure we don't dereference src if it can be nil (JM) }
  837. if len > 0 then
  838. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,len);
  839. len := length(temp);
  840. if len > length(res) then
  841. len := length(res);
  842. {$r-}
  843. move(temp[1],res[0],len);
  844. fillchar(res[len],length(res)-len,0);
  845. {$ifdef RangeCheckWasOn}
  846. {$r+}
  847. {$endif}
  848. end;
  849. procedure fpc_widestr_to_widechararray(out res: array of widechar; const src: WideString); compilerproc;
  850. var
  851. len: SizeInt;
  852. begin
  853. len := length(src);
  854. if len > length(res) then
  855. len := length(res);
  856. {$r-}
  857. { make sure we don't try to access element 1 of the ansistring if it's nil }
  858. if len > 0 then
  859. move(src[1],res[0],len*SizeOf(WideChar));
  860. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  861. {$ifdef RangeCheckWasOn}
  862. {$r+}
  863. {$endif}
  864. end;
  865. procedure fpc_ansistr_to_widechararray(out res: array of widechar; const src: AnsiString); compilerproc;
  866. var
  867. len: SizeInt;
  868. temp: widestring;
  869. begin
  870. len := length(src);
  871. { make sure we don't dereference src if it can be nil (JM) }
  872. if len > 0 then
  873. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  874. len := length(temp);
  875. if len > length(res) then
  876. len := length(res);
  877. {$r-}
  878. move(temp[1],res[0],len*sizeof(widechar));
  879. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  880. {$ifdef RangeCheckWasOn}
  881. {$r+}
  882. {$endif}
  883. end;
  884. procedure fpc_shortstr_to_widechararray(out res: array of widechar; const src: ShortString); compilerproc;
  885. var
  886. len: longint;
  887. temp : widestring;
  888. begin
  889. len := length(src);
  890. { make sure we don't access char 1 if length is 0 (JM) }
  891. if len > 0 then
  892. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  893. len := length(temp);
  894. if len > length(res) then
  895. len := length(res);
  896. {$r-}
  897. move(temp[1],res[0],len*sizeof(widechar));
  898. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  899. {$ifdef RangeCheckWasOn}
  900. {$r+}
  901. {$endif}
  902. end;
  903. {$endif ndef FPC_STRTOCHARARRAYPROC}
  904. Function fpc_WideStr_Compare(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE']; compilerproc;
  905. {
  906. Compares 2 WideStrings;
  907. The result is
  908. <0 if S1<S2
  909. 0 if S1=S2
  910. >0 if S1>S2
  911. }
  912. Var
  913. MaxI,Temp : SizeInt;
  914. begin
  915. if pointer(S1)=pointer(S2) then
  916. begin
  917. fpc_WideStr_Compare:=0;
  918. exit;
  919. end;
  920. Maxi:=Length(S1);
  921. temp:=Length(S2);
  922. If MaxI>Temp then
  923. MaxI:=Temp;
  924. Temp:=CompareWord(S1[1],S2[1],MaxI);
  925. if temp=0 then
  926. temp:=Length(S1)-Length(S2);
  927. fpc_WideStr_Compare:=Temp;
  928. end;
  929. Function fpc_WideStr_Compare_Equal(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE_EQUAL']; compilerproc;
  930. {
  931. Compares 2 WideStrings for equality only;
  932. The result is
  933. 0 if S1=S2
  934. <>0 if S1<>S2
  935. }
  936. Var
  937. MaxI : SizeInt;
  938. begin
  939. if pointer(S1)=pointer(S2) then
  940. exit(0);
  941. Maxi:=Length(S1);
  942. If MaxI<>Length(S2) then
  943. exit(-1)
  944. else
  945. exit(CompareWord(S1[1],S2[1],MaxI));
  946. end;
  947. Procedure fpc_WideStr_CheckZero(p : pointer);[Public,Alias : 'FPC_WIDESTR_CHECKZERO']; compilerproc;
  948. begin
  949. if p=nil then
  950. HandleErrorFrame(201,get_frame);
  951. end;
  952. Procedure fpc_WideStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; compilerproc;
  953. begin
  954. if (index>len div 2) or (Index<1) then
  955. HandleErrorFrame(201,get_frame);
  956. end;
  957. Procedure fpc_WideStr_SetLength(Var S : WideString; l : SizeInt);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; compilerproc;
  958. {
  959. Sets The length of string S to L.
  960. Makes sure S is unique, and contains enough room.
  961. }
  962. Var
  963. Temp : Pointer;
  964. movelen: SizeInt;
  965. begin
  966. if (l>0) then
  967. begin
  968. if Pointer(S)=nil then
  969. begin
  970. { Need a complete new string...}
  971. Pointer(s):=NewWideString(l);
  972. end
  973. { windows doesn't support reallocing widestrings, this code
  974. is anyways subject to be removed because widestrings shouldn't be
  975. ref. counted anymore (FK) }
  976. else
  977. if
  978. {$ifdef MSWINDOWS}
  979. not winwidestringalloc and
  980. {$endif MSWINDOWS}
  981. {$ifdef FPC_WINLIKEWIDESTRING}
  982. not IsWideStringConstant(pointer(S))
  983. {$else}
  984. (PWideRec(Pointer(S)-WideFirstOff)^.Ref = 1)
  985. {$endif FPC_WINLIKEWIDESTRING}
  986. then
  987. begin
  988. Dec(Pointer(S),WideFirstOff);
  989. if SizeUInt(L*sizeof(WideChar)+WideRecLen)>MemSize(Pointer(S)) then
  990. reallocmem(pointer(S), L*sizeof(WideChar)+WideRecLen);
  991. Inc(Pointer(S), WideFirstOff);
  992. end
  993. else
  994. begin
  995. { Reallocation is needed... }
  996. Temp:=Pointer(NewWideString(L));
  997. if Length(S)>0 then
  998. begin
  999. if l < succ(length(s)) then
  1000. movelen := l
  1001. { also move terminating null }
  1002. else
  1003. movelen := succ(length(s));
  1004. Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
  1005. end;
  1006. fpc_widestr_decr_ref(Pointer(S));
  1007. Pointer(S):=Temp;
  1008. end;
  1009. { Force nil termination in case it gets shorter }
  1010. PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
  1011. {$ifdef MSWINDOWS}
  1012. if not winwidestringalloc then
  1013. {$endif MSWINDOWS}
  1014. PWideRec(Pointer(S)-WideFirstOff)^.Len:=l*sizeof(WideChar);
  1015. end
  1016. else
  1017. begin
  1018. { Length=0 }
  1019. if Pointer(S)<>nil then
  1020. fpc_widestr_decr_ref (Pointer(S));
  1021. Pointer(S):=Nil;
  1022. end;
  1023. end;
  1024. {*****************************************************************************
  1025. Public functions, In interface.
  1026. *****************************************************************************}
  1027. function WideCharToString(S : PWideChar) : AnsiString;
  1028. begin
  1029. result:=WideCharLenToString(s,Length(WideString(s)));
  1030. end;
  1031. function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  1032. var
  1033. temp:widestring;
  1034. begin
  1035. widestringmanager.Ansi2WideMoveProc(PChar(Src),temp,Length(Src));
  1036. if Length(temp)<DestSize then
  1037. move(temp[1],Dest^,Length(temp)*SizeOf(WideChar))
  1038. else
  1039. move(temp[1],Dest^,(DestSize-1)*SizeOf(WideChar));
  1040. Dest[DestSize-1]:=#0;
  1041. result:=Dest;
  1042. end;
  1043. function WideCharLenToString(S : PWideChar;Len : SizeInt) : AnsiString;
  1044. begin
  1045. //SetLength(result,Len);
  1046. widestringmanager.Wide2AnsiMoveproc(S,result,Len);
  1047. end;
  1048. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : AnsiString);
  1049. begin
  1050. Dest:=WideCharLenToString(Src,Len);
  1051. end;
  1052. procedure WideCharToStrVar(S : PWideChar;out Dest : AnsiString);
  1053. begin
  1054. Dest:=WideCharToString(S);
  1055. end;
  1056. Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; compilerproc;
  1057. {$ifdef FPC_WINLIKEWIDESTRING}
  1058. begin
  1059. pointer(result) := pointer(s);
  1060. end;
  1061. {$else FPC_WINLIKEWIDESTRING}
  1062. {
  1063. Make sure reference count of S is 1,
  1064. using copy-on-write semantics.
  1065. }
  1066. Var
  1067. SNew : Pointer;
  1068. L : SizeInt;
  1069. begin
  1070. pointer(result) := pointer(s);
  1071. If Pointer(S)=Nil then
  1072. exit;
  1073. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  1074. begin
  1075. L:=PWideRec(Pointer(S)-WideFirstOff)^.len div sizeof(WideChar);
  1076. SNew:=NewWideString (L);
  1077. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  1078. PWideRec(SNew-WideFirstOff)^.len:=L * sizeof(WideChar);
  1079. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  1080. pointer(S):=SNew;
  1081. pointer(result):=SNew;
  1082. end;
  1083. end;
  1084. {$endif FPC_WINLIKEWIDESTRING}
  1085. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : SizeInt) : WideString;compilerproc;
  1086. var
  1087. ResultAddress : Pointer;
  1088. begin
  1089. ResultAddress:=Nil;
  1090. dec(index);
  1091. if Index < 0 then
  1092. Index := 0;
  1093. { Check Size. Accounts for Zero-length S, the double check is needed because
  1094. Size can be maxint and will get <0 when adding index }
  1095. if (Size>Length(S)) or
  1096. (Index+Size>Length(S)) then
  1097. Size:=Length(S)-Index;
  1098. If Size>0 then
  1099. begin
  1100. If Index<0 Then
  1101. Index:=0;
  1102. ResultAddress:=Pointer(NewWideString (Size));
  1103. if ResultAddress<>Nil then
  1104. begin
  1105. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  1106. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size*sizeof(WideChar);
  1107. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  1108. end;
  1109. end;
  1110. fpc_widestr_decr_ref(Pointer(fpc_widestr_copy));
  1111. Pointer(fpc_widestr_Copy):=ResultAddress;
  1112. end;
  1113. Function Pos (Const Substr : WideString; Const Source : WideString) : SizeInt;
  1114. var
  1115. i,MaxLen : SizeInt;
  1116. pc : pwidechar;
  1117. begin
  1118. Pos:=0;
  1119. if Length(SubStr)>0 then
  1120. begin
  1121. MaxLen:=Length(source)-Length(SubStr);
  1122. i:=0;
  1123. pc:=@source[1];
  1124. while (i<=MaxLen) do
  1125. begin
  1126. inc(i);
  1127. if (SubStr[1]=pc^) and
  1128. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  1129. begin
  1130. Pos:=i;
  1131. exit;
  1132. end;
  1133. inc(pc);
  1134. end;
  1135. end;
  1136. end;
  1137. { Faster version for a widechar alone }
  1138. Function Pos (c : WideChar; Const s : WideString) : SizeInt;
  1139. var
  1140. i: SizeInt;
  1141. pc : pwidechar;
  1142. begin
  1143. pc:=@s[1];
  1144. for i:=1 to length(s) do
  1145. begin
  1146. if pc^=c then
  1147. begin
  1148. pos:=i;
  1149. exit;
  1150. end;
  1151. inc(pc);
  1152. end;
  1153. pos:=0;
  1154. end;
  1155. Function Pos (c : WideChar; Const s : AnsiString) : SizeInt;
  1156. var
  1157. i: SizeInt;
  1158. pc : pchar;
  1159. begin
  1160. pc:=@s[1];
  1161. for i:=1 to length(s) do
  1162. begin
  1163. if widechar(pc^)=c then
  1164. begin
  1165. pos:=i;
  1166. exit;
  1167. end;
  1168. inc(pc);
  1169. end;
  1170. pos:=0;
  1171. end;
  1172. Function Pos (c : AnsiString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1173. begin
  1174. result:=Pos(WideString(c),s);
  1175. end;
  1176. Function Pos (c : ShortString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1177. begin
  1178. result:=Pos(WideString(c),s);
  1179. end;
  1180. Function Pos (c : WideString; Const s : AnsiString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1181. begin
  1182. result:=Pos(c,WideString(s));
  1183. end;
  1184. { Faster version for a char alone. Must be implemented because }
  1185. { pos(c: char; const s: shortstring) also exists, so otherwise }
  1186. { using pos(char,pchar) will always call the shortstring version }
  1187. { (exact match for first argument), also with $h+ (JM) }
  1188. Function Pos (c : Char; Const s : WideString) : SizeInt;
  1189. var
  1190. i: SizeInt;
  1191. wc : widechar;
  1192. pc : pwidechar;
  1193. begin
  1194. wc:=c;
  1195. pc:=@s[1];
  1196. for i:=1 to length(s) do
  1197. begin
  1198. if pc^=wc then
  1199. begin
  1200. pos:=i;
  1201. exit;
  1202. end;
  1203. inc(pc);
  1204. end;
  1205. pos:=0;
  1206. end;
  1207. Procedure Delete (Var S : WideString; Index,Size: SizeInt);
  1208. Var
  1209. LS : SizeInt;
  1210. begin
  1211. If Length(S)=0 then
  1212. exit;
  1213. if index<=0 then
  1214. exit;
  1215. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len div sizeof(WideChar);
  1216. if (Index<=LS) and (Size>0) then
  1217. begin
  1218. UniqueString (S);
  1219. if Size+Index>LS then
  1220. Size:=LS-Index+1;
  1221. if Index+Size<=LS then
  1222. begin
  1223. Dec(Index);
  1224. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  1225. end;
  1226. Setlength(s,LS-Size);
  1227. end;
  1228. end;
  1229. Procedure Insert (Const Source : WideString; Var S : WideString; Index : SizeInt);
  1230. var
  1231. Temp : WideString;
  1232. LS : SizeInt;
  1233. begin
  1234. If Length(Source)=0 then
  1235. exit;
  1236. if index <= 0 then
  1237. index := 1;
  1238. Ls:=Length(S);
  1239. if index > LS then
  1240. index := LS+1;
  1241. Dec(Index);
  1242. Pointer(Temp) := NewWideString(Length(Source)+LS);
  1243. SetLength(Temp,Length(Source)+LS);
  1244. If Index>0 then
  1245. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  1246. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  1247. If (LS-Index)>0 then
  1248. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  1249. S:=Temp;
  1250. end;
  1251. function UpCase(const s : WideString) : WideString;
  1252. begin
  1253. result:=widestringmanager.UpperWideStringProc(s);
  1254. end;
  1255. Procedure SetString (Out S : WideString; Buf : PWideChar; Len : SizeInt);
  1256. var
  1257. BufLen: SizeInt;
  1258. begin
  1259. SetLength(S,Len);
  1260. If (Buf<>Nil) and (Len>0) then
  1261. begin
  1262. BufLen := IndexWord(Buf^, Len+1, 0);
  1263. If (BufLen>0) and (BufLen < Len) then
  1264. Len := BufLen;
  1265. Move (Buf[0],S[1],Len*sizeof(WideChar));
  1266. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1267. end;
  1268. end;
  1269. Procedure SetString (Out S : WideString; Buf : PChar; Len : SizeInt);
  1270. var
  1271. BufLen: SizeInt;
  1272. begin
  1273. SetLength(S,Len);
  1274. If (Buf<>Nil) and (Len>0) then
  1275. begin
  1276. BufLen := IndexByte(Buf^, Len+1, 0);
  1277. If (BufLen>0) and (BufLen < Len) then
  1278. Len := BufLen;
  1279. widestringmanager.Ansi2WideMoveProc(Buf,S,Len);
  1280. //PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1281. end;
  1282. end;
  1283. Function fpc_Val_Real_WideStr(Const S : WideString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  1284. Var
  1285. SS : String;
  1286. begin
  1287. fpc_Val_Real_WideStr := 0;
  1288. if length(S) > 255 then
  1289. code := 256
  1290. else
  1291. begin
  1292. SS := S;
  1293. Val(SS,fpc_Val_Real_WideStr,code);
  1294. end;
  1295. end;
  1296. function fpc_val_enum_widestr(str2ordindex:pointer;const s:widestring;out code:valsint):longint;compilerproc;
  1297. var ss:shortstring;
  1298. begin
  1299. if length(s)>255 then
  1300. code:=256
  1301. else
  1302. begin
  1303. ss:=s;
  1304. val(ss,fpc_val_enum_widestr,code);
  1305. end;
  1306. end;
  1307. Function fpc_Val_Currency_WideStr(Const S : WideString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_WIDESTR']; compilerproc;
  1308. Var
  1309. SS : String;
  1310. begin
  1311. if length(S) > 255 then
  1312. begin
  1313. fpc_Val_Currency_WideStr:=0;
  1314. code := 256;
  1315. end
  1316. else
  1317. begin
  1318. SS := S;
  1319. Val(SS,fpc_Val_Currency_WideStr,code);
  1320. end;
  1321. end;
  1322. Function fpc_Val_UInt_WideStr (Const S : WideString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  1323. Var
  1324. SS : ShortString;
  1325. begin
  1326. fpc_Val_UInt_WideStr := 0;
  1327. if length(S) > 255 then
  1328. code := 256
  1329. else
  1330. begin
  1331. SS := S;
  1332. Val(SS,fpc_Val_UInt_WideStr,code);
  1333. end;
  1334. end;
  1335. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  1336. Var
  1337. SS : ShortString;
  1338. begin
  1339. fpc_Val_SInt_WideStr:=0;
  1340. if length(S)>255 then
  1341. code:=256
  1342. else
  1343. begin
  1344. SS := S;
  1345. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1346. end;
  1347. end;
  1348. {$ifndef CPU64}
  1349. Function fpc_Val_qword_WideStr (Const S : WideString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  1350. Var
  1351. SS : ShortString;
  1352. begin
  1353. fpc_Val_qword_WideStr:=0;
  1354. if length(S)>255 then
  1355. code:=256
  1356. else
  1357. begin
  1358. SS := S;
  1359. Val(SS,fpc_Val_qword_WideStr,Code);
  1360. end;
  1361. end;
  1362. Function fpc_Val_int64_WideStr (Const S : WideString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  1363. Var
  1364. SS : ShortString;
  1365. begin
  1366. fpc_Val_int64_WideStr:=0;
  1367. if length(S)>255 then
  1368. code:=256
  1369. else
  1370. begin
  1371. SS := S;
  1372. Val(SS,fpc_Val_int64_WideStr,Code);
  1373. end;
  1374. end;
  1375. {$endif CPU64}
  1376. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : WideString);compilerproc;
  1377. var
  1378. ss : shortstring;
  1379. begin
  1380. str_real(len,fr,d,treal_type(rt),ss);
  1381. s:=ss;
  1382. end;
  1383. procedure fpc_widestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:widestring);compilerproc;
  1384. var ss:shortstring;
  1385. begin
  1386. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1387. s:=ss;
  1388. end;
  1389. {$ifdef FPC_HAS_STR_CURRENCY}
  1390. procedure fpc_WideStr_Currency(c : Currency;len,fr : SizeInt;out s : WideString);compilerproc;
  1391. var
  1392. ss : shortstring;
  1393. begin
  1394. str(c:len:fr,ss);
  1395. s:=ss;
  1396. end;
  1397. {$endif FPC_HAS_STR_CURRENCY}
  1398. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; out S : WideString);compilerproc;
  1399. Var
  1400. SS : ShortString;
  1401. begin
  1402. Str (v:Len,SS);
  1403. S:=SS;
  1404. end;
  1405. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; out S : WideString);compilerproc;
  1406. Var
  1407. SS : ShortString;
  1408. begin
  1409. str(v:Len,SS);
  1410. S:=SS;
  1411. end;
  1412. {$ifndef CPU64}
  1413. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; out S : WideString);compilerproc;
  1414. Var
  1415. SS : ShortString;
  1416. begin
  1417. Str (v:Len,SS);
  1418. S:=SS;
  1419. end;
  1420. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; out S : WideString);compilerproc;
  1421. Var
  1422. SS : ShortString;
  1423. begin
  1424. str(v:Len,SS);
  1425. S:=SS;
  1426. end;
  1427. {$endif CPU64}
  1428. function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1429. begin
  1430. if assigned(Source) then
  1431. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1432. else
  1433. Result:=0;
  1434. end;
  1435. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt;
  1436. var
  1437. i,j : SizeUInt;
  1438. w : word;
  1439. begin
  1440. result:=0;
  1441. if source=nil then
  1442. exit;
  1443. i:=0;
  1444. j:=0;
  1445. if assigned(Dest) then
  1446. begin
  1447. while (i<SourceChars) and (j<MaxDestBytes) do
  1448. begin
  1449. w:=word(Source[i]);
  1450. case w of
  1451. 0..$7f:
  1452. begin
  1453. Dest[j]:=char(w);
  1454. inc(j);
  1455. end;
  1456. $80..$7ff:
  1457. begin
  1458. if j+1>=MaxDestBytes then
  1459. break;
  1460. Dest[j]:=char($c0 or (w shr 6));
  1461. Dest[j+1]:=char($80 or (w and $3f));
  1462. inc(j,2);
  1463. end;
  1464. else
  1465. begin
  1466. if j+2>=MaxDestBytes then
  1467. break;
  1468. Dest[j]:=char($e0 or (w shr 12));
  1469. Dest[j+1]:=char($80 or ((w shr 6)and $3f));
  1470. Dest[j+2]:=char($80 or (w and $3f));
  1471. inc(j,3);
  1472. end;
  1473. end;
  1474. inc(i);
  1475. end;
  1476. if j>SizeUInt(MaxDestBytes-1) then
  1477. j:=MaxDestBytes-1;
  1478. Dest[j]:=#0;
  1479. end
  1480. else
  1481. begin
  1482. while i<SourceChars do
  1483. begin
  1484. case word(Source[i]) of
  1485. $0..$7f:
  1486. inc(j);
  1487. $80..$7ff:
  1488. inc(j,2);
  1489. else
  1490. inc(j,3);
  1491. end;
  1492. inc(i);
  1493. end;
  1494. end;
  1495. result:=j+1;
  1496. end;
  1497. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1498. begin
  1499. if assigned(Source) then
  1500. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1501. else
  1502. Result:=0;
  1503. end;
  1504. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1505. var
  1506. i,j : SizeUInt;
  1507. w: SizeUInt;
  1508. b : byte;
  1509. begin
  1510. if not assigned(Source) then
  1511. begin
  1512. result:=0;
  1513. exit;
  1514. end;
  1515. result:=SizeUInt(-1);
  1516. i:=0;
  1517. j:=0;
  1518. if assigned(Dest) then
  1519. begin
  1520. while (j<MaxDestChars) and (i<SourceBytes) do
  1521. begin
  1522. b:=byte(Source[i]);
  1523. w:=b;
  1524. inc(i);
  1525. // 2 or 3 bytes?
  1526. if b>=$80 then
  1527. begin
  1528. w:=b and $3f;
  1529. if i>=SourceBytes then
  1530. exit;
  1531. // 3 bytes?
  1532. if (b and $20)<>0 then
  1533. begin
  1534. b:=byte(Source[i]);
  1535. inc(i);
  1536. if i>=SourceBytes then
  1537. exit;
  1538. if (b and $c0)<>$80 then
  1539. exit;
  1540. w:=(w shl 6) or (b and $3f);
  1541. end;
  1542. b:=byte(Source[i]);
  1543. w:=(w shl 6) or (b and $3f);
  1544. if (b and $c0)<>$80 then
  1545. exit;
  1546. inc(i);
  1547. end;
  1548. Dest[j]:=WideChar(w);
  1549. inc(j);
  1550. end;
  1551. if j>=MaxDestChars then j:=MaxDestChars-1;
  1552. Dest[j]:=#0;
  1553. end
  1554. else
  1555. begin
  1556. while i<SourceBytes do
  1557. begin
  1558. b:=byte(Source[i]);
  1559. inc(i);
  1560. // 2 or 3 bytes?
  1561. if b>=$80 then
  1562. begin
  1563. if i>=SourceBytes then
  1564. exit;
  1565. // 3 bytes?
  1566. b := b and $3f;
  1567. if (b and $20)<>0 then
  1568. begin
  1569. b:=byte(Source[i]);
  1570. inc(i);
  1571. if i>=SourceBytes then
  1572. exit;
  1573. if (b and $c0)<>$80 then
  1574. exit;
  1575. end;
  1576. if (byte(Source[i]) and $c0)<>$80 then
  1577. exit;
  1578. inc(i);
  1579. end;
  1580. inc(j);
  1581. end;
  1582. end;
  1583. result:=j+1;
  1584. end;
  1585. function UTF8Encode(const s : WideString) : UTF8String;
  1586. var
  1587. i : SizeInt;
  1588. hs : UTF8String;
  1589. begin
  1590. result:='';
  1591. if s='' then
  1592. exit;
  1593. SetLength(hs,length(s)*3);
  1594. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  1595. if i>0 then
  1596. begin
  1597. SetLength(hs,i-1);
  1598. result:=hs;
  1599. end;
  1600. end;
  1601. function UTF8Decode(const s : UTF8String): WideString;
  1602. var
  1603. i : SizeInt;
  1604. hs : WideString;
  1605. begin
  1606. result:='';
  1607. if s='' then
  1608. exit;
  1609. SetLength(hs,length(s));
  1610. i:=Utf8ToUnicode(PWideChar(hs),length(hs)+1,pchar(s),length(s));
  1611. if i>0 then
  1612. begin
  1613. SetLength(hs,i-1);
  1614. result:=hs;
  1615. end;
  1616. end;
  1617. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  1618. begin
  1619. Result:=Utf8Encode(s);
  1620. end;
  1621. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1622. begin
  1623. Result:=Utf8Decode(s);
  1624. end;
  1625. { converts an utf-16 code point or surrogate pair to utf-32 }
  1626. function utf16toutf32(const S: WideString; const index: SizeInt; out len: longint): UCS4Char; [public, alias: 'FPC_UTF16TOUTF32'];
  1627. var
  1628. w: widechar;
  1629. begin
  1630. { UTF-16 points in the range #$0-#$D7FF and #$E000-#$FFFF }
  1631. { are the same in UTF-32 }
  1632. w:=s[index];
  1633. if (w<=#$d7ff) or
  1634. (w>=#$e000) then
  1635. begin
  1636. result:=UCS4Char(w);
  1637. len:=1;
  1638. end
  1639. { valid surrogate pair? }
  1640. else if (w<=#$dbff) and
  1641. { w>=#$d7ff check not needed, checked above }
  1642. (index<length(s)) and
  1643. (s[index+1]>=#$dc00) and
  1644. (s[index+1]<=#$dfff) then
  1645. { convert the surrogate pair to UTF-32 }
  1646. begin
  1647. result:=(UCS4Char(w)-$d800) shl 10 + (UCS4Char(s[index+1])-$dc00) + $10000;
  1648. len:=2;
  1649. end
  1650. else
  1651. { invalid surrogate -> do nothing }
  1652. begin
  1653. result:=UCS4Char(w);
  1654. len:=1;
  1655. end;
  1656. end;
  1657. function WideStringToUCS4String(const s : WideString) : UCS4String;
  1658. var
  1659. i, slen,
  1660. destindex : SizeInt;
  1661. len : longint;
  1662. begin
  1663. slen:=length(s);
  1664. setlength(result,slen+1);
  1665. i:=1;
  1666. destindex:=0;
  1667. while (i<=slen) do
  1668. begin
  1669. result[destindex]:=utf16toutf32(s,i,len);
  1670. inc(destindex);
  1671. inc(i,len);
  1672. end;
  1673. { destindex <= slen (surrogate pairs may have been merged) }
  1674. { destindex+1 for terminating #0 (dynamic arrays are }
  1675. { implicitely filled with zero) }
  1676. setlength(result,destindex+1);
  1677. end;
  1678. { concatenates an utf-32 char to a widestring. S *must* be unique when entering. }
  1679. procedure ConcatUTF32ToWideStr(const nc: UCS4Char; var S: WideString; var index: SizeInt);
  1680. var
  1681. p : PWideChar;
  1682. begin
  1683. { if nc > $ffff, we need two places }
  1684. if (index+ord(nc > $ffff)>length(s)) then
  1685. if (length(s) < 10*256) then
  1686. setlength(s,length(s)+10)
  1687. else
  1688. setlength(s,length(s)+length(s) shr 8);
  1689. { we know that s is unique -> avoid uniquestring calls}
  1690. p:=@s[index];
  1691. if (nc<$ffff) then
  1692. begin
  1693. p^:=widechar(nc);
  1694. inc(index);
  1695. end
  1696. else if (dword(nc)<=$10ffff) then
  1697. begin
  1698. p^:=widechar((nc - $10000) shr 10 + $d800);
  1699. (p+1)^:=widechar((nc - $10000) and $3ff + $dc00);
  1700. inc(index,2);
  1701. end
  1702. else
  1703. { invalid code point }
  1704. begin
  1705. p^:='?';
  1706. inc(index);
  1707. end;
  1708. end;
  1709. function UCS4StringToWideString(const s : UCS4String) : WideString;
  1710. var
  1711. i : SizeInt;
  1712. resindex : SizeInt;
  1713. begin
  1714. { skip terminating #0 }
  1715. SetLength(result,length(s)-1);
  1716. resindex:=1;
  1717. for i:=0 to high(s)-1 do
  1718. ConcatUTF32ToWideStr(s[i],result,resindex);
  1719. { adjust result length (may be too big due to growing }
  1720. { for surrogate pairs) }
  1721. setlength(result,resindex-1);
  1722. end;
  1723. procedure unimplementedwidestring;
  1724. begin
  1725. HandleErrorFrame(215,get_frame);
  1726. end;
  1727. {$warnings off}
  1728. function GenericWideCase(const s : WideString) : WideString;
  1729. begin
  1730. unimplementedwidestring;
  1731. end;
  1732. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  1733. begin
  1734. unimplementedwidestring;
  1735. end;
  1736. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  1737. begin
  1738. unimplementedwidestring;
  1739. end;
  1740. function CharLengthPChar(const Str: PChar): PtrInt;
  1741. begin
  1742. unimplementedwidestring;
  1743. end;
  1744. {$warnings on}
  1745. procedure initwidestringmanager;
  1746. begin
  1747. fillchar(widestringmanager,sizeof(widestringmanager),0);
  1748. {$ifndef HAS_WIDESTRINGMANAGER}
  1749. widestringmanager.Wide2AnsiMoveProc:=@defaultWide2AnsiMove;
  1750. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2WideMove;
  1751. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  1752. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  1753. {$endif HAS_WIDESTRINGMANAGER}
  1754. widestringmanager.CompareWideStringProc:=@CompareWideString;
  1755. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  1756. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  1757. end;