2
0

wstrings.inc 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880
  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. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  234. function fpc_WideStr_To_ShortStr (high_of_res: SizeInt;const S2 : WideString): shortstring;[Public, alias: 'FPC_WIDESTR_TO_SHORTSTR']; compilerproc;
  235. {
  236. Converts a WideString to a ShortString;
  237. }
  238. Var
  239. Size : SizeInt;
  240. temp : ansistring;
  241. begin
  242. result:='';
  243. Size:=Length(S2);
  244. if Size>0 then
  245. begin
  246. If Size>high_of_res then
  247. Size:=high_of_res;
  248. widestringmanager.Wide2AnsiMoveProc(PWideChar(S2),temp,Size);
  249. result:=temp;
  250. end;
  251. end;
  252. {$else FPC_STRTOSHORTSTRINGPROC}
  253. procedure fpc_WideStr_To_ShortStr (out res: ShortString;const S2 : WideString); [Public, alias: 'FPC_WIDESTR_TO_SHORTSTR'];compilerproc;
  254. {
  255. Converts a WideString to a ShortString;
  256. }
  257. Var
  258. Size : SizeInt;
  259. temp : ansistring;
  260. begin
  261. res:='';
  262. Size:=Length(S2);
  263. if Size>0 then
  264. begin
  265. If Size>high(res) then
  266. Size:=high(res);
  267. widestringmanager.Wide2AnsiMoveProc(PWideChar(S2),temp,Size);
  268. res:=temp;
  269. end;
  270. end;
  271. {$endif FPC_STRTOSHORTSTRINGPROC}
  272. Function fpc_ShortStr_To_WideStr (Const S2 : ShortString): WideString;compilerproc;
  273. {
  274. Converts a ShortString to a WideString;
  275. }
  276. Var
  277. Size : SizeInt;
  278. begin
  279. result:='';
  280. Size:=Length(S2);
  281. if Size>0 then
  282. begin
  283. widestringmanager.Ansi2WideMoveProc(PChar(@S2[1]),result,Size);
  284. { Terminating Zero }
  285. PWideChar(Pointer(fpc_ShortStr_To_WideStr)+Size*sizeof(WideChar))^:=#0;
  286. end;
  287. end;
  288. Function fpc_WideStr_To_AnsiStr (const S2 : WideString): AnsiString; compilerproc;
  289. {
  290. Converts a WideString to an AnsiString
  291. }
  292. Var
  293. Size : SizeInt;
  294. begin
  295. result:='';
  296. Size:=Length(S2);
  297. if Size>0 then
  298. widestringmanager.Wide2AnsiMoveProc(PWideChar(Pointer(S2)),result,Size);
  299. end;
  300. Function fpc_AnsiStr_To_WideStr (Const S2 : AnsiString): WideString; compilerproc;
  301. {
  302. Converts an AnsiString to a WideString;
  303. }
  304. Var
  305. Size : SizeInt;
  306. begin
  307. result:='';
  308. Size:=Length(S2);
  309. if Size>0 then
  310. widestringmanager.Ansi2WideMoveProc(PChar(S2),result,Size);
  311. end;
  312. Function fpc_PWideChar_To_AnsiStr(const p : pwidechar): ansistring; compilerproc;
  313. var
  314. Size : SizeInt;
  315. begin
  316. result:='';
  317. if p=nil then
  318. exit;
  319. Size := IndexWord(p^, -1, 0);
  320. if Size>0 then
  321. widestringmanager.Wide2AnsiMoveProc(P,result,Size);
  322. end;
  323. Function fpc_PWideChar_To_WideStr(const p : pwidechar): widestring; compilerproc;
  324. var
  325. Size : SizeInt;
  326. begin
  327. result:='';
  328. if p=nil then
  329. exit;
  330. Size := IndexWord(p^, -1, 0);
  331. Setlength(result,Size);
  332. if Size>0 then
  333. begin
  334. Move(p^,PWideChar(Pointer(result))^,Size*sizeof(WideChar));
  335. { Terminating Zero }
  336. PWideChar(Pointer(result)+Size*sizeof(WideChar))^:=#0;
  337. end;
  338. end;
  339. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  340. Function fpc_PWideChar_To_ShortStr(const p : pwidechar): shortstring; compilerproc;
  341. var
  342. Size : SizeInt;
  343. temp: ansistring;
  344. begin
  345. result:='';
  346. if p=nil then
  347. exit;
  348. Size := IndexWord(p^, $7fffffff, 0);
  349. if Size>0 then
  350. begin
  351. widestringmanager.Wide2AnsiMoveProc(p,temp,Size);
  352. result:=temp;
  353. end;
  354. end;
  355. {$else FPC_STRTOSHORTSTRINGPROC}
  356. procedure fpc_PWideChar_To_ShortStr(out res : shortstring;const p : pwidechar); compilerproc;
  357. var
  358. Size : SizeInt;
  359. temp: ansistring;
  360. begin
  361. res:='';
  362. if p=nil then
  363. exit;
  364. Size:=IndexWord(p^, high(PtrInt), 0);
  365. if Size>0 then
  366. begin
  367. widestringmanager.Wide2AnsiMoveProc(p,temp,Size);
  368. res:=temp;
  369. end;
  370. end;
  371. {$endif FPC_STRTOSHORTSTRINGPROC}
  372. { checked against the ansistring routine, 2001-05-27 (FK) }
  373. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[Public,Alias:'FPC_WIDESTR_ASSIGN']; compilerproc;
  374. {
  375. Assigns S2 to S1 (S1:=S2), taking in account reference counts.
  376. }
  377. begin
  378. {$ifdef FPC_WINLIKEWIDESTRING}
  379. if S1=S2 then exit;
  380. if S2<>nil then
  381. begin
  382. if IsWideStringConstant(S1) then
  383. begin
  384. S1:=NewWidestring(length(WideString(S2)));
  385. move(s2^,s1^,(length(WideString(s1))+1)*sizeof(widechar));
  386. end
  387. else
  388. {$ifdef MSWINDOWS}
  389. if winwidestringalloc then
  390. begin
  391. if SysReAllocStringLen(S1, S2, Length(WideString(S2))) = 0 then
  392. WideStringError;
  393. end
  394. else
  395. {$endif MSWINDOWS}
  396. begin
  397. SetLength(WideString(S1),length(WideString(S2)));
  398. move(s2^,s1^,(length(WideString(s1))+1)*sizeof(widechar));
  399. end;
  400. end
  401. else
  402. begin
  403. { Free S1 }
  404. fpc_widestr_decr_ref (S1);
  405. S1:=nil;
  406. end;
  407. {$else FPC_WINLIKEWIDESTRING}
  408. If S2<>nil then
  409. If PWideRec(S2-WideFirstOff)^.Ref>0 then
  410. inclocked(PWideRec(S2-WideFirstOff)^.ref);
  411. { Decrease the reference count on the old S1 }
  412. fpc_widestr_decr_ref (S1);
  413. s1:=s2;
  414. {$endif FPC_WINLIKEWIDESTRING}
  415. end;
  416. { alias for internal use }
  417. Procedure fpc_WideStr_Assign (Var S1 : Pointer;S2 : Pointer);[external name 'FPC_WIDESTR_ASSIGN'];
  418. {$ifndef STR_CONCAT_PROCS}
  419. function fpc_WideStr_Concat (const S1,S2 : WideString): WideString; compilerproc;
  420. Var
  421. Size,Location : SizeInt;
  422. pc : pwidechar;
  423. begin
  424. { only assign if s1 or s2 is empty }
  425. if (S1='') then
  426. begin
  427. result:=s2;
  428. exit;
  429. end;
  430. if (S2='') then
  431. begin
  432. result:=s1;
  433. exit;
  434. end;
  435. Location:=Length(S1);
  436. Size:=length(S2);
  437. SetLength(result,Size+Location);
  438. pc:=pwidechar(result);
  439. Move(S1[1],pc^,Location*sizeof(WideChar));
  440. inc(pc,location);
  441. Move(S2[1],pc^,(Size+1)*sizeof(WideChar));
  442. end;
  443. function fpc_WideStr_Concat_multi (const sarr:array of Widestring): widestring; compilerproc;
  444. Var
  445. i : Longint;
  446. p : pointer;
  447. pc : pwidechar;
  448. Size,NewSize : SizeInt;
  449. begin
  450. { First calculate size of the result so we can do
  451. a single call to SetLength() }
  452. NewSize:=0;
  453. for i:=low(sarr) to high(sarr) do
  454. inc(Newsize,length(sarr[i]));
  455. SetLength(result,NewSize);
  456. pc:=pwidechar(result);
  457. for i:=low(sarr) to high(sarr) do
  458. begin
  459. p:=pointer(sarr[i]);
  460. if assigned(p) then
  461. begin
  462. Size:=length(widestring(p));
  463. Move(pwidechar(p)^,pc^,(Size+1)*sizeof(WideChar));
  464. inc(pc,size);
  465. end;
  466. end;
  467. end;
  468. {$else STR_CONCAT_PROCS}
  469. procedure fpc_WideStr_Concat (var DestS:Widestring;const S1,S2 : WideString); compilerproc;
  470. Var
  471. Size,Location : SizeInt;
  472. same : boolean;
  473. begin
  474. { only assign if s1 or s2 is empty }
  475. if (S1='') then
  476. begin
  477. DestS:=s2;
  478. exit;
  479. end;
  480. if (S2='') then
  481. begin
  482. DestS:=s1;
  483. exit;
  484. end;
  485. Location:=Length(S1);
  486. Size:=length(S2);
  487. { Use Pointer() typecasts to prevent extra conversion code }
  488. if Pointer(DestS)=Pointer(S1) then
  489. begin
  490. same:=Pointer(S1)=Pointer(S2);
  491. SetLength(DestS,Size+Location);
  492. if same then
  493. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size)*sizeof(WideChar))
  494. else
  495. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  496. end
  497. else if Pointer(DestS)=Pointer(S2) then
  498. begin
  499. SetLength(DestS,Size+Location);
  500. Move(Pointer(DestS)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  501. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(WideChar));
  502. end
  503. else
  504. begin
  505. DestS:='';
  506. SetLength(DestS,Size+Location);
  507. Move(Pointer(S1)^,Pointer(DestS)^,Location*sizeof(WideChar));
  508. Move(Pointer(S2)^,(Pointer(DestS)+Location*sizeof(WideChar))^,(Size+1)*sizeof(WideChar));
  509. end;
  510. end;
  511. procedure fpc_WideStr_Concat_multi (var DestS:Widestring;const sarr:array of Widestring); compilerproc;
  512. Var
  513. lowstart,i : Longint;
  514. p,pc : pointer;
  515. Size,NewLen,
  516. OldDestLen : SizeInt;
  517. destcopy : widestring;
  518. begin
  519. if high(sarr)=0 then
  520. begin
  521. DestS:='';
  522. exit;
  523. end;
  524. lowstart:=low(sarr);
  525. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  526. inc(lowstart);
  527. { Check for another reuse, then we can't use
  528. the append optimization }
  529. for i:=lowstart to high(sarr) do
  530. begin
  531. if Pointer(DestS)=Pointer(sarr[i]) then
  532. begin
  533. { if DestS is used somewhere in the middle of the expression,
  534. we need to make sure the original string still exists after
  535. we empty/modify DestS }
  536. destcopy:=dests;
  537. lowstart:=low(sarr);
  538. break;
  539. end;
  540. end;
  541. { Start with empty DestS if we start with concatting
  542. the first array element }
  543. if lowstart=low(sarr) then
  544. DestS:='';
  545. OldDestLen:=length(DestS);
  546. { Calculate size of the result so we can do
  547. a single call to SetLength() }
  548. NewLen:=0;
  549. for i:=low(sarr) to high(sarr) do
  550. inc(NewLen,length(sarr[i]));
  551. SetLength(DestS,NewLen);
  552. { Concat all strings, except the string we already
  553. copied in DestS }
  554. pc:=Pointer(DestS)+OldDestLen*sizeof(WideChar);
  555. for i:=lowstart to high(sarr) do
  556. begin
  557. p:=pointer(sarr[i]);
  558. if assigned(p) then
  559. begin
  560. Size:=length(widestring(p));
  561. Move(p^,pc^,(Size+1)*sizeof(WideChar));
  562. inc(pc,size*sizeof(WideChar));
  563. end;
  564. end;
  565. end;
  566. {$endif STR_CONCAT_PROCS}
  567. Function fpc_Char_To_WChar(const c : Char): WideChar; compilerproc;
  568. var
  569. w: widestring;
  570. begin
  571. widestringmanager.Ansi2WideMoveProc(@c, w, 1);
  572. fpc_Char_To_WChar:= w[1];
  573. end;
  574. Function fpc_Char_To_WideStr(const c : Char): WideString; compilerproc;
  575. {
  576. Converts a Char to a WideString;
  577. }
  578. begin
  579. Setlength(fpc_Char_To_WideStr,1);
  580. fpc_Char_To_WideStr[1]:=c;
  581. { Terminating Zero }
  582. PWideChar(Pointer(fpc_Char_To_WideStr)+sizeof(WideChar))^:=#0;
  583. end;
  584. Function fpc_WChar_To_Char(const c : WideChar): Char; compilerproc;
  585. {
  586. Converts a WideChar to a Char;
  587. }
  588. var
  589. s: ansistring;
  590. begin
  591. widestringmanager.Wide2AnsiMoveProc(@c, s, 1);
  592. if length(s)=1 then
  593. fpc_WChar_To_Char:= s[1]
  594. else
  595. fpc_WChar_To_Char:='?';
  596. end;
  597. Function fpc_WChar_To_WideStr(const c : WideChar): WideString; compilerproc;
  598. {
  599. Converts a WideChar to a WideString;
  600. }
  601. begin
  602. Setlength (fpc_WChar_To_WideStr,1);
  603. fpc_WChar_To_WideStr[1]:= c;
  604. end;
  605. Function fpc_WChar_To_AnsiStr(const c : WideChar): AnsiString; compilerproc;
  606. {
  607. Converts a WideChar to a AnsiString;
  608. }
  609. begin
  610. widestringmanager.Wide2AnsiMoveProc(@c, fpc_WChar_To_AnsiStr, 1);
  611. end;
  612. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  613. Function fpc_WChar_To_ShortStr(const c : WideChar): ShortString; compilerproc;
  614. {
  615. Converts a WideChar to a ShortString;
  616. }
  617. var
  618. s: ansistring;
  619. begin
  620. widestringmanager.Wide2AnsiMoveProc(@c, s, 1);
  621. fpc_WChar_To_ShortStr:= s;
  622. end;
  623. {$else FPC_STRTOSHORTSTRINGPROC}
  624. procedure fpc_WChar_To_ShortStr(out res : shortstring;const c : WideChar) compilerproc;
  625. {
  626. Converts a WideChar to a ShortString;
  627. }
  628. var
  629. s: ansistring;
  630. begin
  631. widestringmanager.Wide2AnsiMoveProc(@c,s,1);
  632. res:=s;
  633. end;
  634. {$endif FPC_STRTOSHORTSTRINGPROC}
  635. Function fpc_PChar_To_WideStr(const p : pchar): WideString; compilerproc;
  636. Var
  637. L : SizeInt;
  638. begin
  639. if (not assigned(p)) or (p[0]=#0) Then
  640. { result is automatically set to '' }
  641. exit;
  642. l:=IndexChar(p^,-1,#0);
  643. widestringmanager.Ansi2WideMoveProc(P,fpc_PChar_To_WideStr,l);
  644. end;
  645. Function fpc_CharArray_To_WideStr(const arr: array of char; zerobased: boolean = true): WideString; compilerproc;
  646. var
  647. i : SizeInt;
  648. begin
  649. if (zerobased) then
  650. begin
  651. if (arr[0]=#0) Then
  652. { result is automatically set to '' }
  653. exit;
  654. i:=IndexChar(arr,high(arr)+1,#0);
  655. if i = -1 then
  656. i := high(arr)+1;
  657. end
  658. else
  659. i := high(arr)+1;
  660. SetLength(fpc_CharArray_To_WideStr,i);
  661. widestringmanager.Ansi2WideMoveProc (pchar(@arr),fpc_CharArray_To_WideStr,i);
  662. end;
  663. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  664. function fpc_WideCharArray_To_ShortStr(const arr: array of widechar; zerobased: boolean = true): shortstring;[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  665. var
  666. l: longint;
  667. index: longint;
  668. len: byte;
  669. temp: ansistring;
  670. begin
  671. l := high(arr)+1;
  672. if l>=256 then
  673. l:=255
  674. else if l<0 then
  675. l:=0;
  676. if zerobased then
  677. begin
  678. index:=IndexWord(arr[0],l,0);
  679. if (index < 0) then
  680. len := l
  681. else
  682. len := index;
  683. end
  684. else
  685. len := l;
  686. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,len);
  687. fpc_WideCharArray_To_ShortStr := temp;
  688. end;
  689. {$else FPC_STRTOSHORTSTRINGPROC}
  690. procedure fpc_WideCharArray_To_ShortStr(out res : shortstring;const arr: array of widechar; zerobased: boolean = true);[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  691. var
  692. l: longint;
  693. index: ptrint;
  694. len: byte;
  695. temp: ansistring;
  696. begin
  697. l := high(arr)+1;
  698. if l>=high(res)+1 then
  699. l:=high(res)
  700. else if l<0 then
  701. l:=0;
  702. if zerobased then
  703. begin
  704. index:=IndexWord(arr[0],l,0);
  705. if index<0 then
  706. len:=l
  707. else
  708. len:=index;
  709. end
  710. else
  711. len:=l;
  712. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,len);
  713. res:=temp;
  714. end;
  715. {$endif FPC_STRTOSHORTSTRINGPROC}
  716. Function fpc_WideCharArray_To_AnsiStr(const arr: array of widechar; zerobased: boolean = true): AnsiString; compilerproc;
  717. var
  718. i : SizeInt;
  719. begin
  720. if (zerobased) then
  721. begin
  722. i:=IndexWord(arr,high(arr)+1,0);
  723. if i = -1 then
  724. i := high(arr)+1;
  725. end
  726. else
  727. i := high(arr)+1;
  728. SetLength(fpc_WideCharArray_To_AnsiStr,i);
  729. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),fpc_WideCharArray_To_AnsiStr,i);
  730. end;
  731. Function fpc_WideCharArray_To_WideStr(const arr: array of widechar; zerobased: boolean = true): WideString; compilerproc;
  732. var
  733. i : SizeInt;
  734. begin
  735. if (zerobased) then
  736. begin
  737. i:=IndexWord(arr,high(arr)+1,0);
  738. if i = -1 then
  739. i := high(arr)+1;
  740. end
  741. else
  742. i := high(arr)+1;
  743. SetLength(fpc_WideCharArray_To_WideStr,i);
  744. Move(arr[0], Pointer(fpc_WideCharArray_To_WideStr)^,i*sizeof(WideChar));
  745. end;
  746. {$ifndef FPC_STRTOCHARARRAYPROC}
  747. { inside the compiler, the resulttype is modified to that of the actual }
  748. { chararray we're converting to (JM) }
  749. function fpc_widestr_to_chararray(arraysize: SizeInt; const src: WideString): fpc_big_chararray;[public,alias: 'FPC_WIDESTR_TO_CHARARRAY']; compilerproc;
  750. var
  751. len: SizeInt;
  752. temp: ansistring;
  753. begin
  754. len := length(src);
  755. { make sure we don't dereference src if it can be nil (JM) }
  756. if len > 0 then
  757. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,len);
  758. len := length(temp);
  759. if len > arraysize then
  760. len := arraysize;
  761. {$r-}
  762. move(temp[1],fpc_widestr_to_chararray[0],len);
  763. fillchar(fpc_widestr_to_chararray[len],arraysize-len,0);
  764. {$ifdef RangeCheckWasOn}
  765. {$r+}
  766. {$endif}
  767. end;
  768. { inside the compiler, the resulttype is modified to that of the actual }
  769. { widechararray we're converting to (JM) }
  770. function fpc_widestr_to_widechararray(arraysize: SizeInt; const src: WideString): fpc_big_widechararray;[public,alias: 'FPC_WIDESTR_TO_WIDECHARARRAY']; compilerproc;
  771. var
  772. len: SizeInt;
  773. begin
  774. len := length(src);
  775. if len > arraysize then
  776. len := arraysize;
  777. {$r-}
  778. { make sure we don't try to access element 1 of the ansistring if it's nil }
  779. if len > 0 then
  780. move(src[1],fpc_widestr_to_widechararray[0],len*SizeOf(WideChar));
  781. fillchar(fpc_widestr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  782. {$ifdef RangeCheckWasOn}
  783. {$r+}
  784. {$endif}
  785. end;
  786. { inside the compiler, the resulttype is modified to that of the actual }
  787. { chararray we're converting to (JM) }
  788. function fpc_ansistr_to_widechararray(arraysize: SizeInt; const src: AnsiString): fpc_big_widechararray;[public,alias: 'FPC_ANSISTR_TO_WIDECHARARRAY']; compilerproc;
  789. var
  790. len: SizeInt;
  791. temp: widestring;
  792. begin
  793. len := length(src);
  794. { make sure we don't dereference src if it can be nil (JM) }
  795. if len > 0 then
  796. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  797. len := length(temp);
  798. if len > arraysize then
  799. len := arraysize;
  800. {$r-}
  801. move(temp[1],fpc_ansistr_to_widechararray[0],len*sizeof(widechar));
  802. fillchar(fpc_ansistr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  803. {$ifdef RangeCheckWasOn}
  804. {$r+}
  805. {$endif}
  806. end;
  807. function fpc_shortstr_to_widechararray(arraysize: SizeInt; const src: ShortString): fpc_big_widechararray;[public,alias: 'FPC_SHORTSTR_TO_WIDECHARARRAY']; compilerproc;
  808. var
  809. len: longint;
  810. temp : widestring;
  811. begin
  812. len := length(src);
  813. { make sure we don't access char 1 if length is 0 (JM) }
  814. if len > 0 then
  815. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  816. len := length(temp);
  817. if len > arraysize then
  818. len := arraysize;
  819. {$r-}
  820. move(temp[1],fpc_shortstr_to_widechararray[0],len*sizeof(widechar));
  821. fillchar(fpc_shortstr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  822. {$ifdef RangeCheckWasOn}
  823. {$r+}
  824. {$endif}
  825. end;
  826. {$else ndef FPC_STRTOCHARARRAYPROC}
  827. procedure fpc_widestr_to_chararray(out res: array of char; const src: WideString); compilerproc;
  828. var
  829. len: SizeInt;
  830. temp: ansistring;
  831. begin
  832. len := length(src);
  833. { make sure we don't dereference src if it can be nil (JM) }
  834. if len > 0 then
  835. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,len);
  836. len := length(temp);
  837. if len > length(res) then
  838. len := length(res);
  839. {$r-}
  840. move(temp[1],res[0],len);
  841. fillchar(res[len],length(res)-len,0);
  842. {$ifdef RangeCheckWasOn}
  843. {$r+}
  844. {$endif}
  845. end;
  846. procedure fpc_widestr_to_widechararray(out res: array of widechar; const src: WideString); compilerproc;
  847. var
  848. len: SizeInt;
  849. begin
  850. len := length(src);
  851. if len > length(res) then
  852. len := length(res);
  853. {$r-}
  854. { make sure we don't try to access element 1 of the ansistring if it's nil }
  855. if len > 0 then
  856. move(src[1],res[0],len*SizeOf(WideChar));
  857. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  858. {$ifdef RangeCheckWasOn}
  859. {$r+}
  860. {$endif}
  861. end;
  862. procedure fpc_ansistr_to_widechararray(out res: array of widechar; const src: AnsiString); compilerproc;
  863. var
  864. len: SizeInt;
  865. temp: widestring;
  866. begin
  867. len := length(src);
  868. { make sure we don't dereference src if it can be nil (JM) }
  869. if len > 0 then
  870. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  871. len := length(temp);
  872. if len > length(res) then
  873. len := length(res);
  874. {$r-}
  875. move(temp[1],res[0],len*sizeof(widechar));
  876. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  877. {$ifdef RangeCheckWasOn}
  878. {$r+}
  879. {$endif}
  880. end;
  881. procedure fpc_shortstr_to_widechararray(out res: array of widechar; const src: ShortString); compilerproc;
  882. var
  883. len: longint;
  884. temp : widestring;
  885. begin
  886. len := length(src);
  887. { make sure we don't access char 1 if length is 0 (JM) }
  888. if len > 0 then
  889. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  890. len := length(temp);
  891. if len > length(res) then
  892. len := length(res);
  893. {$r-}
  894. move(temp[1],res[0],len*sizeof(widechar));
  895. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  896. {$ifdef RangeCheckWasOn}
  897. {$r+}
  898. {$endif}
  899. end;
  900. {$endif ndef FPC_STRTOCHARARRAYPROC}
  901. Function fpc_WideStr_Compare(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE']; compilerproc;
  902. {
  903. Compares 2 WideStrings;
  904. The result is
  905. <0 if S1<S2
  906. 0 if S1=S2
  907. >0 if S1>S2
  908. }
  909. Var
  910. MaxI,Temp : SizeInt;
  911. begin
  912. if pointer(S1)=pointer(S2) then
  913. begin
  914. fpc_WideStr_Compare:=0;
  915. exit;
  916. end;
  917. Maxi:=Length(S1);
  918. temp:=Length(S2);
  919. If MaxI>Temp then
  920. MaxI:=Temp;
  921. Temp:=CompareWord(S1[1],S2[1],MaxI);
  922. if temp=0 then
  923. temp:=Length(S1)-Length(S2);
  924. fpc_WideStr_Compare:=Temp;
  925. end;
  926. Function fpc_WideStr_Compare_Equal(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE_EQUAL']; compilerproc;
  927. {
  928. Compares 2 WideStrings for equality only;
  929. The result is
  930. 0 if S1=S2
  931. <>0 if S1<>S2
  932. }
  933. Var
  934. MaxI : SizeInt;
  935. begin
  936. if pointer(S1)=pointer(S2) then
  937. exit(0);
  938. Maxi:=Length(S1);
  939. If MaxI<>Length(S2) then
  940. exit(-1)
  941. else
  942. exit(CompareWord(S1[1],S2[1],MaxI));
  943. end;
  944. Procedure fpc_WideStr_CheckZero(p : pointer);[Public,Alias : 'FPC_WIDESTR_CHECKZERO']; compilerproc;
  945. begin
  946. if p=nil then
  947. HandleErrorFrame(201,get_frame);
  948. end;
  949. Procedure fpc_WideStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; compilerproc;
  950. begin
  951. if (index>len div 2) or (Index<1) then
  952. HandleErrorFrame(201,get_frame);
  953. end;
  954. Procedure fpc_WideStr_SetLength(Var S : WideString; l : SizeInt);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; compilerproc;
  955. {
  956. Sets The length of string S to L.
  957. Makes sure S is unique, and contains enough room.
  958. }
  959. Var
  960. Temp : Pointer;
  961. movelen: SizeInt;
  962. begin
  963. if (l>0) then
  964. begin
  965. if Pointer(S)=nil then
  966. begin
  967. { Need a complete new string...}
  968. Pointer(s):=NewWideString(l);
  969. end
  970. { windows doesn't support reallocing widestrings, this code
  971. is anyways subject to be removed because widestrings shouldn't be
  972. ref. counted anymore (FK) }
  973. else
  974. if
  975. {$ifdef MSWINDOWS}
  976. not winwidestringalloc and
  977. {$endif MSWINDOWS}
  978. {$ifdef FPC_WINLIKEWIDESTRING}
  979. not IsWideStringConstant(pointer(S))
  980. {$else}
  981. (PWideRec(Pointer(S)-WideFirstOff)^.Ref = 1)
  982. {$endif FPC_WINLIKEWIDESTRING}
  983. then
  984. begin
  985. Dec(Pointer(S),WideFirstOff);
  986. if SizeUInt(L*sizeof(WideChar)+WideRecLen)>MemSize(Pointer(S)) then
  987. reallocmem(pointer(S), L*sizeof(WideChar)+WideRecLen);
  988. Inc(Pointer(S), WideFirstOff);
  989. end
  990. else
  991. begin
  992. { Reallocation is needed... }
  993. Temp:=Pointer(NewWideString(L));
  994. if Length(S)>0 then
  995. begin
  996. if l < succ(length(s)) then
  997. movelen := l
  998. { also move terminating null }
  999. else
  1000. movelen := succ(length(s));
  1001. Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
  1002. end;
  1003. fpc_widestr_decr_ref(Pointer(S));
  1004. Pointer(S):=Temp;
  1005. end;
  1006. { Force nil termination in case it gets shorter }
  1007. PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
  1008. {$ifdef MSWINDOWS}
  1009. if not winwidestringalloc then
  1010. {$endif MSWINDOWS}
  1011. PWideRec(Pointer(S)-WideFirstOff)^.Len:=l*sizeof(WideChar);
  1012. end
  1013. else
  1014. begin
  1015. { Length=0 }
  1016. if Pointer(S)<>nil then
  1017. fpc_widestr_decr_ref (Pointer(S));
  1018. Pointer(S):=Nil;
  1019. end;
  1020. end;
  1021. {*****************************************************************************
  1022. Public functions, In interface.
  1023. *****************************************************************************}
  1024. function WideCharToString(S : PWideChar) : AnsiString;
  1025. begin
  1026. result:=WideCharLenToString(s,Length(WideString(s)));
  1027. end;
  1028. function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  1029. var
  1030. temp:widestring;
  1031. begin
  1032. widestringmanager.Ansi2WideMoveProc(PChar(Src),temp,Length(Src));
  1033. if Length(temp)<DestSize then
  1034. move(temp[1],Dest^,Length(temp)*SizeOf(WideChar))
  1035. else
  1036. move(temp[1],Dest^,(DestSize-1)*SizeOf(WideChar));
  1037. Dest[DestSize-1]:=#0;
  1038. result:=Dest;
  1039. end;
  1040. function WideCharLenToString(S : PWideChar;Len : SizeInt) : AnsiString;
  1041. begin
  1042. //SetLength(result,Len);
  1043. widestringmanager.Wide2AnsiMoveproc(S,result,Len);
  1044. end;
  1045. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : AnsiString);
  1046. begin
  1047. Dest:=WideCharLenToString(Src,Len);
  1048. end;
  1049. procedure WideCharToStrVar(S : PWideChar;out Dest : AnsiString);
  1050. begin
  1051. Dest:=WideCharToString(S);
  1052. end;
  1053. Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; compilerproc;
  1054. {$ifdef FPC_WINLIKEWIDESTRING}
  1055. begin
  1056. pointer(result) := pointer(s);
  1057. end;
  1058. {$else FPC_WINLIKEWIDESTRING}
  1059. {
  1060. Make sure reference count of S is 1,
  1061. using copy-on-write semantics.
  1062. }
  1063. Var
  1064. SNew : Pointer;
  1065. L : SizeInt;
  1066. begin
  1067. pointer(result) := pointer(s);
  1068. If Pointer(S)=Nil then
  1069. exit;
  1070. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  1071. begin
  1072. L:=PWideRec(Pointer(S)-WideFirstOff)^.len div sizeof(WideChar);
  1073. SNew:=NewWideString (L);
  1074. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  1075. PWideRec(SNew-WideFirstOff)^.len:=L * sizeof(WideChar);
  1076. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  1077. pointer(S):=SNew;
  1078. pointer(result):=SNew;
  1079. end;
  1080. end;
  1081. {$endif FPC_WINLIKEWIDESTRING}
  1082. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : SizeInt) : WideString;compilerproc;
  1083. var
  1084. ResultAddress : Pointer;
  1085. begin
  1086. ResultAddress:=Nil;
  1087. dec(index);
  1088. if Index < 0 then
  1089. Index := 0;
  1090. { Check Size. Accounts for Zero-length S, the double check is needed because
  1091. Size can be maxint and will get <0 when adding index }
  1092. if (Size>Length(S)) or
  1093. (Index+Size>Length(S)) then
  1094. Size:=Length(S)-Index;
  1095. If Size>0 then
  1096. begin
  1097. If Index<0 Then
  1098. Index:=0;
  1099. ResultAddress:=Pointer(NewWideString (Size));
  1100. if ResultAddress<>Nil then
  1101. begin
  1102. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  1103. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size*sizeof(WideChar);
  1104. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  1105. end;
  1106. end;
  1107. Pointer(fpc_widestr_Copy):=ResultAddress;
  1108. end;
  1109. Function Pos (Const Substr : WideString; Const Source : WideString) : SizeInt;
  1110. var
  1111. i,MaxLen : SizeInt;
  1112. pc : pwidechar;
  1113. begin
  1114. Pos:=0;
  1115. if Length(SubStr)>0 then
  1116. begin
  1117. MaxLen:=Length(source)-Length(SubStr);
  1118. i:=0;
  1119. pc:=@source[1];
  1120. while (i<=MaxLen) do
  1121. begin
  1122. inc(i);
  1123. if (SubStr[1]=pc^) and
  1124. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  1125. begin
  1126. Pos:=i;
  1127. exit;
  1128. end;
  1129. inc(pc);
  1130. end;
  1131. end;
  1132. end;
  1133. { Faster version for a widechar alone }
  1134. Function Pos (c : WideChar; Const s : WideString) : SizeInt;
  1135. var
  1136. i: SizeInt;
  1137. pc : pwidechar;
  1138. begin
  1139. pc:=@s[1];
  1140. for i:=1 to length(s) do
  1141. begin
  1142. if pc^=c then
  1143. begin
  1144. pos:=i;
  1145. exit;
  1146. end;
  1147. inc(pc);
  1148. end;
  1149. pos:=0;
  1150. end;
  1151. Function Pos (c : WideChar; Const s : AnsiString) : SizeInt;
  1152. var
  1153. i: SizeInt;
  1154. pc : pchar;
  1155. begin
  1156. pc:=@s[1];
  1157. for i:=1 to length(s) do
  1158. begin
  1159. if widechar(pc^)=c then
  1160. begin
  1161. pos:=i;
  1162. exit;
  1163. end;
  1164. inc(pc);
  1165. end;
  1166. pos:=0;
  1167. end;
  1168. Function Pos (c : AnsiString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1169. begin
  1170. result:=Pos(WideString(c),s);
  1171. end;
  1172. Function Pos (c : ShortString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1173. begin
  1174. result:=Pos(WideString(c),s);
  1175. end;
  1176. Function Pos (c : WideString; Const s : AnsiString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1177. begin
  1178. result:=Pos(c,WideString(s));
  1179. end;
  1180. { Faster version for a char alone. Must be implemented because }
  1181. { pos(c: char; const s: shortstring) also exists, so otherwise }
  1182. { using pos(char,pchar) will always call the shortstring version }
  1183. { (exact match for first argument), also with $h+ (JM) }
  1184. Function Pos (c : Char; Const s : WideString) : SizeInt;
  1185. var
  1186. i: SizeInt;
  1187. wc : widechar;
  1188. pc : pwidechar;
  1189. begin
  1190. wc:=c;
  1191. pc:=@s[1];
  1192. for i:=1 to length(s) do
  1193. begin
  1194. if pc^=wc then
  1195. begin
  1196. pos:=i;
  1197. exit;
  1198. end;
  1199. inc(pc);
  1200. end;
  1201. pos:=0;
  1202. end;
  1203. Procedure Delete (Var S : WideString; Index,Size: SizeInt);
  1204. Var
  1205. LS : SizeInt;
  1206. begin
  1207. If Length(S)=0 then
  1208. exit;
  1209. if index<=0 then
  1210. exit;
  1211. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len div sizeof(WideChar);
  1212. if (Index<=LS) and (Size>0) then
  1213. begin
  1214. UniqueString (S);
  1215. if Size+Index>LS then
  1216. Size:=LS-Index+1;
  1217. if Index+Size<=LS then
  1218. begin
  1219. Dec(Index);
  1220. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  1221. end;
  1222. Setlength(s,LS-Size);
  1223. end;
  1224. end;
  1225. Procedure Insert (Const Source : WideString; Var S : WideString; Index : SizeInt);
  1226. var
  1227. Temp : WideString;
  1228. LS : SizeInt;
  1229. begin
  1230. If Length(Source)=0 then
  1231. exit;
  1232. if index <= 0 then
  1233. index := 1;
  1234. Ls:=Length(S);
  1235. if index > LS then
  1236. index := LS+1;
  1237. Dec(Index);
  1238. Pointer(Temp) := NewWideString(Length(Source)+LS);
  1239. SetLength(Temp,Length(Source)+LS);
  1240. If Index>0 then
  1241. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  1242. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  1243. If (LS-Index)>0 then
  1244. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  1245. S:=Temp;
  1246. end;
  1247. function UpCase(const s : WideString) : WideString;
  1248. begin
  1249. result:=widestringmanager.UpperWideStringProc(s);
  1250. end;
  1251. Procedure SetString (Out S : WideString; Buf : PWideChar; Len : SizeInt);
  1252. var
  1253. BufLen: SizeInt;
  1254. begin
  1255. SetLength(S,Len);
  1256. If (Buf<>Nil) and (Len>0) then
  1257. begin
  1258. BufLen := IndexWord(Buf^, Len+1, 0);
  1259. If (BufLen>0) and (BufLen < Len) then
  1260. Len := BufLen;
  1261. Move (Buf[0],S[1],Len*sizeof(WideChar));
  1262. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1263. end;
  1264. end;
  1265. Procedure SetString (Out S : WideString; Buf : PChar; Len : SizeInt);
  1266. var
  1267. BufLen: SizeInt;
  1268. begin
  1269. SetLength(S,Len);
  1270. If (Buf<>Nil) and (Len>0) then
  1271. begin
  1272. BufLen := IndexByte(Buf^, Len+1, 0);
  1273. If (BufLen>0) and (BufLen < Len) then
  1274. Len := BufLen;
  1275. widestringmanager.Ansi2WideMoveProc(Buf,S,Len);
  1276. //PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1277. end;
  1278. end;
  1279. Function fpc_Val_Real_WideStr(Const S : WideString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  1280. Var
  1281. SS : String;
  1282. begin
  1283. fpc_Val_Real_WideStr := 0;
  1284. if length(S) > 255 then
  1285. code := 256
  1286. else
  1287. begin
  1288. SS := S;
  1289. Val(SS,fpc_Val_Real_WideStr,code);
  1290. end;
  1291. end;
  1292. function fpc_val_enum_widestr(str2ordindex:pointer;const s:widestring;out code:valsint):longint;compilerproc;
  1293. var ss:shortstring;
  1294. begin
  1295. if length(s)>255 then
  1296. code:=256
  1297. else
  1298. begin
  1299. ss:=s;
  1300. val(ss,fpc_val_enum_widestr,code);
  1301. end;
  1302. end;
  1303. Function fpc_Val_Currency_WideStr(Const S : WideString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_WIDESTR']; compilerproc;
  1304. Var
  1305. SS : String;
  1306. begin
  1307. if length(S) > 255 then
  1308. begin
  1309. fpc_Val_Currency_WideStr:=0;
  1310. code := 256;
  1311. end
  1312. else
  1313. begin
  1314. SS := S;
  1315. Val(SS,fpc_Val_Currency_WideStr,code);
  1316. end;
  1317. end;
  1318. Function fpc_Val_UInt_WideStr (Const S : WideString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  1319. Var
  1320. SS : ShortString;
  1321. begin
  1322. fpc_Val_UInt_WideStr := 0;
  1323. if length(S) > 255 then
  1324. code := 256
  1325. else
  1326. begin
  1327. SS := S;
  1328. Val(SS,fpc_Val_UInt_WideStr,code);
  1329. end;
  1330. end;
  1331. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  1332. Var
  1333. SS : ShortString;
  1334. begin
  1335. fpc_Val_SInt_WideStr:=0;
  1336. if length(S)>255 then
  1337. code:=256
  1338. else
  1339. begin
  1340. SS := S;
  1341. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1342. end;
  1343. end;
  1344. {$ifndef CPU64}
  1345. Function fpc_Val_qword_WideStr (Const S : WideString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  1346. Var
  1347. SS : ShortString;
  1348. begin
  1349. fpc_Val_qword_WideStr:=0;
  1350. if length(S)>255 then
  1351. code:=256
  1352. else
  1353. begin
  1354. SS := S;
  1355. Val(SS,fpc_Val_qword_WideStr,Code);
  1356. end;
  1357. end;
  1358. Function fpc_Val_int64_WideStr (Const S : WideString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  1359. Var
  1360. SS : ShortString;
  1361. begin
  1362. fpc_Val_int64_WideStr:=0;
  1363. if length(S)>255 then
  1364. code:=256
  1365. else
  1366. begin
  1367. SS := S;
  1368. Val(SS,fpc_Val_int64_WideStr,Code);
  1369. end;
  1370. end;
  1371. {$endif CPU64}
  1372. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : WideString);compilerproc;
  1373. var
  1374. ss : shortstring;
  1375. begin
  1376. str_real(len,fr,d,treal_type(rt),ss);
  1377. s:=ss;
  1378. end;
  1379. procedure fpc_widestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:widestring);compilerproc;
  1380. var ss:shortstring;
  1381. begin
  1382. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1383. s:=ss;
  1384. end;
  1385. {$ifdef FPC_HAS_STR_CURRENCY}
  1386. procedure fpc_WideStr_Currency(c : Currency;len,fr : SizeInt;out s : WideString);compilerproc;
  1387. var
  1388. ss : shortstring;
  1389. begin
  1390. str(c:len:fr,ss);
  1391. s:=ss;
  1392. end;
  1393. {$endif FPC_HAS_STR_CURRENCY}
  1394. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; out S : WideString);compilerproc;
  1395. Var
  1396. SS : ShortString;
  1397. begin
  1398. Str (v:Len,SS);
  1399. S:=SS;
  1400. end;
  1401. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; out S : WideString);compilerproc;
  1402. Var
  1403. SS : ShortString;
  1404. begin
  1405. str(v:Len,SS);
  1406. S:=SS;
  1407. end;
  1408. {$ifndef CPU64}
  1409. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; out S : WideString);compilerproc;
  1410. Var
  1411. SS : ShortString;
  1412. begin
  1413. Str (v:Len,SS);
  1414. S:=SS;
  1415. end;
  1416. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; out S : WideString);compilerproc;
  1417. Var
  1418. SS : ShortString;
  1419. begin
  1420. str(v:Len,SS);
  1421. S:=SS;
  1422. end;
  1423. {$endif CPU64}
  1424. function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1425. begin
  1426. if assigned(Source) then
  1427. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1428. else
  1429. Result:=0;
  1430. end;
  1431. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt;
  1432. var
  1433. i,j : SizeUInt;
  1434. w : word;
  1435. begin
  1436. result:=0;
  1437. if source=nil then
  1438. exit;
  1439. i:=0;
  1440. j:=0;
  1441. if assigned(Dest) then
  1442. begin
  1443. while (i<SourceChars) and (j<MaxDestBytes) do
  1444. begin
  1445. w:=word(Source[i]);
  1446. case w of
  1447. 0..$7f:
  1448. begin
  1449. Dest[j]:=char(w);
  1450. inc(j);
  1451. end;
  1452. $80..$7ff:
  1453. begin
  1454. if j+1>=MaxDestBytes then
  1455. break;
  1456. Dest[j]:=char($c0 or (w shr 6));
  1457. Dest[j+1]:=char($80 or (w and $3f));
  1458. inc(j,2);
  1459. end;
  1460. else
  1461. begin
  1462. if j+2>=MaxDestBytes then
  1463. break;
  1464. Dest[j]:=char($e0 or (w shr 12));
  1465. Dest[j+1]:=char($80 or ((w shr 6)and $3f));
  1466. Dest[j+2]:=char($80 or (w and $3f));
  1467. inc(j,3);
  1468. end;
  1469. end;
  1470. inc(i);
  1471. end;
  1472. if j>MaxDestBytes-1 then
  1473. j:=MaxDestBytes-1;
  1474. Dest[j]:=#0;
  1475. end
  1476. else
  1477. begin
  1478. while i<SourceChars do
  1479. begin
  1480. case word(Source[i]) of
  1481. $0..$7f:
  1482. inc(j);
  1483. $80..$7ff:
  1484. inc(j,2);
  1485. else
  1486. inc(j,3);
  1487. end;
  1488. inc(i);
  1489. end;
  1490. end;
  1491. result:=j+1;
  1492. end;
  1493. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1494. begin
  1495. if assigned(Source) then
  1496. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1497. else
  1498. Result:=0;
  1499. end;
  1500. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1501. var
  1502. i,j : SizeUInt;
  1503. w: SizeUInt;
  1504. b : byte;
  1505. begin
  1506. if not assigned(Source) then
  1507. begin
  1508. result:=0;
  1509. exit;
  1510. end;
  1511. result:=SizeUInt(-1);
  1512. i:=0;
  1513. j:=0;
  1514. if assigned(Dest) then
  1515. begin
  1516. while (j<MaxDestChars) and (i<SourceBytes) do
  1517. begin
  1518. b:=byte(Source[i]);
  1519. w:=b;
  1520. inc(i);
  1521. // 2 or 3 bytes?
  1522. if b>=$80 then
  1523. begin
  1524. w:=b and $3f;
  1525. if i>=SourceBytes then
  1526. exit;
  1527. // 3 bytes?
  1528. if (b and $20)<>0 then
  1529. begin
  1530. b:=byte(Source[i]);
  1531. inc(i);
  1532. if i>=SourceBytes then
  1533. exit;
  1534. if (b and $c0)<>$80 then
  1535. exit;
  1536. w:=(w shl 6) or (b and $3f);
  1537. end;
  1538. b:=byte(Source[i]);
  1539. w:=(w shl 6) or (b and $3f);
  1540. if (b and $c0)<>$80 then
  1541. exit;
  1542. inc(i);
  1543. end;
  1544. Dest[j]:=WideChar(w);
  1545. inc(j);
  1546. end;
  1547. if j>=MaxDestChars then j:=MaxDestChars-1;
  1548. Dest[j]:=#0;
  1549. end
  1550. else
  1551. begin
  1552. while i<SourceBytes do
  1553. begin
  1554. b:=byte(Source[i]);
  1555. inc(i);
  1556. // 2 or 3 bytes?
  1557. if b>=$80 then
  1558. begin
  1559. if i>=SourceBytes then
  1560. exit;
  1561. // 3 bytes?
  1562. b := b and $3f;
  1563. if (b and $20)<>0 then
  1564. begin
  1565. b:=byte(Source[i]);
  1566. inc(i);
  1567. if i>=SourceBytes then
  1568. exit;
  1569. if (b and $c0)<>$80 then
  1570. exit;
  1571. end;
  1572. if (byte(Source[i]) and $c0)<>$80 then
  1573. exit;
  1574. inc(i);
  1575. end;
  1576. inc(j);
  1577. end;
  1578. end;
  1579. result:=j+1;
  1580. end;
  1581. function UTF8Encode(const s : WideString) : UTF8String;
  1582. var
  1583. i : SizeInt;
  1584. hs : UTF8String;
  1585. begin
  1586. result:='';
  1587. if s='' then
  1588. exit;
  1589. SetLength(hs,length(s)*3);
  1590. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  1591. if i>0 then
  1592. begin
  1593. SetLength(hs,i-1);
  1594. result:=hs;
  1595. end;
  1596. end;
  1597. function UTF8Decode(const s : UTF8String): WideString;
  1598. var
  1599. i : SizeInt;
  1600. hs : WideString;
  1601. begin
  1602. result:='';
  1603. if s='' then
  1604. exit;
  1605. SetLength(hs,length(s));
  1606. i:=Utf8ToUnicode(PWideChar(hs),length(hs)+1,pchar(s),length(s));
  1607. if i>0 then
  1608. begin
  1609. SetLength(hs,i-1);
  1610. result:=hs;
  1611. end;
  1612. end;
  1613. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  1614. begin
  1615. Result:=Utf8Encode(s);
  1616. end;
  1617. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1618. begin
  1619. Result:=Utf8Decode(s);
  1620. end;
  1621. function WideStringToUCS4String(const s : WideString) : UCS4String;
  1622. var
  1623. i : SizeInt;
  1624. begin
  1625. setlength(result,length(s)+1);
  1626. for i:=1 to length(s) do
  1627. result[i-1]:=UCS4Char(s[i]);
  1628. result[length(s)]:=UCS4Char(0);
  1629. end;
  1630. function UCS4StringToWideString(const s : UCS4String) : WideString;
  1631. var
  1632. i : SizeInt;
  1633. begin
  1634. setlength(result,length(s)-1);
  1635. for i:=1 to length(s)-1 do
  1636. result[i]:=WideChar(s[i-1]);
  1637. end;
  1638. procedure unimplementedwidestring;
  1639. begin
  1640. HandleErrorFrame(215,get_frame);
  1641. end;
  1642. {$warnings off}
  1643. function GenericWideCase(const s : WideString) : WideString;
  1644. begin
  1645. unimplementedwidestring;
  1646. end;
  1647. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  1648. begin
  1649. unimplementedwidestring;
  1650. end;
  1651. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  1652. begin
  1653. unimplementedwidestring;
  1654. end;
  1655. function CharLengthPChar(const Str: PChar): PtrInt;
  1656. begin
  1657. unimplementedwidestring;
  1658. end;
  1659. {$warnings on}
  1660. procedure initwidestringmanager;
  1661. begin
  1662. fillchar(widestringmanager,sizeof(widestringmanager),0);
  1663. {$ifndef HAS_WIDESTRINGMANAGER}
  1664. widestringmanager.Wide2AnsiMoveProc:=@defaultWide2AnsiMove;
  1665. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2WideMove;
  1666. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  1667. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  1668. {$endif HAS_WIDESTRINGMANAGER}
  1669. widestringmanager.CompareWideStringProc:=@CompareWideString;
  1670. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  1671. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  1672. end;