wstrings.inc 39 KB

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