wstrings.inc 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by Florian Klaempfl,
  4. member of the Free Pascal development team.
  5. This file implements support routines for WideStrings/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(pwidechar(@arr)^, PWideChar(Pointer(@fpc_WideCharArray_To_WideStr[1]))^,i*sizeof(WideChar));
  623. { Terminating Zero }
  624. PWideChar(Pointer(@fpc_WideCharArray_To_WideStr[1])+i*sizeof(WideChar))^:=#0;
  625. end;
  626. {$ifndef FPC_STRTOCHARARRAYPROC}
  627. { inside the compiler, the resulttype is modified to that of the actual }
  628. { chararray we're converting to (JM) }
  629. function fpc_widestr_to_chararray(arraysize: SizeInt; const src: WideString): fpc_big_chararray;[public,alias: 'FPC_WIDESTR_TO_CHARARRAY']; compilerproc;
  630. var
  631. len: SizeInt;
  632. temp: ansistring;
  633. begin
  634. len := length(src);
  635. { make sure we don't dereference src if it can be nil (JM) }
  636. if len > 0 then
  637. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,len);
  638. len := length(temp);
  639. if len > arraysize then
  640. len := arraysize;
  641. {$r-}
  642. move(temp[1],fpc_widestr_to_chararray[0],len);
  643. fillchar(fpc_widestr_to_chararray[len],arraysize-len,0);
  644. {$ifdef RangeCheckWasOn}
  645. {$r+}
  646. {$endif}
  647. end;
  648. { inside the compiler, the resulttype is modified to that of the actual }
  649. { widechararray we're converting to (JM) }
  650. function fpc_widestr_to_widechararray(arraysize: SizeInt; const src: WideString): fpc_big_widechararray;[public,alias: 'FPC_WIDESTR_TO_WIDECHARARRAY']; compilerproc;
  651. var
  652. len: SizeInt;
  653. begin
  654. len := length(src);
  655. if len > arraysize then
  656. len := arraysize;
  657. {$r-}
  658. { make sure we don't try to access element 1 of the ansistring if it's nil }
  659. if len > 0 then
  660. move(src[1],fpc_widestr_to_widechararray[0],len*SizeOf(WideChar));
  661. fillchar(fpc_widestr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  662. {$ifdef RangeCheckWasOn}
  663. {$r+}
  664. {$endif}
  665. end;
  666. { inside the compiler, the resulttype is modified to that of the actual }
  667. { chararray we're converting to (JM) }
  668. function fpc_ansistr_to_widechararray(arraysize: SizeInt; const src: AnsiString): fpc_big_widechararray;[public,alias: 'FPC_ANSISTR_TO_WIDECHARARRAY']; compilerproc;
  669. var
  670. len: SizeInt;
  671. temp: widestring;
  672. begin
  673. len := length(src);
  674. { make sure we don't dereference src if it can be nil (JM) }
  675. if len > 0 then
  676. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  677. len := length(temp);
  678. if len > arraysize then
  679. len := arraysize;
  680. {$r-}
  681. move(temp[1],fpc_ansistr_to_widechararray[0],len*sizeof(widechar));
  682. fillchar(fpc_ansistr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  683. {$ifdef RangeCheckWasOn}
  684. {$r+}
  685. {$endif}
  686. end;
  687. function fpc_shortstr_to_widechararray(arraysize: SizeInt; const src: ShortString): fpc_big_widechararray;[public,alias: 'FPC_SHORTSTR_TO_WIDECHARARRAY']; compilerproc;
  688. var
  689. len: longint;
  690. temp : widestring;
  691. begin
  692. len := length(src);
  693. { make sure we don't access char 1 if length is 0 (JM) }
  694. if len > 0 then
  695. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  696. len := length(temp);
  697. if len > arraysize then
  698. len := arraysize;
  699. {$r-}
  700. move(temp[1],fpc_shortstr_to_widechararray[0],len*sizeof(widechar));
  701. fillchar(fpc_shortstr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  702. {$ifdef RangeCheckWasOn}
  703. {$r+}
  704. {$endif}
  705. end;
  706. {$else ndef FPC_STRTOCHARARRAYPROC}
  707. procedure fpc_widestr_to_chararray(out res: array of char; const src: WideString); compilerproc;
  708. var
  709. len: SizeInt;
  710. temp: ansistring;
  711. begin
  712. len := length(src);
  713. { make sure we don't dereference src if it can be nil (JM) }
  714. if len > 0 then
  715. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,len);
  716. len := length(temp);
  717. if len > length(res) then
  718. len := length(res);
  719. {$r-}
  720. move(temp[1],res[0],len);
  721. fillchar(res[len],length(res)-len,0);
  722. {$ifdef RangeCheckWasOn}
  723. {$r+}
  724. {$endif}
  725. end;
  726. procedure fpc_widestr_to_widechararray(out res: array of widechar; const src: WideString); compilerproc;
  727. var
  728. len: SizeInt;
  729. begin
  730. len := length(src);
  731. if len > length(res) then
  732. len := length(res);
  733. {$r-}
  734. { make sure we don't try to access element 1 of the ansistring if it's nil }
  735. if len > 0 then
  736. move(src[1],res[0],len*SizeOf(WideChar));
  737. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  738. {$ifdef RangeCheckWasOn}
  739. {$r+}
  740. {$endif}
  741. end;
  742. procedure fpc_ansistr_to_widechararray(out res: array of widechar; const src: AnsiString); compilerproc;
  743. var
  744. len: SizeInt;
  745. temp: widestring;
  746. begin
  747. len := length(src);
  748. { make sure we don't dereference src if it can be nil (JM) }
  749. if len > 0 then
  750. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  751. len := length(temp);
  752. if len > length(res) then
  753. len := length(res);
  754. {$r-}
  755. move(temp[1],res[0],len*sizeof(widechar));
  756. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  757. {$ifdef RangeCheckWasOn}
  758. {$r+}
  759. {$endif}
  760. end;
  761. procedure fpc_shortstr_to_widechararray(out res: array of widechar; const src: ShortString); compilerproc;
  762. var
  763. len: longint;
  764. temp : widestring;
  765. begin
  766. len := length(src);
  767. { make sure we don't access char 1 if length is 0 (JM) }
  768. if len > 0 then
  769. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  770. len := length(temp);
  771. if len > length(res) then
  772. len := length(res);
  773. {$r-}
  774. move(temp[1],res[0],len*sizeof(widechar));
  775. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  776. {$ifdef RangeCheckWasOn}
  777. {$r+}
  778. {$endif}
  779. end;
  780. {$endif ndef FPC_STRTOCHARARRAYPROC}
  781. Function fpc_WideStr_Compare(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE']; compilerproc;
  782. {
  783. Compares 2 WideStrings;
  784. The result is
  785. <0 if S1<S2
  786. 0 if S1=S2
  787. >0 if S1>S2
  788. }
  789. Var
  790. MaxI,Temp : SizeInt;
  791. begin
  792. if pointer(S1)=pointer(S2) then
  793. begin
  794. fpc_WideStr_Compare:=0;
  795. exit;
  796. end;
  797. Maxi:=Length(S1);
  798. temp:=Length(S2);
  799. If MaxI>Temp then
  800. MaxI:=Temp;
  801. Temp:=CompareWord(S1[1],S2[1],MaxI);
  802. if temp=0 then
  803. temp:=Length(S1)-Length(S2);
  804. fpc_WideStr_Compare:=Temp;
  805. end;
  806. Function fpc_WideStr_Compare_Equal(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE_EQUAL']; compilerproc;
  807. {
  808. Compares 2 WideStrings for equality only;
  809. The result is
  810. 0 if S1=S2
  811. <>0 if S1<>S2
  812. }
  813. Var
  814. MaxI : SizeInt;
  815. begin
  816. if pointer(S1)=pointer(S2) then
  817. exit(0);
  818. Maxi:=Length(S1);
  819. If MaxI<>Length(S2) then
  820. exit(-1)
  821. else
  822. exit(CompareWord(S1[1],S2[1],MaxI));
  823. end;
  824. Procedure fpc_WideStr_CheckZero(p : pointer);[Public,Alias : 'FPC_WIDESTR_CHECKZERO']; compilerproc;
  825. begin
  826. if p=nil then
  827. HandleErrorFrame(201,get_frame);
  828. end;
  829. Procedure fpc_WideStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; compilerproc;
  830. begin
  831. if (index>len) or (Index<1) then
  832. HandleErrorFrame(201,get_frame);
  833. end;
  834. Procedure fpc_WideStr_SetLength(Var S : WideString; l : SizeInt);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; compilerproc;
  835. {
  836. Sets The length of string S to L.
  837. Makes sure S is unique, and contains enough room.
  838. }
  839. Var
  840. Temp : Pointer;
  841. movelen: SizeInt;
  842. begin
  843. if (l>0) then
  844. begin
  845. if Pointer(S)=nil then
  846. begin
  847. { Need a complete new string...}
  848. Pointer(s):=NewWideString(l);
  849. end
  850. { windows doesn't support reallocing widestrings, this code
  851. is anyways subject to be removed because widestrings shouldn't be
  852. ref. counted anymore (FK) }
  853. else
  854. if
  855. {$ifdef MSWINDOWS}
  856. not winwidestringalloc and
  857. {$endif MSWINDOWS}
  858. {$ifdef FPC_WINLIKEWIDESTRING}
  859. not IsWideStringConstant(pointer(S))
  860. {$else}
  861. (PWideRec(Pointer(S)-WideFirstOff)^.Ref = 1)
  862. {$endif FPC_WINLIKEWIDESTRING}
  863. then
  864. begin
  865. Dec(Pointer(S),WideFirstOff);
  866. if L*sizeof(WideChar)+WideRecLen>MemSize(Pointer(S)) then
  867. reallocmem(pointer(S), L*sizeof(WideChar)+WideRecLen);
  868. Inc(Pointer(S), WideFirstOff);
  869. end
  870. else
  871. begin
  872. { Reallocation is needed... }
  873. Temp:=Pointer(NewWideString(L));
  874. if Length(S)>0 then
  875. begin
  876. if l < succ(length(s)) then
  877. movelen := l
  878. { also move terminating null }
  879. else
  880. movelen := succ(length(s));
  881. Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
  882. end;
  883. fpc_widestr_decr_ref(Pointer(S));
  884. Pointer(S):=Temp;
  885. end;
  886. { Force nil termination in case it gets shorter }
  887. PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
  888. {$ifdef MSWINDOWS}
  889. if not winwidestringalloc then
  890. {$endif MSWINDOWS}
  891. PWideRec(Pointer(S)-WideFirstOff)^.Len:=l*sizeof(WideChar);
  892. end
  893. else
  894. begin
  895. { Length=0 }
  896. if Pointer(S)<>nil then
  897. fpc_widestr_decr_ref (Pointer(S));
  898. Pointer(S):=Nil;
  899. end;
  900. end;
  901. {*****************************************************************************
  902. Public functions, In interface.
  903. *****************************************************************************}
  904. function WideCharToString(S : PWideChar) : AnsiString;
  905. begin
  906. result:=WideCharLenToString(s,Length(WideString(s)));
  907. end;
  908. function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  909. var
  910. temp:widestring;
  911. begin
  912. widestringmanager.Ansi2WideMoveProc(PChar(Src),temp,Length(Src));
  913. if Length(temp)<DestSize then
  914. move(temp[1],Dest^,Length(temp)*SizeOf(WideChar))
  915. else
  916. move(temp[1],Dest^,(DestSize-1)*SizeOf(WideChar));
  917. Dest[DestSize-1]:=#0;
  918. result:=Dest;
  919. end;
  920. function WideCharLenToString(S : PWideChar;Len : SizeInt) : AnsiString;
  921. begin
  922. //SetLength(result,Len);
  923. widestringmanager.Wide2AnsiMoveproc(S,result,Len);
  924. end;
  925. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : AnsiString);
  926. begin
  927. Dest:=WideCharLenToString(Src,Len);
  928. end;
  929. procedure WideCharToStrVar(S : PWideChar;out Dest : AnsiString);
  930. begin
  931. Dest:=WideCharToString(S);
  932. end;
  933. Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; compilerproc;
  934. {$ifdef FPC_WINLIKEWIDESTRING}
  935. begin
  936. pointer(result) := pointer(s);
  937. end;
  938. {$else FPC_WINLIKEWIDESTRING}
  939. {
  940. Make sure reference count of S is 1,
  941. using copy-on-write semantics.
  942. }
  943. Var
  944. SNew : Pointer;
  945. L : SizeInt;
  946. begin
  947. pointer(result) := pointer(s);
  948. If Pointer(S)=Nil then
  949. exit;
  950. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  951. begin
  952. L:=PWideRec(Pointer(S)-WideFirstOff)^.len div sizeof(WideChar);
  953. SNew:=NewWideString (L);
  954. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  955. PWideRec(SNew-WideFirstOff)^.len:=L * sizeof(WideChar);
  956. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  957. pointer(S):=SNew;
  958. pointer(result):=SNew;
  959. end;
  960. end;
  961. {$endif FPC_WINLIKEWIDESTRING}
  962. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : SizeInt) : WideString;compilerproc;
  963. var
  964. ResultAddress : Pointer;
  965. begin
  966. ResultAddress:=Nil;
  967. dec(index);
  968. if Index < 0 then
  969. Index := 0;
  970. { Check Size. Accounts for Zero-length S, the double check is needed because
  971. Size can be maxint and will get <0 when adding index }
  972. if (Size>Length(S)) or
  973. (Index+Size>Length(S)) then
  974. Size:=Length(S)-Index;
  975. If Size>0 then
  976. begin
  977. If Index<0 Then
  978. Index:=0;
  979. ResultAddress:=Pointer(NewWideString (Size));
  980. if ResultAddress<>Nil then
  981. begin
  982. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  983. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size*sizeof(WideChar);
  984. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  985. end;
  986. end;
  987. Pointer(fpc_widestr_Copy):=ResultAddress;
  988. end;
  989. Function Pos (Const Substr : WideString; Const Source : WideString) : SizeInt;
  990. var
  991. i,MaxLen : SizeInt;
  992. pc : pwidechar;
  993. begin
  994. Pos:=0;
  995. if Length(SubStr)>0 then
  996. begin
  997. MaxLen:=Length(source)-Length(SubStr);
  998. i:=0;
  999. pc:=@source[1];
  1000. while (i<=MaxLen) do
  1001. begin
  1002. inc(i);
  1003. if (SubStr[1]=pc^) and
  1004. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  1005. begin
  1006. Pos:=i;
  1007. exit;
  1008. end;
  1009. inc(pc);
  1010. end;
  1011. end;
  1012. end;
  1013. { Faster version for a widechar alone }
  1014. Function Pos (c : WideChar; Const s : WideString) : SizeInt;
  1015. var
  1016. i: SizeInt;
  1017. pc : pwidechar;
  1018. begin
  1019. pc:=@s[1];
  1020. for i:=1 to length(s) do
  1021. begin
  1022. if pc^=c then
  1023. begin
  1024. pos:=i;
  1025. exit;
  1026. end;
  1027. inc(pc);
  1028. end;
  1029. pos:=0;
  1030. end;
  1031. Function Pos (c : WideChar; Const s : AnsiString) : SizeInt;
  1032. var
  1033. i: SizeInt;
  1034. pc : pchar;
  1035. begin
  1036. pc:=@s[1];
  1037. for i:=1 to length(s) do
  1038. begin
  1039. if widechar(pc^)=c then
  1040. begin
  1041. pos:=i;
  1042. exit;
  1043. end;
  1044. inc(pc);
  1045. end;
  1046. pos:=0;
  1047. end;
  1048. Function Pos (c : AnsiString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1049. begin
  1050. result:=Pos(WideString(c),s);
  1051. end;
  1052. Function Pos (c : ShortString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1053. begin
  1054. result:=Pos(WideString(c),s);
  1055. end;
  1056. Function Pos (c : WideString; Const s : AnsiString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1057. begin
  1058. result:=Pos(c,WideString(s));
  1059. end;
  1060. { Faster version for a char alone. Must be implemented because }
  1061. { pos(c: char; const s: shortstring) also exists, so otherwise }
  1062. { using pos(char,pchar) will always call the shortstring version }
  1063. { (exact match for first argument), also with $h+ (JM) }
  1064. Function Pos (c : Char; Const s : WideString) : SizeInt;
  1065. var
  1066. i: SizeInt;
  1067. wc : widechar;
  1068. pc : pwidechar;
  1069. begin
  1070. wc:=c;
  1071. pc:=@s[1];
  1072. for i:=1 to length(s) do
  1073. begin
  1074. if pc^=wc then
  1075. begin
  1076. pos:=i;
  1077. exit;
  1078. end;
  1079. inc(pc);
  1080. end;
  1081. pos:=0;
  1082. end;
  1083. Procedure Delete (Var S : WideString; Index,Size: SizeInt);
  1084. Var
  1085. LS : SizeInt;
  1086. begin
  1087. If Length(S)=0 then
  1088. exit;
  1089. if index<=0 then
  1090. exit;
  1091. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len div sizeof(WideChar);
  1092. if (Index<=LS) and (Size>0) then
  1093. begin
  1094. UniqueString (S);
  1095. if Size+Index>LS then
  1096. Size:=LS-Index+1;
  1097. if Index+Size<=LS then
  1098. begin
  1099. Dec(Index);
  1100. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  1101. end;
  1102. Setlength(s,LS-Size);
  1103. end;
  1104. end;
  1105. Procedure Insert (Const Source : WideString; Var S : WideString; Index : SizeInt);
  1106. var
  1107. Temp : WideString;
  1108. LS : SizeInt;
  1109. begin
  1110. If Length(Source)=0 then
  1111. exit;
  1112. if index <= 0 then
  1113. index := 1;
  1114. Ls:=Length(S);
  1115. if index > LS then
  1116. index := LS+1;
  1117. Dec(Index);
  1118. Pointer(Temp) := NewWideString(Length(Source)+LS);
  1119. SetLength(Temp,Length(Source)+LS);
  1120. If Index>0 then
  1121. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  1122. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  1123. If (LS-Index)>0 then
  1124. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  1125. S:=Temp;
  1126. end;
  1127. function UpCase(const s : WideString) : WideString;
  1128. begin
  1129. result:=widestringmanager.UpperWideStringProc(s);
  1130. end;
  1131. Procedure SetString (Out S : WideString; Buf : PWideChar; Len : SizeInt);
  1132. var
  1133. BufLen: SizeInt;
  1134. begin
  1135. SetLength(S,Len);
  1136. If (Buf<>Nil) and (Len>0) then
  1137. begin
  1138. BufLen := IndexWord(Buf^, Len+1, 0);
  1139. If (BufLen>0) and (BufLen < Len) then
  1140. Len := BufLen;
  1141. Move (Buf[0],S[1],Len*sizeof(WideChar));
  1142. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1143. end;
  1144. end;
  1145. Procedure SetString (Out S : WideString; Buf : PChar; Len : SizeInt);
  1146. var
  1147. BufLen: SizeInt;
  1148. begin
  1149. SetLength(S,Len);
  1150. If (Buf<>Nil) and (Len>0) then
  1151. begin
  1152. BufLen := IndexByte(Buf^, Len+1, 0);
  1153. If (BufLen>0) and (BufLen < Len) then
  1154. Len := BufLen;
  1155. widestringmanager.Ansi2WideMoveProc(Buf,S,Len);
  1156. //PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1157. end;
  1158. end;
  1159. Function fpc_Val_Real_WideStr(Const S : WideString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  1160. Var
  1161. SS : String;
  1162. begin
  1163. fpc_Val_Real_WideStr := 0;
  1164. if length(S) > 255 then
  1165. code := 256
  1166. else
  1167. begin
  1168. SS := S;
  1169. Val(SS,fpc_Val_Real_WideStr,code);
  1170. end;
  1171. end;
  1172. function fpc_val_enum_widestr(str2ordindex:pointer;const s:widestring;out code:valsint):longint;compilerproc;
  1173. var ss:shortstring;
  1174. begin
  1175. if length(s)>255 then
  1176. code:=256
  1177. else
  1178. begin
  1179. ss:=s;
  1180. val(ss,fpc_val_enum_widestr,code);
  1181. end;
  1182. end;
  1183. Function fpc_Val_Currency_WideStr(Const S : WideString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_WIDESTR']; compilerproc;
  1184. Var
  1185. SS : String;
  1186. begin
  1187. if length(S) > 255 then
  1188. begin
  1189. fpc_Val_Currency_WideStr:=0;
  1190. code := 256;
  1191. end
  1192. else
  1193. begin
  1194. SS := S;
  1195. Val(SS,fpc_Val_Currency_WideStr,code);
  1196. end;
  1197. end;
  1198. Function fpc_Val_UInt_WideStr (Const S : WideString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  1199. Var
  1200. SS : ShortString;
  1201. begin
  1202. fpc_Val_UInt_WideStr := 0;
  1203. if length(S) > 255 then
  1204. code := 256
  1205. else
  1206. begin
  1207. SS := S;
  1208. Val(SS,fpc_Val_UInt_WideStr,code);
  1209. end;
  1210. end;
  1211. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  1212. Var
  1213. SS : ShortString;
  1214. begin
  1215. fpc_Val_SInt_WideStr:=0;
  1216. if length(S)>255 then
  1217. code:=256
  1218. else
  1219. begin
  1220. SS := S;
  1221. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1222. end;
  1223. end;
  1224. {$ifndef CPU64}
  1225. Function fpc_Val_qword_WideStr (Const S : WideString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  1226. Var
  1227. SS : ShortString;
  1228. begin
  1229. fpc_Val_qword_WideStr:=0;
  1230. if length(S)>255 then
  1231. code:=256
  1232. else
  1233. begin
  1234. SS := S;
  1235. Val(SS,fpc_Val_qword_WideStr,Code);
  1236. end;
  1237. end;
  1238. Function fpc_Val_int64_WideStr (Const S : WideString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  1239. Var
  1240. SS : ShortString;
  1241. begin
  1242. fpc_Val_int64_WideStr:=0;
  1243. if length(S)>255 then
  1244. code:=256
  1245. else
  1246. begin
  1247. SS := S;
  1248. Val(SS,fpc_Val_int64_WideStr,Code);
  1249. end;
  1250. end;
  1251. {$endif CPU64}
  1252. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : WideString);compilerproc;
  1253. var
  1254. ss : shortstring;
  1255. begin
  1256. str_real(len,fr,d,treal_type(rt),ss);
  1257. s:=ss;
  1258. end;
  1259. procedure fpc_widestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:widestring);compilerproc;
  1260. var ss:shortstring;
  1261. begin
  1262. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1263. s:=ss;
  1264. end;
  1265. {$ifdef FPC_HAS_STR_CURRENCY}
  1266. procedure fpc_WideStr_Currency(c : Currency;len,fr : SizeInt;out s : WideString);compilerproc;
  1267. var
  1268. ss : shortstring;
  1269. begin
  1270. str(c:len:fr,ss);
  1271. s:=ss;
  1272. end;
  1273. {$endif FPC_HAS_STR_CURRENCY}
  1274. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; out S : WideString);compilerproc;
  1275. Var
  1276. SS : ShortString;
  1277. begin
  1278. Str (v:Len,SS);
  1279. S:=SS;
  1280. end;
  1281. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; out S : WideString);compilerproc;
  1282. Var
  1283. SS : ShortString;
  1284. begin
  1285. str(v:Len,SS);
  1286. S:=SS;
  1287. end;
  1288. {$ifndef CPU64}
  1289. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; out S : WideString);compilerproc;
  1290. Var
  1291. SS : ShortString;
  1292. begin
  1293. Str (v:Len,SS);
  1294. S:=SS;
  1295. end;
  1296. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; out S : WideString);compilerproc;
  1297. Var
  1298. SS : ShortString;
  1299. begin
  1300. str(v:Len,SS);
  1301. S:=SS;
  1302. end;
  1303. {$endif CPU64}
  1304. function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1305. begin
  1306. if assigned(Source) then
  1307. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1308. else
  1309. Result:=0;
  1310. end;
  1311. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt;
  1312. var
  1313. i,j : SizeUInt;
  1314. w : word;
  1315. begin
  1316. result:=0;
  1317. if source=nil then
  1318. exit;
  1319. i:=0;
  1320. j:=0;
  1321. if assigned(Dest) then
  1322. begin
  1323. while (i<SourceChars) and (j<MaxDestBytes) do
  1324. begin
  1325. w:=word(Source[i]);
  1326. case w of
  1327. 0..$7f:
  1328. begin
  1329. Dest[j]:=char(w);
  1330. inc(j);
  1331. end;
  1332. $80..$7ff:
  1333. begin
  1334. if j+1>=MaxDestBytes then
  1335. break;
  1336. Dest[j]:=char($c0 or (w shr 6));
  1337. Dest[j+1]:=char($80 or (w and $3f));
  1338. inc(j,2);
  1339. end;
  1340. else
  1341. begin
  1342. if j+2>=MaxDestBytes then
  1343. break;
  1344. Dest[j]:=char($e0 or (w shr 12));
  1345. Dest[j+1]:=char($80 or ((w shr 6)and $3f));
  1346. Dest[j+2]:=char($80 or (w and $3f));
  1347. inc(j,3);
  1348. end;
  1349. end;
  1350. inc(i);
  1351. end;
  1352. if j>MaxDestBytes-1 then
  1353. j:=MaxDestBytes-1;
  1354. Dest[j]:=#0;
  1355. end
  1356. else
  1357. begin
  1358. while i<SourceChars do
  1359. begin
  1360. case word(Source[i]) of
  1361. $0..$7f:
  1362. inc(j);
  1363. $80..$7ff:
  1364. inc(j,2);
  1365. else
  1366. inc(j,3);
  1367. end;
  1368. inc(i);
  1369. end;
  1370. end;
  1371. result:=j+1;
  1372. end;
  1373. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1374. begin
  1375. if assigned(Source) then
  1376. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1377. else
  1378. Result:=0;
  1379. end;
  1380. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1381. var
  1382. i,j : SizeUInt;
  1383. w: SizeUInt;
  1384. b : byte;
  1385. begin
  1386. if not assigned(Source) then
  1387. begin
  1388. result:=0;
  1389. exit;
  1390. end;
  1391. result:=SizeUInt(-1);
  1392. i:=0;
  1393. j:=0;
  1394. if assigned(Dest) then
  1395. begin
  1396. while (j<MaxDestChars) and (i<SourceBytes) do
  1397. begin
  1398. b:=byte(Source[i]);
  1399. w:=b;
  1400. inc(i);
  1401. // 2 or 3 bytes?
  1402. if b>=$80 then
  1403. begin
  1404. w:=b and $3f;
  1405. if i>=SourceBytes then
  1406. exit;
  1407. // 3 bytes?
  1408. if (b and $20)<>0 then
  1409. begin
  1410. b:=byte(Source[i]);
  1411. inc(i);
  1412. if i>=SourceBytes then
  1413. exit;
  1414. if (b and $c0)<>$80 then
  1415. exit;
  1416. w:=(w shl 6) or (b and $3f);
  1417. end;
  1418. b:=byte(Source[i]);
  1419. w:=(w shl 6) or (b and $3f);
  1420. if (b and $c0)<>$80 then
  1421. exit;
  1422. inc(i);
  1423. end;
  1424. Dest[j]:=WideChar(w);
  1425. inc(j);
  1426. end;
  1427. if j>=MaxDestChars then j:=MaxDestChars-1;
  1428. Dest[j]:=#0;
  1429. end
  1430. else
  1431. begin
  1432. while i<SourceBytes do
  1433. begin
  1434. b:=byte(Source[i]);
  1435. inc(i);
  1436. // 2 or 3 bytes?
  1437. if b>=$80 then
  1438. begin
  1439. if i>=SourceBytes then
  1440. exit;
  1441. // 3 bytes?
  1442. b := b and $3f;
  1443. if (b and $20)<>0 then
  1444. begin
  1445. b:=byte(Source[i]);
  1446. inc(i);
  1447. if i>=SourceBytes then
  1448. exit;
  1449. if (b and $c0)<>$80 then
  1450. exit;
  1451. end;
  1452. if (byte(Source[i]) and $c0)<>$80 then
  1453. exit;
  1454. inc(i);
  1455. end;
  1456. inc(j);
  1457. end;
  1458. end;
  1459. result:=j+1;
  1460. end;
  1461. function UTF8Encode(const s : WideString) : UTF8String;
  1462. var
  1463. i : SizeInt;
  1464. hs : UTF8String;
  1465. begin
  1466. result:='';
  1467. if s='' then
  1468. exit;
  1469. SetLength(hs,length(s)*3);
  1470. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  1471. if i>0 then
  1472. begin
  1473. SetLength(hs,i-1);
  1474. result:=hs;
  1475. end;
  1476. end;
  1477. function UTF8Decode(const s : UTF8String): WideString;
  1478. var
  1479. i : SizeInt;
  1480. hs : WideString;
  1481. begin
  1482. result:='';
  1483. if s='' then
  1484. exit;
  1485. SetLength(hs,length(s));
  1486. i:=Utf8ToUnicode(PWideChar(hs),length(hs)+1,pchar(s),length(s));
  1487. if i>0 then
  1488. begin
  1489. SetLength(hs,i-1);
  1490. result:=hs;
  1491. end;
  1492. end;
  1493. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  1494. begin
  1495. Result:=Utf8Encode(s);
  1496. end;
  1497. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1498. begin
  1499. Result:=Utf8Decode(s);
  1500. end;
  1501. function WideStringToUCS4String(const s : WideString) : UCS4String;
  1502. var
  1503. i : SizeInt;
  1504. begin
  1505. setlength(result,length(s)+1);
  1506. for i:=1 to length(s) do
  1507. result[i-1]:=UCS4Char(s[i]);
  1508. result[length(s)]:=UCS4Char(0);
  1509. end;
  1510. function UCS4StringToWideString(const s : UCS4String) : WideString;
  1511. var
  1512. i : SizeInt;
  1513. begin
  1514. setlength(result,length(s)-1);
  1515. for i:=1 to length(s)-1 do
  1516. result[i]:=WideChar(s[i-1]);
  1517. end;
  1518. procedure unimplementedwidestring;
  1519. begin
  1520. HandleErrorFrame(215,get_frame);
  1521. end;
  1522. {$warnings off}
  1523. function GenericWideCase(const s : WideString) : WideString;
  1524. begin
  1525. unimplementedwidestring;
  1526. end;
  1527. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  1528. begin
  1529. unimplementedwidestring;
  1530. end;
  1531. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  1532. begin
  1533. unimplementedwidestring;
  1534. end;
  1535. function CharLengthPChar(const Str: PChar): PtrInt;
  1536. begin
  1537. unimplementedwidestring;
  1538. end;
  1539. {$warnings on}
  1540. procedure initwidestringmanager;
  1541. begin
  1542. fillchar(widestringmanager,sizeof(widestringmanager),0);
  1543. {$ifndef HAS_WIDESTRINGMANAGER}
  1544. widestringmanager.Wide2AnsiMoveProc:=@defaultWide2AnsiMove;
  1545. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2WideMove;
  1546. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  1547. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  1548. {$endif HAS_WIDESTRINGMANAGER}
  1549. widestringmanager.CompareWideStringProc:=@CompareWideString;
  1550. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  1551. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  1552. end;