wustrings.inc 49 KB

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