wstrings.inc 38 KB

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