wstrings.inc 38 KB

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