wustring22.inc 49 KB

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