wustrings.inc 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  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 : pointer;
  518. begin
  519. if high(sarr)=0 then
  520. begin
  521. DestS:='';
  522. exit;
  523. end;
  524. destcopy:=nil;
  525. lowstart:=low(sarr);
  526. if Pointer(DestS)=Pointer(sarr[lowstart]) then
  527. inc(lowstart);
  528. { Check for another reuse, then we can't use
  529. the append optimization }
  530. for i:=lowstart to high(sarr) do
  531. begin
  532. if Pointer(DestS)=Pointer(sarr[i]) then
  533. begin
  534. { if DestS is used somewhere in the middle of the expression,
  535. we need to make sure the original string still exists after
  536. we empty/modify DestS }
  537. destcopy:=pointer(dests);
  538. fpc_WideStr_Incr_Ref(destcopy);
  539. lowstart:=low(sarr);
  540. break;
  541. end;
  542. end;
  543. { Start with empty DestS if we start with concatting
  544. the first array element }
  545. if lowstart=low(sarr) then
  546. DestS:='';
  547. OldDestLen:=length(DestS);
  548. { Calculate size of the result so we can do
  549. a single call to SetLength() }
  550. NewLen:=0;
  551. for i:=low(sarr) to high(sarr) do
  552. inc(NewLen,length(sarr[i]));
  553. SetLength(DestS,NewLen);
  554. { Concat all strings, except the string we already
  555. copied in DestS }
  556. pc:=Pointer(DestS)+OldDestLen*sizeof(WideChar);
  557. for i:=lowstart to high(sarr) do
  558. begin
  559. p:=pointer(sarr[i]);
  560. if assigned(p) then
  561. begin
  562. Size:=length(widestring(p));
  563. Move(p^,pc^,(Size+1)*sizeof(WideChar));
  564. inc(pc,size*sizeof(WideChar));
  565. end;
  566. end;
  567. fpc_WideStr_Decr_Ref(destcopy);
  568. end;
  569. {$endif STR_CONCAT_PROCS}
  570. Function fpc_Char_To_WChar(const c : Char): WideChar; compilerproc;
  571. var
  572. w: widestring;
  573. begin
  574. widestringmanager.Ansi2WideMoveProc(@c, w, 1);
  575. fpc_Char_To_WChar:= w[1];
  576. end;
  577. Function fpc_Char_To_WideStr(const c : Char): WideString; compilerproc;
  578. {
  579. Converts a Char to a WideString;
  580. }
  581. begin
  582. Setlength(fpc_Char_To_WideStr,1);
  583. fpc_Char_To_WideStr[1]:=c;
  584. { Terminating Zero }
  585. PWideChar(Pointer(fpc_Char_To_WideStr)+sizeof(WideChar))^:=#0;
  586. end;
  587. Function fpc_WChar_To_Char(const c : WideChar): Char; compilerproc;
  588. {
  589. Converts a WideChar to a Char;
  590. }
  591. var
  592. s: ansistring;
  593. begin
  594. widestringmanager.Wide2AnsiMoveProc(@c, s, 1);
  595. if length(s)=1 then
  596. fpc_WChar_To_Char:= s[1]
  597. else
  598. fpc_WChar_To_Char:='?';
  599. end;
  600. Function fpc_WChar_To_WideStr(const c : WideChar): WideString; compilerproc;
  601. {
  602. Converts a WideChar to a WideString;
  603. }
  604. begin
  605. Setlength (fpc_WChar_To_WideStr,1);
  606. fpc_WChar_To_WideStr[1]:= c;
  607. end;
  608. Function fpc_WChar_To_AnsiStr(const c : WideChar): AnsiString; compilerproc;
  609. {
  610. Converts a WideChar to a AnsiString;
  611. }
  612. begin
  613. widestringmanager.Wide2AnsiMoveProc(@c, fpc_WChar_To_AnsiStr, 1);
  614. end;
  615. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  616. Function fpc_WChar_To_ShortStr(const c : WideChar): ShortString; compilerproc;
  617. {
  618. Converts a WideChar to a ShortString;
  619. }
  620. var
  621. s: ansistring;
  622. begin
  623. widestringmanager.Wide2AnsiMoveProc(@c, s, 1);
  624. fpc_WChar_To_ShortStr:= s;
  625. end;
  626. {$else FPC_STRTOSHORTSTRINGPROC}
  627. procedure fpc_WChar_To_ShortStr(out res : shortstring;const c : WideChar) compilerproc;
  628. {
  629. Converts a WideChar to a ShortString;
  630. }
  631. var
  632. s: ansistring;
  633. begin
  634. widestringmanager.Wide2AnsiMoveProc(@c,s,1);
  635. res:=s;
  636. end;
  637. {$endif FPC_STRTOSHORTSTRINGPROC}
  638. Function fpc_PChar_To_WideStr(const p : pchar): WideString; compilerproc;
  639. Var
  640. L : SizeInt;
  641. begin
  642. if (not assigned(p)) or (p[0]=#0) Then
  643. begin
  644. fpc_pchar_to_widestr := '';
  645. exit;
  646. end;
  647. l:=IndexChar(p^,-1,#0);
  648. widestringmanager.Ansi2WideMoveProc(P,fpc_PChar_To_WideStr,l);
  649. end;
  650. Function fpc_CharArray_To_WideStr(const arr: array of char; zerobased: boolean = true): WideString; compilerproc;
  651. var
  652. i : SizeInt;
  653. begin
  654. if (zerobased) then
  655. begin
  656. if (arr[0]=#0) Then
  657. begin
  658. fpc_chararray_to_widestr := '';
  659. exit;
  660. end;
  661. i:=IndexChar(arr,high(arr)+1,#0);
  662. if i = -1 then
  663. i := high(arr)+1;
  664. end
  665. else
  666. i := high(arr)+1;
  667. SetLength(fpc_CharArray_To_WideStr,i);
  668. widestringmanager.Ansi2WideMoveProc (pchar(@arr),fpc_CharArray_To_WideStr,i);
  669. end;
  670. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  671. function fpc_WideCharArray_To_ShortStr(const arr: array of widechar; zerobased: boolean = true): shortstring;[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  672. var
  673. l: longint;
  674. index: longint;
  675. len: byte;
  676. temp: ansistring;
  677. begin
  678. l := high(arr)+1;
  679. if l>=256 then
  680. l:=255
  681. else if l<0 then
  682. l:=0;
  683. if zerobased then
  684. begin
  685. index:=IndexWord(arr[0],l,0);
  686. if (index < 0) then
  687. len := l
  688. else
  689. len := index;
  690. end
  691. else
  692. len := l;
  693. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,len);
  694. fpc_WideCharArray_To_ShortStr := temp;
  695. end;
  696. {$else FPC_STRTOSHORTSTRINGPROC}
  697. procedure fpc_WideCharArray_To_ShortStr(out res : shortstring;const arr: array of widechar; zerobased: boolean = true);[public,alias:'FPC_WIDECHARARRAY_TO_SHORTSTR']; compilerproc;
  698. var
  699. l: longint;
  700. index: ptrint;
  701. len: byte;
  702. temp: ansistring;
  703. begin
  704. l := high(arr)+1;
  705. if l>=high(res)+1 then
  706. l:=high(res)
  707. else if l<0 then
  708. l:=0;
  709. if zerobased then
  710. begin
  711. index:=IndexWord(arr[0],l,0);
  712. if index<0 then
  713. len:=l
  714. else
  715. len:=index;
  716. end
  717. else
  718. len:=l;
  719. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),temp,len);
  720. res:=temp;
  721. end;
  722. {$endif FPC_STRTOSHORTSTRINGPROC}
  723. Function fpc_WideCharArray_To_AnsiStr(const arr: array of widechar; zerobased: boolean = true): AnsiString; compilerproc;
  724. var
  725. i : SizeInt;
  726. begin
  727. if (zerobased) then
  728. begin
  729. i:=IndexWord(arr,high(arr)+1,0);
  730. if i = -1 then
  731. i := high(arr)+1;
  732. end
  733. else
  734. i := high(arr)+1;
  735. SetLength(fpc_WideCharArray_To_AnsiStr,i);
  736. widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),fpc_WideCharArray_To_AnsiStr,i);
  737. end;
  738. Function fpc_WideCharArray_To_WideStr(const arr: array of widechar; zerobased: boolean = true): WideString; compilerproc;
  739. var
  740. i : SizeInt;
  741. begin
  742. if (zerobased) then
  743. begin
  744. i:=IndexWord(arr,high(arr)+1,0);
  745. if i = -1 then
  746. i := high(arr)+1;
  747. end
  748. else
  749. i := high(arr)+1;
  750. SetLength(fpc_WideCharArray_To_WideStr,i);
  751. Move(arr[0], Pointer(fpc_WideCharArray_To_WideStr)^,i*sizeof(WideChar));
  752. end;
  753. {$ifndef FPC_STRTOCHARARRAYPROC}
  754. { inside the compiler, the resulttype is modified to that of the actual }
  755. { chararray we're converting to (JM) }
  756. function fpc_widestr_to_chararray(arraysize: SizeInt; const src: WideString): fpc_big_chararray;[public,alias: 'FPC_WIDESTR_TO_CHARARRAY']; compilerproc;
  757. var
  758. len: SizeInt;
  759. temp: ansistring;
  760. begin
  761. len := length(src);
  762. { make sure we don't dereference src if it can be nil (JM) }
  763. if len > 0 then
  764. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,len);
  765. len := length(temp);
  766. if len > arraysize then
  767. len := arraysize;
  768. {$r-}
  769. move(temp[1],fpc_widestr_to_chararray[0],len);
  770. fillchar(fpc_widestr_to_chararray[len],arraysize-len,0);
  771. {$ifdef RangeCheckWasOn}
  772. {$r+}
  773. {$endif}
  774. end;
  775. { inside the compiler, the resulttype is modified to that of the actual }
  776. { widechararray we're converting to (JM) }
  777. function fpc_widestr_to_widechararray(arraysize: SizeInt; const src: WideString): fpc_big_widechararray;[public,alias: 'FPC_WIDESTR_TO_WIDECHARARRAY']; compilerproc;
  778. var
  779. len: SizeInt;
  780. begin
  781. len := length(src);
  782. if len > arraysize then
  783. len := arraysize;
  784. {$r-}
  785. { make sure we don't try to access element 1 of the ansistring if it's nil }
  786. if len > 0 then
  787. move(src[1],fpc_widestr_to_widechararray[0],len*SizeOf(WideChar));
  788. fillchar(fpc_widestr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  789. {$ifdef RangeCheckWasOn}
  790. {$r+}
  791. {$endif}
  792. end;
  793. { inside the compiler, the resulttype is modified to that of the actual }
  794. { chararray we're converting to (JM) }
  795. function fpc_ansistr_to_widechararray(arraysize: SizeInt; const src: AnsiString): fpc_big_widechararray;[public,alias: 'FPC_ANSISTR_TO_WIDECHARARRAY']; compilerproc;
  796. var
  797. len: SizeInt;
  798. temp: widestring;
  799. begin
  800. len := length(src);
  801. { make sure we don't dereference src if it can be nil (JM) }
  802. if len > 0 then
  803. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  804. len := length(temp);
  805. if len > arraysize then
  806. len := arraysize;
  807. {$r-}
  808. move(temp[1],fpc_ansistr_to_widechararray[0],len*sizeof(widechar));
  809. fillchar(fpc_ansistr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  810. {$ifdef RangeCheckWasOn}
  811. {$r+}
  812. {$endif}
  813. end;
  814. function fpc_shortstr_to_widechararray(arraysize: SizeInt; const src: ShortString): fpc_big_widechararray;[public,alias: 'FPC_SHORTSTR_TO_WIDECHARARRAY']; compilerproc;
  815. var
  816. len: longint;
  817. temp : widestring;
  818. begin
  819. len := length(src);
  820. { make sure we don't access char 1 if length is 0 (JM) }
  821. if len > 0 then
  822. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  823. len := length(temp);
  824. if len > arraysize then
  825. len := arraysize;
  826. {$r-}
  827. move(temp[1],fpc_shortstr_to_widechararray[0],len*sizeof(widechar));
  828. fillchar(fpc_shortstr_to_widechararray[len],(arraysize-len)*SizeOf(WideChar),0);
  829. {$ifdef RangeCheckWasOn}
  830. {$r+}
  831. {$endif}
  832. end;
  833. {$else ndef FPC_STRTOCHARARRAYPROC}
  834. procedure fpc_widestr_to_chararray(out res: array of char; const src: WideString); compilerproc;
  835. var
  836. len: SizeInt;
  837. temp: ansistring;
  838. begin
  839. len := length(src);
  840. { make sure we don't dereference src if it can be nil (JM) }
  841. if len > 0 then
  842. widestringmanager.wide2ansimoveproc(pwidechar(@src[1]),temp,len);
  843. len := length(temp);
  844. if len > length(res) then
  845. len := length(res);
  846. {$r-}
  847. move(temp[1],res[0],len);
  848. fillchar(res[len],length(res)-len,0);
  849. {$ifdef RangeCheckWasOn}
  850. {$r+}
  851. {$endif}
  852. end;
  853. procedure fpc_widestr_to_widechararray(out res: array of widechar; const src: WideString); compilerproc;
  854. var
  855. len: SizeInt;
  856. begin
  857. len := length(src);
  858. if len > length(res) then
  859. len := length(res);
  860. {$r-}
  861. { make sure we don't try to access element 1 of the ansistring if it's nil }
  862. if len > 0 then
  863. move(src[1],res[0],len*SizeOf(WideChar));
  864. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  865. {$ifdef RangeCheckWasOn}
  866. {$r+}
  867. {$endif}
  868. end;
  869. procedure fpc_ansistr_to_widechararray(out res: array of widechar; const src: AnsiString); compilerproc;
  870. var
  871. len: SizeInt;
  872. temp: widestring;
  873. begin
  874. len := length(src);
  875. { make sure we don't dereference src if it can be nil (JM) }
  876. if len > 0 then
  877. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  878. len := length(temp);
  879. if len > length(res) then
  880. len := length(res);
  881. {$r-}
  882. move(temp[1],res[0],len*sizeof(widechar));
  883. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  884. {$ifdef RangeCheckWasOn}
  885. {$r+}
  886. {$endif}
  887. end;
  888. procedure fpc_shortstr_to_widechararray(out res: array of widechar; const src: ShortString); compilerproc;
  889. var
  890. len: longint;
  891. temp : widestring;
  892. begin
  893. len := length(src);
  894. { make sure we don't access char 1 if length is 0 (JM) }
  895. if len > 0 then
  896. widestringmanager.ansi2widemoveproc(pchar(@src[1]),temp,len);
  897. len := length(temp);
  898. if len > length(res) then
  899. len := length(res);
  900. {$r-}
  901. move(temp[1],res[0],len*sizeof(widechar));
  902. fillchar(res[len],(length(res)-len)*SizeOf(WideChar),0);
  903. {$ifdef RangeCheckWasOn}
  904. {$r+}
  905. {$endif}
  906. end;
  907. {$endif ndef FPC_STRTOCHARARRAYPROC}
  908. Function fpc_WideStr_Compare(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE']; compilerproc;
  909. {
  910. Compares 2 WideStrings;
  911. The result is
  912. <0 if S1<S2
  913. 0 if S1=S2
  914. >0 if S1>S2
  915. }
  916. Var
  917. MaxI,Temp : SizeInt;
  918. begin
  919. if pointer(S1)=pointer(S2) then
  920. begin
  921. fpc_WideStr_Compare:=0;
  922. exit;
  923. end;
  924. Maxi:=Length(S1);
  925. temp:=Length(S2);
  926. If MaxI>Temp then
  927. MaxI:=Temp;
  928. Temp:=CompareWord(S1[1],S2[1],MaxI);
  929. if temp=0 then
  930. temp:=Length(S1)-Length(S2);
  931. fpc_WideStr_Compare:=Temp;
  932. end;
  933. Function fpc_WideStr_Compare_Equal(const S1,S2 : WideString): SizeInt;[Public,Alias : 'FPC_WIDESTR_COMPARE_EQUAL']; compilerproc;
  934. {
  935. Compares 2 WideStrings for equality only;
  936. The result is
  937. 0 if S1=S2
  938. <>0 if S1<>S2
  939. }
  940. Var
  941. MaxI : SizeInt;
  942. begin
  943. if pointer(S1)=pointer(S2) then
  944. exit(0);
  945. Maxi:=Length(S1);
  946. If MaxI<>Length(S2) then
  947. exit(-1)
  948. else
  949. exit(CompareWord(S1[1],S2[1],MaxI));
  950. end;
  951. Procedure fpc_WideStr_CheckZero(p : pointer);[Public,Alias : 'FPC_WIDESTR_CHECKZERO']; compilerproc;
  952. begin
  953. if p=nil then
  954. HandleErrorFrame(201,get_frame);
  955. end;
  956. Procedure fpc_WideStr_CheckRange(len,index : SizeInt);[Public,Alias : 'FPC_WIDESTR_RANGECHECK']; compilerproc;
  957. begin
  958. if (index>len div 2) or (Index<1) then
  959. HandleErrorFrame(201,get_frame);
  960. end;
  961. Procedure fpc_WideStr_SetLength(Var S : WideString; l : SizeInt);[Public,Alias : 'FPC_WIDESTR_SETLENGTH']; compilerproc;
  962. {
  963. Sets The length of string S to L.
  964. Makes sure S is unique, and contains enough room.
  965. }
  966. Var
  967. Temp : Pointer;
  968. movelen: SizeInt;
  969. begin
  970. if (l>0) then
  971. begin
  972. if Pointer(S)=nil then
  973. begin
  974. { Need a complete new string...}
  975. Pointer(s):=NewWideString(l);
  976. end
  977. { windows doesn't support reallocing widestrings, this code
  978. is anyways subject to be removed because widestrings shouldn't be
  979. ref. counted anymore (FK) }
  980. else
  981. if
  982. {$ifdef MSWINDOWS}
  983. not winwidestringalloc and
  984. {$endif MSWINDOWS}
  985. {$ifdef FPC_WINLIKEWIDESTRING}
  986. not IsWideStringConstant(pointer(S))
  987. {$else}
  988. (PWideRec(Pointer(S)-WideFirstOff)^.Ref = 1)
  989. {$endif FPC_WINLIKEWIDESTRING}
  990. then
  991. begin
  992. Dec(Pointer(S),WideFirstOff);
  993. if SizeUInt(L*sizeof(WideChar)+WideRecLen)>MemSize(Pointer(S)) then
  994. reallocmem(pointer(S), L*sizeof(WideChar)+WideRecLen);
  995. Inc(Pointer(S), WideFirstOff);
  996. end
  997. else
  998. begin
  999. { Reallocation is needed... }
  1000. Temp:=Pointer(NewWideString(L));
  1001. if Length(S)>0 then
  1002. begin
  1003. if l < succ(length(s)) then
  1004. movelen := l
  1005. { also move terminating null }
  1006. else
  1007. movelen := succ(length(s));
  1008. Move(Pointer(S)^,Temp^,movelen * Sizeof(WideChar));
  1009. end;
  1010. fpc_widestr_decr_ref(Pointer(S));
  1011. Pointer(S):=Temp;
  1012. end;
  1013. { Force nil termination in case it gets shorter }
  1014. PWord(Pointer(S)+l*sizeof(WideChar))^:=0;
  1015. {$ifdef MSWINDOWS}
  1016. if not winwidestringalloc then
  1017. {$endif MSWINDOWS}
  1018. PWideRec(Pointer(S)-WideFirstOff)^.Len:=l*sizeof(WideChar);
  1019. end
  1020. else
  1021. begin
  1022. { Length=0 }
  1023. if Pointer(S)<>nil then
  1024. fpc_widestr_decr_ref (Pointer(S));
  1025. Pointer(S):=Nil;
  1026. end;
  1027. end;
  1028. {*****************************************************************************
  1029. Public functions, In interface.
  1030. *****************************************************************************}
  1031. function WideCharToString(S : PWideChar) : AnsiString;
  1032. begin
  1033. result:=WideCharLenToString(s,Length(WideString(s)));
  1034. end;
  1035. function StringToWideChar(const Src : AnsiString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
  1036. var
  1037. temp:widestring;
  1038. begin
  1039. widestringmanager.Ansi2WideMoveProc(PChar(Src),temp,Length(Src));
  1040. if Length(temp)<DestSize then
  1041. move(temp[1],Dest^,Length(temp)*SizeOf(WideChar))
  1042. else
  1043. move(temp[1],Dest^,(DestSize-1)*SizeOf(WideChar));
  1044. Dest[DestSize-1]:=#0;
  1045. result:=Dest;
  1046. end;
  1047. function WideCharLenToString(S : PWideChar;Len : SizeInt) : AnsiString;
  1048. begin
  1049. //SetLength(result,Len);
  1050. widestringmanager.Wide2AnsiMoveproc(S,result,Len);
  1051. end;
  1052. procedure WideCharLenToStrVar(Src : PWideChar;Len : SizeInt;out Dest : AnsiString);
  1053. begin
  1054. Dest:=WideCharLenToString(Src,Len);
  1055. end;
  1056. procedure WideCharToStrVar(S : PWideChar;out Dest : AnsiString);
  1057. begin
  1058. Dest:=WideCharToString(S);
  1059. end;
  1060. Function fpc_widestr_Unique(Var S : Pointer): Pointer; [Public,Alias : 'FPC_WIDESTR_UNIQUE']; compilerproc;
  1061. {$ifdef FPC_WINLIKEWIDESTRING}
  1062. begin
  1063. pointer(result) := pointer(s);
  1064. end;
  1065. {$else FPC_WINLIKEWIDESTRING}
  1066. {
  1067. Make sure reference count of S is 1,
  1068. using copy-on-write semantics.
  1069. }
  1070. Var
  1071. SNew : Pointer;
  1072. L : SizeInt;
  1073. begin
  1074. pointer(result) := pointer(s);
  1075. If Pointer(S)=Nil then
  1076. exit;
  1077. if PWideRec(Pointer(S)-WideFirstOff)^.Ref<>1 then
  1078. begin
  1079. L:=PWideRec(Pointer(S)-WideFirstOff)^.len div sizeof(WideChar);
  1080. SNew:=NewWideString (L);
  1081. Move (PWideChar(S)^,SNew^,(L+1)*sizeof(WideChar));
  1082. PWideRec(SNew-WideFirstOff)^.len:=L * sizeof(WideChar);
  1083. fpc_widestr_decr_ref (Pointer(S)); { Thread safe }
  1084. pointer(S):=SNew;
  1085. pointer(result):=SNew;
  1086. end;
  1087. end;
  1088. {$endif FPC_WINLIKEWIDESTRING}
  1089. Function Fpc_WideStr_Copy (Const S : WideString; Index,Size : SizeInt) : WideString;compilerproc;
  1090. var
  1091. ResultAddress : Pointer;
  1092. begin
  1093. ResultAddress:=Nil;
  1094. dec(index);
  1095. if Index < 0 then
  1096. Index := 0;
  1097. { Check Size. Accounts for Zero-length S, the double check is needed because
  1098. Size can be maxint and will get <0 when adding index }
  1099. if (Size>Length(S)) or
  1100. (Index+Size>Length(S)) then
  1101. Size:=Length(S)-Index;
  1102. If Size>0 then
  1103. begin
  1104. If Index<0 Then
  1105. Index:=0;
  1106. ResultAddress:=Pointer(NewWideString (Size));
  1107. if ResultAddress<>Nil then
  1108. begin
  1109. Move (PWideChar(S)[Index],ResultAddress^,Size*sizeof(WideChar));
  1110. PWideRec(ResultAddress-WideFirstOff)^.Len:=Size*sizeof(WideChar);
  1111. PWideChar(ResultAddress+Size*sizeof(WideChar))^:=#0;
  1112. end;
  1113. end;
  1114. fpc_widestr_decr_ref(Pointer(fpc_widestr_copy));
  1115. Pointer(fpc_widestr_Copy):=ResultAddress;
  1116. end;
  1117. Function Pos (Const Substr : WideString; Const Source : WideString) : SizeInt;
  1118. var
  1119. i,MaxLen : SizeInt;
  1120. pc : pwidechar;
  1121. begin
  1122. Pos:=0;
  1123. if Length(SubStr)>0 then
  1124. begin
  1125. MaxLen:=Length(source)-Length(SubStr);
  1126. i:=0;
  1127. pc:=@source[1];
  1128. while (i<=MaxLen) do
  1129. begin
  1130. inc(i);
  1131. if (SubStr[1]=pc^) and
  1132. (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
  1133. begin
  1134. Pos:=i;
  1135. exit;
  1136. end;
  1137. inc(pc);
  1138. end;
  1139. end;
  1140. end;
  1141. { Faster version for a widechar alone }
  1142. Function Pos (c : WideChar; Const s : WideString) : SizeInt;
  1143. var
  1144. i: SizeInt;
  1145. pc : pwidechar;
  1146. begin
  1147. pc:=@s[1];
  1148. for i:=1 to length(s) do
  1149. begin
  1150. if pc^=c then
  1151. begin
  1152. pos:=i;
  1153. exit;
  1154. end;
  1155. inc(pc);
  1156. end;
  1157. pos:=0;
  1158. end;
  1159. Function Pos (c : WideChar; Const s : AnsiString) : SizeInt;
  1160. var
  1161. i: SizeInt;
  1162. pc : pchar;
  1163. begin
  1164. pc:=@s[1];
  1165. for i:=1 to length(s) do
  1166. begin
  1167. if widechar(pc^)=c then
  1168. begin
  1169. pos:=i;
  1170. exit;
  1171. end;
  1172. inc(pc);
  1173. end;
  1174. pos:=0;
  1175. end;
  1176. Function Pos (c : AnsiString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1177. begin
  1178. result:=Pos(WideString(c),s);
  1179. end;
  1180. Function Pos (c : ShortString; Const s : WideString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1181. begin
  1182. result:=Pos(WideString(c),s);
  1183. end;
  1184. Function Pos (c : WideString; Const s : AnsiString) : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1185. begin
  1186. result:=Pos(c,WideString(s));
  1187. end;
  1188. { Faster version for a char alone. Must be implemented because }
  1189. { pos(c: char; const s: shortstring) also exists, so otherwise }
  1190. { using pos(char,pchar) will always call the shortstring version }
  1191. { (exact match for first argument), also with $h+ (JM) }
  1192. Function Pos (c : Char; Const s : WideString) : SizeInt;
  1193. var
  1194. i: SizeInt;
  1195. wc : widechar;
  1196. pc : pwidechar;
  1197. begin
  1198. wc:=c;
  1199. pc:=@s[1];
  1200. for i:=1 to length(s) do
  1201. begin
  1202. if pc^=wc then
  1203. begin
  1204. pos:=i;
  1205. exit;
  1206. end;
  1207. inc(pc);
  1208. end;
  1209. pos:=0;
  1210. end;
  1211. Procedure Delete (Var S : WideString; Index,Size: SizeInt);
  1212. Var
  1213. LS : SizeInt;
  1214. begin
  1215. If Length(S)=0 then
  1216. exit;
  1217. if index<=0 then
  1218. exit;
  1219. LS:=PWideRec(Pointer(S)-WideFirstOff)^.Len div sizeof(WideChar);
  1220. if (Index<=LS) and (Size>0) then
  1221. begin
  1222. UniqueString (S);
  1223. if Size+Index>LS then
  1224. Size:=LS-Index+1;
  1225. if Index+Size<=LS then
  1226. begin
  1227. Dec(Index);
  1228. Move(PWideChar(S)[Index+Size],PWideChar(S)[Index],(LS-Index+1)*sizeof(WideChar));
  1229. end;
  1230. Setlength(s,LS-Size);
  1231. end;
  1232. end;
  1233. Procedure Insert (Const Source : WideString; Var S : WideString; Index : SizeInt);
  1234. var
  1235. Temp : WideString;
  1236. LS : SizeInt;
  1237. begin
  1238. If Length(Source)=0 then
  1239. exit;
  1240. if index <= 0 then
  1241. index := 1;
  1242. Ls:=Length(S);
  1243. if index > LS then
  1244. index := LS+1;
  1245. Dec(Index);
  1246. Pointer(Temp) := NewWideString(Length(Source)+LS);
  1247. SetLength(Temp,Length(Source)+LS);
  1248. If Index>0 then
  1249. move (PWideChar(S)^,PWideChar(Temp)^,Index*sizeof(WideChar));
  1250. Move (PWideChar(Source)^,PWideChar(Temp)[Index],Length(Source)*sizeof(WideChar));
  1251. If (LS-Index)>0 then
  1252. Move(PWideChar(S)[Index],PWideChar(temp)[Length(Source)+index],(LS-Index)*sizeof(WideChar));
  1253. S:=Temp;
  1254. end;
  1255. function UpCase(const s : WideString) : WideString;
  1256. begin
  1257. result:=widestringmanager.UpperWideStringProc(s);
  1258. end;
  1259. Procedure SetString (Out S : WideString; Buf : PWideChar; Len : SizeInt);
  1260. var
  1261. BufLen: SizeInt;
  1262. begin
  1263. SetLength(S,Len);
  1264. If (Buf<>Nil) and (Len>0) then
  1265. begin
  1266. BufLen := IndexWord(Buf^, Len+1, 0);
  1267. If (BufLen>0) and (BufLen < Len) then
  1268. Len := BufLen;
  1269. Move (Buf[0],S[1],Len*sizeof(WideChar));
  1270. PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1271. end;
  1272. end;
  1273. Procedure SetString (Out S : WideString; Buf : PChar; Len : SizeInt);
  1274. var
  1275. BufLen: SizeInt;
  1276. begin
  1277. SetLength(S,Len);
  1278. If (Buf<>Nil) and (Len>0) then
  1279. begin
  1280. BufLen := IndexByte(Buf^, Len+1, 0);
  1281. If (BufLen>0) and (BufLen < Len) then
  1282. Len := BufLen;
  1283. widestringmanager.Ansi2WideMoveProc(Buf,S,Len);
  1284. //PWideChar(Pointer(S)+Len*sizeof(WideChar))^:=#0;
  1285. end;
  1286. end;
  1287. Function fpc_Val_Real_WideStr(Const S : WideString; out Code : ValSInt): ValReal; [public, alias:'FPC_VAL_REAL_WIDESTR']; compilerproc;
  1288. Var
  1289. SS : String;
  1290. begin
  1291. fpc_Val_Real_WideStr := 0;
  1292. if length(S) > 255 then
  1293. code := 256
  1294. else
  1295. begin
  1296. SS := S;
  1297. Val(SS,fpc_Val_Real_WideStr,code);
  1298. end;
  1299. end;
  1300. function fpc_val_enum_widestr(str2ordindex:pointer;const s:widestring;out code:valsint):longint;compilerproc;
  1301. var ss:shortstring;
  1302. begin
  1303. if length(s)>255 then
  1304. code:=256
  1305. else
  1306. begin
  1307. ss:=s;
  1308. val(ss,fpc_val_enum_widestr,code);
  1309. end;
  1310. end;
  1311. Function fpc_Val_Currency_WideStr(Const S : WideString; out Code : ValSInt): Currency; [public, alias:'FPC_VAL_CURRENCY_WIDESTR']; compilerproc;
  1312. Var
  1313. SS : String;
  1314. begin
  1315. if length(S) > 255 then
  1316. begin
  1317. fpc_Val_Currency_WideStr:=0;
  1318. code := 256;
  1319. end
  1320. else
  1321. begin
  1322. SS := S;
  1323. Val(SS,fpc_Val_Currency_WideStr,code);
  1324. end;
  1325. end;
  1326. Function fpc_Val_UInt_WideStr (Const S : WideString; out Code : ValSInt): ValUInt; [public, alias:'FPC_VAL_UINT_WIDESTR']; compilerproc;
  1327. Var
  1328. SS : ShortString;
  1329. begin
  1330. fpc_Val_UInt_WideStr := 0;
  1331. if length(S) > 255 then
  1332. code := 256
  1333. else
  1334. begin
  1335. SS := S;
  1336. Val(SS,fpc_Val_UInt_WideStr,code);
  1337. end;
  1338. end;
  1339. Function fpc_Val_SInt_WideStr (DestSize: SizeInt; Const S : WideString; out Code : ValSInt): ValSInt; [public, alias:'FPC_VAL_SINT_WIDESTR']; compilerproc;
  1340. Var
  1341. SS : ShortString;
  1342. begin
  1343. fpc_Val_SInt_WideStr:=0;
  1344. if length(S)>255 then
  1345. code:=256
  1346. else
  1347. begin
  1348. SS := S;
  1349. fpc_Val_SInt_WideStr := int_Val_SInt_ShortStr(DestSize,SS,Code);
  1350. end;
  1351. end;
  1352. {$ifndef CPU64}
  1353. Function fpc_Val_qword_WideStr (Const S : WideString; out Code : ValSInt): qword; [public, alias:'FPC_VAL_QWORD_WIDESTR']; compilerproc;
  1354. Var
  1355. SS : ShortString;
  1356. begin
  1357. fpc_Val_qword_WideStr:=0;
  1358. if length(S)>255 then
  1359. code:=256
  1360. else
  1361. begin
  1362. SS := S;
  1363. Val(SS,fpc_Val_qword_WideStr,Code);
  1364. end;
  1365. end;
  1366. Function fpc_Val_int64_WideStr (Const S : WideString; out Code : ValSInt): Int64; [public, alias:'FPC_VAL_INT64_WIDESTR']; compilerproc;
  1367. Var
  1368. SS : ShortString;
  1369. begin
  1370. fpc_Val_int64_WideStr:=0;
  1371. if length(S)>255 then
  1372. code:=256
  1373. else
  1374. begin
  1375. SS := S;
  1376. Val(SS,fpc_Val_int64_WideStr,Code);
  1377. end;
  1378. end;
  1379. {$endif CPU64}
  1380. procedure fpc_WideStr_Float(d : ValReal;len,fr,rt : SizeInt;out s : WideString);compilerproc;
  1381. var
  1382. ss : shortstring;
  1383. begin
  1384. str_real(len,fr,d,treal_type(rt),ss);
  1385. s:=ss;
  1386. end;
  1387. procedure fpc_widestr_enum(ordinal,len:sizeint;typinfo,ord2strindex:pointer;out s:widestring);compilerproc;
  1388. var ss:shortstring;
  1389. begin
  1390. fpc_shortstr_enum(ordinal,len,typinfo,ord2strindex,ss);
  1391. s:=ss;
  1392. end;
  1393. {$ifdef FPC_HAS_STR_CURRENCY}
  1394. procedure fpc_WideStr_Currency(c : Currency;len,fr : SizeInt;out s : WideString);compilerproc;
  1395. var
  1396. ss : shortstring;
  1397. begin
  1398. str(c:len:fr,ss);
  1399. s:=ss;
  1400. end;
  1401. {$endif FPC_HAS_STR_CURRENCY}
  1402. Procedure fpc_WideStr_SInt(v : ValSint; Len : SizeInt; out S : WideString);compilerproc;
  1403. Var
  1404. SS : ShortString;
  1405. begin
  1406. Str (v:Len,SS);
  1407. S:=SS;
  1408. end;
  1409. Procedure fpc_WideStr_UInt(v : ValUInt;Len : SizeInt; out S : WideString);compilerproc;
  1410. Var
  1411. SS : ShortString;
  1412. begin
  1413. str(v:Len,SS);
  1414. S:=SS;
  1415. end;
  1416. {$ifndef CPU64}
  1417. Procedure fpc_WideStr_Int64(v : Int64; Len : SizeInt; out S : WideString);compilerproc;
  1418. Var
  1419. SS : ShortString;
  1420. begin
  1421. Str (v:Len,SS);
  1422. S:=SS;
  1423. end;
  1424. Procedure fpc_WideStr_Qword(v : Qword;Len : SizeInt; out S : WideString);compilerproc;
  1425. Var
  1426. SS : ShortString;
  1427. begin
  1428. str(v:Len,SS);
  1429. S:=SS;
  1430. end;
  1431. {$endif CPU64}
  1432. function UnicodeToUtf8(Dest: PChar; Source: PWideChar; MaxBytes: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1433. begin
  1434. if assigned(Source) then
  1435. Result:=UnicodeToUtf8(Dest,MaxBytes,Source,IndexWord(Source^,-1,0))
  1436. else
  1437. Result:=0;
  1438. end;
  1439. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: SizeUInt; Source: PWideChar; SourceChars: SizeUInt): SizeUInt;
  1440. var
  1441. i,j : SizeUInt;
  1442. w : word;
  1443. begin
  1444. result:=0;
  1445. if source=nil then
  1446. exit;
  1447. i:=0;
  1448. j:=0;
  1449. if assigned(Dest) then
  1450. begin
  1451. while (i<SourceChars) and (j<MaxDestBytes) do
  1452. begin
  1453. w:=word(Source[i]);
  1454. case w of
  1455. 0..$7f:
  1456. begin
  1457. Dest[j]:=char(w);
  1458. inc(j);
  1459. end;
  1460. $80..$7ff:
  1461. begin
  1462. if j+1>=MaxDestBytes then
  1463. break;
  1464. Dest[j]:=char($c0 or (w shr 6));
  1465. Dest[j+1]:=char($80 or (w and $3f));
  1466. inc(j,2);
  1467. end;
  1468. else
  1469. begin
  1470. if j+2>=MaxDestBytes then
  1471. break;
  1472. Dest[j]:=char($e0 or (w shr 12));
  1473. Dest[j+1]:=char($80 or ((w shr 6)and $3f));
  1474. Dest[j+2]:=char($80 or (w and $3f));
  1475. inc(j,3);
  1476. end;
  1477. end;
  1478. inc(i);
  1479. end;
  1480. if j>SizeUInt(MaxDestBytes-1) then
  1481. j:=MaxDestBytes-1;
  1482. Dest[j]:=#0;
  1483. end
  1484. else
  1485. begin
  1486. while i<SourceChars do
  1487. begin
  1488. case word(Source[i]) of
  1489. $0..$7f:
  1490. inc(j);
  1491. $80..$7ff:
  1492. inc(j,2);
  1493. else
  1494. inc(j,3);
  1495. end;
  1496. inc(i);
  1497. end;
  1498. end;
  1499. result:=j+1;
  1500. end;
  1501. function Utf8ToUnicode(Dest: PWideChar; Source: PChar; MaxChars: SizeInt): SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1502. begin
  1503. if assigned(Source) then
  1504. Result:=Utf8ToUnicode(Dest,MaxChars,Source,strlen(Source))
  1505. else
  1506. Result:=0;
  1507. end;
  1508. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;
  1509. var
  1510. i,j : SizeUInt;
  1511. w: SizeUInt;
  1512. b : byte;
  1513. begin
  1514. if not assigned(Source) then
  1515. begin
  1516. result:=0;
  1517. exit;
  1518. end;
  1519. result:=SizeUInt(-1);
  1520. i:=0;
  1521. j:=0;
  1522. if assigned(Dest) then
  1523. begin
  1524. while (j<MaxDestChars) and (i<SourceBytes) do
  1525. begin
  1526. b:=byte(Source[i]);
  1527. w:=b;
  1528. inc(i);
  1529. // 2 or 3 bytes?
  1530. if b>=$80 then
  1531. begin
  1532. w:=b and $3f;
  1533. if i>=SourceBytes then
  1534. exit;
  1535. // 3 bytes?
  1536. if (b and $20)<>0 then
  1537. begin
  1538. b:=byte(Source[i]);
  1539. inc(i);
  1540. if i>=SourceBytes then
  1541. exit;
  1542. if (b and $c0)<>$80 then
  1543. exit;
  1544. w:=(w shl 6) or (b and $3f);
  1545. end;
  1546. b:=byte(Source[i]);
  1547. w:=(w shl 6) or (b and $3f);
  1548. if (b and $c0)<>$80 then
  1549. exit;
  1550. inc(i);
  1551. end;
  1552. Dest[j]:=WideChar(w);
  1553. inc(j);
  1554. end;
  1555. if j>=MaxDestChars then j:=MaxDestChars-1;
  1556. Dest[j]:=#0;
  1557. end
  1558. else
  1559. begin
  1560. while i<SourceBytes do
  1561. begin
  1562. b:=byte(Source[i]);
  1563. inc(i);
  1564. // 2 or 3 bytes?
  1565. if b>=$80 then
  1566. begin
  1567. if i>=SourceBytes then
  1568. exit;
  1569. // 3 bytes?
  1570. b := b and $3f;
  1571. if (b and $20)<>0 then
  1572. begin
  1573. b:=byte(Source[i]);
  1574. inc(i);
  1575. if i>=SourceBytes then
  1576. exit;
  1577. if (b and $c0)<>$80 then
  1578. exit;
  1579. end;
  1580. if (byte(Source[i]) and $c0)<>$80 then
  1581. exit;
  1582. inc(i);
  1583. end;
  1584. inc(j);
  1585. end;
  1586. end;
  1587. result:=j+1;
  1588. end;
  1589. function UTF8Encode(const s : WideString) : UTF8String;
  1590. var
  1591. i : SizeInt;
  1592. hs : UTF8String;
  1593. begin
  1594. result:='';
  1595. if s='' then
  1596. exit;
  1597. SetLength(hs,length(s)*3);
  1598. i:=UnicodeToUtf8(pchar(hs),length(hs)+1,PWideChar(s),length(s));
  1599. if i>0 then
  1600. begin
  1601. SetLength(hs,i-1);
  1602. result:=hs;
  1603. end;
  1604. end;
  1605. function UTF8Decode(const s : UTF8String): WideString;
  1606. var
  1607. i : SizeInt;
  1608. hs : WideString;
  1609. begin
  1610. result:='';
  1611. if s='' then
  1612. exit;
  1613. SetLength(hs,length(s));
  1614. i:=Utf8ToUnicode(PWideChar(hs),length(hs)+1,pchar(s),length(s));
  1615. if i>0 then
  1616. begin
  1617. SetLength(hs,i-1);
  1618. result:=hs;
  1619. end;
  1620. end;
  1621. function AnsiToUtf8(const s : ansistring): UTF8String;{$ifdef SYSTEMINLINE}inline;{$endif}
  1622. begin
  1623. Result:=Utf8Encode(s);
  1624. end;
  1625. function Utf8ToAnsi(const s : UTF8String) : ansistring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1626. begin
  1627. Result:=Utf8Decode(s);
  1628. end;
  1629. { converts an utf-16 code point or surrogate pair to utf-32 }
  1630. function utf16toutf32(const S: WideString; const index: SizeInt; out len: longint): UCS4Char; [public, alias: 'FPC_UTF16TOUTF32'];
  1631. var
  1632. w: widechar;
  1633. begin
  1634. { UTF-16 points in the range #$0-#$D7FF and #$E000-#$FFFF }
  1635. { are the same in UTF-32 }
  1636. w:=s[index];
  1637. if (w<=#$d7ff) or
  1638. (w>=#$e000) then
  1639. begin
  1640. result:=UCS4Char(w);
  1641. len:=1;
  1642. end
  1643. { valid surrogate pair? }
  1644. else if (w<=#$dbff) and
  1645. { w>=#$d7ff check not needed, checked above }
  1646. (index<length(s)) and
  1647. (s[index+1]>=#$dc00) and
  1648. (s[index+1]<=#$dfff) then
  1649. { convert the surrogate pair to UTF-32 }
  1650. begin
  1651. result:=(UCS4Char(w)-$d800) shl 10 + (UCS4Char(s[index+1])-$dc00) + $10000;
  1652. len:=2;
  1653. end
  1654. else
  1655. { invalid surrogate -> do nothing }
  1656. begin
  1657. result:=UCS4Char(w);
  1658. len:=1;
  1659. end;
  1660. end;
  1661. function WideStringToUCS4String(const s : WideString) : UCS4String;
  1662. var
  1663. i, slen,
  1664. destindex : SizeInt;
  1665. len : longint;
  1666. begin
  1667. slen:=length(s);
  1668. setlength(result,slen+1);
  1669. i:=1;
  1670. destindex:=0;
  1671. while (i<=slen) do
  1672. begin
  1673. result[destindex]:=utf16toutf32(s,i,len);
  1674. inc(destindex);
  1675. inc(i,len);
  1676. end;
  1677. { destindex <= slen (surrogate pairs may have been merged) }
  1678. { destindex+1 for terminating #0 (dynamic arrays are }
  1679. { implicitely filled with zero) }
  1680. setlength(result,destindex+1);
  1681. end;
  1682. { concatenates an utf-32 char to a widestring. S *must* be unique when entering. }
  1683. procedure ConcatUTF32ToWideStr(const nc: UCS4Char; var S: WideString; var index: SizeInt);
  1684. var
  1685. p : PWideChar;
  1686. begin
  1687. { if nc > $ffff, we need two places }
  1688. if (index+ord(nc > $ffff)>length(s)) then
  1689. if (length(s) < 10*256) then
  1690. setlength(s,length(s)+10)
  1691. else
  1692. setlength(s,length(s)+length(s) shr 8);
  1693. { we know that s is unique -> avoid uniquestring calls}
  1694. p:=@s[index];
  1695. if (nc<$ffff) then
  1696. begin
  1697. p^:=widechar(nc);
  1698. inc(index);
  1699. end
  1700. else if (dword(nc)<=$10ffff) then
  1701. begin
  1702. p^:=widechar((nc - $10000) shr 10 + $d800);
  1703. (p+1)^:=widechar((nc - $10000) and $3ff + $dc00);
  1704. inc(index,2);
  1705. end
  1706. else
  1707. { invalid code point }
  1708. begin
  1709. p^:='?';
  1710. inc(index);
  1711. end;
  1712. end;
  1713. function UCS4StringToWideString(const s : UCS4String) : WideString;
  1714. var
  1715. i : SizeInt;
  1716. resindex : SizeInt;
  1717. begin
  1718. { skip terminating #0 }
  1719. SetLength(result,length(s)-1);
  1720. resindex:=1;
  1721. for i:=0 to high(s)-1 do
  1722. ConcatUTF32ToWideStr(s[i],result,resindex);
  1723. { adjust result length (may be too big due to growing }
  1724. { for surrogate pairs) }
  1725. setlength(result,resindex-1);
  1726. end;
  1727. procedure unimplementedwidestring;
  1728. begin
  1729. HandleErrorFrame(215,get_frame);
  1730. end;
  1731. {$warnings off}
  1732. function GenericWideCase(const s : WideString) : WideString;
  1733. begin
  1734. unimplementedwidestring;
  1735. end;
  1736. function CompareWideString(const s1, s2 : WideString) : PtrInt;
  1737. begin
  1738. unimplementedwidestring;
  1739. end;
  1740. function CompareTextWideString(const s1, s2 : WideString): PtrInt;
  1741. begin
  1742. unimplementedwidestring;
  1743. end;
  1744. function CharLengthPChar(const Str: PChar): PtrInt;
  1745. begin
  1746. unimplementedwidestring;
  1747. end;
  1748. {$warnings on}
  1749. procedure initwidestringmanager;
  1750. begin
  1751. fillchar(widestringmanager,sizeof(widestringmanager),0);
  1752. {$ifndef HAS_WIDESTRINGMANAGER}
  1753. widestringmanager.Wide2AnsiMoveProc:=@defaultWide2AnsiMove;
  1754. widestringmanager.Ansi2WideMoveProc:=@defaultAnsi2WideMove;
  1755. widestringmanager.UpperWideStringProc:=@GenericWideCase;
  1756. widestringmanager.LowerWideStringProc:=@GenericWideCase;
  1757. {$endif HAS_WIDESTRINGMANAGER}
  1758. widestringmanager.CompareWideStringProc:=@CompareWideString;
  1759. widestringmanager.CompareTextWideStringProc:=@CompareTextWideString;
  1760. widestringmanager.CharLengthPCharProc:=@CharLengthPChar;
  1761. end;