wustring22.inc 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013
  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. begin
  1300. BufLen := IndexByte(Buf^, Len+1, 0);
  1301. If (BufLen>0) and (BufLen < Len) then
  1302. Len := BufLen;
  1303. widestringmanager.Ansi2WideMoveProc(Buf,S,Len);
  1304. //PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1305. end;
  1306. end;
  1307. {$ifndef FPUNONE}
  1308. Function fpc_Val_Real_WideStr(Const S : WideString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  1309. Var
  1310. SS : String;
  1311. begin
  1312. fpc_Val_Real_WideStr := 0;
  1313. if length(S) > 255 then
  1314. code := 256
  1315. else
  1316. begin
  1317. SS := S;
  1318. Val(SS,fpc_Val_Real_WideStr,code);
  1319. end;
  1320. end;
  1321. {$endif}
  1322. function fpc_val_enum_widestr(str2ordindex:pointer;const s:widestring;out code:valsint):longint;compilerproc;
  1323. var ss:shortstring;
  1324. begin
  1325. if length(s)>255 then
  1326. code:=256
  1327. else
  1328. begin
  1329. ss:=s;
  1330. val(ss,fpc_val_enum_widestr,code);
  1331. end;
  1332. end;
  1333. Function fpc_Val_Currency_WideStr(Const S : WideString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_WIDESTR']; compilerproc;
  1334. Var
  1335. SS : String;
  1336. begin
  1337. if length(S) > 255 then
  1338. begin
  1339. fpc_Val_Currency_WideStr:=0;
  1340. code := 256;
  1341. end
  1342. else
  1343. begin
  1344. SS := S;
  1345. Val(SS,fpc_Val_Currency_WideStr,code);
  1346. end;
  1347. end;
  1348. Function fpc_Val_UInt_WideStr (Const S : WideString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  1349. Var
  1350. SS : ShortString;
  1351. begin
  1352. fpc_Val_UInt_WideStr := 0;
  1353. if length(S) > 255 then
  1354. code := 256
  1355. else
  1356. begin
  1357. SS := S;
  1358. Val(SS,fpc_Val_UInt_WideStr,code);
  1359. end;
  1360. end;
  1361. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  1362. Var
  1363. SS : ShortString;
  1364. begin
  1365. fpc_Val_SInt_WideStr:=0;
  1366. if length(S)>255 then
  1367. code:=256
  1368. else
  1369. begin
  1370. SS := S;
  1371. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1372. end;
  1373. end;
  1374. {$ifndef CPU64}
  1375. Function fpc_Val_qword_WideStr (Const S : WideString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  1376. Var
  1377. SS : ShortString;
  1378. begin
  1379. fpc_Val_qword_WideStr:=0;
  1380. if length(S)>255 then
  1381. code:=256
  1382. else
  1383. begin
  1384. SS := S;
  1385. Val(SS,fpc_Val_qword_WideStr,Code);
  1386. end;
  1387. end;
  1388. Function fpc_Val_int64_WideStr (Const S : WideString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  1389. Var
  1390. SS : ShortString;
  1391. begin
  1392. fpc_Val_int64_WideStr:=0;
  1393. if length(S)>255 then
  1394. code:=256
  1395. else
  1396. begin
  1397. SS := S;
  1398. Val(SS,fpc_Val_int64_WideStr,Code);
  1399. end;
  1400. end;
  1401. {$endif CPU64}
  1402. {$ifndef FPUNONE}
  1403. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : WideString);compilerproc;
  1404. var
  1405. ss : shortstring;
  1406. begin
  1407. str_real(len,fr,d,treal_type(rt),ss);
  1408. s:=ss;
  1409. end;
  1410. {$endif}
  1411. procedure fpc_widestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:widestring);compilerproc;
  1412. var ss:shortstring;
  1413. begin
  1414. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1415. s:=ss;
  1416. end;
  1417. {$ifdef FPC_HAS_STR_CURRENCY}
  1418. procedure fpc_WideStr_Currency(c : Currency;len,fr : SizeInt;out s : WideString);compilerproc;
  1419. var
  1420. ss : shortstring;
  1421. begin
  1422. str(c:len:fr,ss);
  1423. s:=ss;
  1424. end;
  1425. {$endif FPC_HAS_STR_CURRENCY}
  1426. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; out S : WideString);compilerproc;
  1427. Var
  1428. SS : ShortString;
  1429. begin
  1430. Str (v:Len,SS);
  1431. S:=SS;
  1432. end;
  1433. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; out S : WideString);compilerproc;
  1434. Var
  1435. SS : ShortString;
  1436. begin
  1437. str(v:Len,SS);
  1438. S:=SS;
  1439. end;
  1440. {$ifndef CPU64}
  1441. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; out S : WideString);compilerproc;
  1442. Var
  1443. SS : ShortString;
  1444. begin
  1445. Str (v:Len,SS);
  1446. S:=SS;
  1447. end;
  1448. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; out S : WideString);compilerproc;
  1449. Var
  1450. SS : ShortString;
  1451. begin
  1452. str(v:Len,SS);
  1453. S:=SS;
  1454. end;
  1455. {$endif CPU64}
  1456. function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1457. begin
  1458. if assigned(Source) then
  1459. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1460. else
  1461. Result:=0;
  1462. end;
  1463. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt;
  1464. var
  1465. i,j : SizeUInt;
  1466. w : word;
  1467. begin
  1468. result:=0;
  1469. if source=nil then
  1470. exit;
  1471. i:=0;
  1472. j:=0;
  1473. if assigned(Dest) then
  1474. begin
  1475. while (i<SourceChars) and (j<MaxDestBytes) do
  1476. begin
  1477. w:=word(Source[i]);
  1478. case w of
  1479. 0..$7f:
  1480. begin
  1481. Dest[j]:=char(w);
  1482. inc(j);
  1483. end;
  1484. $80..$7ff:
  1485. begin
  1486. if j+1>=MaxDestBytes then
  1487. break;
  1488. Dest[j]:=char($c0 or (w shr 6));
  1489. Dest[j+1]:=char($80 or (w and $3f));
  1490. inc(j,2);
  1491. end;
  1492. else
  1493. begin
  1494. if j+2>=MaxDestBytes then
  1495. break;
  1496. Dest[j]:=char($e0 or (w shr 12));
  1497. Dest[j+1]:=char($80 or ((w shr 6)and $3f));
  1498. Dest[j+2]:=char($80 or (w and $3f));
  1499. inc(j,3);
  1500. end;
  1501. end;
  1502. inc(i);
  1503. end;
  1504. if j>SizeUInt(MaxDestBytes-1) then
  1505. j:=MaxDestBytes-1;
  1506. Dest[j]:=#0;
  1507. end
  1508. else
  1509. begin
  1510. while i<SourceChars do
  1511. begin
  1512. case word(Source[i]) of
  1513. $0..$7f:
  1514. inc(j);
  1515. $80..$7ff:
  1516. inc(j,2);
  1517. else
  1518. inc(j,3);
  1519. end;
  1520. inc(i);
  1521. end;
  1522. end;
  1523. result:=j+1;
  1524. end;
  1525. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1526. begin
  1527. if assigned(Source) then
  1528. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1529. else
  1530. Result:=0;
  1531. end;
  1532. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1533. var
  1534. i,j : SizeUInt;
  1535. w: SizeUInt;
  1536. b : byte;
  1537. begin
  1538. if not assigned(Source) then
  1539. begin
  1540. result:=0;
  1541. exit;
  1542. end;
  1543. result:=SizeUInt(-1);
  1544. i:=0;
  1545. j:=0;
  1546. if assigned(Dest) then
  1547. begin
  1548. while (j<MaxDestChars) and (i<SourceBytes) do
  1549. begin
  1550. b:=byte(Source[i]);
  1551. w:=b;
  1552. inc(i);
  1553. // 2 or 3 bytes?
  1554. if b>=$80 then
  1555. begin
  1556. w:=b and $3f;
  1557. if i>=SourceBytes then
  1558. exit;
  1559. // 3 bytes?
  1560. if (b and $20)<>0 then
  1561. begin
  1562. b:=byte(Source[i]);
  1563. inc(i);
  1564. if i>=SourceBytes then
  1565. exit;
  1566. if (b and $c0)<>$80 then
  1567. exit;
  1568. w:=(w shl 6) or (b and $3f);
  1569. end;
  1570. b:=byte(Source[i]);
  1571. w:=(w shl 6) or (b and $3f);
  1572. if (b and $c0)<>$80 then
  1573. exit;
  1574. inc(i);
  1575. end;
  1576. Dest[j]:=WideChar(w);
  1577. inc(j);
  1578. end;
  1579. if j>=MaxDestChars then j:=MaxDestChars-1;
  1580. Dest[j]:=#0;
  1581. end
  1582. else
  1583. begin
  1584. while i<SourceBytes do
  1585. begin
  1586. b:=byte(Source[i]);
  1587. inc(i);
  1588. // 2 or 3 bytes?
  1589. if b>=$80 then
  1590. begin
  1591. if i>=SourceBytes then
  1592. exit;
  1593. // 3 bytes?
  1594. b := b and $3f;
  1595. if (b and $20)<>0 then
  1596. begin
  1597. b:=byte(Source[i]);
  1598. inc(i);
  1599. if i>=SourceBytes then
  1600. exit;
  1601. if (b and $c0)<>$80 then
  1602. exit;
  1603. end;
  1604. if (byte(Source[i]) and $c0)<>$80 then
  1605. exit;
  1606. inc(i);
  1607. end;
  1608. inc(j);
  1609. end;
  1610. end;
  1611. result:=j+1;
  1612. end;
  1613. function UTF8Encode(const s : WideString) : UTF8String;
  1614. var
  1615. i : SizeInt;
  1616. hs : UTF8String;
  1617. begin
  1618. result:='';
  1619. if s='' then
  1620. exit;
  1621. SetLength(hs,length(s)*3);
  1622. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  1623. if i>0 then
  1624. begin
  1625. SetLength(hs,i-1);
  1626. result:=hs;
  1627. end;
  1628. end;
  1629. function UTF8Decode(const s : UTF8String): WideString;
  1630. var
  1631. i : SizeInt;
  1632. hs : WideString;
  1633. begin
  1634. result:='';
  1635. if s='' then
  1636. exit;
  1637. SetLength(hs,length(s));
  1638. i:=Utf8ToUnicode(PWideChar(hs),length(hs)+1,pchar(s),length(s));
  1639. if i>0 then
  1640. begin
  1641. SetLength(hs,i-1);
  1642. result:=hs;
  1643. end;
  1644. end;
  1645. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  1646. begin
  1647. Result:=Utf8Encode(s);
  1648. end;
  1649. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1650. begin
  1651. Result:=Utf8Decode(s);
  1652. end;
  1653. { converts an utf-16 code point or surrogate pair to utf-32 }
  1654. function utf16toutf32(const S: WideString; const index: SizeInt; out len: longint): UCS4Char; [public, alias: 'FPC_UTF16TOUTF32'];
  1655. var
  1656. w: widechar;
  1657. begin
  1658. { UTF-16 points in the range #$0-#$D7FF and #$E000-#$FFFF }
  1659. { are the same in UTF-32 }
  1660. w:=s[index];
  1661. if (w<=#$d7ff) or
  1662. (w>=#$e000) then
  1663. begin
  1664. result:=UCS4Char(w);
  1665. len:=1;
  1666. end
  1667. { valid surrogate pair? }
  1668. else if (w<=#$dbff) and
  1669. { w>=#$d7ff check not needed, checked above }
  1670. (index<length(s)) and
  1671. (s[index+1]>=#$dc00) and
  1672. (s[index+1]<=#$dfff) then
  1673. { convert the surrogate pair to UTF-32 }
  1674. begin
  1675. result:=(UCS4Char(w)-$d800) shl 10 + (UCS4Char(s[index+1])-$dc00) + $10000;
  1676. len:=2;
  1677. end
  1678. else
  1679. { invalid surrogate -> do nothing }
  1680. begin
  1681. result:=UCS4Char(w);
  1682. len:=1;
  1683. end;
  1684. end;
  1685. function WideStringToUCS4String(const s : WideString) : UCS4String;
  1686. var
  1687. i, slen,
  1688. destindex : SizeInt;
  1689. len : longint;
  1690. begin
  1691. slen:=length(s);
  1692. setlength(result,slen+1);
  1693. i:=1;
  1694. destindex:=0;
  1695. while (i<=slen) do
  1696. begin
  1697. result[destindex]:=utf16toutf32(s,i,len);
  1698. inc(destindex);
  1699. inc(i,len);
  1700. end;
  1701. { destindex <= slen (surrogate pairs may have been merged) }
  1702. { destindex+1 for terminating #0 (dynamic arrays are }
  1703. { implicitely filled with zero) }
  1704. setlength(result,destindex+1);
  1705. end;
  1706. { concatenates an utf-32 char to a widestring. S *must* be unique when entering. }
  1707. procedure ConcatUTF32ToWideStr(const nc: UCS4Char; var S: WideString; var index: SizeInt);
  1708. var
  1709. p : PWideChar;
  1710. begin
  1711. { if nc > $ffff, we need two places }
  1712. if (index+ord(nc > $ffff)>length(s)) then
  1713. if (length(s) < 10*256) then
  1714. setlength(s,length(s)+10)
  1715. else
  1716. setlength(s,length(s)+length(s) shr 8);
  1717. { we know that s is unique -> avoid uniquestring calls}
  1718. p:=@s[index];
  1719. if (nc<$ffff) then
  1720. begin
  1721. p^:=widechar(nc);
  1722. inc(index);
  1723. end
  1724. else if (dword(nc)<=$10ffff) then
  1725. begin
  1726. p^:=widechar((nc - $10000) shr 10 + $d800);
  1727. (p+1)^:=widechar((nc - $10000) and $3ff + $dc00);
  1728. inc(index,2);
  1729. end
  1730. else
  1731. { invalid code point }
  1732. begin
  1733. p^:='?';
  1734. inc(index);
  1735. end;
  1736. end;
  1737. function UCS4StringToWideString(const s : UCS4String) : WideString;
  1738. var
  1739. i : SizeInt;
  1740. resindex : SizeInt;
  1741. begin
  1742. { skip terminating #0 }
  1743. SetLength(result,length(s)-1);
  1744. resindex:=1;
  1745. for i:=0 to high(s)-1 do
  1746. ConcatUTF32ToWideStr(s[i],result,resindex);
  1747. { adjust result length (may be too big due to growing }
  1748. { for surrogate pairs) }
  1749. setlength(result,resindex-1);
  1750. end;
  1751. const
  1752. SNoWidestrings = 'This binary has no widestrings support compiled in.';
  1753. SRecompileWithWidestrings = 'Recompile the application with a widestrings-manager in the program uses clause.';
  1754. procedure unimplementedwidestring;
  1755. begin
  1756. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1757. If IsConsole then
  1758. begin
  1759. Writeln(StdErr,SNoWidestrings);
  1760. Writeln(StdErr,SRecompileWithWidestrings);
  1761. end;
  1762. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1763. HandleErrorFrame(233,get_frame);
  1764. end;
  1765. {$warnings off}
  1766. function GenericWideCase(const s : WideString) : WideString;
  1767. begin
  1768. unimplementedwidestring;
  1769. end;
  1770. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  1771. begin
  1772. unimplementedwidestring;
  1773. end;
  1774. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  1775. begin
  1776. unimplementedwidestring;
  1777. end;
  1778. function CharLengthPChar(const Str: PChar): PtrInt;
  1779. begin
  1780. unimplementedwidestring;
  1781. end;
  1782. {$warnings on}
  1783. procedure initwidestringmanager;
  1784. begin
  1785. fillchar(widestringmanager,sizeof(widestringmanager),0);
  1786. {$ifndef HAS_WIDESTRINGMANAGER}
  1787. widestringmanager.Wide2AnsiMoveProc:=@defaultWide2AnsiMove;
  1788. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2WideMove;
  1789. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  1790. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  1791. {$endif HAS_WIDESTRINGMANAGER}
  1792. widestringmanager.CompareWideStringProc:=@CompareWideString;
  1793. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  1794. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  1795. end;