wstrings.inc 42 KB

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