wstrings.inc 34 KB

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