wustrings.inc 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  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. Pointer(fpc_widestr_Copy):=ResultAddress;
  1111. end;
  1112. Function Pos (Const Substr : WideString; Const Source : WideString) : SizeInt;
  1113. var
  1114. i,MaxLen : SizeInt;
  1115. pc : pwidechar;
  1116. begin
  1117. Pos:=0;
  1118. if Length(SubStr)>0 then
  1119. begin
  1120. MaxLen:=Length(source)-Length(SubStr);
  1121. i:=0;
  1122. pc:=@source[1];
  1123. while (i<=MaxLen) do
  1124. begin
  1125. inc(i);
  1126. if (SubStr[1]=pc^) and
  1127. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  1128. begin
  1129. Pos:=i;
  1130. exit;
  1131. end;
  1132. inc(pc);
  1133. end;
  1134. end;
  1135. end;
  1136. { Faster version for a widechar alone }
  1137. Function Pos (c : WideChar; Const s : WideString) : SizeInt;
  1138. var
  1139. i: SizeInt;
  1140. pc : pwidechar;
  1141. begin
  1142. pc:=@s[1];
  1143. for i:=1 to length(s) do
  1144. begin
  1145. if pc^=c then
  1146. begin
  1147. pos:=i;
  1148. exit;
  1149. end;
  1150. inc(pc);
  1151. end;
  1152. pos:=0;
  1153. end;
  1154. Function Pos (c : WideChar; Const s : AnsiString) : SizeInt;
  1155. var
  1156. i: SizeInt;
  1157. pc : pchar;
  1158. begin
  1159. pc:=@s[1];
  1160. for i:=1 to length(s) do
  1161. begin
  1162. if widechar(pc^)=c then
  1163. begin
  1164. pos:=i;
  1165. exit;
  1166. end;
  1167. inc(pc);
  1168. end;
  1169. pos:=0;
  1170. end;
  1171. Function Pos (c : AnsiString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1172. begin
  1173. result:=Pos(WideString(c),s);
  1174. end;
  1175. Function Pos (c : ShortString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1176. begin
  1177. result:=Pos(WideString(c),s);
  1178. end;
  1179. Function Pos (c : WideString; Const s : AnsiString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1180. begin
  1181. result:=Pos(c,WideString(s));
  1182. end;
  1183. { Faster version for a char alone. Must be implemented because }
  1184. { pos(c: char; const s: shortstring) also exists, so otherwise }
  1185. { using pos(char,pchar) will always call the shortstring version }
  1186. { (exact match for first argument), also with $h+ (JM) }
  1187. Function Pos (c : Char; Const s : WideString) : SizeInt;
  1188. var
  1189. i: SizeInt;
  1190. wc : widechar;
  1191. pc : pwidechar;
  1192. begin
  1193. wc:=c;
  1194. pc:=@s[1];
  1195. for i:=1 to length(s) do
  1196. begin
  1197. if pc^=wc then
  1198. begin
  1199. pos:=i;
  1200. exit;
  1201. end;
  1202. inc(pc);
  1203. end;
  1204. pos:=0;
  1205. end;
  1206. Procedure Delete (Var S : WideString; Index,Size: SizeInt);
  1207. Var
  1208. LS : SizeInt;
  1209. begin
  1210. If Length(S)=0 then
  1211. exit;
  1212. if index<=0 then
  1213. exit;
  1214. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len div sizeof(WideChar);
  1215. if (Index<=LS) and (Size>0) then
  1216. begin
  1217. UniqueString (S);
  1218. if Size+Index>LS then
  1219. Size:=LS-Index+1;
  1220. if Index+Size<=LS then
  1221. begin
  1222. Dec(Index);
  1223. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  1224. end;
  1225. Setlength(s,LS-Size);
  1226. end;
  1227. end;
  1228. Procedure Insert (Const Source : WideString; Var S : WideString; Index : SizeInt);
  1229. var
  1230. Temp : WideString;
  1231. LS : SizeInt;
  1232. begin
  1233. If Length(Source)=0 then
  1234. exit;
  1235. if index <= 0 then
  1236. index := 1;
  1237. Ls:=Length(S);
  1238. if index > LS then
  1239. index := LS+1;
  1240. Dec(Index);
  1241. Pointer(Temp) := NewWideString(Length(Source)+LS);
  1242. SetLength(Temp,Length(Source)+LS);
  1243. If Index>0 then
  1244. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  1245. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  1246. If (LS-Index)>0 then
  1247. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  1248. S:=Temp;
  1249. end;
  1250. function UpCase(const s : WideString) : WideString;
  1251. begin
  1252. result:=widestringmanager.UpperWideStringProc(s);
  1253. end;
  1254. Procedure SetString (Out S : WideString; Buf : PWideChar; Len : SizeInt);
  1255. var
  1256. BufLen: SizeInt;
  1257. begin
  1258. SetLength(S,Len);
  1259. If (Buf<>Nil) and (Len>0) then
  1260. begin
  1261. BufLen := IndexWord(Buf^, Len+1, 0);
  1262. If (BufLen>0) and (BufLen < Len) then
  1263. Len := BufLen;
  1264. Move (Buf[0],S[1],Len*sizeof(WideChar));
  1265. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1266. end;
  1267. end;
  1268. Procedure SetString (Out S : WideString; Buf : PChar; Len : SizeInt);
  1269. var
  1270. BufLen: SizeInt;
  1271. begin
  1272. SetLength(S,Len);
  1273. If (Buf<>Nil) and (Len>0) then
  1274. begin
  1275. BufLen := IndexByte(Buf^, Len+1, 0);
  1276. If (BufLen>0) and (BufLen < Len) then
  1277. Len := BufLen;
  1278. widestringmanager.Ansi2WideMoveProc(Buf,S,Len);
  1279. //PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1280. end;
  1281. end;
  1282. Function fpc_Val_Real_WideStr(Const S : WideString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  1283. Var
  1284. SS : String;
  1285. begin
  1286. fpc_Val_Real_WideStr := 0;
  1287. if length(S) > 255 then
  1288. code := 256
  1289. else
  1290. begin
  1291. SS := S;
  1292. Val(SS,fpc_Val_Real_WideStr,code);
  1293. end;
  1294. end;
  1295. function fpc_val_enum_widestr(str2ordindex:pointer;const s:widestring;out code:valsint):longint;compilerproc;
  1296. var ss:shortstring;
  1297. begin
  1298. if length(s)>255 then
  1299. code:=256
  1300. else
  1301. begin
  1302. ss:=s;
  1303. val(ss,fpc_val_enum_widestr,code);
  1304. end;
  1305. end;
  1306. Function fpc_Val_Currency_WideStr(Const S : WideString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_WIDESTR']; compilerproc;
  1307. Var
  1308. SS : String;
  1309. begin
  1310. if length(S) > 255 then
  1311. begin
  1312. fpc_Val_Currency_WideStr:=0;
  1313. code := 256;
  1314. end
  1315. else
  1316. begin
  1317. SS := S;
  1318. Val(SS,fpc_Val_Currency_WideStr,code);
  1319. end;
  1320. end;
  1321. Function fpc_Val_UInt_WideStr (Const S : WideString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  1322. Var
  1323. SS : ShortString;
  1324. begin
  1325. fpc_Val_UInt_WideStr := 0;
  1326. if length(S) > 255 then
  1327. code := 256
  1328. else
  1329. begin
  1330. SS := S;
  1331. Val(SS,fpc_Val_UInt_WideStr,code);
  1332. end;
  1333. end;
  1334. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  1335. Var
  1336. SS : ShortString;
  1337. begin
  1338. fpc_Val_SInt_WideStr:=0;
  1339. if length(S)>255 then
  1340. code:=256
  1341. else
  1342. begin
  1343. SS := S;
  1344. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1345. end;
  1346. end;
  1347. {$ifndef CPU64}
  1348. Function fpc_Val_qword_WideStr (Const S : WideString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  1349. Var
  1350. SS : ShortString;
  1351. begin
  1352. fpc_Val_qword_WideStr:=0;
  1353. if length(S)>255 then
  1354. code:=256
  1355. else
  1356. begin
  1357. SS := S;
  1358. Val(SS,fpc_Val_qword_WideStr,Code);
  1359. end;
  1360. end;
  1361. Function fpc_Val_int64_WideStr (Const S : WideString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  1362. Var
  1363. SS : ShortString;
  1364. begin
  1365. fpc_Val_int64_WideStr:=0;
  1366. if length(S)>255 then
  1367. code:=256
  1368. else
  1369. begin
  1370. SS := S;
  1371. Val(SS,fpc_Val_int64_WideStr,Code);
  1372. end;
  1373. end;
  1374. {$endif CPU64}
  1375. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : WideString);compilerproc;
  1376. var
  1377. ss : shortstring;
  1378. begin
  1379. str_real(len,fr,d,treal_type(rt),ss);
  1380. s:=ss;
  1381. end;
  1382. procedure fpc_widestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:widestring);compilerproc;
  1383. var ss:shortstring;
  1384. begin
  1385. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1386. s:=ss;
  1387. end;
  1388. {$ifdef FPC_HAS_STR_CURRENCY}
  1389. procedure fpc_WideStr_Currency(c : Currency;len,fr : SizeInt;out s : WideString);compilerproc;
  1390. var
  1391. ss : shortstring;
  1392. begin
  1393. str(c:len:fr,ss);
  1394. s:=ss;
  1395. end;
  1396. {$endif FPC_HAS_STR_CURRENCY}
  1397. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; out S : WideString);compilerproc;
  1398. Var
  1399. SS : ShortString;
  1400. begin
  1401. Str (v:Len,SS);
  1402. S:=SS;
  1403. end;
  1404. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; out S : WideString);compilerproc;
  1405. Var
  1406. SS : ShortString;
  1407. begin
  1408. str(v:Len,SS);
  1409. S:=SS;
  1410. end;
  1411. {$ifndef CPU64}
  1412. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; out S : WideString);compilerproc;
  1413. Var
  1414. SS : ShortString;
  1415. begin
  1416. Str (v:Len,SS);
  1417. S:=SS;
  1418. end;
  1419. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; out S : WideString);compilerproc;
  1420. Var
  1421. SS : ShortString;
  1422. begin
  1423. str(v:Len,SS);
  1424. S:=SS;
  1425. end;
  1426. {$endif CPU64}
  1427. function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1428. begin
  1429. if assigned(Source) then
  1430. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1431. else
  1432. Result:=0;
  1433. end;
  1434. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt;
  1435. var
  1436. i,j : SizeUInt;
  1437. w : word;
  1438. begin
  1439. result:=0;
  1440. if source=nil then
  1441. exit;
  1442. i:=0;
  1443. j:=0;
  1444. if assigned(Dest) then
  1445. begin
  1446. while (i<SourceChars) and (j<MaxDestBytes) do
  1447. begin
  1448. w:=word(Source[i]);
  1449. case w of
  1450. 0..$7f:
  1451. begin
  1452. Dest[j]:=char(w);
  1453. inc(j);
  1454. end;
  1455. $80..$7ff:
  1456. begin
  1457. if j+1>=MaxDestBytes then
  1458. break;
  1459. Dest[j]:=char($c0 or (w shr 6));
  1460. Dest[j+1]:=char($80 or (w and $3f));
  1461. inc(j,2);
  1462. end;
  1463. else
  1464. begin
  1465. if j+2>=MaxDestBytes then
  1466. break;
  1467. Dest[j]:=char($e0 or (w shr 12));
  1468. Dest[j+1]:=char($80 or ((w shr 6)and $3f));
  1469. Dest[j+2]:=char($80 or (w and $3f));
  1470. inc(j,3);
  1471. end;
  1472. end;
  1473. inc(i);
  1474. end;
  1475. if j>SizeUInt(MaxDestBytes-1) then
  1476. j:=MaxDestBytes-1;
  1477. Dest[j]:=#0;
  1478. end
  1479. else
  1480. begin
  1481. while i<SourceChars do
  1482. begin
  1483. case word(Source[i]) of
  1484. $0..$7f:
  1485. inc(j);
  1486. $80..$7ff:
  1487. inc(j,2);
  1488. else
  1489. inc(j,3);
  1490. end;
  1491. inc(i);
  1492. end;
  1493. end;
  1494. result:=j+1;
  1495. end;
  1496. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1497. begin
  1498. if assigned(Source) then
  1499. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1500. else
  1501. Result:=0;
  1502. end;
  1503. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1504. var
  1505. i,j : SizeUInt;
  1506. w: SizeUInt;
  1507. b : byte;
  1508. begin
  1509. if not assigned(Source) then
  1510. begin
  1511. result:=0;
  1512. exit;
  1513. end;
  1514. result:=SizeUInt(-1);
  1515. i:=0;
  1516. j:=0;
  1517. if assigned(Dest) then
  1518. begin
  1519. while (j<MaxDestChars) and (i<SourceBytes) do
  1520. begin
  1521. b:=byte(Source[i]);
  1522. w:=b;
  1523. inc(i);
  1524. // 2 or 3 bytes?
  1525. if b>=$80 then
  1526. begin
  1527. w:=b and $3f;
  1528. if i>=SourceBytes then
  1529. exit;
  1530. // 3 bytes?
  1531. if (b and $20)<>0 then
  1532. begin
  1533. b:=byte(Source[i]);
  1534. inc(i);
  1535. if i>=SourceBytes then
  1536. exit;
  1537. if (b and $c0)<>$80 then
  1538. exit;
  1539. w:=(w shl 6) or (b and $3f);
  1540. end;
  1541. b:=byte(Source[i]);
  1542. w:=(w shl 6) or (b and $3f);
  1543. if (b and $c0)<>$80 then
  1544. exit;
  1545. inc(i);
  1546. end;
  1547. Dest[j]:=WideChar(w);
  1548. inc(j);
  1549. end;
  1550. if j>=MaxDestChars then j:=MaxDestChars-1;
  1551. Dest[j]:=#0;
  1552. end
  1553. else
  1554. begin
  1555. while i<SourceBytes do
  1556. begin
  1557. b:=byte(Source[i]);
  1558. inc(i);
  1559. // 2 or 3 bytes?
  1560. if b>=$80 then
  1561. begin
  1562. if i>=SourceBytes then
  1563. exit;
  1564. // 3 bytes?
  1565. b := b and $3f;
  1566. if (b and $20)<>0 then
  1567. begin
  1568. b:=byte(Source[i]);
  1569. inc(i);
  1570. if i>=SourceBytes then
  1571. exit;
  1572. if (b and $c0)<>$80 then
  1573. exit;
  1574. end;
  1575. if (byte(Source[i]) and $c0)<>$80 then
  1576. exit;
  1577. inc(i);
  1578. end;
  1579. inc(j);
  1580. end;
  1581. end;
  1582. result:=j+1;
  1583. end;
  1584. function UTF8Encode(const s : WideString) : UTF8String;
  1585. var
  1586. i : SizeInt;
  1587. hs : UTF8String;
  1588. begin
  1589. result:='';
  1590. if s='' then
  1591. exit;
  1592. SetLength(hs,length(s)*3);
  1593. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  1594. if i>0 then
  1595. begin
  1596. SetLength(hs,i-1);
  1597. result:=hs;
  1598. end;
  1599. end;
  1600. function UTF8Decode(const s : UTF8String): WideString;
  1601. var
  1602. i : SizeInt;
  1603. hs : WideString;
  1604. begin
  1605. result:='';
  1606. if s='' then
  1607. exit;
  1608. SetLength(hs,length(s));
  1609. i:=Utf8ToUnicode(PWideChar(hs),length(hs)+1,pchar(s),length(s));
  1610. if i>0 then
  1611. begin
  1612. SetLength(hs,i-1);
  1613. result:=hs;
  1614. end;
  1615. end;
  1616. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  1617. begin
  1618. Result:=Utf8Encode(s);
  1619. end;
  1620. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1621. begin
  1622. Result:=Utf8Decode(s);
  1623. end;
  1624. { converts an utf-16 code point or surrogate pair to utf-32 }
  1625. function utf16toutf32(const S: WideString; const index: SizeInt; out len: longint): UCS4Char; [public, alias: 'FPC_UTF16TOUTF32'];
  1626. var
  1627. w: widechar;
  1628. begin
  1629. { UTF-16 points in the range #$0-#$D7FF and #$E000-#$FFFF }
  1630. { are the same in UTF-32 }
  1631. w:=s[index];
  1632. if (w<=#$d7ff) or
  1633. (w>=#$e000) then
  1634. begin
  1635. result:=UCS4Char(w);
  1636. len:=1;
  1637. end
  1638. { valid surrogate pair? }
  1639. else if (w<=#$dbff) and
  1640. { w>=#$d7ff check not needed, checked above }
  1641. (index<length(s)) and
  1642. (s[index+1]>=#$dc00) and
  1643. (s[index+1]<=#$dfff) then
  1644. { convert the surrogate pair to UTF-32 }
  1645. begin
  1646. result:=(UCS4Char(w)-$d800) shl 10 + (UCS4Char(s[index+1])-$dc00) + $10000;
  1647. len:=2;
  1648. end
  1649. else
  1650. { invalid surrogate -> do nothing }
  1651. begin
  1652. result:=UCS4Char(w);
  1653. len:=1;
  1654. end;
  1655. end;
  1656. function WideStringToUCS4String(const s : WideString) : UCS4String;
  1657. var
  1658. i, slen,
  1659. destindex : SizeInt;
  1660. len : longint;
  1661. begin
  1662. slen:=length(s);
  1663. setlength(result,slen+1);
  1664. i:=1;
  1665. destindex:=0;
  1666. while (i<=slen) do
  1667. begin
  1668. result[destindex]:=utf16toutf32(s,i,len);
  1669. inc(destindex);
  1670. inc(i,len);
  1671. end;
  1672. { destindex <= slen (surrogate pairs may have been merged) }
  1673. { destindex+1 for terminating #0 (dynamic arrays are }
  1674. { implicitely filled with zero) }
  1675. setlength(result,destindex+1);
  1676. end;
  1677. { concatenates an utf-32 char to a widestring. S *must* be unique when entering. }
  1678. procedure ConcatUTF32ToWideStr(const nc: UCS4Char; var S: WideString; var index: SizeInt);
  1679. var
  1680. p : PWideChar;
  1681. begin
  1682. { if nc > $ffff, we need two places }
  1683. if (index+ord(nc > $ffff)>length(s)) then
  1684. if (length(s) < 10*256) then
  1685. setlength(s,length(s)+10)
  1686. else
  1687. setlength(s,length(s)+length(s) shr 8);
  1688. { we know that s is unique -> avoid uniquestring calls}
  1689. p:=@s[index];
  1690. if (nc<$ffff) then
  1691. begin
  1692. p^:=widechar(nc);
  1693. inc(index);
  1694. end
  1695. else if (dword(nc)<=$10ffff) then
  1696. begin
  1697. p^:=widechar((nc - $10000) shr 10 + $d800);
  1698. (p+1)^:=widechar((nc - $10000) and $3ff + $dc00);
  1699. inc(index,2);
  1700. end
  1701. else
  1702. { invalid code point }
  1703. begin
  1704. p^:='?';
  1705. inc(index);
  1706. end;
  1707. end;
  1708. function UCS4StringToWideString(const s : UCS4String) : WideString;
  1709. var
  1710. i : SizeInt;
  1711. resindex : SizeInt;
  1712. begin
  1713. { skip terminating #0 }
  1714. SetLength(result,length(s)-1);
  1715. resindex:=1;
  1716. for i:=0 to high(s)-1 do
  1717. ConcatUTF32ToWideStr(s[i],result,resindex);
  1718. { adjust result length (may be too big due to growing }
  1719. { for surrogate pairs) }
  1720. setlength(result,resindex-1);
  1721. end;
  1722. procedure unimplementedwidestring;
  1723. begin
  1724. HandleErrorFrame(215,get_frame);
  1725. end;
  1726. {$warnings off}
  1727. function GenericWideCase(const s : WideString) : WideString;
  1728. begin
  1729. unimplementedwidestring;
  1730. end;
  1731. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  1732. begin
  1733. unimplementedwidestring;
  1734. end;
  1735. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  1736. begin
  1737. unimplementedwidestring;
  1738. end;
  1739. function CharLengthPChar(const Str: PChar): PtrInt;
  1740. begin
  1741. unimplementedwidestring;
  1742. end;
  1743. {$warnings on}
  1744. procedure initwidestringmanager;
  1745. begin
  1746. fillchar(widestringmanager,sizeof(widestringmanager),0);
  1747. {$ifndef HAS_WIDESTRINGMANAGER}
  1748. widestringmanager.Wide2AnsiMoveProc:=@defaultWide2AnsiMove;
  1749. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2WideMove;
  1750. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  1751. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  1752. {$endif HAS_WIDESTRINGMANAGER}
  1753. widestringmanager.CompareWideStringProc:=@CompareWideString;
  1754. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  1755. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  1756. end;