wstrings.inc 44 KB

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