wstrings.inc 46 KB

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