wustrings.inc 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  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. fpc_widestr_decr_ref(Pointer(fpc_widestr_copy));
  1143. Pointer(fpc_widestr_Copy):=ResultAddress;
  1144. end;
  1145. Function Pos (Const Substr : WideString; Const Source : WideString) : SizeInt;
  1146. var
  1147. i,MaxLen : SizeInt;
  1148. pc : pwidechar;
  1149. begin
  1150. Pos:=0;
  1151. if Length(SubStr)>0 then
  1152. begin
  1153. MaxLen:=Length(source)-Length(SubStr);
  1154. i:=0;
  1155. pc:=@source[1];
  1156. while (i<=MaxLen) do
  1157. begin
  1158. inc(i);
  1159. if (SubStr[1]=pc^) and
  1160. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  1161. begin
  1162. Pos:=i;
  1163. exit;
  1164. end;
  1165. inc(pc);
  1166. end;
  1167. end;
  1168. end;
  1169. { Faster version for a widechar alone }
  1170. Function Pos (c : WideChar; Const s : WideString) : SizeInt;
  1171. var
  1172. i: SizeInt;
  1173. pc : pwidechar;
  1174. begin
  1175. pc:=@s[1];
  1176. for i:=1 to length(s) do
  1177. begin
  1178. if pc^=c then
  1179. begin
  1180. pos:=i;
  1181. exit;
  1182. end;
  1183. inc(pc);
  1184. end;
  1185. pos:=0;
  1186. end;
  1187. Function Pos (c : WideChar; Const s : AnsiString) : SizeInt;
  1188. var
  1189. i: SizeInt;
  1190. pc : pchar;
  1191. begin
  1192. pc:=@s[1];
  1193. for i:=1 to length(s) do
  1194. begin
  1195. if widechar(pc^)=c then
  1196. begin
  1197. pos:=i;
  1198. exit;
  1199. end;
  1200. inc(pc);
  1201. end;
  1202. pos:=0;
  1203. end;
  1204. Function Pos (c : AnsiString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1205. begin
  1206. result:=Pos(WideString(c),s);
  1207. end;
  1208. Function Pos (c : ShortString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1209. begin
  1210. result:=Pos(WideString(c),s);
  1211. end;
  1212. Function Pos (c : WideString; Const s : AnsiString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1213. begin
  1214. result:=Pos(c,WideString(s));
  1215. end;
  1216. { Faster version for a char alone. Must be implemented because }
  1217. { pos(c: char; const s: shortstring) also exists, so otherwise }
  1218. { using pos(char,pchar) will always call the shortstring version }
  1219. { (exact match for first argument), also with $h+ (JM) }
  1220. Function Pos (c : Char; Const s : WideString) : SizeInt;
  1221. var
  1222. i: SizeInt;
  1223. wc : widechar;
  1224. pc : pwidechar;
  1225. begin
  1226. wc:=c;
  1227. pc:=@s[1];
  1228. for i:=1 to length(s) do
  1229. begin
  1230. if pc^=wc then
  1231. begin
  1232. pos:=i;
  1233. exit;
  1234. end;
  1235. inc(pc);
  1236. end;
  1237. pos:=0;
  1238. end;
  1239. Procedure Delete (Var S : WideString; Index,Size: SizeInt);
  1240. Var
  1241. LS : SizeInt;
  1242. begin
  1243. If Length(S)=0 then
  1244. exit;
  1245. if index<=0 then
  1246. exit;
  1247. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len div sizeof(WideChar);
  1248. if (Index<=LS) and (Size>0) then
  1249. begin
  1250. UniqueString (S);
  1251. if Size+Index>LS then
  1252. Size:=LS-Index+1;
  1253. if Index+Size<=LS then
  1254. begin
  1255. Dec(Index);
  1256. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index-Size+1)*sizeof(WideChar));
  1257. end;
  1258. Setlength(s,LS-Size);
  1259. end;
  1260. end;
  1261. Procedure Insert (Const Source : WideString; Var S : WideString; Index : SizeInt);
  1262. var
  1263. Temp : WideString;
  1264. LS : SizeInt;
  1265. begin
  1266. If Length(Source)=0 then
  1267. exit;
  1268. if index <= 0 then
  1269. index := 1;
  1270. Ls:=Length(S);
  1271. if index > LS then
  1272. index := LS+1;
  1273. Dec(Index);
  1274. Pointer(Temp) := NewWideString(Length(Source)+LS);
  1275. SetLength(Temp,Length(Source)+LS);
  1276. If Index>0 then
  1277. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  1278. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  1279. If (LS-Index)>0 then
  1280. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  1281. S:=Temp;
  1282. end;
  1283. function UpCase(const s : WideString) : WideString;
  1284. begin
  1285. result:=widestringmanager.UpperWideStringProc(s);
  1286. end;
  1287. Procedure SetString (Out S : WideString; Buf : PWideChar; Len : SizeInt);
  1288. var
  1289. BufLen: SizeInt;
  1290. begin
  1291. SetLength(S,Len);
  1292. If (Buf<>Nil) and (Len>0) then
  1293. begin
  1294. BufLen := IndexWord(Buf^, Len+1, 0);
  1295. If (BufLen>0) and (BufLen < Len) then
  1296. Len := BufLen;
  1297. Move (Buf[0],S[1],Len*sizeof(WideChar));
  1298. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1299. end;
  1300. end;
  1301. Procedure SetString (Out S : WideString; Buf : PChar; Len : SizeInt);
  1302. var
  1303. BufLen: SizeInt;
  1304. begin
  1305. SetLength(S,Len);
  1306. If (Buf<>Nil) and (Len>0) then
  1307. begin
  1308. BufLen := IndexByte(Buf^, Len+1, 0);
  1309. If (BufLen>0) and (BufLen < Len) then
  1310. Len := BufLen;
  1311. widestringmanager.Ansi2WideMoveProc(Buf,S,Len);
  1312. //PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1313. end;
  1314. end;
  1315. {$ifndef FPUNONE}
  1316. Function fpc_Val_Real_WideStr(Const S : WideString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  1317. Var
  1318. SS : String;
  1319. begin
  1320. fpc_Val_Real_WideStr := 0;
  1321. if length(S) > 255 then
  1322. code := 256
  1323. else
  1324. begin
  1325. SS := S;
  1326. Val(SS,fpc_Val_Real_WideStr,code);
  1327. end;
  1328. end;
  1329. {$endif}
  1330. function fpc_val_enum_widestr(str2ordindex:pointer;const s:widestring;out code:valsint):longint;compilerproc;
  1331. var ss:shortstring;
  1332. begin
  1333. if length(s)>255 then
  1334. code:=256
  1335. else
  1336. begin
  1337. ss:=s;
  1338. val(ss,fpc_val_enum_widestr,code);
  1339. end;
  1340. end;
  1341. Function fpc_Val_Currency_WideStr(Const S : WideString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_WIDESTR']; compilerproc;
  1342. Var
  1343. SS : String;
  1344. begin
  1345. if length(S) > 255 then
  1346. begin
  1347. fpc_Val_Currency_WideStr:=0;
  1348. code := 256;
  1349. end
  1350. else
  1351. begin
  1352. SS := S;
  1353. Val(SS,fpc_Val_Currency_WideStr,code);
  1354. end;
  1355. end;
  1356. Function fpc_Val_UInt_WideStr (Const S : WideString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  1357. Var
  1358. SS : ShortString;
  1359. begin
  1360. fpc_Val_UInt_WideStr := 0;
  1361. if length(S) > 255 then
  1362. code := 256
  1363. else
  1364. begin
  1365. SS := S;
  1366. Val(SS,fpc_Val_UInt_WideStr,code);
  1367. end;
  1368. end;
  1369. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  1370. Var
  1371. SS : ShortString;
  1372. begin
  1373. fpc_Val_SInt_WideStr:=0;
  1374. if length(S)>255 then
  1375. code:=256
  1376. else
  1377. begin
  1378. SS := S;
  1379. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1380. end;
  1381. end;
  1382. {$ifndef CPU64}
  1383. Function fpc_Val_qword_WideStr (Const S : WideString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  1384. Var
  1385. SS : ShortString;
  1386. begin
  1387. fpc_Val_qword_WideStr:=0;
  1388. if length(S)>255 then
  1389. code:=256
  1390. else
  1391. begin
  1392. SS := S;
  1393. Val(SS,fpc_Val_qword_WideStr,Code);
  1394. end;
  1395. end;
  1396. Function fpc_Val_int64_WideStr (Const S : WideString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  1397. Var
  1398. SS : ShortString;
  1399. begin
  1400. fpc_Val_int64_WideStr:=0;
  1401. if length(S)>255 then
  1402. code:=256
  1403. else
  1404. begin
  1405. SS := S;
  1406. Val(SS,fpc_Val_int64_WideStr,Code);
  1407. end;
  1408. end;
  1409. {$endif CPU64}
  1410. {$ifndef FPUNONE}
  1411. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : WideString);compilerproc;
  1412. var
  1413. ss : shortstring;
  1414. begin
  1415. str_real(len,fr,d,treal_type(rt),ss);
  1416. s:=ss;
  1417. end;
  1418. {$endif}
  1419. procedure fpc_widestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:widestring);compilerproc;
  1420. var ss:shortstring;
  1421. begin
  1422. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1423. s:=ss;
  1424. end;
  1425. {$ifdef FPC_HAS_STR_CURRENCY}
  1426. procedure fpc_WideStr_Currency(c : Currency;len,fr : SizeInt;out s : WideString);compilerproc;
  1427. var
  1428. ss : shortstring;
  1429. begin
  1430. str(c:len:fr,ss);
  1431. s:=ss;
  1432. end;
  1433. {$endif FPC_HAS_STR_CURRENCY}
  1434. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; out S : WideString);compilerproc;
  1435. Var
  1436. SS : ShortString;
  1437. begin
  1438. Str (v:Len,SS);
  1439. S:=SS;
  1440. end;
  1441. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; out S : WideString);compilerproc;
  1442. Var
  1443. SS : ShortString;
  1444. begin
  1445. str(v:Len,SS);
  1446. S:=SS;
  1447. end;
  1448. {$ifndef CPU64}
  1449. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; out S : WideString);compilerproc;
  1450. Var
  1451. SS : ShortString;
  1452. begin
  1453. Str (v:Len,SS);
  1454. S:=SS;
  1455. end;
  1456. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; out S : WideString);compilerproc;
  1457. Var
  1458. SS : ShortString;
  1459. begin
  1460. str(v:Len,SS);
  1461. S:=SS;
  1462. end;
  1463. {$endif CPU64}
  1464. function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1465. begin
  1466. if assigned(Source) then
  1467. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1468. else
  1469. Result:=0;
  1470. end;
  1471. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt;
  1472. var
  1473. i,j : SizeUInt;
  1474. w : word;
  1475. begin
  1476. result:=0;
  1477. if source=nil then
  1478. exit;
  1479. i:=0;
  1480. j:=0;
  1481. if assigned(Dest) then
  1482. begin
  1483. while (i<SourceChars) and (j<MaxDestBytes) do
  1484. begin
  1485. w:=word(Source[i]);
  1486. case w of
  1487. 0..$7f:
  1488. begin
  1489. Dest[j]:=char(w);
  1490. inc(j);
  1491. end;
  1492. $80..$7ff:
  1493. begin
  1494. if j+1>=MaxDestBytes then
  1495. break;
  1496. Dest[j]:=char($c0 or (w shr 6));
  1497. Dest[j+1]:=char($80 or (w and $3f));
  1498. inc(j,2);
  1499. end;
  1500. else
  1501. begin
  1502. if j+2>=MaxDestBytes then
  1503. break;
  1504. Dest[j]:=char($e0 or (w shr 12));
  1505. Dest[j+1]:=char($80 or ((w shr 6)and $3f));
  1506. Dest[j+2]:=char($80 or (w and $3f));
  1507. inc(j,3);
  1508. end;
  1509. end;
  1510. inc(i);
  1511. end;
  1512. if j>SizeUInt(MaxDestBytes-1) then
  1513. j:=MaxDestBytes-1;
  1514. Dest[j]:=#0;
  1515. end
  1516. else
  1517. begin
  1518. while i<SourceChars do
  1519. begin
  1520. case word(Source[i]) of
  1521. $0..$7f:
  1522. inc(j);
  1523. $80..$7ff:
  1524. inc(j,2);
  1525. else
  1526. inc(j,3);
  1527. end;
  1528. inc(i);
  1529. end;
  1530. end;
  1531. result:=j+1;
  1532. end;
  1533. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1534. begin
  1535. if assigned(Source) then
  1536. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1537. else
  1538. Result:=0;
  1539. end;
  1540. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1541. var
  1542. i,j : SizeUInt;
  1543. w: SizeUInt;
  1544. b : byte;
  1545. begin
  1546. if not assigned(Source) then
  1547. begin
  1548. result:=0;
  1549. exit;
  1550. end;
  1551. result:=SizeUInt(-1);
  1552. i:=0;
  1553. j:=0;
  1554. if assigned(Dest) then
  1555. begin
  1556. while (j<MaxDestChars) and (i<SourceBytes) do
  1557. begin
  1558. b:=byte(Source[i]);
  1559. w:=b;
  1560. inc(i);
  1561. // 2 or 3 bytes?
  1562. if b>=$80 then
  1563. begin
  1564. w:=b and $3f;
  1565. if i>=SourceBytes then
  1566. exit;
  1567. // 3 bytes?
  1568. if (b and $20)<>0 then
  1569. begin
  1570. b:=byte(Source[i]);
  1571. inc(i);
  1572. if i>=SourceBytes then
  1573. exit;
  1574. if (b and $c0)<>$80 then
  1575. exit;
  1576. w:=(w shl 6) or (b and $3f);
  1577. end;
  1578. b:=byte(Source[i]);
  1579. w:=(w shl 6) or (b and $3f);
  1580. if (b and $c0)<>$80 then
  1581. exit;
  1582. inc(i);
  1583. end;
  1584. Dest[j]:=WideChar(w);
  1585. inc(j);
  1586. end;
  1587. if j>=MaxDestChars then j:=MaxDestChars-1;
  1588. Dest[j]:=#0;
  1589. end
  1590. else
  1591. begin
  1592. while i<SourceBytes do
  1593. begin
  1594. b:=byte(Source[i]);
  1595. inc(i);
  1596. // 2 or 3 bytes?
  1597. if b>=$80 then
  1598. begin
  1599. if i>=SourceBytes then
  1600. exit;
  1601. // 3 bytes?
  1602. b := b and $3f;
  1603. if (b and $20)<>0 then
  1604. begin
  1605. b:=byte(Source[i]);
  1606. inc(i);
  1607. if i>=SourceBytes then
  1608. exit;
  1609. if (b and $c0)<>$80 then
  1610. exit;
  1611. end;
  1612. if (byte(Source[i]) and $c0)<>$80 then
  1613. exit;
  1614. inc(i);
  1615. end;
  1616. inc(j);
  1617. end;
  1618. end;
  1619. result:=j+1;
  1620. end;
  1621. function UTF8Encode(const s : WideString) : UTF8String;
  1622. var
  1623. i : SizeInt;
  1624. hs : UTF8String;
  1625. begin
  1626. result:='';
  1627. if s='' then
  1628. exit;
  1629. SetLength(hs,length(s)*3);
  1630. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  1631. if i>0 then
  1632. begin
  1633. SetLength(hs,i-1);
  1634. result:=hs;
  1635. end;
  1636. end;
  1637. function UTF8Decode(const s : UTF8String): WideString;
  1638. var
  1639. i : SizeInt;
  1640. hs : WideString;
  1641. begin
  1642. result:='';
  1643. if s='' then
  1644. exit;
  1645. SetLength(hs,length(s));
  1646. i:=Utf8ToUnicode(PWideChar(hs),length(hs)+1,pchar(s),length(s));
  1647. if i>0 then
  1648. begin
  1649. SetLength(hs,i-1);
  1650. result:=hs;
  1651. end;
  1652. end;
  1653. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  1654. begin
  1655. Result:=Utf8Encode(s);
  1656. end;
  1657. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1658. begin
  1659. Result:=Utf8Decode(s);
  1660. end;
  1661. { converts an utf-16 code point or surrogate pair to utf-32 }
  1662. function utf16toutf32(const S: WideString; const index: SizeInt; out len: longint): UCS4Char; [public, alias: 'FPC_UTF16TOUTF32'];
  1663. var
  1664. w: widechar;
  1665. begin
  1666. { UTF-16 points in the range #$0-#$D7FF and #$E000-#$FFFF }
  1667. { are the same in UTF-32 }
  1668. w:=s[index];
  1669. if (w<=#$d7ff) or
  1670. (w>=#$e000) then
  1671. begin
  1672. result:=UCS4Char(w);
  1673. len:=1;
  1674. end
  1675. { valid surrogate pair? }
  1676. else if (w<=#$dbff) and
  1677. { w>=#$d7ff check not needed, checked above }
  1678. (index<length(s)) and
  1679. (s[index+1]>=#$dc00) and
  1680. (s[index+1]<=#$dfff) then
  1681. { convert the surrogate pair to UTF-32 }
  1682. begin
  1683. result:=(UCS4Char(w)-$d800) shl 10 + (UCS4Char(s[index+1])-$dc00) + $10000;
  1684. len:=2;
  1685. end
  1686. else
  1687. { invalid surrogate -> do nothing }
  1688. begin
  1689. result:=UCS4Char(w);
  1690. len:=1;
  1691. end;
  1692. end;
  1693. function WideStringToUCS4String(const s : WideString) : UCS4String;
  1694. var
  1695. i, slen,
  1696. destindex : SizeInt;
  1697. len : longint;
  1698. begin
  1699. slen:=length(s);
  1700. setlength(result,slen+1);
  1701. i:=1;
  1702. destindex:=0;
  1703. while (i<=slen) do
  1704. begin
  1705. result[destindex]:=utf16toutf32(s,i,len);
  1706. inc(destindex);
  1707. inc(i,len);
  1708. end;
  1709. { destindex <= slen (surrogate pairs may have been merged) }
  1710. { destindex+1 for terminating #0 (dynamic arrays are }
  1711. { implicitely filled with zero) }
  1712. setlength(result,destindex+1);
  1713. end;
  1714. { concatenates an utf-32 char to a widestring. S *must* be unique when entering. }
  1715. procedure ConcatUTF32ToWideStr(const nc: UCS4Char; var S: WideString; var index: SizeInt);
  1716. var
  1717. p : PWideChar;
  1718. begin
  1719. { if nc > $ffff, we need two places }
  1720. if (index+ord(nc > $ffff)>length(s)) then
  1721. if (length(s) < 10*256) then
  1722. setlength(s,length(s)+10)
  1723. else
  1724. setlength(s,length(s)+length(s) shr 8);
  1725. { we know that s is unique -> avoid uniquestring calls}
  1726. p:=@s[index];
  1727. if (nc<$ffff) then
  1728. begin
  1729. p^:=widechar(nc);
  1730. inc(index);
  1731. end
  1732. else if (dword(nc)<=$10ffff) then
  1733. begin
  1734. p^:=widechar((nc - $10000) shr 10 + $d800);
  1735. (p+1)^:=widechar((nc - $10000) and $3ff + $dc00);
  1736. inc(index,2);
  1737. end
  1738. else
  1739. { invalid code point }
  1740. begin
  1741. p^:='?';
  1742. inc(index);
  1743. end;
  1744. end;
  1745. function UCS4StringToWideString(const s : UCS4String) : WideString;
  1746. var
  1747. i : SizeInt;
  1748. resindex : SizeInt;
  1749. begin
  1750. { skip terminating #0 }
  1751. SetLength(result,length(s)-1);
  1752. resindex:=1;
  1753. for i:=0 to high(s)-1 do
  1754. ConcatUTF32ToWideStr(s[i],result,resindex);
  1755. { adjust result length (may be too big due to growing }
  1756. { for surrogate pairs) }
  1757. setlength(result,resindex-1);
  1758. end;
  1759. const
  1760. SNoWidestrings = 'This binary has no widestrings support compiled in.';
  1761. SRecompileWithWidestrings = 'Recompile the application with a widestrings-manager in the program uses clause.';
  1762. procedure unimplementedwidestring;
  1763. begin
  1764. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  1765. If IsConsole then
  1766. begin
  1767. Writeln(StdErr,SNoWidestrings);
  1768. Writeln(StdErr,SRecompileWithWidestrings);
  1769. end;
  1770. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  1771. HandleErrorFrame(233,get_frame);
  1772. end;
  1773. {$warnings off}
  1774. function GenericWideCase(const s : WideString) : WideString;
  1775. begin
  1776. unimplementedwidestring;
  1777. end;
  1778. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  1779. begin
  1780. unimplementedwidestring;
  1781. end;
  1782. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  1783. begin
  1784. unimplementedwidestring;
  1785. end;
  1786. function CharLengthPChar(const Str: PChar): PtrInt;
  1787. begin
  1788. unimplementedwidestring;
  1789. end;
  1790. {$warnings on}
  1791. procedure initwidestringmanager;
  1792. begin
  1793. fillchar(widestringmanager,sizeof(widestringmanager),0);
  1794. {$ifndef HAS_WIDESTRINGMANAGER}
  1795. widestringmanager.Wide2AnsiMoveProc:=@defaultWide2AnsiMove;
  1796. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2WideMove;
  1797. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  1798. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  1799. {$endif HAS_WIDESTRINGMANAGER}
  1800. widestringmanager.CompareWideStringProc:=@CompareWideString;
  1801. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  1802. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  1803. end;